Getting method call information from AST

How to get the names of methods called in each method declaration of a program using the AST parser (Abstract Syntax Tree)? So far, I managed to get all the names of the method declaration and all the names of the called methods, but I want to know which method calls these methods. For example, I want to see that the method m1 calls the methods mA and mB , while the method m2 calls the methods mC and mD , etc.

[EDIT 11/9/2011 IDB, rewriting the newcomer an extended comment in the body of the original question. I hope I rewrote it correctly. I hope the author comes back and revises as necessary]:

My problem is that the (Eclipse) MethodDeclaration api does not have a GetInvokedMethodName function to call. Here is my code:

  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; } } ... 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 invoked: " + method1.getName() ); } 
+4
source share
2 answers

If you want to know which particular mB method (of all those called "mB" in your extensive array of classes) is called by m1, you need more than just an AST. You need a complete character table that maps each character to possible definitions that match it.

The process of computing such a symbol table is difficult for many languages ​​and very difficult for Java (but not as bad as for C ++). Someone has to code the rules for how an identifier is looked up before (local) areas, inheritance, overloads, implied drops, etc. And the Java reference manual devotes a significant part of its content trying to explain it. You do not want to do it yourself.

What you really need is a complete Java interface that has both AST and corresponding symbol tables for each method that you want to test. You can get this, I think, from interfaces to the (Sun?) Java compiler (I personally don’t know how to do this), from the Jikes compiler, from the Eclipse Java AST module (?), And from tools like our Java Front End . Another approach is to process class files containing method calls in the JVM form, with recommendations that all JVM commands have built using a character table.

If you want to calculate the m1 calls, then the ma calls the mQ .... mZ calls, you need a tool that is ready to read in the entire source code database right away. Compilers will not do this for you, but you can use Eclipse or our interface to do this.

+2
source

I had the same problem. This was my solution:

 final HashMap<MethodDeclaration, ArrayList<MethodInvocation>> invocationsForMethods = new HashMap<MethodDeclaration, ArrayList<MethodInvocation>>(); CompilationUnit cu = (CompilationUnit) ap.createAST(null); cu.accept(new ASTVisitor() { private MethodDeclaration activeMethod; @Override public boolean visit(MethodDeclaration node) { activeMethod = node; return super.visit(node); } @Override public boolean visit(MethodInvocation node) { if (invocationsForMethods.get(activeMethod) == null) { invocationsForMethods.put(activeMethod, new ArrayList<MethodInvocation>()); } invocationsForMethods.get(activeMethod).add(node); return super.visit(node); } }); 

Now you can request invocationsForMethods.keySet() to get all the method declarations for the AST used and invocationsForMethods.get(key) returns all method calls for the declaration specified as the key.

+3
source

Source: https://habr.com/ru/post/1380435/


All Articles