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);
   }

   // ...
 }
 
  • Field Details

    • HANDLERS_NAME

      private static final String HANDLERS_NAME
      See Also:
    • HANDLERS_DESCRIPTOR

      private static final String HANDLERS_DESCRIPTOR
      See Also:
    • HANDLER_TYPE

      private static final String HANDLER_TYPE
    • HANDLER_ARRAY_TYPE

      private static final String HANDLER_ARRAY_TYPE
    • INVOKERS_NAME

      private static final String INVOKERS_NAME
      See Also:
    • INVOKERS_DESCRIPTOR

      private static final String INVOKERS_DESCRIPTOR
      See Also:
    • CALLBACK_DESCRIPTOR

      private static final String CALLBACK_DESCRIPTOR
      See Also:
    • METAFACTORY_DESCRIPTOR

      private static final String METAFACTORY_DESCRIPTOR
      See Also:
    • 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 Map<Method,Method> bridgeDelegates
    • checkcastToProxy

      private final String checkcastToProxy
  • Constructor Details

  • Method Details

    • generateGlue

      protected byte[] generateGlue(Collection<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, Constructor<?> constructor)
      Generate enhanced constructor that takes a handler array along with the expected arguments.
    • enhanceMethod

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

      protected void generateConstructorInvoker(org.objectweb.asm.MethodVisitor mv, 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, 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, Method bridge, Method target)
      Override the original bridge method and replace it with virtual dispatch to the target.
    • lookupInvokerTable

      protected MethodHandle lookupInvokerTable(Class<?> glueClass) throws 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:
      Throwable
    • exceptionNames

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