Bug 354067 - Parsing annotations in java source file is not giving results
Summary: Parsing annotations in java source file is not giving results
Status: VERIFIED WORKSFORME
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.8   Edit
Hardware: PC Windows Vista
: P3 major (vote)
Target Milestone: 3.8 M2   Edit
Assignee: Stephan Herrmann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-08-05 22:12 EDT by sagar CLA
Modified: 2011-09-13 12:07 EDT (History)
2 users (show)

See Also:


Attachments
Above is main program which uses InputFile.java as input to parse it , and MyVisitor.java code given for visitor class (5.08 KB, application/octet-stream)
2011-08-05 22:16 EDT, sagar CLA
no flags Details
MyVisitor.java is visitor class to parse methods, annotations (3.38 KB, application/octet-stream)
2011-08-05 22:17 EDT, sagar CLA
no flags Details
InputFile.java is the input java file to be parsed by Test1.java using MyVisitor.java as visitor (567 bytes, application/octet-stream)
2011-08-05 22:17 EDT, sagar CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description sagar CLA 2011-08-05 22:12:37 EDT
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.
Comment 1 sagar CLA 2011-08-05 22:16:36 EDT
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);
 }

}
Comment 2 sagar CLA 2011-08-05 22:17:14 EDT
Created attachment 201034 [details]
MyVisitor.java is visitor class to parse methods, annotations
Comment 3 sagar CLA 2011-08-05 22:17:53 EDT
Created attachment 201035 [details]
InputFile.java is the input java file to be parsed by Test1.java using MyVisitor.java as visitor
Comment 4 Stephan Herrmann CLA 2011-08-06 08:44:17 EDT
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>.
Comment 5 sagar CLA 2011-08-06 09:07:51 EDT
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
Comment 6 Stephan Herrmann CLA 2011-08-06 09:42:32 EDT
(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.
Comment 7 Stephan Herrmann CLA 2011-08-06 10:22:31 EDT
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.
Comment 8 Stephan Herrmann CLA 2011-08-06 12:14:59 EDT
Sagar has reported that it's working for him now. Closing.
Comment 9 Olivier Thomann CLA 2011-09-13 12:07:49 EDT
Verified for 3.8M2 as the initial reporter confirmed that it is working now.