Skip to content

Commit

Permalink
lightbendGH-97 Add basic support for passing annotations to generated…
Browse files Browse the repository at this point in the history
… Java stubs
  • Loading branch information
ilx committed Apr 23, 2019
1 parent 7482573 commit 129d7a2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
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 {
def sig: String = {
s"""
|${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
12 changes: 12 additions & 0 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,8 @@ 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 All @@ -18,6 +20,13 @@ object GenJavadocPlugin {

private val defaultFilterString = "$$"

val defaultAnnotations = Set(
"com.xebialabs.xlplatform.documentation.PublicApi",
"com.xebialabs.xlplatform.documentation.PublicApiRef",
"com.xebialabs.xlplatform.documentation.PublicApiMember",
"com.xebialabs.xlplatform.documentation.ShowOnlyPublicApiMembers"
).mkString(",")

def stringToFilter(s: String): Set[String] = s.split(",").toSet

val defaultFilteredStrings = stringToFilter(defaultFilterString)
Expand Down Expand Up @@ -48,6 +57,7 @@ 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", defaultAnnotations))

private object MyComponent extends PluginComponent with Transform {

Expand Down Expand Up @@ -78,6 +88,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]
}

0 comments on commit 129d7a2

Please sign in to comment.