0

in the method intercept of CGLib proxy:

public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
    // TODO Auto-generated method stub
    Performance performance = new Performance();
    performance.begin(arg0.getClass().getName() + "." + arg1.getName());
    Object result = arg3.invokeSuper(arg0, arg2);//or just arg3.invokeSuper(arg0, arg2); and return null
    performance.end();
    return result;
}

and in the method invoke of JDK proxy

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    // TODO Auto-generated method stub
    Performance performance = new Performance();
    performance.begin(target.getClass().getName() + ":" + method.getName());
    Object obj = method.invoke(target, args);//or just method.invoke(target, args); and return null
    performance.end();
    return obj;
}

but when I return null,nothing changed.So why these methods need a return value?

Lydoo
  • 15
  • 3

2 Answers2

0

If you return null from these interceptor method, then the return value of your method call on proxied class will always be null even if the actual method returned non null values.

Returning null is working for you because you may have only method which returns void but as soon as you have any return value in your method, your code will break.

barunsthakur
  • 1,196
  • 1
  • 7
  • 18
0

You are intercepting a invocation method, if you changes the result, the logic-behaviour will change. Important: If the original method returns a primitive type, you will get a pretty NullPointerExpection from a JDK proxy.

David Pérez Cabrera
  • 4,960
  • 2
  • 23
  • 37
  • thanks And in jdk method it did throw a NullpointerExpectation, but in CGLib returns 0, when i try to return int in my original method. – Lydoo Aug 28 '16 at 10:50