Proxy类

来源:互联网 发布:mysql select into 编辑:程序博客网 时间:2024/05/22 03:11
public class Proxy extends Object implements Serializable

Proxy provides static methods for creating dynamic proxy classes and instances, and it is also the superclass of all dynamic proxy classes created by those methods.
To create a proxy for some(某个) interface Foo:

 InvocationHandler handler = new MyInvocationHandler(...);     Class proxyClass = Proxy.getProxyClass(         Foo.class.getClassLoader(), new Class[] { Foo.class });     Foo f = (Foo) proxyClass.         getConstructor(new Class[] { InvocationHandler.class }).         newInstance(new Object[] { handler });

or more simply:

     Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),                                          new Class[] { Foo.class },                                          handler);

A dynamic proxy class (simply referred to as a proxy class below) is a class that implements a list of interfaces specified at runtime when the class is created, with behavior as described below. A proxy interface is such an interface that is implemented by a proxy class. A proxy instance is an instance of a proxy class. Each proxy instance has an associated invocation handler object, which implements the interface InvocationHandler. A method invocation on a proxy instance through one of its proxy interfaces will be dispatched to the invoke method of the instance’s invocation handler, passing the proxy instance, a java.lang.reflect.Method object identifying the method that was invoked, and an array of type Object containing the arguments. The invocation handler processes the encoded method invocation as appropriate and the result that it returns will be returned as the result of the method invocation on the proxy instance.

public static Object newProxyInstance(ClassLoader loader,                      Class<?>[] interfaces,                      InvocationHandler h)                               throws IllegalArgumentException``Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler. This method is equivalent to:`
Proxy.getProxyClass(loader, interfaces).     getConstructor(new Class[] { InvocationHandler.class }).     newInstance(new Object[] { handler });

“`
Proxy.newProxyInstance throws IllegalArgumentException for the same reasons that Proxy.getProxyClass does.

Parameters:

loader - the class loader to define the proxy class
interfaces - the list of interfaces for the proxy class to implement
h - the invocation handler to dispatch method invocations to

Returns:

a proxy instance with the specified invocation handler of a proxy class that is defined by the specified class loader and that implements the specified interfaces

Throws:

IllegalArgumentException - if any of the restrictions on the parameters that may be passed to getProxyClass are violated
NullPointerException - if the interfaces array argument or any of its elements are null, or if the invocation handler, h, is null
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.