This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 193703 - URLMapper is null at the annotations processing phase
Summary: URLMapper is null at the annotations processing phase
Status: RESOLVED INVALID
Alias: None
Product: platform
Classification: Unclassified
Component: Filesystems (show other bugs)
Version: 7.0
Hardware: All All
: P1 normal (vote)
Assignee: Jaroslav Tulach
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-12-21 18:37 UTC by simpatico
Modified: 2010-12-21 20:32 UTC (History)
3 users (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
sample project to reproduce issue (6.27 KB, application/x-zip-compressed)
2010-12-21 18:37 UTC, simpatico
Details

Note You need to log in before you can comment on or make changes to this bug.
Description simpatico 2010-12-21 18:37:08 UTC
Created attachment 104367 [details]
sample project to reproduce issue

A URLMapper is not registered in the lookup when in the process method of an annotations processor. Add the following code to attached maven project:

 URLMapper mapper = Lookup.getDefault().lookup(URLMapper.class);
       if (mapper == null)
           throw new RuntimeException("Missing URLMapper");

What would be a workaround? How to explicitly register a URLMapper in there?

P1: until workaround is found. 

http://forums.netbeans.org/post-93375.html
Comment 1 Jaroslav Tulach 2010-12-21 19:17:21 UTC
P1? That must be a joke!

Dear sympatic user, URLMapper seeks for implementations via Lookup.getDefault(). That is likely to work in any environment. Just make sure the proper implementation is on classpath.
Comment 2 simpatico 2010-12-21 19:30:03 UTC
> URLMapper seeks for implementations via Lookup.getDefault(). 
That is likely to work in any environment. Just make sure
the proper implementation is on classpath.

How do you do that? Any code/tutorial/sample? I've no clue and didn't expect to need to. Is NB dragging back into plumbing?

Also, I consider this a bug, because it's not documented anywhere that this will not work at that phase:

File tutorialFile = getFile(getSourceDir(), "/org/netbeans/test/codegen/Tutorial1.java");
    JavaSource tutorialSource = JavaSource.forFileObject(FileUtil.toFileObject(tutorialFile));

http://wiki.netbeans.org/JavaHT_Modification

But if you want, consider it a feature request? Can we avoid plumbing at that phase too?
Comment 3 Jaroslav Tulach 2010-12-21 19:44:21 UTC
I believe you are mixing apples and bananas. You are trying to use APIs of a java.source module during compilation, in side an AnnotationProcessor? http://wiki.netbeans.org/JavaHT_Modification

That is insane. AnnotationProcessor comes from JDK, it is NetBeans IDE independent. You can't use arbitrary NetBeans APIs in it!

If you want to perform a modification of your sources files, consider writing a NetBeans module.
Comment 4 simpatico 2010-12-21 19:53:53 UTC
I don't see why do the APIs I'm accessing need to be nbm modules (NB dependant). They provide generic utility/wrappers of the JDK classes in the first place. The dependance comes once one needs to access the Document editor, for example.
Likewise, in the sample project you see that indeed the method I'm complaining about works inside a unit test (without the need of being within an nbm module). The inconsistency is that it won't work inside the process() method.
Comment 5 edvin 2010-12-21 20:07:40 UTC
This is due to a classloader issue when running the annotation processor. Just add this line in the beginning of the process() method, and you're golden:

Thread.currentThread().setContextClassLoader(getClass().getClassLoader());

Has nothing to do with NetBeans API's :)
Comment 6 simpatico 2010-12-21 20:14:25 UTC
(In reply to comment #5)
> This is due to a classloader issue when running the annotation processor. Just

Is this an issue/bug in the jdk then? Why is this boilerplate line required? Would it not be a sensible default?

> add this line in the beginning of the process() method, and you're golden:
> 
> Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
> 


This indeed solved the FileObject fileObj = FileUtil.toFileObject(srcFile) issue, but not: final JavaSource classSource = JavaSource.forFileObject(fileObj), which is now null.

> Has nothing to do with NetBeans API's :)
I now see why too.
Comment 7 edvin 2010-12-21 20:17:33 UTC
JavaSource.forFileObject(fileObj) is null because fileObj represents "test.txt". Point it to a class file, and it will load just fine. Rename test.txt to Test.java and put "public class Test {}" in it, you'll see :)
Comment 8 edvin 2010-12-21 20:18:43 UTC
s/class file/java source file/g
Comment 9 Jan Lahoda 2010-12-21 20:32:33 UTC
Using NB Java infrastructure inside an annotation processor is not reasonably possible, in my opinion (except very rare edge cases). It depends on the NB's version of javac, and on the NB module system to mask out the JDK's version of the javac classes (in tests this is solved by prepending the NB version on the bootclasspath).