Explicitly importing code generated by APT

Jim Sproch jsproch at google.com
Mon Jun 26 23:38:33 UTC 2017


Hi,

I'm trying to understand the intended usage of code generation within APT.
In particular, I'm wondering if it is considered a good practice to have
users explicitly importing classes that are generated during the annotation
processing phase of javac.

Example:

import com.generated.MyAutomaticallyGeneratedWidget;
> @MakeMagical
> public class MyWidget {
>   public MyAutomaticallyGeneratedWidget doSomething() {
>     return new MyAutomaticallyGeneratedWidget("whatever");
>   }
> }



Where the @MakeMagical annotation will look at the classname being
annotated and automatically generate a class based on the class being
annotated.

The problem being that the classes in com.generated.* are not available
until I recompile MyWidget, so the IDE is confused and complains instead of
offering autocomplete suggestions.

Is this a bug in the IDE?  Or is this code an antipattern?  Should the
generated code be imported, or should annotation processors design their
API to ensure the generated code is an implementation detail of the
annotation processor?

Thanks,
Jim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20170626/3f7f8a6b/attachment.html>


More information about the compiler-dev mailing list