Compiler plugin
The palest ink is better than the best memory.
— Chinese proverb
The Scala 2 compiler has a short memory. It reads every doc comment while it compiles a file, then lets go of it, and nothing of the comment reaches the bytecode. A tool that looks at your classes later, from another module or out of a jar, finds the types intact and the prose gone.
The compiler plugin writes the prose down before it’s forgotten. For every documented class, it:
- validates the comment. One the parser can’t read, like one with a misaligned border, is reported as a warning in the
scaladoccategory and isn’t embedded.-Xfatal-warnings, or-Wconf:cat=scaladoc:eon 2.13, turns it into an error - packs the comment into a
@ScaladocCarrierannotation on the class, which stays in the bytecode
Scaladoc.of finds the annotation, so it works for classes from any module or jar compiled with the plugin. The plugin is for Scala 2.12 and 2.13. Scala 3 doesn’t need it, since it keeps comments in TASTy on its own.
The sbt and Gradle plugins add it on Scala 2 modules, and Maven builds list it in scala-maven-plugin’s compilerPlugins. In sbt, by hand:
addCompilerPlugin("io.github.andyglow" %% "scaladoc-compiler-plugin" % "0.5.0")
The plugin’s jar has everything it needs, so a module whose scaladoc gets embedded needs no library besides it.
Reading embedded scaladoc without this library
The annotation is a public, documented format, and Scaladoc.of is just one way to read it. It’s a plain Java annotation, scaladoc.annotation.ScaladocCarrier, with runtime retention:
| Element | Holds |
|---|---|
text() |
the doc comment as it is in the source |
tags() |
whether text() has the comment’s /** and */ markers and * border (true), or is just its content |
It lives in a jar of its own, scaladoc-annotation, which has no dependencies and no Scala version suffix. A tool that reads embedded scaladoc its own way, at runtime with reflection or in any JVM language, needs only that jar:
libraryDependencies += "io.github.andyglow" % "scaladoc-annotation" % "0.5.0"
ScaladocCarrier carrier = User.class.getAnnotation(ScaladocCarrier.class);
String comment = carrier == null ? null : carrier.text(); // null: no scaladoc was embedded
The text is then yours to process, with this library’s parser (Scaladoc.fromString(text, hasTags = carrier.tags())) or anything else. Tools that read class files directly, without loading them, find the annotation by its name and need no jar at all.
The annotation’s class doesn’t have to be present wherever the annotated classes are used: the JVM loads and runs them without it, and Java reflection then just leaves the annotation out.
Choosing what to embed
By default the plugin embeds the scaladoc of every documented class. That’s often more than needed, and a comment meant for the team may not be meant for every jar. Include and exclude patterns narrow it down. With sbt-scaladoc they’re settings:
scaladocIncludePaths := Seq("**/model/**")
scaladocIncludeClasses := Seq("com.acme.api.*")
scaladocExcludePaths := Seq("**/generated/**")
scaladocExcludeClasses := Seq("com.acme.**.internal.*")
Without it, they’re options of the compiler plugin, and each can be given several times:
scalacOptions ++= Seq(
"-P:scaladoc-compiler-plugin:include:path:**/model/**",
"-P:scaladoc-compiler-plugin:include:class:com.acme.api.*",
"-P:scaladoc-compiler-plugin:exclude:path:**/generated/**",
"-P:scaladoc-compiler-plugin:exclude:class:com.acme.**.internal.*"
)
| Option | Matches |
|---|---|
include:path:<glob> |
source files whose path matches the glob |
exclude:path:<glob> |
the same, to leave out |
include:class:<pattern> |
classes whose fully qualified name matches the pattern |
exclude:class:<pattern> |
the same, to leave out |
The rules, in the order they’re applied:
- Without any
include, every class is included. With some, a class is included if its file matches aninclude:pathor its name matches aninclude:class. - An
excludealways wins over aninclude. - Path globs use
java.nioglob syntax and are matched against the absolute path of the source file, so they usually start with**/. - In class patterns,
*matches within one segment of the name,**across segments, and**.zero or more packages:com.acme.**.internal.*matches bothcom.acme.internal.Secretandcom.acme.a.b.internal.Secret. Nested classes are written with dots, as incom.acme.Outer.Inner. - A malformed option fails the compilation, rather than quietly embedding everything.