Class Proxy
- java.lang.Object
-
- manifold.ext.rt.proxy.Proxy
-
- All Implemented Interfaces:
Serializable
public class Proxy extends Object implements Serializable
Adapted from java.lang.reflect.Proxy to remove the CHECKCAST instruction if the return type is a Structural interface, otherwise the proxy method call fails on such a call because the return value doesn't nominally implement the interface. Note this Proxy class is an amalgamation of the Java 8 and Java 9+ Proxy classes to support both with a single codebase, as manifold does. This Proxy class is mostly the Java 8 one, but handles modules when running in a Java 9+ VM by calling into the Java 9+ Proxy.ProxyBuilder to provide module for the proxy.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected InvocationHandlerhthe invocation handler for this proxy instance.
-
Constructor Summary
Constructors Modifier Constructor Description protectedProxy(InvocationHandler h)Constructs a newProxyinstance from a subclass (typically, a dynamic proxy class) with the specified value for its invocation handler.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Class<?>defineProxyClass(Object module, String name, byte[] b, int off, int len, ClassLoader loader)static InvocationHandlergetInvocationHandler(Object proxy)Returns the invocation handler for the specified proxy instance.static Class<?>getProxyClass(ClassLoader loader, Class<?>... interfaces)Returns thejava.lang.Classobject for a proxy class given a class loader and an array of interfaces.static booleanisProxyClass(Class<?> cl)Returns true if and only if the specified class was dynamically generated to be a proxy class using thegetProxyClassmethod or thenewProxyInstancemethod.static ObjectnewProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler.
-
-
-
Field Detail
-
h
protected InvocationHandler h
the invocation handler for this proxy instance.
-
-
Constructor Detail
-
Proxy
protected Proxy(InvocationHandler h)
Constructs a newProxyinstance from a subclass (typically, a dynamic proxy class) with the specified value for its invocation handler.- Parameters:
h- the invocation handler for this proxy instance- Throws:
NullPointerException- if the given invocation handler,h, isnull.
-
-
Method Detail
-
getProxyClass
public static Class<?> getProxyClass(ClassLoader loader, Class<?>... interfaces) throws IllegalArgumentException
Returns thejava.lang.Classobject for a proxy class given a class loader and an array of interfaces. The proxy class will be defined by the specified class loader and will implement all of the supplied interfaces. If any of the given interfaces is non-public, the proxy class will be non-public. If a proxy class for the same permutation of interfaces has already been defined by the class loader, then the existing proxy class will be returned; otherwise, a proxy class for those interfaces will be generated dynamically and defined by the class loader.There are several restrictions on the parameters that may be passed to
Proxy.getProxyClass:- All of the
Classobjects in theinterfacesarray must represent interfaces, not classes or primitive types. - No two elements in the
interfacesarray may refer to identicalClassobjects. - All of the interface types must be visible by name through the
specified class loader. In other words, for class loader
cland every interfacei, the following expression must be true:Class.forName(i.getName(), false, cl) == i - All non-public interfaces must be in the same package; otherwise, it would not be possible for the proxy class to implement all of the interfaces, regardless of what package it is defined in.
- For any set of member methods of the specified interfaces
that have the same signature:
- If the return type of any of the methods is a primitive type or void, then all of the methods must have that same return type.
- Otherwise, one of the methods must have a return type that is assignable to all of the return types of the rest of the methods.
- The resulting proxy class must not exceed any limits imposed
on classes by the virtual machine. For example, the VM may limit
the number of interfaces that a class may implement to 65535; in
that case, the size of the
interfacesarray must not exceed 65535.
If any of these restrictions are violated,
Proxy.getProxyClasswill throw anIllegalArgumentException. If theinterfacesarray argument or any of its elements arenull, aNullPointerExceptionwill be thrown.Note that the order of the specified proxy interfaces is significant: two requests for a proxy class with the same combination of interfaces but in a different order will result in two distinct proxy classes.
- Parameters:
loader- the class loader to define the proxy classinterfaces- the list of interfaces for the proxy class to implement- Returns:
- a proxy class that is defined in the specified class loader and that implements the specified interfaces
- Throws:
IllegalArgumentException- if any of the restrictions on the parameters that may be passed togetProxyClassare violatedSecurityException- if a security manager, s, is present and any of the following conditions is met:- the given
loaderisnulland the caller's class loader is notnulland the invocation ofs.checkPermissionwithRuntimePermission("getClassLoader")permission denies access. - for each proxy interface,
intf, the caller's class loader is not the same as or an ancestor of the class loader forintfand invocation ofs.checkPackageAccess()denies access tointf.
- the given
NullPointerException- if theinterfacesarray argument or any of its elements arenull
- All of the
-
newProxyInstance
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.Proxy.newProxyInstancethrowsIllegalArgumentExceptionfor the same reasons thatProxy.getProxyClassdoes.- Parameters:
loader- the class loader to define the proxy classinterfaces- the list of interfaces for the proxy class to implementh- 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 togetProxyClassare violatedSecurityException- if a security manager, s, is present and any of the following conditions is met:- the given
loaderisnulland the caller's class loader is notnulland the invocation ofs.checkPermissionwithRuntimePermission("getClassLoader")permission denies access; - for each proxy interface,
intf, the caller's class loader is not the same as or an ancestor of the class loader forintfand invocation ofs.checkPackageAccess()denies access tointf; - any of the given proxy interfaces is non-public and the
caller class is not in the same runtime package
as the non-public interface and the invocation of
s.checkPermissionwithReflectPermission("newProxyInPackage.{package name}")permission denies access.
- the given
NullPointerException- if theinterfacesarray argument or any of its elements arenull, or if the invocation handler,h, isnull
-
isProxyClass
public static boolean isProxyClass(Class<?> cl)
Returns true if and only if the specified class was dynamically generated to be a proxy class using thegetProxyClassmethod or thenewProxyInstancemethod.The reliability of this method is important for the ability to use it to make security decisions, so its implementation should not just test if the class in question extends
Proxy.- Parameters:
cl- the class to test- Returns:
trueif the class is a proxy class andfalseotherwise- Throws:
NullPointerException- ifclisnull
-
getInvocationHandler
public static InvocationHandler getInvocationHandler(Object proxy) throws IllegalArgumentException
Returns the invocation handler for the specified proxy instance.- Parameters:
proxy- the proxy instance to return the invocation handler for- Returns:
- the invocation handler for the proxy instance
- Throws:
IllegalArgumentException- if the argument is not a proxy instanceSecurityException- if a security manager, s, is present and the caller's class loader is not the same as or an ancestor of the class loader for the invocation handler and invocation ofs.checkPackageAccess()denies access to the invocation handler's class.
-
defineProxyClass
public static Class<?> defineProxyClass(Object module, String name, byte[] b, int off, int len, ClassLoader loader)
-
-