简体   繁体   中英

Java dynamic proxy questions

1.Does dynamic proxy instance 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM