Class Enhancer
- java.lang.Object
-
- com.google.inject.internal.aop.AbstractGlueGenerator
-
- com.google.inject.internal.aop.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 Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.reflect.Method,java.lang.reflect.Method>
bridgeDelegates
private static java.lang.String
CALLBACK_DESCRIPTOR
private java.lang.String
checkcastToProxy
private static java.lang.String
HANDLER_ARRAY_TYPE
private static java.lang.String
HANDLER_TYPE
private static java.lang.String
HANDLERS_DESCRIPTOR
private static java.lang.String
HANDLERS_NAME
private static org.objectweb.asm.Type
INDEX_TO_INVOKER_METHOD_TYPE
private static org.objectweb.asm.Type
INVOKER_METHOD_TYPE
private static java.lang.String
INVOKERS_DESCRIPTOR
private static java.lang.String
INVOKERS_NAME
private static java.lang.String
METAFACTORY_DESCRIPTOR
private static org.objectweb.asm.Type
RAW_INVOKER_METHOD_TYPE
-
Fields inherited from class com.google.inject.internal.aop.AbstractGlueGenerator
GENERATED_SOURCE, hostClass, hostName, proxyName, TRAMPOLINE_DESCRIPTOR, TRAMPOLINE_NAME
-
-
Constructor Summary
Constructors Constructor Description Enhancer(java.lang.Class<?> hostClass, java.util.Map<java.lang.reflect.Method,java.lang.reflect.Method> bridgeDelegates)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
enhanceConstructor(org.objectweb.asm.ClassWriter cw, java.lang.reflect.Constructor<?> constructor)
Generate enhanced constructor that takes a handler array along with the expected arguments.private void
enhanceMethod(org.objectweb.asm.ClassWriter cw, java.lang.reflect.Method method, int methodIndex)
Generate enhanced method that calls the handler with the same index.private static java.lang.String[]
exceptionNames(java.lang.reflect.Executable member)
Returns internal names of exceptions declared by the given constructor/method.protected 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 byte[]
generateGlue(java.util.Collection<java.lang.reflect.Executable> members)
Generates enhancer/fast-class bytecode for the given constructors/methods.protected 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.private void
generateVirtualBridge(org.objectweb.asm.ClassWriter cw, java.lang.reflect.Method bridge, java.lang.reflect.Method target)
Override the original bridge method and replace it with virtual dispatch to the target.protected java.lang.invoke.MethodHandle
lookupInvokerTable(java.lang.Class<?> glueClass)
Lookup the invoker table; this may be represented by a function or a trampoline.private void
setupInvokerTable(org.objectweb.asm.ClassWriter cw)
Generate static initializer to setup invoker table based on the trampoline.-
Methods inherited from class com.google.inject.internal.aop.AbstractGlueGenerator
generateTrampoline, glue
-
-
-
-
Field Detail
-
HANDLERS_NAME
private static final java.lang.String HANDLERS_NAME
- See Also:
- Constant Field Values
-
HANDLERS_DESCRIPTOR
private static final java.lang.String HANDLERS_DESCRIPTOR
- See Also:
- Constant Field Values
-
HANDLER_TYPE
private static final java.lang.String HANDLER_TYPE
-
HANDLER_ARRAY_TYPE
private static final java.lang.String HANDLER_ARRAY_TYPE
-
INVOKERS_NAME
private static final java.lang.String INVOKERS_NAME
- See Also:
- Constant Field Values
-
INVOKERS_DESCRIPTOR
private static final java.lang.String INVOKERS_DESCRIPTOR
- See Also:
- Constant Field Values
-
CALLBACK_DESCRIPTOR
private static final java.lang.String CALLBACK_DESCRIPTOR
- See Also:
- Constant Field Values
-
METAFACTORY_DESCRIPTOR
private static final java.lang.String METAFACTORY_DESCRIPTOR
- See Also:
- Constant Field Values
-
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 java.util.Map<java.lang.reflect.Method,java.lang.reflect.Method> bridgeDelegates
-
checkcastToProxy
private final java.lang.String checkcastToProxy
-
-
Method Detail
-
generateGlue
protected byte[] generateGlue(java.util.Collection<java.lang.reflect.Executable> members)
Description copied from class:AbstractGlueGenerator
Generates enhancer/fast-class bytecode for the given constructors/methods.- Specified by:
generateGlue
in classAbstractGlueGenerator
-
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, java.lang.reflect.Constructor<?> constructor)
Generate enhanced constructor that takes a handler array along with the expected arguments.
-
enhanceMethod
private void enhanceMethod(org.objectweb.asm.ClassWriter cw, java.lang.reflect.Method method, int methodIndex)
Generate enhanced method that calls the handler with the same index.
-
generateConstructorInvoker
protected void generateConstructorInvoker(org.objectweb.asm.MethodVisitor mv, java.lang.reflect.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 classAbstractGlueGenerator
-
generateMethodInvoker
protected void generateMethodInvoker(org.objectweb.asm.MethodVisitor mv, java.lang.reflect.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 classAbstractGlueGenerator
-
generateVirtualBridge
private void generateVirtualBridge(org.objectweb.asm.ClassWriter cw, java.lang.reflect.Method bridge, java.lang.reflect.Method target)
Override the original bridge method and replace it with virtual dispatch to the target.
-
lookupInvokerTable
protected java.lang.invoke.MethodHandle lookupInvokerTable(java.lang.Class<?> glueClass) throws java.lang.Throwable
Description copied from class:AbstractGlueGenerator
Lookup the invoker table; this may be represented by a function or a trampoline.- Specified by:
lookupInvokerTable
in classAbstractGlueGenerator
- Throws:
java.lang.Throwable
-
exceptionNames
private static java.lang.String[] exceptionNames(java.lang.reflect.Executable member)
Returns internal names of exceptions declared by the given constructor/method.
-
-