The point of the dynamic proxy is that you don't care about the actual class of the proxy that is being returned. You just care that it implements all desired and specified interfaces.
The Javadocs for newProxyInstance
state:
Returns:
a proxy instance with the specified invocation handler of a proxy class that is defined by the specified class loader and that implements the specified interfaces
So, newProxyInstance
returned an instance of com.sun.proxy.$Proxy0
(whatever that is, we don't really care), but it also made sure that it's a MyServelet
. You should be able to cast it to MyServelet
without a ClassCastException
, because newProxyInstance
created the class to implement that interface, and you specified that it should by passing in new Class[]{MyServelet.class}
.
For type casting not to have failed, Java generics must have inferred that T
is MyServelet
, not MyServeletImplementation
. Perhaps when you called instrument
, you did so like this:
MyServelet proxy = YourClass.instrument(new MyServeletImplementation());
So the cast was to MyServelet
which should succeed, and not to MyServeletImplementation
, which should fail.