Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » AspectJ » 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 #707755] Tue, 02 August 2011 02:41 Go to next message
sagar  is currently offline sagar Friend
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 #708488 is a reply to message #707755] Tue, 02 August 2011 21:46 Go to previous messageGo to next message
Andrew Eisenberg is currently offline Andrew EisenbergFriend
Messages: 382
Registered: July 2009
Senior Member
I don't understand what you are trying to do or what you are having a problem with.

Have you created an AST using the ASTParser class? If so, you can use an ASTVisitor to visit the class.

Please note that this is the AJDT forum (Aspectj development tools). Java questions should go on the JDT forum.
Re: abstract syntax tree: How to parse annotations? [message #708500 is a reply to message #708488] Tue, 02 August 2011 22:07 Go to previous messageGo to next message
sagar  is currently offline sagar Friend
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?
Re: abstract syntax tree: How to parse annotations? [message #708544 is a reply to message #708500] Tue, 02 August 2011 23:21 Go to previous message
Andrew Eisenberg is currently offline Andrew EisenbergFriend
Messages: 382
Registered: July 2009
Senior Member
A note on terminology: the methods and annotations are already parsed after using the ASTParser. What you need to do is visit them. See this article on the ASTParser:

http://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation_AST/index.html

Also, google for Eclipse ASTVisitor for more information. The ASTVisitor uses a visitor pattern to access the AST. You may want to familiarize yourself with this pattern in order to understand how the ASTVisitor works.
Previous Topic:Weaving bundles using equinox aspects and AJDT
Next Topic:CompilationParticipant
Goto Forum:
  


Current Time: Thu Apr 25 20:02:35 GMT 2024

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

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

Back to the top