Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Filer's originating elements useful?

I'm 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 originating Element that triggered the generated code.

The Filer docs say that this is intended to aid incremental builds:

"This information may be used in an incremental environment to determine the need to rerun processors or remove generated files. Non-incremental environments may ignore the originating element information."

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

like image 858
Jesse Wilson Avatar asked May 09 '13 01:05

Jesse Wilson


1 Answers

Does anyone know of an incremental environment that use this information?

Sorry, I don't.

Does javac or the Eclipse compiler use this information?

  1. javac:

    javac's internal annotation processing environment uses the javax.annotation.processing.Filer implementation class com.sun.tools.javac.processing.JavacFiler. Here's the relevant code snippet from this class:

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

    i.e. it simply throws away the originating element(s), not using it/them in any way.

    Same is done for createClassFile and createResourceFile.

    So that's a big no.

  2. Eclipse compiler:

    In Eclipse, you enable java 6 -style annotation processing via

    Project properties -> Java Compiler -> Annotation Processing -> Check “Enable Project Specific Settings” & Check “Enable annotation processing”

    This delegates annotation processing to JDT-Core internal compiler, which uses it's own implementation of annotation processing. Eclipse 4.2, 3.7 and 3.6 source code does not contain the strings javax.annotation.processing or originatingElements - i.e. does not use the new java 6 annotation processing API at all. When searching for Filer, we see that it uses the old jdk-5 style Filer interface:

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

    All of these 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 that's a big no.

like image 121
Glen Best Avatar answered Nov 04 '22 14:11

Glen Best