AST parser [message #755967] |
Thu, 10 November 2011 10:56 |
katsikiwtis Messages: 2 Registered: November 2011 |
Junior Member |
|
|
Hi,how can I get the names of the methods invoked in each method declaration of a program using AST (Abstract Syntax Tree) parser? So far, I have managed to get all the names of the methods' declaration and all the names of the methods being invoked in the programme, but I want to know which method call which methods. For example, I want to know that method m1 calls methods mA and mB, while method m2 calls methods mC and mD, etc. It seems to be that my problem is that MethodDeclaration class has no GetInvokedMethodName in its api. I 've written this class:
public class MethodVisitor extends ASTVisitor {
List<MethodDeclaration> methods = new ArrayList<MethodDeclaration>();
@Override
public boolean visit(MethodDeclaration node) {
methods.add(node);
return super.visit(node);
}
public List<MethodDeclaration> getMethods() {
return methods;
}
List<MethodInvocation> methods1 = new ArrayList<MethodInvocation>();
@Override
public boolean visit(MethodInvocation node) {
methods1.add(node);
return super.visit(node);
}
public List<MethodInvocation> getMethods1() {
return methods1;
}
}
and in another class i have:
MethodVisitor visitor = new MethodVisitor();
parse.accept(visitor);
for (MethodDeclaration method : visitor
.getMethods()) {
System.out.println("Method name: "
+ method.getName()
+ " Return type: "
+ method.getReturnType2()
+ " Is constructor: "
+ method.isConstructor()
+ " Method invoked: "
+ ASTNode.METHOD_INVOCATION
);
}
for (MethodInvocation method1 : visitor
.getMethods1()) {
System.out.println("Method name: "
+ method1.getName()
);
}
[Updated on: Thu, 10 November 2011 11:12] Report message to a moderator
|
|
|
Re: AST parser [message #773914 is a reply to message #755967] |
Mon, 02 January 2012 21:52 |
|
You probably just need something simple like this.
Map<MethodDeclaration, List<MethodInvocation>> map = new HashMap<MethodDeclaration, List<MethodInvocation>>();
List<MethodInvocation> tmp;
public boolean visit(MethodDeclaration node){
tmp = new LinkedList<MethodInvocation>();
}
public void endVisit(MethodDeclaration node){
map.put(node, tmp);
tmp = null;
}
public boolean visit(MethodInvocation node) {
tmp.add(node);
}
|
|
|
Powered by
FUDForum. Page generated in 0.03808 seconds