Class BytecodeGen


  • public final class BytecodeGen
    extends java.lang.Object
    Utility methods for circular proxies, faster reflection, and method interception.

    This class makes heavy use of Function and BiFunction types when interacting with generated fast-classes and enhanced proxies. This is a deliberate design decision to avoid using Guice-specific types in the generated classes. This means generated classes can be defined in the same ClassLoader as their host class without needing access to Guice's own ClassLoader. (In other words it removes any need for bridge ClassLoaders.)

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  BytecodeGen.EnhancerBuilder
      Builder of enhanced classes.
    • Constructor Summary

      Constructors 
      Constructor Description
      BytecodeGen()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static java.util.function.BiFunction<java.lang.Object,​java.lang.Object[],​java.lang.Object> enhancedConstructor​(java.util.function.Function<java.lang.String,​java.util.function.BiFunction<java.lang.Object,​java.lang.Object[],​java.lang.Object>> enhancer, java.lang.reflect.Constructor<?> constructor)
      Returns an invoker that constructs an enhanced instance.
      (package private) static BytecodeGen.EnhancerBuilder enhancerBuilder​(java.lang.Class<?> hostClass)
      Create a builder of enhancers for the given class.
      private static java.util.function.Function<java.lang.String,​java.util.function.BiFunction<java.lang.Object,​java.lang.Object[],​java.lang.Object>> fastClass​(java.lang.reflect.Executable member)
      Prepares the class declaring the given member for fast invocation using bytecode generation.
      (package private) static java.util.function.BiFunction<java.lang.Object,​java.lang.Object[],​java.lang.Object> fastConstructor​(java.lang.reflect.Constructor<?> constructor)
      Returns a fast invoker for the given constructor.
      (package private) static java.util.function.BiFunction<java.lang.Object,​java.lang.Object[],​java.lang.Object> fastMethod​(java.lang.reflect.Method method)
      Returns a fast invoker for the given method.
      static boolean isCircularProxy​(java.lang.Object object)
      Returns true if the given object is a circular proxy.
      (package private) static <T> T newCircularProxy​(java.lang.Class<T> type, java.lang.reflect.InvocationHandler handler)
      Creates a new circular proxy for the given type.
      (package private) static java.util.function.BiFunction<java.lang.Object,​java.lang.Object[],​java.lang.Object> superMethod​(java.util.function.Function<java.lang.String,​java.util.function.BiFunction<java.lang.Object,​java.lang.Object[],​java.lang.Object>> enhancer, java.lang.reflect.Method method)
      Returns an invoker that calls the original unenhanced method.
      • Methods inherited from class java.lang.Object

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

      • circularProxyTypeCache

        private static final java.util.Map<java.lang.Class<?>,​java.lang.Boolean> circularProxyTypeCache
      • ENHANCER_BY_GUICE_MARKER

        public static final java.lang.String ENHANCER_BY_GUICE_MARKER
        See Also:
        Constant Field Values
      • FASTCLASS_BY_GUICE_MARKER

        public static final java.lang.String FASTCLASS_BY_GUICE_MARKER
        See Also:
        Constant Field Values
      • FAST_CLASSES

        private static final java.lang.ClassValue<java.util.function.Function<java.lang.String,​java.util.function.BiFunction<java.lang.Object,​java.lang.Object[],​java.lang.Object>>> FAST_CLASSES
        Lazy association between classes and their generated fast-classes.
    • Constructor Detail

      • BytecodeGen

        public BytecodeGen()
    • Method Detail

      • isCircularProxy

        public static boolean isCircularProxy​(java.lang.Object object)
        Returns true if the given object is a circular proxy.
      • newCircularProxy

        static <T> T newCircularProxy​(java.lang.Class<T> type,
                                      java.lang.reflect.InvocationHandler handler)
        Creates a new circular proxy for the given type.
      • enhancerBuilder

        static BytecodeGen.EnhancerBuilder enhancerBuilder​(java.lang.Class<?> hostClass)
        Create a builder of enhancers for the given class.
      • enhancedConstructor

        static java.util.function.BiFunction<java.lang.Object,​java.lang.Object[],​java.lang.Object> enhancedConstructor​(java.util.function.Function<java.lang.String,​java.util.function.BiFunction<java.lang.Object,​java.lang.Object[],​java.lang.Object>> enhancer,
                                                                                                                                   java.lang.reflect.Constructor<?> constructor)
        Returns an invoker that constructs an enhanced instance. The invoker function accepts an array of invocation handlers plus an array of arguments for the original constructor.
      • superMethod

        static java.util.function.BiFunction<java.lang.Object,​java.lang.Object[],​java.lang.Object> superMethod​(java.util.function.Function<java.lang.String,​java.util.function.BiFunction<java.lang.Object,​java.lang.Object[],​java.lang.Object>> enhancer,
                                                                                                                           java.lang.reflect.Method method)
        Returns an invoker that calls the original unenhanced method. The invoker function accepts an enhanced instance plus an array of arguments for the original method.
      • fastConstructor

        static java.util.function.BiFunction<java.lang.Object,​java.lang.Object[],​java.lang.Object> fastConstructor​(java.lang.reflect.Constructor<?> constructor)
        Returns a fast invoker for the given constructor. The invoker function ignores the first parameter and accepts an array of arguments for the constructor in the second parameter.

        Returns null if the constructor cannot be "fast-invoked" due to visibility issues.

      • fastMethod

        static java.util.function.BiFunction<java.lang.Object,​java.lang.Object[],​java.lang.Object> fastMethod​(java.lang.reflect.Method method)
        Returns a fast invoker for the given method. The invoker function accepts an instance, which will be null for static methods, and an array of arguments for the method.

        Returns null if the method cannot be "fast-invoked" due to visibility issues.

      • fastClass

        private static java.util.function.Function<java.lang.String,​java.util.function.BiFunction<java.lang.Object,​java.lang.Object[],​java.lang.Object>> fastClass​(java.lang.reflect.Executable member)
        Prepares the class declaring the given member for fast invocation using bytecode generation.