Package java.lang.reflect
Class Proxy
java.lang.Object
java.lang.reflect.Proxy
- All Implemented Interfaces:
Serializable
public class Proxy extends Object implements Serializable
Proxy defines methods for creating dynamic proxy classes and instances.
A proxy class implements a declared set of interfaces and delegates method
invocations to an InvocationHandler.- Since:
- 1.3
- See Also:
InvocationHandler, Serialized Form
-
Field Summary
Fields Modifier and Type Field Description protected InvocationHandlerhThe invocation handler on which the method calls are dispatched. -
Constructor Summary
Constructors Modifier Constructor Description protectedProxy(InvocationHandler h)Constructs a newProxyinstance with the specified invocation handler. -
Method Summary
Modifier and Type Method Description static InvocationHandlergetInvocationHandler(Object proxy)Returns the invocation handler of the specified proxy instance.static Class<?>getProxyClass(ClassLoader loader, Class<?>... interfaces)Returns the dynamically builtClassfor the specified interfaces.static booleanisProxyClass(Class<?> cl)Indicates whether or not the specified class is a dynamically generated proxy class.static ObjectnewProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)Returns an instance of the dynamically built class for the specified interfaces.
-
Field Details
-
h
The invocation handler on which the method calls are dispatched.
-
-
Constructor Details
-
Proxy
Constructs a newProxyinstance with the specified invocation handler.- Parameters:
h- the invocation handler for the newly created proxy
-
-
Method Details
-
getProxyClass
public static Class<?> getProxyClass(ClassLoader loader, Class<?>... interfaces) throws IllegalArgumentExceptionReturns the dynamically builtClassfor the specified interfaces. Creates a newClasswhen necessary. The order of the interfaces is relevant. Invocations of this method with the same interfaces but different order result in different generated classes. The interfaces must be visible from the supplied class loader; no duplicates are permitted. All non-public interfaces must be defined in the same package.- Parameters:
loader- the class loader that will define the proxy classinterfaces- an array ofClassobjects, each one identifying an interface that will be implemented by the returned proxy class- Returns:
- a proxy class that implements all of the interfaces referred to
in the contents of
interfaces - Throws:
IllegalArgumentException- if any of the interface restrictions are violatedNullPointerException- if eitherinterfacesor any of its elements arenull
-
newProxyInstance
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentExceptionReturns an instance of the dynamically built class for the specified interfaces. Method invocations on the returned instance are forwarded to the specified invocation handler. The interfaces must be visible from the supplied class loader; no duplicates are permitted. All non-public interfaces must be defined in the same package.- Parameters:
loader- the class loader that will define the proxy classinterfaces- an array ofClassobjects, each one identifying an interface that will be implemented by the returned proxy objecth- the invocation handler that handles the dispatched method invocations- Returns:
- a new proxy object that delegates to the handler
h - Throws:
IllegalArgumentException- if any of the interface restrictions are violatedNullPointerException- if the interfaces or any of its elements are null
-
isProxyClass
Indicates whether or not the specified class is a dynamically generated proxy class.- Parameters:
cl- the class- Returns:
trueif the class is a proxy class,falseotherwise- Throws:
NullPointerException- if the class isnull
-
getInvocationHandler
Returns the invocation handler of the specified proxy instance.- Parameters:
proxy- the proxy instance- Returns:
- the invocation handler of the specified proxy instance
- Throws:
IllegalArgumentException- if the suppliedproxyis not a proxy object
-