It seems that reflection.proxy is not what is expected when there are overridden methods. In detail, starting with a simple application:
static void debug( String fmt, Object... args ) { System.out.println( String.format(fmt,args)); } interface I { void m1(); void m2(); } static class A implements I { public void m1() { System.out.println( "A.m1" ); m2(); } public void m2() { System.out.println( "A.m2" ); } } static class B extends A { @Override public void m2() { System.out.println( "B.m2" ); } } public static void main( String[] args ) { B b = new B(); b.m1(); }
output, as expected:
A.m1 B.m2
Now we are trying to maximize the calls to all the "B b" methods. New code added:
static public class HelloInvocationHandler implements InvocationHandler { I proxied; HelloInvocationHandler( I proxied ) { this.proxied = proxied; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName(); debug( "HelloInvocationHandler: invoke method %s", methodName); return method.invoke(proxied,args); } } public static void main( String[] args ) { B b = new B(); HelloInvocationHandler handler = new HelloInvocationHandler(b); I pb = (I) Proxy.newProxyInstance( I.class.getClassLoader(), new Class[] { I.class }, handler); pb.m1(); }
and new output:
HelloInvocationHandler: invoke method m1 A.m1 B.m2
as you can see, the m2 call is not made through the proxy. If all calls to Method B were associated with a proxy server, the line string "HelloInvocationHandler: invoke method m2" should appear in the output.
Any clues?
Thanks.
source share