Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » abstract syntax tree: How to parse annotations? (I am writing a java code in which contract is written for method. How can I parse the contract using abstract syntax tree by using JDT. )
abstract syntax tree: How to parse annotations? [message #707758] Tue, 02 August 2011 02:43 Go to next message
sagar  is currently offline sagar
Messages: 12
Registered: August 2011
Junior Member
I have generated Abstract syntax tree for the following code, how do I parse the annotations of the method @ensures or @requires using JDT and abstract syntax tree?


import com.google.java.contract.Ensures;
import com.google.java.contract.Requires;

public class tempContract{

public static void main(String[] args)
{
System.out.println(new Numbers1().add(-10, 5));
}
}

class Numbers1 {

@Requires({ "a > 0 ", " b > 0"})

@Ensures({ "result > a", "result > b" })

int add(int a, int b)
{
return a + b;
}
}

Kindly help. I have generated AST using JDT. but dont know the classes or methods to parse the annotations @Requires and @ensures in above code?
Re: abstract syntax tree: How to parse annotations? [message #708156 is a reply to message #707758] Tue, 02 August 2011 14:13 Go to previous messageGo to next message
Satyam Kandula is currently offline Satyam Kandula
Messages: 444
Registered: July 2009
Senior Member
After generating the AST, you can use ASTVisitor() overriding visit(SingleMemberAnnotation) and ofcourse other visit(*Annotation) functions.
Re: abstract syntax tree: How to parse annotations? [message #708313 is a reply to message #707758] Tue, 02 August 2011 17:14 Go to previous messageGo to next message
sagar  is currently offline sagar
Messages: 12
Registered: August 2011
Junior Member
Can you please provide a basic structure of the code , how do I do it? I am very new to JDT and abstract syntax tree concept. A basic structure (skeleton of the code will really help me. please.
Re: abstract syntax tree: How to parse annotations? [message #708722 is a reply to message #708313] Wed, 03 August 2011 05:47 Go to previous messageGo to next message
Satyam Kandula is currently offline Satyam Kandula
Messages: 444
Registered: July 2009
Senior Member
You can look at http://www.vogella.de/articles/EclipseJDT/article.html
Re: abstract syntax tree: How to parse annotations? [message #711313 is a reply to message #708722] Sat, 06 August 2011 02:03 Go to previous messageGo to next message
sagar  is currently offline sagar
Messages: 12
Registered: August 2011
Junior Member
Hey hi Satyam
I did try as per your instruction. still I am not able to parse the annotations.
See my code please. Below is my MyVisitor class
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.*;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor;
import org.eclipse.jdt.core.dom.ChildPropertyDescriptor;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.SimplePropertyDescriptor;
import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
import org.eclipse.jdt.core.dom.MethodDeclaration;

// class for method visitor .... use its object to let the parser accept this visitor
public class MyVisitor extends ASTVisitor
{


List<MethodDeclaration> methods = new ArrayList<MethodDeclaration>();
List<Modifier> modifier = new ArrayList<Modifier>();
List<SingleMemberAnnotation> SMAnnotation = new ArrayList<SingleMemberAnnotation>();

@Override
public boolean visit(MethodDeclaration node) {
methods.add(node);
return super.visit(node);
}

public List<MethodDeclaration> getMethods() {
return methods;
}



public boolean visit(SingleMemberAnnotation node){
SMAnnotation.add(node);
return super.visit(node);
}
public List<SingleMemberAnnotation> getSMAnnotation() {
System.out.println("sma length:" + SMAnnotation.size());
return SMAnnotation;
}


public boolean visit(Modifier node){
modifier.add(node);
return super.visit(node);
}
public List<Modifier> getModifier() {
System.out.println("mdf lllength:" + modifier.size());
return modifier;
}
}

And the above visitor class I am using in my original code, so that I should be able to parse the Annotations: I want to separate annotations and want to apply heuristics/transformation on them to generate code for an empty method.
Below is my main code which uses the visitor declared above.

import org.apache.commons.io.FileUtils;
import org.eclipse.jface.text.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.jface.text.Document;


import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.Annotation;
import org.eclipse.jdt.core.dom.BodyDeclaration;
import org.eclipse.jdt.core.dom.CastExpression;
import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;

public class Test1 {

public static void main(String args[]){
String source="";

ASTParser parser = ASTParser.newParser(AST.JLS3);

File file=new File("InputFile.java");
try {
source = FileUtils.readFileToString(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


parser.setSource(source.toCharArray());

parser.setKind(ASTParser.K_COMPILATION_UNIT);
//ASTNode node = parser.createAST(null);

// Document document = new Document("sdfd");
MyVisitor visitor = new MyVisitor(); // object of my visitor class

final CompilationUnit cu = (CompilationUnit) parser.createAST(null);

cu.accept(visitor);

List<Modifier> ann = new ArrayList<Modifier>();

for(SingleMemberAnnotation sma: visitor.getSMAnnotation()){
System.out.println("sma ismarkeranno:" +sma.isMarkerAnnotation());
System.out.println("sma is annotation:" +sma.isAnnotation());
System.out.println("sma isNormal anno:" +sma.isNormalAnnotation());

}

for(Modifier mdf: visitor.getModifier()){

System.out.println("mdf get length:" +mdf.getLength());
System.out.println("mdfto string:" +mdf.toString());
//System.out.println("sma isNormal anno:" +mdf.isNormalAnnotation());

}
for (MethodDeclaration method : visitor.getMethods()) {
ann= method.modifiers();



for (int i=0; i< ann.size(); i++)
{
System.out.println( "ann("+ i+ ")" +ann.get(i) );


if(ann.get(i).getNodeType()==ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION){

System.out.println("****Its an annotated type member method:"+method.getName());
}
if(ann.get(i).getNodeType()==ASTNode.ANNOTATION_TYPE_DECLARATION){
System.out.println("&&&Its an annotated type method:"+method.getName());
}

}// end of for
System.out.println("After for --Ann:"+ ann);
System.out.println("After for --Ann Size:"+ ann.size());
//System.out.println("Ann Size:"+ method.size());

//System.out.println("javadoc property"+method.getJavadocProperty());
System.out.print("Method name: "
+ method.getName()
+ " Return type2: "
+ method.getReturnType2()+"\n");
}

}
}

I have tried several methods above to get the annotations, but couldn't get them. Kindly help
Re: abstract syntax tree: How to parse annotations? [message #715180 is a reply to message #711313] Fri, 12 August 2011 15:36 Go to previous message
Chris Parnin is currently offline Chris Parnin
Messages: 1
Registered: August 2011
Junior Member
I've been able to get the name of the annotation and parent like this:

ASTNode node = parser.createAST(null);
node.accept(new AnnotationVisitor());

public class AnnotationVisitor extends ASTVisitor {
@Override
public boolean visit(NormalAnnotation annotation)
{
// Walk up AST until MethDecl or ClassDecl
String parent = GetEnclosingContainerName(annotation);
//annotation.getTypeNameProperty();
String property = annotation.getTypeNameProperty().toString();
Name typeName = annotation.getTypeName();
if( parent != null )
{
WriteLine(parent + ":" + property+ ":" + typeName.getFullyQualifiedName());
}
return true;
}
//NormalAnnotation;
//MarkerAnnotation;
//SingleMemberAnnotation;
Previous Topic:Creating a new Java project: Diff between two project patterns "Java project" ?
Next Topic:google app engine
Goto Forum:
  


Current Time: Thu Sep 18 21:56:27 GMT 2014

Powered by FUDForum. Page generated in 0.03047 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software