1. Using a proxy, you can create a new class that implements a given set of interfaces at run time. This functionality is only necessary when the compiler cannot determine which interface to implement.
2. The calling processor is a class object that implements the Invacationhandler interface.
3. Whenever a proxy object is invoked, the Invoke method of the calling processor is called, passing the method object and the original invocation parameter to it. The calling processor must give a way to process the call.
4. To create a proxy object, you need to use the Newproxyinstance method of the proxy class.
The three parameters of the method:
A. A class loader
B. An array of class objects
C. One call processor.
5. Whenever a method is called with a proxy, the name and parameters of the method are printed and then called with value.
6. The proxy class is created when the program is run, and once created, it becomes a regular class, no different from any other class in the virtual machine.
7. All proxy classes are extended to the proxy class. Only one instance domain of a proxy class calls the processor, which is defined in the superclass of the proxy.
8. In order to perform the duties of the proxy object, any additional data required must be stored in the calling processor.
9. All proxy classes override the methods in the object class ToString, Hashcode, and equals. Other methods in the object class have not been redefined.
10. No name is defined for the proxy class, and the proxy class in the Sun virtual machine will produce a class name that begins with the string $proxy.
11. There can be only one proxy class for a particular class loader and a set of interfaces that are preset.
12. The proxy class must be public and final.
13. You can detect whether a particular class object represents a proxy class by calling the Isproxyclass method in the proxy class.
Instance Code
Test class
Import Java.lang.reflect.invocationhandler;import Java.lang.reflect.proxy;import Java.util.arrays;import Java.util.random;public class Test {public static void main (string[] args) {object[] ele = new Object[1000];for (int i = 0;i < ele.length;i++) {Integer value = i + 1;invocationhandler handler = new Tracehandler (value); Object proxy = PROXY.NEWPR Oxyinstance (NULL, New Class[]{comparable.class}, handler); ele[i] = proxy;} Integer key = new Random (). Nextint (ele.length) + 1;int result = Arrays.binarysearch (Ele, key); if (result >= 0) System.out . println (Ele[result]);}}
Function class
Import Java.lang.reflect.invocationhandler;import Java.lang.reflect.method;public class Tracehandler implements Invocationhandler {Private Object Target;public Tracehandler (Object t) {target = t;} public object Invoke (Object Proxy,method m,object[] args) throws Throwable{system.out.print (target); System.out.print ("." + m.getname () + "("); if (args! = null) {for (int i = 0;i < args.length;i++) {System.out.print (Args[i] if (i < args.length-1) System.out.print (",");}} System.out.println (")"); return M.invoke (Target,args);}}
Output Results
My Java Learning notes (19) About proxies