Class GeneratedSources
JavaFile, or written as text.
Type mapper = Type.newClassBuilder("FooMapper")
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addSuperinterface(TypeName.of(mapperType))
.addMethod(...)
.build();
context.sources().write(JavaFile.newBuilder("com.example", mapper).build(), mapperElement);
The same as text, naming each type through a SourceScope, which works out the imports:
context.sources().write("com.example.FooMapper", scope -> """
%s
public final class FooMapper implements %s {
public %s map(%s source) { ... }
}
""".formatted(scope.generated(), scope.name(mapperType), scope.name(targetType), scope.name(sourceType)),
mapperElement);
The body is the source after the imports: the type declaration, with its annotations and Javadoc. Every type it
refers to should be named through the SourceScope, which works out the imports.
Either way, the file is written in ASCII only: every character after U+007F, in identifiers, comments, Javadoc and
literals alike, is written as a Unicode escape, as \u00e9 for é, and each half of a
supplementary character such as an emoji as an escape of its own. The filer encodes a source with the
-encoding of the compilation, which would otherwise replace the characters it cannot represent, so that
"日" and "月" would both become "?". Javac translates the escapes back before anything else, so
the code means exactly what it would with the characters, under any encoding. Only the file is escaped:
JavaFile.toString() and the text of a body keep the characters as they are.
-
Method Summary
Modifier and TypeMethodDescriptionbooleanisAvailable(ClassName name) Returns whether a type can be generated with a name: no type of the compilation has it, in any module, whether from the sources, the class or module path, the platform, or the files generated in the previous rounds; nor did this object write a file for it, in this round or an earlier one.booleanWrites a source file, unless it was already created.booleanWrites a source file, unless it was already created.
-
Method Details
-
write
Writes a source file, unless it was already created. Its top-level type gets a@Generatedannotation naming the processor, unless it has one already or the generated code cannot see it. The types ofjava.langthat a type of the package hides, such asOverridenext to aexample.Override, are written qualified. The file is written in ASCII only, with Unicode escapes.- Parameters:
file- the fileoriginatingElements- the elements the file is generated from, so that incremental builds regenerate or delete it when they change; the first one also decides whether@Generatedis visible- Returns:
trueif the file was written,falseif it had already been created during this compilation, by this processor or another one- Throws:
UncheckedIOException- if the file cannot be written, such as when a type of the sources being compiled has that name
-
write
public boolean write(String qualifiedName, Function<SourceScope, String> body, Element... originatingElements) Writes a source file, unless it was already created. The file is written in ASCII only, with Unicode escapes. Ifbodyfails, nothing is written, and the file can be written again.- Parameters:
qualifiedName- the fully qualified name of the top-level type the file declaresbody- writes the source after the imports, naming types through the given scopeoriginatingElements- the elements the file is generated from, so that incremental builds regenerate or delete it when they change; the first one also decides whether@Generatedis visible- Returns:
trueif the file was written,falseif it had already been created during this compilation, by this processor or another one- Throws:
IllegalArgumentException- ifqualifiedNameis not a qualified name: Java identifiers, none of them a keyword, separated by dotsUncheckedIOException- if the file cannot be written, such as when a type of the sources being compiled has that name
-
isAvailable
Returns whether a type can be generated with a name: no type of the compilation has it, in any module, whether from the sources, the class or module path, the platform, or the files generated in the previous rounds; nor did this object write a file for it, in this round or an earlier one.Generating a type whose name another type has fails the compilation with a duplicate class, or hides the other type. A processor that names the types it generates by a convention checks the name first, to report the clash on the element it generates for, or to leave alone a type the user wrote by hand:
ClassName name = ClassName.of(type).generatedName("Mapper"); if (!context.sources().isAvailable(name)) { context.diagnostics().on(type).error("Cannot generate " + name + ": a type with that name already exists"); return; } context.sources().write(JavaFile.newBuilder(name.packageName(), mapper(name)).build(), type);Another processor's files of the current round are not known until the next round: writing one of them returns
false. Checking a name does not reserve it.- Parameters:
name- the name of the type, such as oneClassName.generatedName(String)gives- Returns:
trueif no type has that name and this object wrote no file for it
-