Are Filer source elements useful?

I am working on a Java program ( Dagger ) that uses the Java Annotation Processing API to generate code. When our program encounters an annotation in Foo.java , it generates Foo$$InjectAdapter.java . We use the Filer API to attach the source Element that invoked the generated code.

Filer docs say this is intended to support incremental builds:

"This information can be used in an incremental environment to determine whether to restart the processors or delete the generated files. Non-incremental environments can ignore information about the source element."

Does anyone know of an incremental environment using this information? Does javac or the Eclipse compiler use this information?

+4
source share
1 answer

Does anyone know of an incremental environment using this information?

Sorry, I do not.

Does javac or the Eclipse compiler use this information?

  • Javac:

    The javac internal annotation processing environment uses the javax.annotation.processing.Filer com.sun.tools.javac.processing.JavacFiler class. Here is the corresponding code snippet from this class:

     public JavaFileObject createSourceFile(CharSequence paramCharSequence, Element[] paramArrayOfElement) throws IOException { return createSourceOrClassFile(true, paramCharSequence.toString()); } 

    i.e. it simply discards the original element (s) without using it / them in any way.

    The same thing is done for createClassFile and createResourceFile.

    So big no.

  • Eclipse Compiler:

    In Eclipse, you enable java annotation processing with 6 steps through

    Project Properties -> Java Compiler -> Annotation Processing -> Select the Enable Project Settings check box and select the Enable Annotation Processing check box

    This delegate handles annotation processing for the internal JDT-Core compiler, which uses its own annotation processing implementation. The source code of Eclipse 4.2, 3.7 and 3.6 does not contain javax.annotation.processing or originatingElements lines - that is, it does not use the new java 6 annotation processing API at all. When we search for Filer we see that it uses the old Filer style interface jdk-5:

    org.eclipse.jdt.apt.core.internal.env.BuildFilerImpl extends org.eclipse.jdt.apt.core.internal.env.FilerImpl that implements com.sun.mirror.apt.Filer

    All of them have method signatures:

     PrintWriter createSourceFile(String name) throws IOException; OutputStream createClassFile(String name) throws IOException; PrintWriter createTextFile(Location loc, String pkg, File relPath, String charsetName) throws IOException; OutputStream createBinaryFile(Location loc, String pkg, File relPath) throws IOException; 

    So big no.

+7
source

Source: https://habr.com/ru/post/1479834/


All Articles