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:
String.getBytes()
String.getBytes(String)
PrintStream.println(String)
Integer.parseInt(String)
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.
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.
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.
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 ).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With