3 Replies Latest reply: Nov 10, 2008 1:08 PM by 843793 RSS

    Problem with apt -cp/-factorypath

    843793
      I've created my annotation @interface definitions, AnnotationProcessor and AnnotationProcessorFactory classes, and bundled them up in a jar. Now I have some code that uses my annotations. When I run apt I specify the -cp option to include my annotations jar in the classpath, but I get a warning saying that the annotations have no processor. The same thing happens if I use -factorypath instead. If I specify -cp and -factory together to specify my factory class, it works fine.

      The way I understood it from the documentation, apt is supposed to scan the classpath/factorypath to locate AnnotationFactory classes but either I'm doing something wrong or it's not working as the documentation says, because it certainly looks like it isn't finding my factory class even though it's in the classpath.

      Does anyone have any idea what I'm doing wrong here, or is this a known problem?
        • 1. Re: Problem with apt -cp/-factorypath
          843793
          Problem solved.
          • 2. Re: Problem with apt -cp/-factorypath
            843793
            PeteFord wrote:
            Problem solved.
            It would be nice if you posted what the problem was. If others find this thread they might benefit from your posting.
            • 3. Re: Problem with apt -cp/-factorypath
              843793
              Ok. The root problem was that to create my processor I followed a tutorial that, I'm guessing, is out of date - it used explicit references to com.sun.* packages rather than the javax.annotations.* packages. Because the AnnotationProcessor interface in the com.sun.* packages are not even close to being the same as the javax stuff, the whole thing was broken in that apt didn't know how to locate the factory class, and if I explicitly gave it the processor class it threw it out because it didn't implement the Processor interface. Basically my whole problem boiled down to confusion regarding which packages I should have been using.

              The fog cleared once I realized that Java 6's javac handles annotations without requiring apt - provided you use the right classes to start with. So I rewrote my processor using the javax patterns and got it to compile, and built a jar that I could use against test sources with my annotations in place. I still had a problem in that the discovery mechanism wasn't finding my processor when compiling the test sources; that particular wisp of fog cleared once I read that you need a META-INF/services/javax.annotation.processing.Processor text file in the classpath. With that in place, everything started working.

              My processor now uses my annotations and can generate output Java sources (using Velocity templates for the sources; the Processor just builds the contexts from the annotated Class and Method declarations) that get compiled alongside the annotated sources without me having to take any special actions. Sweet. I used to do this junk using reflection, but using annotations gives me better control (I can limit the methods I'm concerned with by annotating just those) and I don't need a three step compile-generate-compile process.