Skip to main content


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 10:56 Go to next message
katsikiwtis  is currently offline katsikiwtis Friend
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 Go to previous message
Rylan Cottrell is currently offline Rylan CottrellFriend
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: Tue Mar 19 02:41:54 GMT 2024

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

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

Back to the top