java.lang.reflect
public class Proxy extends Object implements Serializable
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);
public
and final
,
and is neither abstract
nor an inner class.proxy instanceof Foo
will return true,
and the expression (Foo) proxy
will succeed without
a {@link ClassCastException}.In short, if a method is declared in Object (namely, hashCode, equals, or toString), then Object will be used; otherwise, the leftmost interface that inherits or declares a method will be used, even if it has a more permissive throws clause than what the proxy class is allowed. Thus, in the invocation handler, it is not always safe to assume that every class listed in the throws clause of the passed Method object can safely be thrown; fortunately, the Proxy instance is robust enough to wrap all illegal checked exceptions in {@link UndeclaredThrowableException}.
Since: 1.3
See Also: InvocationHandler UndeclaredThrowableException Class
UNKNOWN: updated to 1.5, except for the use of ProtectionDomain
Field Summary | |
---|---|
protected InvocationHandler | h
The invocation handler for this proxy instance. |
Constructor Summary | |
---|---|
protected | Proxy(InvocationHandler handler)
Constructs a new Proxy from a subclass (usually a proxy class),
with the specified invocation handler.
|
Method Summary | |
---|---|
static InvocationHandler | getInvocationHandler(Object proxy)
Returns the invocation handler for the given proxy instance. NOTE: We guarantee a non-null result if successful, but Sun allows the creation of a proxy instance with a null handler. |
static Class<?> | getProxyClass(ClassLoader loader, Class<?>... interfaces)
Returns the proxy {@link Class} for the given ClassLoader and array
of interfaces, dynamically generating it if necessary.
|
static boolean | isProxyClass(Class<?> clazz)
Returns true if and only if the Class object is a dynamically created
proxy class (created by getProxyClass or by the
syntactic sugar of newProxyInstance ).
|
static Object | newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler handler)
Combines several methods into one. |
Serial: invocation handler associated with this proxy instance
Parameters: handler the invocation handler, may be null if the subclass is not a proxy class
Throws: NullPointerException if handler is null and this is a proxy instance
NOTE: We guarantee a non-null result if successful, but Sun allows the creation of a proxy instance with a null handler. See the comments for {@link #Proxy(InvocationHandler)}.
Parameters: proxy the proxy instance, must not be null
Returns: the invocation handler, guaranteed non-null.
Throws: IllegalArgumentException if
Proxy.isProxyClass(proxy.getClass())
returns false. NullPointerException if proxy is null
There are several restrictions on this method, the violation of which will result in an IllegalArgumentException or NullPointerException:
Class.forName(i.getName(), false, loader) == i
must be true.Note that different orders of interfaces produce distinct classes.
Parameters: loader the class loader to define the proxy class in; null implies the bootstrap class loader interfaces the array of interfaces the proxy class implements, may be empty, but not null
Returns: the Class object of the proxy class
Throws: IllegalArgumentException if the constraints above were violated, except for problems with null NullPointerException if `interfaces' is null or contains a null entry
getProxyClass
or by the
syntactic sugar of newProxyInstance
).
This check is secure (in other words, it is not simply
clazz.getSuperclass() == Proxy.class
), it will not
be spoofed by non-proxy classes that extend Proxy.
Parameters: clazz the class to check, must not be null
Returns: true if the class represents a proxy class
Throws: NullPointerException if clazz is null
Proxy.getProxyClass(loader, interfaces) .getConstructor(new Class[] {InvocationHandler.class}) .newInstance(new Object[] {handler});except that it will not fail with the normal problems caused by reflection. It can still fail for the same reasons documented in getProxyClass, or if handler is null.
Parameters: loader the class loader to define the proxy class in; null implies the bootstrap class loader interfaces the array of interfaces the proxy class implements, may be empty, but not null handler the invocation handler, may not be null
Returns: a proxy instance implementing the specified interfaces
Throws: IllegalArgumentException if the constraints for getProxyClass were violated, except for problems with null NullPointerException if `interfaces' is null or contains a null entry, or if handler is null
See Also: (ClassLoader, Class[])
(Class[])
Constructor#newInstance(Object[])