Class AbstractGlueGenerator

java.lang.Object
com.google.inject.internal.aop.AbstractGlueGenerator
Direct Known Subclasses:
Enhancer, FastClass

abstract class AbstractGlueGenerator extends Object
Support code for generating enhancer/fast-class glue.

Each glue class has a trampoline that accepts an index, context object, and argument array:

 public static Object GUICE$TRAMPOLINE(int index, Object context, Object[] args) {
   switch (index) {
     case 0: {
       return ...;
     }
     case 1: {
       return ...;
     }
   }
   return null;
 }
 
Each indexed statement in the trampoline invokes a constructor or method, returning the result. The expected context object depends on the statement; it could be the invocation target, some additional constructor context, or it may be unused. Arguments are unpacked from the array onto the call stack, unboxing or casting them as necessary. Primitive results are autoboxed before being returned.

Where possible the trampoline is converted into a lookup Function mapping an integer to an invoker function, each invoker represented as a BiFunction that accepts a context object plus argument array and returns the result. These functional interfaces are used to avoid introducing a dependency from the glue class to Guice specific types. This means the glue class can be loaded anywhere that can see the host class, it doesn't need access to Guice's own ClassLoader. (In other words it removes any need for bridge ClassLoaders.)

  • Field Details

    • GENERATED_SOURCE

      protected static final String GENERATED_SOURCE
      See Also:
    • TRAMPOLINE_NAME

      protected static final String TRAMPOLINE_NAME
      See Also:
    • TRAMPOLINE_DESCRIPTOR

      protected static final String TRAMPOLINE_DESCRIPTOR
      The trampoline method takes an index, along with a context object and an array of argument objects, and invokes the appropriate constructor/method returning the result as an object.
      See Also:
    • hostClass

      protected final Class<?> hostClass
    • hostName

      protected final String hostName
    • proxyName

      protected final String proxyName
    • COUNTER

      private static final AtomicInteger COUNTER
  • Constructor Details

    • AbstractGlueGenerator

      protected AbstractGlueGenerator(Class<?> hostClass, String marker)
  • Method Details

    • proxyName

      private static String proxyName(String hostName, String marker, int hash)
      Generates a unique name based on the original class name and marker.
    • glue

      Generates the enhancer/fast-class and returns a mapping from signature to invoker.
    • generateGlue

      protected abstract byte[] generateGlue(Collection<Executable> members)
      Generates enhancer/fast-class bytecode for the given constructors/methods.
    • lookupInvokerTable

      protected abstract MethodHandle lookupInvokerTable(Class<?> glueClass) throws Throwable
      Lookup the invoker table; this may be represented by a function or a trampoline.
      Throws:
      Throwable
    • bindSignaturesToInvokers

      private static Function<String,BiFunction<Object,Object[],Object>> bindSignaturesToInvokers(ToIntFunction<String> signatureTable, MethodHandle invokerTable)
      Combines the signature and invoker tables into a mapping from signature to invoker.
    • asIfUnchecked

      private static <E extends Throwable> RuntimeException asIfUnchecked(Throwable e) throws E
      Generics trick to get compiler to treat given exception as if unchecked (as JVM does).
      Throws:
      E extends Throwable
    • generateTrampoline

      protected final void generateTrampoline(org.objectweb.asm.ClassWriter cw, Collection<Executable> members)
      Generate trampoline that takes an index, along with a context object and array of argument objects, and invokes the appropriate constructor/method returning the result as an object.
    • generateConstructorInvoker

      protected abstract void generateConstructorInvoker(org.objectweb.asm.MethodVisitor mv, Constructor<?> constructor)
      Generate invoker that takes a context and an argument array and calls the constructor.
    • generateMethodInvoker

      protected abstract void generateMethodInvoker(org.objectweb.asm.MethodVisitor mv, Method method)
      Generate invoker that takes an instance and an argument array and calls the method.