Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » ASTParser: binding between statements(Java)
ASTParser: binding between statements [message #1262692] Mon, 03 March 2014 22:47
Ines AFFO is currently offline Ines AFFO
Messages: 1
Registered: March 2014
Junior Member
I'm writing an eclipse plug-in that uses JDT ASTParser to parse a java project and collect all informations (packages, source files, methods, all kind of statements ) and the binding between all those data. My code allows me to get (I think) all those data but my problem is essentially how to get the binding between them in order to build a tree of the java project. Here is my method createAST. Help me please!

private void createAST(IPackageFragment mypackage)throws JavaModelException { for (ICompilationUnit unit : mypackage.getCompilationUnits()) {
System.out.println("\t" + "Source file " + unit.getElementName());
Document doc = new Document(unit.getSource());
System.out.println("\t" + "Has number of lines: " + doc.getNumberOfLines());

CompilationUnit parse = parse(unit);
MethodVisitor visitor = new MethodVisitor();
parse.accept(visitor);


for (MethodDeclaration method : visitor.getMethods()) {
System.out.println("\t" + "\t" + "Method name: " + method.getName()
+ " Return type: " + method.getReturnType2() + " binding: " + method.resolveBinding());

try {
Block b = method.getBody();
List L = b.statements();



for(int k =0; k< L.size();k++)
{
Statement St = (Statement) L.get(k);
rp = St.toString();



ASTParser sparser = ASTParser.newParser(AST.JLS3);
sparser.setSource(rp.toCharArray());

sparser.setKind(ASTParser.K_STATEMENTS);

Block block = (Block) sparser.createAST(null);


String str = block.statements().get(0).toString();

System.out.println( "\t \t \t " + str);

block.accept(new ASTVisitor() {


public boolean visit(VariableDeclarationFragment var) {
System.out.println(" met.var "+ var.getName().getFullyQualifiedName());
return true;
}


public boolean visit(SingleVariableDeclaration Svar) {
System.out.println(" met.Svar "+ Svar.getName().getFullyQualifiedName());
return true;
}
public boolean visit(MethodInvocation meth) {

System.out.println(" int.meth "+ meth.getName().getFullyQualifiedName() );
return true;
}


public boolean visit(QualifiedName node) {

System.out.println(" \t \t \t \t Qname: " + node.getFullyQualifiedName());

return true;
}

public boolean visit(IfStatement myif) {
System.out.println(" if.statement "+ myif.toString());
return true;
}


public boolean visit(SimpleName node) {

System.out.println(" \t \t \t \t SName: " + node.getIdentifier());

return true;
}



});

}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
}
Previous Topic:Null Analysis With JSR-305 Annotations In Java 8
Next Topic:Why was the Annotation processing made so different from javac?
Goto Forum:
  


Current Time: Thu Oct 23 05:33:13 GMT 2014

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

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