Community
Participate
Working Groups
Build Identifier: 3.6.1 I am using JDT, to parse the annotations written using COFOJA in eclipse, but I am not getting any results when using annotations methods in JDT. I have written MyVisitor class which extends ASTVisitor. kindly see the attached files. Thanks Reproducible: Always Steps to Reproduce: 1.Copy/load both files Test1.java and MyVisitor.java and InputFile.java in eclipse. 2. Execute Test1.java 3. Output will be displayed , which shows parsing of methods from InputFile.java, but it does not parse Annotations of method.
Created attachment 201033 [details] Above is main program which uses InputFile.java as input to parse it , and MyVisitor.java code given for visitor class InputFile.java import java.util.*; import com.google.java.contract.Requires; import com.google.java.contract.Ensures; public class InputFile{ int cnt=10; Vector v; public void display(){ int dispVar=0; System.out.println("Value of count is :"+ cnt); } @Ensures({"result==a+b"}) public int add(int a,int b){ int addVar=0; int result = a + b; System.out.println("Value of addition is:"+ result); return result; } public static void main(String args[]) { InputFile isfObj = new InputFile(); isfObj.display(); isfObj.add(10,5); } }
Created attachment 201034 [details] MyVisitor.java is visitor class to parse methods, annotations
Created attachment 201035 [details] InputFile.java is the input java file to be parsed by Test1.java using MyVisitor.java as visitor
What exactly are you seeing? When I run Test1.java (after creating a properly configured project) I see: sma length:1 sma ismarkeranno:false sma is annotation:true sma isNormal anno:false mdf lllength:5 mdf get length:6 mdfto string:public mdf get length:6 mdfto string:public mdf get length:6 mdfto string:public mdf get length:6 mdfto string:public mdf get length:6 mdfto string:static ann(0)public After for --Ann:[public] After for --Ann Size:1 Method name: display Return type2: void ann(0)@Ensures({"result==a+b"}) Exception in thread "main" java.lang.ClassCastException: org.eclipse.jdt.core.dom.SingleMemberAnnotation cannot be cast to org.eclipse.jdt.core.dom.Modifier at Test1.main(Test1.java:83) Looks like annotations are indeed seen. I should add that this is when using the latest of JDT/Core. Please paste the output showing the bug. Note, that the CCE is due to a wrong unchecked assignment: method.modifiers() is not List<Modifier> but List<IExtendedModifier>.
Hey..thanks stephan for reply. I am getting following output after executing Test1.java sma length:0 mdf lllength:5 mdf get length:6 mdfto string:public mdf get length:6 mdfto string:public mdf get length:6 mdfto string:public mdf get length:6 mdfto string:public mdf get length:6 mdfto string:static ann(0)public After for --Ann:[public] After for --Ann Size:1 Method name: display Return type2: void ann(0)public After for --Ann:[public] After for --Ann Size:1 Method name: add Return type2: int ann(0)public ann(1)static After for --Ann:[public, static] After for --Ann Size:2 Method name: main Return type2: void
(In reply to comment #5) > Hey..thanks stephan for reply. > I am getting following output after executing Test1.java > [...] I don't know how you produce that result. I've now tried several versions of jdt.core and for me all produce the result in comment 4. You may want to add System.out.println(cu); right after creating the AST to make sure it has been parsed properly (should include the annotations in the output). So far I cannot reproduce the problem.
If AST creation doesn't include the annotations try debugging into the following call chain (line numbers from version 3.6.1): SingleMemberAnnotation.<init>(AST) line: 89 ASTConverter.convert(SingleMemberAnnotation) line: 2393 ASTConverter.convert(Annotation) line: 810 ASTConverter.setModifiers(BodyDeclaration, Annotation[], int) line: 4439 ASTConverter.setModifiers(MethodDeclaration, AbstractMethodDeclaration) line: 4528 ASTConverter.convert(AbstractMethodDeclaration) line: 424 ASTConverter.buildBodyDeclarations(TypeDeclaration, AbstractTypeDeclaration) line: 180 ASTConverter.convert(TypeDeclaration) line: 2709 ASTConverter.convert(CompilationUnitDeclaration, char[]) line: 1266 CompilationUnitResolver.convert(CompilationUnitDeclaration, char[], int, Map, boolean, WorkingCopyOwner, DefaultBindingResolver$BindingTables, int, IProgressMonitor, boolean) line: 289 ASTParser.internalCreateAST(IProgressMonitor) line: 1201 ASTParser.createAST(IProgressMonitor) line: 801 Test1.main(String[]) line: 51 Depending on your project setup you may have to tell Eclipse where to find the sources for jdt.core, you'll find them in org.eclipse.jdt.core.source_3.6.1.v_A68_R36x.jar. If you don't see the above call chain, try to find out where it stops. BTW, how did you configure the project to find the jdt.core? This should allow you to find out which exact version you are using.
Sagar has reported that it's working for him now. Closing.
Verified for 3.8M2 as the initial reporter confirmed that it is working now.