I use java.lang.reflect.Proxy to proxy objects.
I have this class:
public class TransportableImpl extends Transportable{
public class OrderInvoker extends InvocationHandler{
...
}
}
Here i build the Proxy:
Transportable t = new TransportableImpl();
Order myOrder = new OrderImpl();
Class proxyClass = Proxy.getProxyClass(getClass().getClassLoader(), Transportable.class, Order.class);
Object serializable = proxyClass.getConstructor(new Class[]{InvocationHandler.class}).newInstance(t.new OrderInvoker(myOrder));
Problem is: Class is raw type and
Class<? extends Order & Transportable> proxyClass =
(Class<? extends Order & Transportable>)
Proxy.getProxyClass(getClass().getClassLoader(),
Transportable.class, Order.class);
is hard to read.
Any ideas?
The
Proxy#getProxyClass(ClassLoader, Class)method is declared asIts return type is therefore
Class<?>. The normal syntax would beTechnically you could do (with a warning)
but that gains you nothing as you will pretty much never need to use that
Tvariable. TheClassclass provides very little methods that makes use of it, namelygetConstructor(Class...)andnewInstance(). But again, the whole point of reflection is that you only know the class types at run time, not at compile time where generics are useful.