| Summary: | Annotations not recovered from class file while disassembling in WORKING_COPY mode | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Product: | [Eclipse Project] JDT | Reporter: | Ekta Shetty <ekta.shetty> | ||||||||
| Component: | Core | Assignee: | Ayushman Jain <amj87.iitr> | ||||||||
| Status: | CLOSED WONTFIX | QA Contact: | |||||||||
| Severity: | normal | ||||||||||
| Priority: | P3 | CC: | amj87.iitr, sebastian.zarnekow, srikanth_sankaran | ||||||||
| Version: | 3.8 | ||||||||||
| Target Milestone: | --- | ||||||||||
| Hardware: | PC | ||||||||||
| OS: | Windows XP | ||||||||||
| Whiteboard: | stalebug | ||||||||||
| Attachments: |
|
||||||||||
Can you confirm that the CompilationUnitFactory uses the correct Java language level? CompilationFactory is created by us to create compilation unit depending upon if it is class or java.
Yes, we set the compiler options to set the source level compliant with Java 1.5 or above. We tried with 1.6 too.
When we set this, the typeRoot does not have the catergorized problem created. If we don’t set it, creates the DefaultProblem object in the type.
HashMap options = new HashMap();
ASTParser newParser = ASTParser.newParser(AST.JLS3);
newParser.setSource(compilationUnit.getSource().toCharArray());
JavaCore.setComplianceOptions("1.5", options);
newParser.setCompilerOptions(options);
CompilationUnit result = (CompilationUnit) newParser.createAST(null);
result.getRoot();
IAnnotation[] annotations = iType.getAnnotations();
Hi, the problem may be in the retention property of the annotations. If they don't have the RetentionPolicy.CLASS attribute set, the annotations will not be preserved in the class file. (In reply to comment #3) > Hi, the problem may be in the retention property of the annotations. If they > don't have the RetentionPolicy.CLASS attribute set, the annotations will not be > preserved in the class file. If the Retention annotation is present on an annotation type declaration, then it should either be RetentionPolicy.CLASS or RetentionPolicy.RUNTIME. The best way to make progress is to show a (a) java file in source form that you read in binary form with ==> (b) your code snippet that is in a form that we can actually compile, run and observe. CompilationFactory is created by us to create compilation unit depending upon if it is class or java.
Yes, we set the compiler options to set the source level compliant with Java 1.5 or above. We tried with 1.6 too.
When we set this, the typeRoot does not have the catergorized problem created. If we don’t set it, creates the DefaultProblem object in the type.
HashMap options = new HashMap();
ASTParser newParser = ASTParser.newParser(AST.JLS3);
newParser.setSource(compilationUnit.getSource().toCharArray());
JavaCore.setComplianceOptions("1.5", options);
newParser.setCompilerOptions(options);
CompilationUnit result = (CompilationUnit) newParser.createAST(null);
result.getRoot();
IAnnotation[] annotations = iType.getAnnotations();
Created attachment 217536 [details]
WebServlet Class file
Trying to read annotations from the attached WebServlet class file
Thankyou for your response. I am trying to read a WebServlet.class which has RetentionPolicy.RUNTIME. Please refer :http://grepcode.com/file/repo1.maven.org/maven2/org.mortbay.jetty/servlet-api/3.0.20100224/javax/servlet/annotation/WebServlet.java#WebServlet Have Attached the WebServlet.class file (In reply to comment #7) > Thankyou for your response. > > I am trying to read a WebServlet.class which has RetentionPolicy.RUNTIME. > Please refer > :http://grepcode.com/file/repo1.maven.org/maven2/org.mortbay.jetty/servlet-api/3.0.20100224/javax/servlet/annotation/WebServlet.java#WebServlet > > Have Attached the WebServlet.class file I dont actually see any annotations in this file. Yes, the class is an annotation itself and it will be present in class files of java classes where this annotations was used, but as such this .class file does not seem to have any annotations. A good way to test if there's actually a bug in JDT is to try getting annotations from the .class file for the below code: @javax.servlet.annotation.WebServlet class Test { } You should obtain 1 annotation from Test.class Hi ,
Please refer the link i have given.
The WebServlet has annotations
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
So should return me 3 annotations.
Problem occurs when I try reading the annotations(from Test.class).It gives me nothing which should actually give me 1 annotation.
Note : Things work fine with the Test.java file.
Hi ,
Please refer the link i have given.
The WebServlet has annotations
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
So should return me 3 annotations.
Problem occurs when I try reading the annotations(from Test.class).It gives me nothing which should actually give me 1 annotation.
Note : Things work fine with the Test.java file.
(In reply to comment #9) > Hi , > > Please refer the link i have given. > The WebServlet has annotations > > @Target({ElementType.TYPE}) > @Retention(RetentionPolicy.RUNTIME) > @Documented > > So should return me 3 annotations. Oops, sorry I dont know why I ignored these. Let me investigate I tried using the class file directly to obtain the type and then called getAnnotations(), and it seems to work as expected. Perhaps something's amiss in the compilation unit factory you're using. Can you give the complete code? Alternatively, can you try IClassFile#getType(..) to get the type and then ask for annotations? IClassFile createClassFileFrom = JavaCore.createClassFileFrom(file);
IAnnotation[] annotations = createClassFileFrom.getType().getAnnotations();
The above code returns me the annotations but if I create compilation unit by
JavaCore.createClassFileFrom(file).getWorkingCopy( new WorkingCopyOwner() {}, monitor);
and then try getting annotations from the compilation unit gives me empty array.
(In reply to comment #13) > IClassFile createClassFileFrom = JavaCore.createClassFileFrom(file); > IAnnotation[] annotations = createClassFileFrom.getType().getAnnotations(); > > The above code returns me the annotations but if I create compilation unit by > > JavaCore.createClassFileFrom(file).getWorkingCopy( new > WorkingCopyOwner() {}, monitor); > > and then try getting annotations from the compilation unit gives me empty > array. For me both of these work. Can you please give me the plugin so that I can try reproducing the problem exactly as you are? Thanks! (In reply to comment #14) > For me both of these work. Can you please give me the plugin so that I can try > reproducing the problem exactly as you are? Thanks! Btw, I'm using 3.8 RC4 build while you seem to be using an older one. It'll also help if you could upgrade to a newer build and then try once. Thanks, But I am not doing anything apart from the code I have sent and I am working with eclipse 3.6 SR2 Helios since its our project requirement.Could you please check with the same. (In reply to comment #16) > Thanks, > But I am not doing anything apart from the code I have sent and I am working > with eclipse 3.6 SR2 Helios since its our project requirement.Could you please > check with the same. Can you please give me the entire code that you used, including the one to read the class file? Attach a plugin or a java file here please? It'll quickly help me reproduce the problem and tell you if this is a bug and when it was fixed. Chances are that you'll have to upgrade to a newer Eclipse build since it works fine for me in 3.8. 3.6 maintenance is now closed. Created attachment 217594 [details]
Attached the plug - in.
Hi Ayushman,
I have attached the plug-in.
Hi Ayushman, just run the RCP,then File->Open which will read the WebServlet.class file .Check OpenHandler for details. Hi Ayushman,
Awaiting your response.I have installed Version: 3.8.0
Build id: I20120608-1200, but the problem still persist when I run the RCP which I have attached.
Created attachment 217687 [details] plugin which works for class files (In reply to comment #20) > Hi Ayushman, > just run the RCP,then File->Open which will read the WebServlet.class file > .Check OpenHandler for details. Sorry for repying late. Your plugin didn't work for me because it had some dependencies. I changed the code a bit and yes, I could see that the class file approach works but compilation unit doesn't. This is because the latter approach is not correct IMO. I'd suggest you to not use the compilation unit anyway since that is for source files and not class files. Please read annotations directly from the IClassFile as suggested in comment 12. I'm attaching a slightly different version of your plugin. Let me know if this works for you and I'll close the bug. Thanks! Satyam found out that we're not recovering runtime annotations while disassembling in the WORKING_COPY mode, but we do so only in DETAILED mode. See ClassFileWorkingCopy line 95. Ekta, thanks for the bug report. Please continue to use the IClassFile directly and that should suffice. This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant. -- The automated Eclipse Genie. |
Build Identifier: Build id: 20100917-0705 Hi Team, I am creating compilation units from the jar input which consist of class and java files.My goal is to collect the annotations applied to these files. I am only able to read annotations of java file but annotations for class returns empty.Following is the snapshot of the code. ICompilationUnit compilationUnit = CompilationUnitFactory.getCompilationUnit(extractedFile, monitor); IType[] allTypes = compilationUnit.getAllTypes(); for (IType iType : allTypes) { if(iType.isAnnotation()){ IAnnotation[] annotations = iType.getAnnotations(); } Note: If compilation unit is a class ,iType.getAnnotations() returns be empty array. Please help. Thanks, Ekta Reproducible: Always