Class Enhancer


  • final class Enhancer
    extends AbstractGlueGenerator
    Generates enhanced classes.

    Each enhancer has the same number of constructors as the class it enhances, but each constructor takes an additional handler array before the rest of the expected arguments.

    Enhanced methods are overridden to call the handler with the same index as the method. The handler delegates to the interceptor stack. Once the last interceptor returns the handler will call back into the trampoline with the method index, which invokes the superclass method.

    The trampoline also provides access to constructor invokers that take a context object (the handler array) with an argument array and invokes the appropriate enhanced constructor. These invokers are used in the proxy factory to create enhanced instances.

    Enhanced classes have the following pseudo-Java structure:

     public class HostClass$$EnhancerByGuice
       extends HostClass
     {
       // InterceptorStackCallbacks, one per enhanced method
       private final InvocationHandler[] GUICE$HANDLERS;
    
       public HostClass$$EnhancerByGuice(InvocationHandler[] handlers, ...) {
          // JVM lets us store this before calling the superclass constructor
         GUICE$HANDLERS = handlers;
         super(...);
       }
    
       public static Object GUICE$TRAMPOLINE(int index, Object context, Object[] args) {
         switch (index) {
           case 0: {
             return new HostClass$$EnhancerByGuice((InvocationHandler[]) context, ...);
           }
           case 1: {
             return context.super.instanceMethod(...); // call original unenhanced method
           }
         }
         return null;
       }
    
       // enhanced method
       public final Object instanceMethod(...) {
         // pack arguments and trigger the associated InterceptorStackCallback
         return GUICE$HANDLERS[0].invoke(this, null, args);
       }
    
       // ...
     }
     
    • Constructor Summary

      Constructors 
      Constructor Description
      Enhancer​(java.lang.Class<?> hostClass, java.util.Map<java.lang.reflect.Method,​java.lang.reflect.Method> bridgeDelegates)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void enhanceConstructor​(org.objectweb.asm.ClassWriter cw, java.lang.reflect.Constructor<?> constructor)
      Generate enhanced constructor that takes a handler array along with the expected arguments.
      private void enhanceMethod​(org.objectweb.asm.ClassWriter cw, java.lang.reflect.Method method, int methodIndex)
      Generate enhanced method that calls the handler with the same index.
      private static java.lang.String[] exceptionNames​(java.lang.reflect.Executable member)
      Returns internal names of exceptions declared by the given constructor/method.
      protected void generateConstructorInvoker​(org.objectweb.asm.MethodVisitor mv, java.lang.reflect.Constructor<?> constructor)
      Generate invoker that takes a context and an argument array and calls the constructor.
      protected byte[] generateGlue​(java.util.Collection<java.lang.reflect.Executable> members)
      Generates enhancer/fast-class bytecode for the given constructors/methods.
      protected void generateMethodInvoker​(org.objectweb.asm.MethodVisitor mv, java.lang.reflect.Method method)
      Generate invoker that takes an instance and an argument array and calls the method.
      private void generateVirtualBridge​(org.objectweb.asm.ClassWriter cw, java.lang.reflect.Method bridge, java.lang.reflect.Method target)
      Override the original bridge method and replace it with virtual dispatch to the target.
      protected java.lang.invoke.MethodHandle lookupInvokerTable​(java.lang.Class<?> glueClass)
      Lookup the invoker table; this may be represented by a function or a trampoline.
      private void setupInvokerTable​(org.objectweb.asm.ClassWriter cw)
      Generate static initializer to setup invoker table based on the trampoline.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • HANDLERS_DESCRIPTOR

        private static final java.lang.String HANDLERS_DESCRIPTOR
        See Also:
        Constant Field Values
      • HANDLER_TYPE

        private static final java.lang.String HANDLER_TYPE
      • HANDLER_ARRAY_TYPE

        private static final java.lang.String HANDLER_ARRAY_TYPE
      • INVOKERS_DESCRIPTOR

        private static final java.lang.String INVOKERS_DESCRIPTOR
        See Also:
        Constant Field Values
      • CALLBACK_DESCRIPTOR

        private static final java.lang.String CALLBACK_DESCRIPTOR
        See Also:
        Constant Field Values
      • METAFACTORY_DESCRIPTOR

        private static final java.lang.String METAFACTORY_DESCRIPTOR
        See Also:
        Constant Field Values
      • INDEX_TO_INVOKER_METHOD_TYPE

        private static final org.objectweb.asm.Type INDEX_TO_INVOKER_METHOD_TYPE
      • RAW_INVOKER_METHOD_TYPE

        private static final org.objectweb.asm.Type RAW_INVOKER_METHOD_TYPE
      • INVOKER_METHOD_TYPE

        private static final org.objectweb.asm.Type INVOKER_METHOD_TYPE
      • bridgeDelegates

        private final java.util.Map<java.lang.reflect.Method,​java.lang.reflect.Method> bridgeDelegates
      • checkcastToProxy

        private final java.lang.String checkcastToProxy
    • Constructor Detail

      • Enhancer

        Enhancer​(java.lang.Class<?> hostClass,
                 java.util.Map<java.lang.reflect.Method,​java.lang.reflect.Method> bridgeDelegates)
    • Method Detail

      • generateGlue

        protected byte[] generateGlue​(java.util.Collection<java.lang.reflect.Executable> members)
        Description copied from class: AbstractGlueGenerator
        Generates enhancer/fast-class bytecode for the given constructors/methods.
        Specified by:
        generateGlue in class AbstractGlueGenerator
      • setupInvokerTable

        private void setupInvokerTable​(org.objectweb.asm.ClassWriter cw)
        Generate static initializer to setup invoker table based on the trampoline.
      • enhanceConstructor

        private void enhanceConstructor​(org.objectweb.asm.ClassWriter cw,
                                        java.lang.reflect.Constructor<?> constructor)
        Generate enhanced constructor that takes a handler array along with the expected arguments.
      • enhanceMethod

        private void enhanceMethod​(org.objectweb.asm.ClassWriter cw,
                                   java.lang.reflect.Method method,
                                   int methodIndex)
        Generate enhanced method that calls the handler with the same index.
      • generateConstructorInvoker

        protected void generateConstructorInvoker​(org.objectweb.asm.MethodVisitor mv,
                                                  java.lang.reflect.Constructor<?> constructor)
        Description copied from class: AbstractGlueGenerator
        Generate invoker that takes a context and an argument array and calls the constructor.
        Specified by:
        generateConstructorInvoker in class AbstractGlueGenerator
      • generateMethodInvoker

        protected void generateMethodInvoker​(org.objectweb.asm.MethodVisitor mv,
                                             java.lang.reflect.Method method)
        Description copied from class: AbstractGlueGenerator
        Generate invoker that takes an instance and an argument array and calls the method.
        Specified by:
        generateMethodInvoker in class AbstractGlueGenerator
      • generateVirtualBridge

        private void generateVirtualBridge​(org.objectweb.asm.ClassWriter cw,
                                           java.lang.reflect.Method bridge,
                                           java.lang.reflect.Method target)
        Override the original bridge method and replace it with virtual dispatch to the target.
      • lookupInvokerTable

        protected java.lang.invoke.MethodHandle lookupInvokerTable​(java.lang.Class<?> glueClass)
                                                            throws java.lang.Throwable
        Description copied from class: AbstractGlueGenerator
        Lookup the invoker table; this may be represented by a function or a trampoline.
        Specified by:
        lookupInvokerTable in class AbstractGlueGenerator
        Throws:
        java.lang.Throwable
      • exceptionNames

        private static java.lang.String[] exceptionNames​(java.lang.reflect.Executable member)
        Returns internal names of exceptions declared by the given constructor/method.