In the previous two articles
Three java proxy mode
jdk dynamic proxy and cglib strengths and weaknesses and why jdk dynamic proxy interface
Discussed jdk dynamic proxy
This article from source-level look at the underlying source code, plus
System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
In essence annotations (yet) , we also have to do it, to get out of the proxy class
Proxy0.class
public final class $Proxy0 extends Proxy implements IUserDao { private static Method m1; private static Method m3; private static Method m2; private static Method m0; public $Proxy0(InvocationHandler var1) throws { great (var1); } public final boolean equals(Object var1) throws { try { return ((Boolean)super.h.invoke(this, m1, new Object[]{var1})).booleanValue(); } catch (RuntimeException | Error var3) { throw var3; } catch (Throwable var4) { throw new UndeclaredThrowableException(var4); } } public final void save() throws { try { super.h.invoke(this, m3, (Object[])null); } catch (RuntimeException | Error var2) { throw var2; } catch (Throwable var3) { throw new UndeclaredThrowableException(var3); } }
h:
public class ProxyFactory implements InvocationHandler { // maintain a target object private Object target; public ProxyFactory(Object target){ this.target = target; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object returnValue = method.invoke(target, args); return returnValue; } // generate the proxy object to the target object public Object getProxyInstance(){ return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this); } }