From 129d7a2843ae9af5fe58ed7c0401616935898e54 Mon Sep 17 00:00:00 2001 From: Ivica Loncar Date: Tue, 23 Apr 2019 10:15:59 +0200 Subject: [PATCH] GH-97 Add basic support for passing annotations to generated Java stubs --- .../scala/com/typesafe/genjavadoc/AST.scala | 33 ++++++++++++++++--- .../com/typesafe/genjavadoc/Plugin.scala | 12 +++++++ .../typesafe/genjavadoc/TransformCake.scala | 2 ++ 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/plugin/src/main/scala/com/typesafe/genjavadoc/AST.scala b/plugin/src/main/scala/com/typesafe/genjavadoc/AST.scala index d5bd7f4..ba8c15c 100644 --- a/plugin/src/main/scala/com/typesafe/genjavadoc/AST.scala +++ b/plugin/src/main/scala/com/typesafe/genjavadoc/AST.scala @@ -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 = { @@ -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) @@ -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) @@ -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)) } /** diff --git a/plugin/src/main/scala/com/typesafe/genjavadoc/Plugin.scala b/plugin/src/main/scala/com/typesafe/genjavadoc/Plugin.scala index 23fac0b..3ba740b 100644 --- a/plugin/src/main/scala/com/typesafe/genjavadoc/Plugin.scala +++ b/plugin/src/main/scala/com/typesafe/genjavadoc/Plugin.scala @@ -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", @@ -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) @@ -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 { @@ -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 } } } diff --git a/plugin/src/main/scala/com/typesafe/genjavadoc/TransformCake.scala b/plugin/src/main/scala/com/typesafe/genjavadoc/TransformCake.scala index 633709a..b3c71bf 100644 --- a/plugin/src/main/scala/com/typesafe/genjavadoc/TransformCake.scala +++ b/plugin/src/main/scala/com/typesafe/genjavadoc/TransformCake.scala @@ -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] }