Skip to content

Commit 044f052

Browse files
ilxraboof
authored andcommitted
GH-97 Add basic support for passing annotations to the generated Java stubs (#171)
1 parent 7482573 commit 044f052

File tree

7 files changed

+84
-5
lines changed

7 files changed

+84
-5
lines changed

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,36 @@ GenJavadoc can also be integrated into a Maven build (inspired by [this answer o
103103
</profile>
104104
~~~
105105

106+
You can integrate genjavadoc with gradle build:
107+
108+
~~~ groovy
109+
apply plugin: 'scala'
110+
111+
configurations {
112+
scalaCompilerPlugin
113+
}
114+
115+
dependencies {
116+
// ...
117+
scalaCompilerPlugin "com.typesafe.genjavadoc:genjavadoc-plugin_${scalaFullVersion}:0.13"
118+
119+
}
120+
121+
tasks.withType(ScalaCompile) {
122+
scalaCompileOptions.with {
123+
additionalParameters = [
124+
"-Xplugin:" + configurations.scalaCompilerPlugin.asPath,
125+
"-P:genjavadoc:out=$buildDir/generated/java".toString()
126+
]
127+
}
128+
}
129+
130+
tasks.withType(Javadoc) {
131+
dependsOn("compileScala")
132+
source = [sourceSets.main.allJava, "$buildDir/generated/java"]
133+
}
134+
~~~
135+
106136
### Translation of Scaladoc comments
107137

108138
Comments found within the Scala sources are transferred to the corresponding Java sources including some modifications. These are necessary since Scaladoc supports different mark-up elements than Javadoc. The modifications are:

plugin/src/main/scala/com/typesafe/genjavadoc/AST.scala

+38-5
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,24 @@ trait AST { this: TransformCake =>
2828
static: Boolean,
2929
var firstConstructor: Boolean) extends Templ {
3030

31-
def sig = pattern(name, access)
31+
def sig: String = {
32+
s"$addAnnotations${pattern(name, access)}"
33+
}
34+
3235
def file = filepattern(name)
3336

37+
private def addAnnotations: String = {
38+
val annotations = sym.annotations
39+
.filter(a => allowedAnnotations.contains(a.symbol.fullName('.')))
40+
.map { a => s"@${a.symbol.fullName('.')}" }
41+
.mkString(System.lineSeparator())
42+
if (!annotations.isEmpty) {
43+
annotations + System.lineSeparator()
44+
} else {
45+
annotations
46+
}
47+
}
48+
3449
def addMember(t: Templ) = copy(members = members :+ t)
3550

3651
def constructor: Boolean = {
@@ -49,7 +64,7 @@ trait AST { this: TransformCake =>
4964
object ClassInfo {
5065
def apply(c: ImplDef, comment: Seq[String], topLevel: Boolean): ClassInfo = {
5166
c match {
52-
case ClassDef(mods, _, tparams, impl) =>
67+
case cd@ClassDef(mods, _, tparams, impl) =>
5368
val name = c.name.toString
5469
val acc = access(mods, topLevel)
5570
val fl = flags(mods)
@@ -93,9 +108,27 @@ trait AST { this: TransformCake =>
93108
}
94109
}
95110

96-
case class MethodInfo(access: String, pattern: String => String, ret: String, name: String, comment: Seq[String]) extends Templ {
97-
def sig = pattern(s"$ret $name")
111+
case class MethodInfo(access: String, pattern: String => String, ret: String, name: String, comment: Seq[String], d: Option[DefDef] = None) extends Templ {
112+
def sig: String = {
113+
s"$addAnnotations${pattern(s"$ret $name")}"
114+
}
115+
116+
private def addAnnotations: String = d match {
117+
case Some(definition) => {
118+
val annotations = definition.symbol.annotations
119+
.filter(a => allowedAnnotations.contains(a.symbol.fullName('.')))
120+
.map { a => s"@${a.symbol.fullName('.')}" }
121+
.mkString(System.lineSeparator())
122+
if (!annotations.isEmpty) {
123+
annotations + System.lineSeparator()
124+
} else {
125+
annotations
126+
}
127+
}
128+
case None => ""
129+
}
98130
}
131+
99132
object MethodInfo {
100133
def apply(d: DefDef, interface: Boolean, comment: Seq[String], hasVararg: Boolean, deprecation: Option[DeprecationInfo]): MethodInfo = {
101134
val acc = methodAccess(d.symbol, interface) + methodFlags(d.mods, interface)
@@ -139,7 +172,7 @@ trait AST { this: TransformCake =>
139172
case Some(deprec) => deprec.appendToComment(commentWithParams)
140173
case _ => commentWithParams
141174
}
142-
MethodInfo(acc, pattern, ret, name, commentWithParamsAndDeprec)
175+
MethodInfo(acc, pattern, ret, name, commentWithParamsAndDeprec, Some(d))
143176
}
144177

145178
/**

plugin/src/main/scala/com/typesafe/genjavadoc/Plugin.scala

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import nsc.transform.Transform
88
import java.io.File
99
import java.util.Properties
1010

11+
1112
object GenJavadocPlugin {
1213

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

5254
private object MyComponent extends PluginComponent with Transform {
5355

@@ -78,6 +80,8 @@ class GenJavadocPlugin(val global: Global) extends Plugin {
7880
override def transformUnit(unit: CompilationUnit) = newTransformUnit(unit)
7981
override def javaKeywords = GenJavadocPlugin.javaKeywords
8082
override def filteredStrings = GenJavadocPlugin.this.filteredStrings
83+
84+
override def allowedAnnotations: Set[String] = GenJavadocPlugin.this.allowedAnnotations
8185
}
8286
}
8387
}

plugin/src/main/scala/com/typesafe/genjavadoc/TransformCake.scala

+2
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ trait TransformCake extends JavaSig with Output with Comments with BasicTransfor
1414
def javaKeywords: Set[String]
1515

1616
def filteredStrings: Set[String]
17+
18+
def allowedAnnotations: Set[String]
1719
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package akka;
2+
@java.lang.SuppressWarnings
3+
public class WithAnnotation {
4+
public WithAnnotation () { throw new RuntimeException(); }
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package akka
2+
3+
@SuppressWarnings(value=Array(""))
4+
class WithAnnotation

plugin/src/test/scala/com/typesafe/genjavadoc/BasicSpec.scala

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ object BasicSpec {
1313
/** Test basic behaviour of genjavadoc with standard settings */
1414
class BasicSpec extends CompilerSpec {
1515

16+
override def extraSettings: Seq[String] = Seq("annotations=java.lang.SuppressWarnings")
1617
override def sources = BasicSpec.sources
1718
override def expectedPath: String = {
1819
val scalaVersion = scala.util.Properties.versionNumberString.split("-").head

0 commit comments

Comments
 (0)