|
|
Re: abstract syntax tree: How to parse annotations? [message #708500 is a reply to message #708488] |
Tue, 02 August 2011 22:07 |
sagar Messages: 12 Registered: August 2011 |
Junior Member |
|
|
Hey..thanks for reply Andrew. I am working on a java project in which I have to convert assertions to Code. I am writing assertions using COFOJA. and the input to my project will be the above code. given again
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" ," result= a+b" })
int add(int a, int b)
{
// empty method, code to be generated based on contract
}
}
Now, to generate code from assertion, I have to parse the annotations @requires and @ensures first. So, I am generating Abstract syntax tree using JDT in eclipse. For generating AST, I have written following code:
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.dom.*;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.internal.compiler.CompilationResult;
import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocumentAdapter;
//import org.eclipse.text.edits.TextEdit;
import org.eclipse.jface.text.IDocument;
public class Test{
public static void main(String[] args){
Test t= new Test();
t.runtest();
}
void runtest(){
Document doc = new Document("import java.util.List;\nclass X { public void disp(){ System.out.println("+"hello"+"); }\n ");
System.out.println("$$:No of lines b4 addition: "+doc.getNumberOfLines());
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setResolveBindings(true);
parser.setSource(doc.get().toCharArray());
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
cu.recordModifications();
AST ast = cu.getAST();
// to add new import declaration in code
ImportDeclaration id = ast.newImportDeclaration();
id.setName(ast.newName(new String[] {"java", "util", "Set"}));
cu.imports().add(id); // add import declaration at end
TextEdit edits = cu.rewrite(doc, null);
try {
edits.apply(doc);
} catch (MalformedTreeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("modification count: "+ ast.modificationCount());
System.out.println(doc.computeNumberOfLines("sagar"));
System.out.println("No of lines in doc after addition of 1 import is:"+doc.getNumberOfLines());
System.out.println("docToarrayis:\n"+doc.get());
System.out.println("\n New CU is:\n"+cu.toString());
}
}
I want to know, after generating AST, how can I parse the methods, the annotations of the methods and how can i get to which methods particular annotations are applied? How to use ASTVisitor? Can anyone give me skeleton of the code for using ASTVisitor?
|
|
|
|
Powered by
FUDForum. Page generated in 0.02734 seconds