-4

i have this code i need to print no methods found if no methods are found how to do it.

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());
        }
    });

} }

  • Have you tried reading about the reflection in Java? – Yassin Hajaj Sep 17 '15 at 20:57
  • 1
    You need a byte code library, like [Apache Commons BCEL™](https://commons.apache.org/proper/commons-bcel/). – Andreas Sep 17 '15 at 20:58
  • 1
    @YassinHH Not relevant, because reflection requires the class to be in the classpath, which is not the case here, and it can't give you the byte code instructions. – Andreas Sep 17 '15 at 21:00
  • @Andreas Ok thank you for the answer – Yassin Hajaj Sep 17 '15 at 21:02
  • You may also go and study the [Class File Format](http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html) and the [JVM Instruction Set](http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html) and write a parser yourself without the aid of a 3rd party library. But it’s a mystery how you could “search everywhere” but not find neither, one of the dozen libraries nor the documentation… – Holger Sep 18 '15 at 15:05
  • I have changed my question if anyone know how to get this done please help me!!! – Kandekumbura Sep 21 '15 at 11:26
  • i have this code i need to print no methods found if no methods are found how to do it – Kandekumbura Sep 21 '15 at 17:40
  • You mean if you haven't processed a single methodCall you want to print something? Or you want to print something if you don't find the method getItem1 ? – pabrantes Sep 22 '15 at 12:14
  • print something if the given method doesnt process a single methodCall – Kandekumbura Sep 23 '15 at 11:35

1 Answers1

0

Javassist doesn't allow you to achieve your requirements out of the box. But you can easily extend the behaviour of ExprEditor to achieve it.

The first step is to create an ExprEditor subclass,let's call it ExprEditorMethodCallAware. The only thing you need to know is that ExprEditor's entry point is called doit as you can easily see in the code.

public class ExprEditorMethodCallAware extends ExprEditor {

    private boolean processedMethodCalls;

    public void setProcessedMethodCalls(boolean processedMethodCalls) {
        this.processedMethodCalls = processedMethodCalls;
    }

    public boolean isProcessedMethodCalls() {
        return this.processedMethodCalls;
    }

    @Override
    public boolean doit(CtClass arg0, MethodInfo arg1)
            throws CannotCompileException {
        processedMethodCalls = false;
        boolean doit = super.doit(arg0, arg1);
        return doit;
    }
}

Now with this small nifty trick you rewrite your code as the following:

 /// ... your existing code

 // we create an instance using our base class instead of ExprEditor
 ExprEditorMethodCallAware exprEditor =    new ExprEditorMethodCallAware() {
    public void edit(MethodCall m)
                  throws CannotCompileException
    {
        // notice the set
        setProcessedMethodCalls(true);
        System.out.println(m.getClassName() + "." + m.getMethodName() + " " + m.getSignature());
    }
 };

// we now instrument the code as you were already doing it
method.instrument(exprEditor);

// And now you check if there were or not methodCalls processed 
if(!exprEditor.isProcessedMethodCalls()) {
   System.out.println("No methodCalls found in " + method.getMethodName());
}
pabrantes
  • 2,161
  • 1
  • 28
  • 34