开发者

Java dynamic proxy questions

开发者 https://www.devze.com 2022-12-27 08:29 出处:网络
1.Does dynamic proxy instanc开发者_StackOverflow中文版e subclass the target class? The java doc says the proxy instance implements \"a list of interfaces\", says nothing about subclassing, but throug

1.Does dynamic proxy instanc开发者_StackOverflow中文版e subclass the target class? The java doc says the proxy instance implements "a list of interfaces", says nothing about subclassing, but through debugging, I saw that the proxy instance did inherit the target class properites.What does the "a list of interfaces " mean? Can I exclude those interfaces implemented by target class ?

2.Can I invoke target class specific methods on a proxy instance?

3. I think dynamic proxy is an interface methods invocation proxy but rather than a target class proxy, is that right (I am deeply infected by hibernate proxy object concept)?


If you're talking about the java.lang.reflect.Proxy class: there's no such thing as a "target class" in general.

The proxy is constructed by specifying a list of interfaces that the proxy object will implement, and an invocation handler whose invoke() method all method calls on the proxy will be forwarded to. The invocation handler can do anything with them, including forwarding them to a "target class" instance it holds a reference to.


  1. I think you have misunderstood. Every Class object passed to getProxyClass() must be a class object for an interface, not a concrete class. So String.class would not be a valid argument but List.class would be. As it says "All of the Class objects in the interfaces array must represent interfaces, not classes or primitive types". As a result subclasses are irrelevant.

  2. No (as classes are not relevant here, only interfaces). If you need to access them add an interface.

  3. Correct.

0

精彩评论

暂无评论...
验证码 换一张
取 消