This discussion is archived
3 Replies Latest reply: Dec 12, 2009 2:45 PM by 843793 RSS

How to generate list of services?

843793 Newbie
Currently Being Moderated
I've written an annotation processor that I'm using to map classes to integer ids. My generated stub looks like:
//Source
@MyAnnotation
public class Foo
{
}

//Generated stub
public class FooStub extends com.kitfox.Stub {
    public int getId() { return 1; }
    public Class<Foo> getStubClass() { return Foo.class; }
}
I would also like to generate an index of all com.kitfox.Stub that I can place in /META-INF/services. Unfortunately, the apt mechanism seems to only present the annotation processor with files that don't have existing .class files generated which makes it difficult to integrate incremental changes into the build process.

Is there a way to force annotation processing to handle all files on the source path regardless of whether .class files exist?

Is there some other way to generate services indices? I imagine there would be a lot of folks wanting to do this.
  • 1. Re: How to generate list of services?
    843793 Newbie
    Currently Being Moderated
    Have you tried to touch the source files so they have a more recent timestamp than the corresponding class files? That should trigger the processor AND compiler on.

    Edited by: Robert.Bossy on Nov 26, 2009 6:11 PM
  • 2. Re: How to generate list of services?
    843793 Newbie
    Currently Being Moderated
    Mark_McKay wrote:
    I've written an annotation processor that I'm using to map classes to integer ids. My generated stub looks like:
    //Source
    @MyAnnotation
    public class Foo
    {
    }
    
    //Generated stub
    public class FooStub extends com.kitfox.Stub {
    public int getId() { return 1; }
    public Class<Foo> getStubClass() { return Foo.class; }
    }
    I would also like to generate an index of all com.kitfox.Stub that I can place in /META-INF/services. Unfortunately, the apt mechanism seems to only present the annotation processor with files that don't have existing .class files generated which makes it difficult to integrate incremental changes into the build process.

    Is there a way to force annotation processing to handle all files on the source path regardless of whether .class files exist?
    The annotation processors are free to examine whatever types they want, including types not being compiled.
    Is there some other way to generate services indices? I imagine there would be a lot of folks wanting to do this.
    See Kohsuke Kawaguchi's META-INF/services generator
    http://weblogs.java.net/blog/2009/03/26/my-project-day-meta-infservices-file-generator

    His generator also handles incremental compilation.
  • 3. Re: How to generate list of services?
    843793 Newbie
    Currently Being Moderated
    I had a look at the link you posted, but can't find a link to the source anywhere on it. Is it still there?

    Alternately, I could just alter my own code if I could figure out how to examine the types not being compiled. Is there a way to do this? There's very little documentation on Annotation processing that I've been able to find through Google, so I've been mostly guessing from the Javadoc and the docs for the out-of-date Apt for Java 5.

    Also, I had tried to add incremental compilation to my processor, but run into problems with the Filer. To incrementally change the services file, I need to first read in what it contains, then write out my modified version with additions. However, once I call Filer.getResource() to read the file, it later prevents me from later calling Filer.createResource() for writing because it allows file objects to only be touched once. The filer also provides no clue as to what the root directory is, so I can't create the file using ordinary java.io file methods.

    Edited by: Mark_McKay on Dec 12, 2009 2:39 PM