10

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)
eebbesen
  • 5,070
  • 8
  • 48
  • 70
user
  • 245
  • 1
  • 5
  • 13
  • 2
    It is pretty difficult to figure out what you mean, please be a bit more specific. In what context? Do you want to parse Java source files? Do you want this information in runtime? What do you want this for? – ppeterka Dec 01 '12 at 11:40
  • It is really difficult to figure out the problem – Vishal Dec 01 '12 at 11:42
  • It's tagged *bytecode*, so I guess he wants to analyze the .class file – Raffaele Dec 01 '12 at 12:07
  • Javassist might be of use: http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/ – NPE Dec 01 '12 at 12:48

1 Answers1

19

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   
Oleg Šelajev
  • 3,530
  • 1
  • 17
  • 25
  • 1
    thank you very much, it's really what I'm looking for. since you seem to know very well _javassist_, you can apply the _methodfinder_ for methods of classes (such as java.lang.Date) present in the libraries? – user Dec 03 '12 at 10:58
  • yes you can, CtClass ctClass = cp.get("java.util.Date"); and you're good to go. – Oleg Šelajev Dec 03 '12 at 16:04
  • javassist also allows you to create a parsing method parameters? For example, from (ILjava/lang/String;I) is obtained (int, java.lang.String, int) – user Dec 04 '12 at 00:01
  • I don't know if the detailed API exists, but m.getMethod().getLongName() will give you human-readable string, like: java.io.PrintStream.println(java.lang.String) – Oleg Šelajev Dec 04 '12 at 09:21