Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » AST parser (How to get info from AST)
AST parser [message #755967] Thu, 10 November 2011 05:56 Go to next message
katsikiwtis  is currently offline 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 06:12]

Report message to a moderator

Re: AST parser [message #773914 is a reply to message #755967] Mon, 02 January 2012 16:52 Go to previous message
Rylan Cottrell is currently offline Rylan Cottrell
Messages: 5
Registered: January 2012
Location: Calgary, Alberta
Junior Member

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);
}
Previous Topic:Using Run
Next Topic:bugs.eclipse.org blocks my login on one box, but logged in on the other
Goto Forum:
  


Current Time: Fri Jul 25 12:14:42 EDT 2014

Powered by FUDForum. Page generated in 0.01561 seconds