Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 352949 - Impossible to process package-level annotations with an AnnotationProcessor
Summary: Impossible to process package-level annotations with an AnnotationProcessor
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: APT (show other bugs)
Version: 3.7   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: 3.8 M7   Edit
Assignee: Satyam Kandula CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-07-24 10:26 EDT by Yves Bontemps CLA
Modified: 2012-04-30 05:17 EDT (History)
5 users (show)

See Also:
eclipse: review+


Attachments
Stack trace (4.17 KB, application/octet-stream)
2011-07-24 10:28 EDT, Yves Bontemps CLA
no flags Details
Patch to fix the exception (4.89 KB, patch)
2012-04-26 09:01 EDT, Satyam Kandula CLA
no flags Details | Diff
Patch to support annotations on package. (16.62 KB, patch)
2012-04-26 09:05 EDT, Satyam Kandula CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Yves Bontemps CLA 2011-07-24 10:26:34 EDT
Build Identifier: 20110615-0604

In org.eclipse.jdt.apt.core.internal.util.Factory#createDeclaration, there is no case statement to handle the case where the binding is a Package.  

The consequence is that an AnnotationProcessor contributed via the annotationProcessorFactory extension point will crash whenever there are package-level annotations (provided via a package-info.java file, typically).


Reproducible: Always

Steps to Reproduce:
1. Create a package-level annotation type
2. Create and contribute an AnnotationProcessor (via the org.eclipse.jdt.apt.core.annotationProcessorFactory extension point) that simply enumerates all the annotated types in the current environment.
3. Start a debug workbench and set it up to use your annotation processor.
4. Create a java project and annotate a package in it with a package-info.java file.

When building the project, the annotation processor crashes (see attached stack trace)
Comment 1 Yves Bontemps CLA 2011-07-24 10:28:13 EDT
Created attachment 200242 [details]
Stack trace
Comment 2 Walter Harley CLA 2011-07-24 12:24:30 EDT
Package-level annotations actually present quite a few problems; because of the way they were specified, there is no way to write an efficient processor that uses them in an incremental compilation situation. (Basically, the compiler can't tell if a package-level annotation exists without searching every element in every fragment of the package, across the entire classpath including jars. In general package-info.java won't have changed in a given compilation, so it won't be recompiled, so it's invisible without a search.)

But that doesn't excuse the crash! Thanks for the clear report; I'll try to get this fixed in 3.7.1.
Comment 3 Olivier Thomann CLA 2011-08-16 11:23:04 EDT
Moving to 3.7.2 once available. Too late for 3.7.1 given that there is no patch attached to it.
Comment 4 Markus Keller CLA 2012-01-05 14:36:11 EST
Assigning a target milestone to at least fix the crash for 3.8.
Comment 5 Olivier Thomann CLA 2012-01-06 10:28:13 EST
Yves, could you please provide a test case that will help us to recreate the issue?
Thanks.
Comment 6 Srikanth Sankaran CLA 2012-03-20 02:14:51 EDT
(In reply to comment #5)
> Yves, could you please provide a test case that will help us to recreate the
> issue?

Any update on this request Yves ? Thanks.
Comment 7 Srikanth Sankaran CLA 2012-03-20 11:32:59 EDT
If you plan to include a fix for this in 3.8 M7, please adjust the
target suitably, so it becomes easier to track.
Comment 8 Satyam Kandula CLA 2012-04-26 09:01:08 EDT
Created attachment 214597 [details]
Patch to fix the exception

Here is a simple patch to just fix the exception during the processing of the annotations.
apt.exe is returning null for the call to getDeclarationsAnnotatedWith() even when the processing file is just package-info.java. Hence, this patch does the same - better than throwing an exception.
Comment 9 Satyam Kandula CLA 2012-04-26 09:05:50 EDT
Created attachment 214599 [details]
Patch to support annotations on package.

This is a better patch which tries to imitate 'apt' much more further. This still doesn't fix getDeclarationsAnnotatedWith() to return package-info, but handles getPackage() and getAnnotationMirrors() on the package better.
Comment 10 Satyam Kandula CLA 2012-04-26 09:11:51 EDT
Walter, M7 is next week and we better fix this bug by this week. Sorry for pushing it so late :(. I have included here two patches. If you have sufficient time, can you look at both the patches and give your comments. We can try to give a better support in M7 if we can manage it. 
If you don't have time, please spare little time :) and do a quick check for the patch in comment 8 (Patch to fix the exception) and see if this is is good enough for 3.8. We could take care of a better fix in 3.9.  Thanks in advance.
Comment 11 Walter Harley CLA 2012-04-26 18:20:41 EDT
Apologies for slow response - will look these over tomorrow (Friday).
Comment 12 Walter Harley CLA 2012-04-27 19:29:53 EDT
The patch to fix the exception looks good.  

I glanced over the patch to add real support and I don't see anything wrong with it, but I probably would need to spend more time than I did to add much value.
Comment 13 Satyam Kandula CLA 2012-04-28 01:12:11 EDT
(In reply to comment #12)
> The patch to fix the exception looks good.
> 
> I glanced over the patch to add real support and I don't see anything wrong
> with it, but I probably would need to spend more time than I did to add much
> value.
Walter, Thanks for your comments. Filed bug 377990 to support the package-level annotation in detail.
Comment 14 Satyam Kandula CLA 2012-04-28 01:13:33 EDT
Released the 'patch to fix the exception' using commit b308074bc5b31f3a5857e47f87565296fbba4a15.
Comment 15 Srikanth Sankaran CLA 2012-04-30 05:17:48 EDT
Verified for 3.8 M7 using build id: I20120429-2000