java.lang.reflect.InvocationHandlerjava
InvocationHandler is the interface implemented by the invocation handler of a proxy instance.數組
Each proxy instance has an associated invocation handler. When a method is invoked on a proxy instance, the method invocation is encoded and dispatched to the invoke method of its invocation handler.app
public Object invoke(Object proxy, Method method, Object[] args)dom
Parameters: proxy the proxy instance that the method was invoked onthis
method the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.spa
args an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.代理
Returns:code
the value to return from the method invocation on the proxy instance. If the declared return type of the interface method is a primitive type, then the value returned by this method must be an instance of the corresponding primitive wrapper class; otherwise, it must be a type assignable to the declared return type. If the value returned by this method is null and the interface method's return type is primitive, then a NullPointerException will be thrown by the method invocation on the proxy instance. If the value returned by this method is otherwise not compatible with the interface method's declared return type as described above, a ClassCastException will be thrown by the method invocation on the proxy instance.接口
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces,InvocationHandler h)ci
Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) 大概意思是生成一個 Proxy (代理),這個 Proxy 會實現 interfaces 數組所指定的接口,當這個 Proxy 執行指定接口的的指定方法的時候,就會調用相關聯的 InvocationHandler (處理器)的 public Object invoke(Object proxy, Method method, Object[] args) 方法。
package fileop; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.Arrays; import java.util.Random; /** * This program demonstrates the use of proxies. * @version 1.00 2000-04-13 * @author Cay Horstmann */ public class ProxyTest { public static void main(String[] args) { Object[] elements = new Object[1000]; // fill elements with proxies for the integers 1 ... 1000 for (int i = 0; i < elements.length; i++) { Integer value = i + 1; InvocationHandler handler = new TraceHandler(value); Object proxy = Proxy.newProxyInstance(null, new Class[] { Comparable.class } , handler); elements[i] = proxy; } // construct a random integer Integer key = new Random().nextInt(elements.length) + 1; // search for the key int result = Arrays.binarySearch(elements, key); // print match if found if (result >= 0) System.out.println(elements[result]); } } /** * An invocation handler that prints out the method name and parameters, then * invokes the original method */ class TraceHandler implements InvocationHandler { private Object target; /** * Constructs a TraceHandler * @param t the implicit parameter of the method call */ public TraceHandler(Object t) { target = t; } public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { // print implicit argument System.out.print(target); // print method name System.out.print("." + m.getName() + "("); // print explicit arguments 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(")"); // invoke actual method return m.invoke(target, args); } }