Explicitly importing code generated by APT

Jonathan Gibbons jonathan.gibbons at oracle.com
Mon Jun 26 23:58:43 UTC 2017


Some IDEs have explicit support for annotation processing and dealing 
with generated sources. Check your IDE for details.

-- Jon

On 06/26/2017 04:38 PM, Jim Sproch wrote:
> 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/f1716126/attachment.html>

More information about the compiler-dev mailing list