we can use
System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
to generate $proxy.class
file.
decomplie the .class
file, we will find this class every java methods (except native method) are invoked by InvocationHandler
public final boolean equals(Object var1) throws {
try {
return (Boolean)super.h.invoke(this, m1, new Object[]{var1});
} catch (RuntimeException | Error var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
}
public final String toString() throws {
try {
return (String)super.h.invoke(this, m2, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
super.h
object is the InvocationHandler instance.
use System.out.println(proxy)
in invocationHandler.invoke
will cause infinite recursion and StackOverflowException.
so I think the only function about proxy parameter is use getClass()
method
to get the GeneratedProxy.class
information, like which interfaces are implemented.
In InvocationHandler invoke method implemention, we can know the type of proxy object
So we can use an single InvocationHander class to deal with multiple proxy interfaces.