Java Dynamic Proxy Issues

1. Does a dynamic proxy instance use a subclass of the target class? The java document says that the proxy instance implements a "list of interfaces", says nothing about the subclass, but through debugging I saw that the proxy instance inherits the properties of the target class. What does a list of interfaces mean? Is it possible to exclude interfaces implemented by the target class?

2. Can I refer to specific class methods on a proxy instance?

3. I think that a dynamic proxy is a proxy invocation of interface methods and not a proxy of the target class, is it true (I am deeply infected with the idea of ​​a hibernate proxy object)?

+4
source share
2 answers

If you are talking about the java.lang.reflect.Proxy class: there is no such thing as a β€œtarget class” at all.

A proxy server is created by specifying a list of interfaces that the proxy object will implement and a call handler, invoke() all method calls to the proxy will be sent. The call handler can do anything with them, including forwarding them to an instance of the "target class" that contains the link.

+5
source
  • I think you misunderstood. Each Class object passed to getProxyClass () must be a class object for an interface, not a specific class. Therefore String.class will not be a valid argument, but List.class will. As the saying goes: "All class objects in an array of interfaces should represent interfaces, not classes or primitive types." As a result, subclasses are irrelevant.

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

  • Right.

+1
source

Source: https://habr.com/ru/post/1306899/


All Articles