The JDK Proxy class only accepts interfaces in the factory method newProxyInstance().
Is there a workaround available, or alternative implementations?
The use cases are limited if I have to extract methods to an interface in order to enable them for use with a proxy. I would like to wrap them to apply annotation based actions during runtime.
public static T getProxy(T obj) {
InvocationHandler ih = new InjectProxy( obj );
ClassLoader classLoader = InjectProxy.class.getClassLoader();
return (T) Proxy.newProxyInstance( classLoader, obj.getClass().getInterfaces(), ih );
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
} Well, in principle you can use a bytecode library like ASM to create a class-based proxy yourself.
以上就是Why does JDK dynamic Proxy only work with Interfaces?的详细内容,更多请关注web前端其它相关文章!