Class 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 Detail

    • Constructor Detail

      • Proxy

        protected Proxy​(InvocationHandler h)
        Constructs a new Proxy instance 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, is null.
    • Method Detail

      • getProxyClass

        public static Class<?> getProxyClass​(ClassLoader loader,
                                             Class<?>... interfaces)
                                      throws IllegalArgumentException
        Returns the java.lang.Class object 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 Class objects in the interfaces array must represent interfaces, not classes or primitive types.
        • No two elements in the interfaces array may refer to identical Class objects.
        • All of the interface types must be visible by name through the specified class loader. In other words, for class loader cl and every interface i, 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 interfaces array must not exceed 65535.

        If any of these restrictions are violated, Proxy.getProxyClass will throw an IllegalArgumentException. If the interfaces array argument or any of its elements are null, a NullPointerException will 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 class
        interfaces - 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 to getProxyClass are violated
        SecurityException - if a security manager, s, is present and any of the following conditions is met:
        • the given loader is null and the caller's class loader is not null and the invocation of s.checkPermission with RuntimePermission("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 for intf and invocation of s.checkPackageAccess() denies access to intf.
        NullPointerException - if the interfaces array argument or any of its elements are null
      • 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.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
        SecurityException - if a security manager, s, is present and any of the following conditions is met:
        • the given loader is null and the caller's class loader is not null and the invocation of s.checkPermission with RuntimePermission("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 for intf and invocation of s.checkPackageAccess() denies access to intf;
        • 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.checkPermission with ReflectPermission("newProxyInPackage.{package name}") permission denies access.
        NullPointerException - if the interfaces array argument or any of its elements are null, or if the invocation handler, h, is null
      • 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 the getProxyClass method or the newProxyInstance method.

        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:
        true if the class is a proxy class and false otherwise
        Throws:
        NullPointerException - if cl is null
      • 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 instance
        SecurityException - 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 of s.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)