participate


Annotation Processing - Filer.getResource(SOURCE_PATH...) throws NPE if -sourcepath not specified
This question is not answered.

<<   Back to Forum  |   Give us Feedback Topics: « Previous | Next
This topic has 3 replies on 1 page.
sahoo
Posts:69
Registered: 11/19/04
Filer.getResource(SOURCE_PATH...) throws NPE if -sourcepath not specified   
Nov 27, 2007 9:28 PM
 
 
In my annotation processor, I wanted to locate a file in source path, so I wrote the following code:
FileObject fo = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "", "META-INF/persistence.xml");

It throws NullPointerException if I do not specify -sourcepath option while invoking javac. I have two questions:

1) Is "." not the default value for source search path?

2) Instead of throwing an IOException as per the javadocs of getResource, why is it throwing an NPE?

The stack trace is:
java.lang.NullPointerException
at com.sun.tools.javac.util.DefaultFileManager.getFileForOutput(DefaultFileManager.java:974)
at com.sun.tools.javac.util.DefaultFileManager.getFileForOutput(DefaultFileManager.java:950)
at com.sun.tools.javac.processing.JavacFiler.getResource(JavacFiler.java:434)
at foo.Ap.readDD(Ap.java:182)
at foo.Ap.init(Ap.java:101)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$ProcessorState.<init>(JavacProcessingEnvironment.java:339)
...

Sahoo
 
brucechapman
Posts:692
Registered: 24/11/98
Re: Filer.getResource(SOURCE_PATH...) throws NPE if -sourcepath not specifi   
Nov 28, 2007 12:47 PM (reply 1 of 3)  (In reply to original post )
 
 
Sahoo,

This looks like a bug to me.

Despite the fact that it shouldn't throw an NPE, are you sure the file exists in your SOURCE_PATH?

I have read your blog, and it would seem to me that if you are generating the META-INF/persistence.xml file, then if that has already been done, it ought to be in CLASS_OUTPUT (that is the location where I put generated service lookup files).

Also, you might like to take a look at the hickory project on java.net which has a couple of things that could simplify your code.

First there is a class to handle collecting state and maintaining it over multiple incremental compiles. This would enable your processor to work correctly when you remove an annotation from a single class, and just recompile that one class. I am not sure if your existing code can correctly rebuild the persistence.xml file in that case, but hickory's StateSaver takes most of the pain away from those situations where a single output class or resource is derives from multiple source files.

You may also be interested in the prisms mechanism that makes handling known annotations much easier. It gives the best of both worlds between getting the runtime annotation via Element.getAnnotation(Class<A> annotationType)
and using AnnotationMirrors.

Also there is a package there designed to support testing annotation processors which you might find useful.

https://hickory.dev.java.net/nonav/apidocs/index.html

Bruce
 
sahoo
Posts:69
Registered: 11/19/04
Re: Filer.getResource(SOURCE_PATH...) throws NPE if -sourcepath not specifi   
Nov 28, 2007 8:29 PM (reply 2 of 3)  (In reply to #1 )
 
 
Bruce,

Yes, the file does not exist in default sourcepath, ".", yet I get NPE if I don't specify -sourcepath.

I plan to do some more work on annotation processing, so I will take a look at the hickory project, it does sound interesting. I am not answering questions specific to the annotation processor here as they can be distracting to someone following this thread. If you ask them in my blog, I can answer them.


Thanks,
Sahoo
 
j.d.darcy
Posts:175
Registered: 3/8/05
Re: Filer.getResource(SOURCE_PATH...) throws NPE if -sourcepath not specifi   
Jan 7, 2008 8:29 PM (reply 3 of 3)  (In reply to original post )
 
 
I've filed Sun bug 6647998 to track this issue.
 
This topic has 3 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 28
  • Guests : 133

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums