ASTParser: binding between statements [message #1262692] |
Mon, 03 March 2014 17:47 |
Eclipse User |
|
|
|
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();
}
}
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.03252 seconds