2
public class A{
    void methodA(){
        add(1, 2);
        add(1.2, 2.5);
    }

    void add(int a, int b){
        // add two integers
    }

    void add(double a, double b){
        // add two double numbers
    }
}

Now I have used below code to extract method call inside a method

   new VoidVisitorAdapter<Object>() {
        @Override
        public void visit(MethodCallExpr n, Object arg) {
            super.visit(n, arg);

            System.out.println(n.getNameAsString());
        }
    }.visit(JavaParser.parse(code), null);

Now here how can I differentiate those two method call add(1, 2) and add(1.2, 2.5) inside methodA using MethodCallExpr ?

  • 3
    You do not need to. Type of your actual parameters will determine the exact method. This is the whole purpose of [method overloading](https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html). – PM 77-1 Feb 15 '18 at 14:37
  • I just wanted to know how can I differentiate those two methodcall using MethodCallExpr inside another mehtod @PM77-1 – Pritom Saha Akash Feb 15 '18 at 14:45
  • So the actual question is: How to specify parameter type?, right? – PM 77-1 Feb 15 '18 at 15:28
  • How to give two methods different signature so that I can identify those two methods as different when extracting from method calls? @PM77-1 – Pritom Saha Akash Feb 15 '18 at 15:42
  • getTypeArgs maybe http://static.javadoc.io/com.github.javaparser/javaparser-core/2.5.1/com/github/javaparser/ast/expr/MethodCallExpr.html#getTypeArgs-- – diedu Feb 15 '18 at 18:10

1 Answers1

1

You can if you use the symbol resolution features. Once you have configured symbol resolution you can simply call resolveInvokedMethod and you will get an instance of ResolvedMethodDeclaration. On that object you can call getQualifiedSignature() or examine the parameters if you prefer.

To learn how to configure the symbol resolution please refer to the documentation.

Note: I am a JavaParser contributor

Federico Tomassetti
  • 2,100
  • 1
  • 19
  • 26
  • I just wanted to know after method calls using MethodCallExpr how can I know which method it is? – Pritom Saha Akash Feb 15 '18 at 15:57
  • This is what I tried to answer to :) When you get the full signature you can easy distinguish the two methods from it. Or you can look into the type of parameters in the ResolvedMethodDeclaration. What is not clear? – Federico Tomassetti Feb 15 '18 at 16:04