Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find all methods called in a method?

how to take the methods of other classes invoked in a specific method?

EXAMPLE

method getItem1()

public String getItem1() throws UnsupportedEncodingException{
    String a = "2";
    a.getBytes();
    a.getBytes("we");
    System.out.println(a);
    int t = Integer.parseInt(a);
    return a;
}

The methods called in getItem1() are:

  1. String.getBytes()
  2. String.getBytes(String)
  3. PrintStream.println(String)
  4. Integer.parseInt(String)
like image 530
user Avatar asked Dec 01 '12 11:12

user


People also ask

How do you get the method name inside the method?

Method Class | getName() Method in Java Method class is helpful to get the name of methods, as a String. To get name of all methods of a class, get all the methods of that class object. Then call getName() on those method objects. Return Value: It returns the name of the method, as String.

How do I see all methods in Python?

To list the methods for this class, one approach is to use the dir() function in Python. The dir() function will return all functions and properties of the class.

Can you call methods within methods?

Many functional programming languages support method within method. But you can achieve nested method functionality in Java 7 or older version by define local classes, class within method so this does compile. And in java 8 and newer version you achieve it by lambda expression.

How do you check where all the method is called in Eclipse?

Select mymethod() and press ctrl + alt + h . To see some detailed Information about any method you can use this by selecting that particular Object or method and right click. you can see the "OpenCallHierarchy" ( Ctrl + Alt + H ).


1 Answers

I would do this with javassist.

So let's say you have the following class accessible in your classpath and want to find all methods invoked from getItem1():

class MyClass {
  public String getItem1() throws UnsupportedEncodingException{
    String a = "2";
    a.getBytes();
    a.getBytes("we");
    System.out.println(a);
    int t = Integer.parseInt(a);
    return a;
  }
}

And you have this MyClass compiled. Create another class that uses javassist api:

public class MethodFinder {

  public static void main(String[] args) throws Throwable {
    ClassPool cp = ClassPool.getDefault();
    CtClass ctClass = cp.get("MyClass");
    CtMethod method = ctClass.getDeclaredMethod("getItem1");
    method.instrument(
        new ExprEditor() {
            public void edit(MethodCall m)
                          throws CannotCompileException
            {
                System.out.println(m.getClassName() + "." + m.getMethodName() + " " + m.getSignature());
            }
        });
  }
}

the output of the MethodFinder run is:

java.lang.String.getBytes ()[B   
java.lang.String.getBytes (Ljava/lang/String;)[B   
java.io.PrintStream.println (Ljava/lang/String;)V   
java.lang.Integer.parseInt (Ljava/lang/String;)I   
like image 147
Oleg Šelajev Avatar answered Sep 28 '22 06:09

Oleg Šelajev