Class AbstractGlueGenerator

  • Direct Known Subclasses:
    Enhancer, FastClass

    abstract class AbstractGlueGenerator
    extends java.lang.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 Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.concurrent.atomic.AtomicInteger COUNTER  
      protected static java.lang.String GENERATED_SOURCE  
      protected java.lang.Class<?> hostClass  
      protected java.lang.String hostName  
      protected java.lang.String proxyName  
      protected static java.lang.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.
      protected static java.lang.String TRAMPOLINE_NAME  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected AbstractGlueGenerator​(java.lang.Class<?> hostClass, java.lang.String marker)  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      private static <E extends java.lang.Throwable>
      java.lang.RuntimeException
      asIfUnchecked​(java.lang.Throwable e)
      Generics trick to get compiler to treat given exception as if unchecked (as JVM does).
      private static java.util.function.Function<java.lang.String,​java.util.function.BiFunction<java.lang.Object,​java.lang.Object[],​java.lang.Object>> bindSignaturesToInvokers​(java.util.function.ToIntFunction<java.lang.String> signatureTable, java.lang.invoke.MethodHandle invokerTable)
      Combines the signature and invoker tables into a mapping from signature to invoker.
      protected abstract 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 abstract byte[] generateGlue​(java.util.Collection<java.lang.reflect.Executable> members)
      Generates enhancer/fast-class bytecode for the given constructors/methods.
      protected abstract 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.
      protected void generateTrampoline​(org.objectweb.asm.ClassWriter cw, java.util.Collection<java.lang.reflect.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.
      java.util.function.Function<java.lang.String,​java.util.function.BiFunction<java.lang.Object,​java.lang.Object[],​java.lang.Object>> glue​(java.util.NavigableMap<java.lang.String,​java.lang.reflect.Executable> glueMap)
      Generates the enhancer/fast-class and returns a mapping from signature to invoker.
      protected abstract java.lang.invoke.MethodHandle lookupInvokerTable​(java.lang.Class<?> glueClass)
      Lookup the invoker table; this may be represented by a function or a trampoline.
      private static java.lang.String proxyName​(java.lang.String hostName, java.lang.String marker, int hash)
      Generates a unique name based on the original class name and marker.
      • Methods inherited from class java.lang.Object

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

      • GENERATED_SOURCE

        protected static final java.lang.String GENERATED_SOURCE
        See Also:
        Constant Field Values
      • TRAMPOLINE_NAME

        protected static final java.lang.String TRAMPOLINE_NAME
        See Also:
        Constant Field Values
      • TRAMPOLINE_DESCRIPTOR

        protected static final java.lang.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:
        Constant Field Values
      • hostClass

        protected final java.lang.Class<?> hostClass
      • hostName

        protected final java.lang.String hostName
      • proxyName

        protected final java.lang.String proxyName
      • COUNTER

        private static final java.util.concurrent.atomic.AtomicInteger COUNTER
    • Constructor Detail

      • AbstractGlueGenerator

        protected AbstractGlueGenerator​(java.lang.Class<?> hostClass,
                                        java.lang.String marker)
    • Method Detail

      • proxyName

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

        public final java.util.function.Function<java.lang.String,​java.util.function.BiFunction<java.lang.Object,​java.lang.Object[],​java.lang.Object>> glue​(java.util.NavigableMap<java.lang.String,​java.lang.reflect.Executable> glueMap)
        Generates the enhancer/fast-class and returns a mapping from signature to invoker.
      • generateGlue

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

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

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

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

        protected final void generateTrampoline​(org.objectweb.asm.ClassWriter cw,
                                                java.util.Collection<java.lang.reflect.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,
                                                           java.lang.reflect.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,
                                                      java.lang.reflect.Method method)
        Generate invoker that takes an instance and an argument array and calls the method.