Skip to main content

apt not discovering my factory

5 replies [Last post]
loosed
Offline
Joined: 2006-01-26
Points: 0

I'm using apt to generate Java code from an annotated enum but for some reason the factory discovery process described in "Getting Started with apt" doesn't find my factory. The command line I'm using looks like:

apt -source 1.5 -classpath "c:\path\to\lib.jar;c:\path\to\factory.jar" -d build -s src\gen src\java\path\to\MyEnum.java

Running that yields

warning: No annotation processors found but annotations present.
1 warning

Interestingly, if I change the command line to point directly at the factory like so:

apt -source 1.5 -classpath "c:\path\to\lib.jar;c:\path\to\factory.jar" -d build -s src\gen -factory "package.of.MyAnnotationProcessorFactory" src\java\path\to\MyEnum.java

it works perfectly. If I only needed to use one factory, I would be fine with that, but I already have another piece of infrastructure built on apt that processes a different set of annotations. I guess I could do it in two passes, but I'd really rather have apt automatically discover the processor factories for me. Can someone tell me what's going on?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
gatto
Offline
Joined: 2004-07-10
Points: 0

Hi loosed,

Try changing "-classpath" to "-factorypath". According to apt's usage message:

-factorypath
Specify where to find annotation processor factories

Regards,
Gatto

loosed
Offline
Joined: 2006-01-26
Points: 0

Thanks for the response. When I do

apt -source 1.5 -classpath "c:\path\to\lib.jar;c:\path\to\factory.jar" -factorpath "c:\path\to\factory.jar" -d build -s src\gen src\java\path\to\MyEnum.java

I still get the same warning. In any case, the factorypath option is just an override for classpath. The documentation says that if factorypath isn't specified then the classpath is used.

I thought maybe the issue was that I'm using JAR so I extracted the classes and tried pointing factorypath at that. No luck.

I'm beginning to wonder if APT's discovery process is just broken. Has anyone gotten APT to discover an annotation processor factory?

gatto
Offline
Joined: 2004-07-10
Points: 0

Hi loosed,

I got it working :-) Here's a couple things you should check:

1. Your factories config file should be called META-INF/services/com.sun.mirror.apt.AnnotationProcessorFactory and should contain one fully qualified class name per line.

2. Your factories should properly return which annotations it supports, like this:

public Collection supportedAnnotationTypes() {
return Arrays.asList("br.com.vidatis.annotations.struts.*");
}

3. Finally, try putting debug statements in your factory, particularly in a static {} block (to ensure it was actually loaded by APT), constructors, and in the supportedAnnotationTypes().

Regards,
Gatto

loosed
Offline
Joined: 2006-01-26
Points: 0

Thanks gatto,

That fixed my problem. Can I ask where you found that information? I've tried googling for it but all I can dig up is someone's blog. I'd really like to see where this is documented.

gatto
Offline
Joined: 2004-07-10
Points: 0

Loosed,

Yes, there aren't many APT resources around. The only documentation I ever found on it is the JDK documentation:

JAVA_HOME/docs/guide/apt/index.html

Particularly "Getting Started with apt" and "Mirror API". There's a section on Getting Started called "Specifying the Annotations to Process" that covers how a factory should state the annotations it can proccess.

Regards,
Gatto