Top Description Inners Fields Constructors Methods
com.google.inject.internal

public final Class BytecodeGen

extends Object
Class Inheritance
Static Imports
com.google.common.base.Preconditions.checkArgument, com.google.inject.internal.aop.ClassBuilding.buildFastClass, .ClassBuilding.canEnhance, .ClassBuilding.canFastInvoke, .ClassBuilding.signature

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.)

Authors
[email protected] (Stuart McCulloch), [email protected] (Jesse Wilson)

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
BytecodeGen.EnhancerBuilder

Builder of enhanced classes.

Field Summary

Modifier and TypeField and Description
private static final Map<Class<?>, Boolean>
private static final LoadingCache<Class<?>, BytecodeGen.EnhancerBuilder>
public static final String
private static final ClassValue<Function<String, BiFunction<Object, Object[], Object>>>
FAST_CLASSES

Lazy association between classes and their generated fast-classes.

public static final String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
pack-priv static BiFunction<Object, Object[], Object>
enhancedConstructor(Function<String, BiFunction<Object, Object[], Object>> enhancer, Constructor<?> constructor)

Returns an invoker that constructs an enhanced instance.

pack-priv static BytecodeGen.EnhancerBuilder
enhancerBuilder(Class<?> hostClass)

Create a builder of enhancers for the given class.

private static Function<String, BiFunction<Object, Object[], Object>>
fastClass(Executable member)

Prepares the class declaring the given member for fast invocation using bytecode generation.

pack-priv static BiFunction<Object, Object[], Object>
fastConstructor(Constructor<?> constructor)

Returns a fast invoker for the given constructor.

pack-priv static BiFunction<Object, Object[], Object>
fastMethod(Method method)

Returns a fast invoker for the given method.

public static boolean
isCircularProxy(Object object)

Returns true if the given object is a circular proxy.

pack-priv static <T> T
newCircularProxy(Class<T> type, InvocationHandler handler)

Creates a new circular proxy for the given type.

pack-priv static BiFunction<Object, Object[], Object>
superMethod(Function<String, BiFunction<Object, Object[], Object>> enhancer, Method method)

Returns an invoker that calls the original unenhanced method.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

circularProxyTypeCacheback to summary
private static final Map<Class<?>, Boolean> circularProxyTypeCache
ENHANCER_BUILDERSback to summary
private static final LoadingCache<Class<?>, BytecodeGen.EnhancerBuilder> ENHANCER_BUILDERS

Cache of recent EnhancerBuilders.

Uses weak values so builders can be collected after they're done enhancing.

ENHANCER_BY_GUICE_MARKERback to summary
public static final String ENHANCER_BY_GUICE_MARKER
FAST_CLASSESback to summary
private static final ClassValue<Function<String, BiFunction<Object, Object[], Object>>> FAST_CLASSES

Lazy association between classes and their generated fast-classes.

FASTCLASS_BY_GUICE_MARKERback to summary
public static final String FASTCLASS_BY_GUICE_MARKER

Constructor Detail

BytecodeGenback to summary
public BytecodeGen()

Method Detail

enhancedConstructorback to summary
pack-priv static BiFunction<Object, Object[], Object> enhancedConstructor(Function<String, BiFunction<Object, Object[], Object>> enhancer, 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.

enhancerBuilderback to summary
pack-priv static BytecodeGen.EnhancerBuilder enhancerBuilder(Class<?> hostClass)

Create a builder of enhancers for the given class.

fastClassback to summary
private static Function<String, BiFunction<Object, Object[], Object>> fastClass(Executable member)

Prepares the class declaring the given member for fast invocation using bytecode generation.

fastConstructorback to summary
pack-priv static BiFunction<Object, Object[], Object> fastConstructor(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.

fastMethodback to summary
pack-priv static BiFunction<Object, Object[], Object> fastMethod(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.

isCircularProxyback to summary
public static boolean isCircularProxy(Object object)

Returns true if the given object is a circular proxy.

newCircularProxyback to summary
pack-priv static <T> T newCircularProxy(Class<T> type, InvocationHandler handler)

Creates a new circular proxy for the given type.

superMethodback to summary
pack-priv static BiFunction<Object, Object[], Object> superMethod(Function<String, BiFunction<Object, Object[], Object>> enhancer, 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.

com.google.inject.internal back to summary

public Interface BytecodeGen.EnhancerBuilder

Known Direct Implementers
com.google.inject.internal.aop.EnhancerBuilderImpl

Builder of enhanced classes.

Method Summary

Modifier and TypeMethod and Description
public Function<String, BiFunction<Object, Object[], Object>>
buildEnhancer(BitSet methodIndices)

Generates an enhancer for the selected subset of methods.

public Method[]
getEnhanceableMethods()

Lists the methods in the host class that can be enhanced.

Method Detail

buildEnhancerback to summary
public Function<String, BiFunction<Object, Object[], Object>> buildEnhancer(BitSet methodIndices)

Generates an enhancer for the selected subset of methods.

The enhancer maps constructor and method signatures to invokers, where each invoker is represented as a BiFunction that accepts a context object and an argument array.

Constructor invokers take an array of InvocationHandlers as their context object. This is stored in the enhanced class before the original host class constructor is called, with arguments unpacked from the argument array. The enhanced instance is then returned.

Method invokers take an enhanced instance as their context object and call the original super-method with arguments unpacked from the argument array, ie. provides super-invocation.

getEnhanceableMethodsback to summary
public Method[] getEnhanceableMethods()

Lists the methods in the host class that can be enhanced.

This always includes public and protected methods that are neither static nor final.

Package-private methods can only be enhanced if they're in the same package as the host and we can define types in the same class loader with Unsafe. The finalize method can never be enhanced.