Class GeneratedSources

java.lang.Object
be.imgn.gaia.processing.GeneratedSources

public final class GeneratedSources extends Object
Writes the Java source files a processor generates, with their package declaration and imports: modelled as a 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 Type
    Method
    Description
    boolean
    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.
    boolean
    write(JavaFile file, Element... originatingElements)
    Writes a source file, unless it was already created.
    boolean
    write(String qualifiedName, Function<SourceScope, String> body, Element... originatingElements)
    Writes a source file, unless it was already created.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • write

      public boolean write(JavaFile file, Element... originatingElements)
      Writes a source file, unless it was already created. Its top-level type gets a @Generated annotation naming the processor, unless it has one already or the generated code cannot see it. The types of java.lang that a type of the package hides, such as Override next to a example.Override, are written qualified. The file is written in ASCII only, with Unicode escapes.
      Parameters:
      file - the file
      originatingElements - the elements the file is generated from, so that incremental builds regenerate or delete it when they change; the first one also decides whether @Generated is visible
      Returns:
      true if the file was written, false if 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. If body fails, nothing is written, and the file can be written again.
      Parameters:
      qualifiedName - the fully qualified name of the top-level type the file declares
      body - writes the source after the imports, naming types through the given scope
      originatingElements - the elements the file is generated from, so that incremental builds regenerate or delete it when they change; the first one also decides whether @Generated is visible
      Returns:
      true if the file was written, false if it had already been created during this compilation, by this processor or another one
      Throws:
      IllegalArgumentException - if qualifiedName is not a qualified name: Java identifiers, none of them a keyword, separated by dots
      UncheckedIOException - if the file cannot be written, such as when a type of the sources being compiled has that name
    • isAvailable

      public boolean isAvailable(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.

      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 one ClassName.generatedName(String) gives
      Returns:
      true if no type has that name and this object wrote no file for it