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)

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