Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-97 Add basic support for passing annotations to the generated Java stubs #171

Merged
merged 4 commits into from
May 6, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions plugin/src/main/scala/com/typesafe/genjavadoc/AST.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,19 @@ trait AST { this: TransformCake =>
static: Boolean,
var firstConstructor: Boolean) extends Templ {

def sig = pattern(name, access)
def sig: String = {
s"""
|${addAnnotations}
|${pattern(name, access)}""".stripMargin
}

def file = filepattern(name)

private def addAnnotations: String = sym.annotations
.filter(a => allowedAnnotations.contains(a.symbol.fullName('.')))
.map { a => s"@${a.symbol.fullName('.')}" }
.mkString(System.lineSeparator())

def addMember(t: Templ) = copy(members = members :+ t)

def constructor: Boolean = {
Expand All @@ -49,7 +59,7 @@ trait AST { this: TransformCake =>
object ClassInfo {
def apply(c: ImplDef, comment: Seq[String], topLevel: Boolean): ClassInfo = {
c match {
case ClassDef(mods, _, tparams, impl) =>
case cd@ClassDef(mods, _, tparams, impl) =>
val name = c.name.toString
val acc = access(mods, topLevel)
val fl = flags(mods)
Expand Down Expand Up @@ -93,9 +103,22 @@ trait AST { this: TransformCake =>
}
}

case class MethodInfo(access: String, pattern: String => String, ret: String, name: String, comment: Seq[String]) extends Templ {
def sig = pattern(s"$ret $name")
case class MethodInfo(access: String, pattern: String => String, ret: String, name: String, comment: Seq[String], d: Option[DefDef] = None) extends Templ {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might have been nice to only include the annotations here rather than the full DefDef?

def sig: String = {
s"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The additional whitespace here makes tests fail

|${addAnnotations}
|${pattern(s"$ret $name")}""".stripMargin
}

private def addAnnotations: String = d match {
case Some(definition) => definition.symbol.annotations
.filter(a => allowedAnnotations.contains(a.symbol.fullName('.')))
.map { a => s"@${a.symbol.fullName('.')}" }
.mkString(System.lineSeparator())
case None => ""
}
}

object MethodInfo {
def apply(d: DefDef, interface: Boolean, comment: Seq[String], hasVararg: Boolean, deprecation: Option[DeprecationInfo]): MethodInfo = {
val acc = methodAccess(d.symbol, interface) + methodFlags(d.mods, interface)
Expand Down Expand Up @@ -139,7 +162,7 @@ trait AST { this: TransformCake =>
case Some(deprec) => deprec.appendToComment(commentWithParams)
case _ => commentWithParams
}
MethodInfo(acc, pattern, ret, name, commentWithParamsAndDeprec)
MethodInfo(acc, pattern, ret, name, commentWithParamsAndDeprec, Some(d))
}

/**
Expand Down
6 changes: 4 additions & 2 deletions plugin/src/main/scala/com/typesafe/genjavadoc/Plugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import nsc.transform.Transform
import java.io.File
import java.util.Properties


object GenJavadocPlugin {

val javaKeywords = Set("abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue",
Expand Down Expand Up @@ -48,11 +49,10 @@ class GenJavadocPlugin(val global: Global) extends Plugin {
lazy val filteredStrings: Set[String] = stringToFilter(myOptions.getProperty("filter", defaultFilterString))
lazy val fabricateParams = java.lang.Boolean.parseBoolean(myOptions.getProperty("fabricateParams", "true"))
lazy val strictVisibility = java.lang.Boolean.parseBoolean(myOptions.getProperty("strictVisibility", "false"))
lazy val allowedAnnotations: Set[String] = stringToFilter(myOptions.getProperty("annotations", ""))

private object MyComponent extends PluginComponent with Transform {

import global._
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently this imported CompilationUnit, perhaps best to bring it back?


type GT = GenJavadocPlugin.this.global.type

override val global: GT = GenJavadocPlugin.this.global
Expand All @@ -78,6 +78,8 @@ class GenJavadocPlugin(val global: Global) extends Plugin {
override def transformUnit(unit: CompilationUnit) = newTransformUnit(unit)
override def javaKeywords = GenJavadocPlugin.javaKeywords
override def filteredStrings = GenJavadocPlugin.this.filteredStrings

override def allowedAnnotations: Set[String] = GenJavadocPlugin.this.allowedAnnotations
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ trait TransformCake extends JavaSig with Output with Comments with BasicTransfor
def javaKeywords: Set[String]

def filteredStrings: Set[String]

def allowedAnnotations: Set[String]
}