final class Enhancer extends AbstractGlueGenerator
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); } // ... }
Modifier and Type | Field and 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 |
GENERATED_SOURCE, hostClass, hostName, proxyName, TRAMPOLINE_DESCRIPTOR, TRAMPOLINE_NAME
Constructor and Description |
---|
Enhancer(java.lang.Class<?> hostClass,
java.util.Map<java.lang.reflect.Method,java.lang.reflect.Method> bridgeDelegates) |
Modifier and Type | Method and 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.
|
generateTrampoline, glue
private static final java.lang.String HANDLERS_NAME
private static final java.lang.String HANDLERS_DESCRIPTOR
private static final java.lang.String HANDLER_TYPE
private static final java.lang.String HANDLER_ARRAY_TYPE
private static final java.lang.String INVOKERS_NAME
private static final java.lang.String INVOKERS_DESCRIPTOR
private static final java.lang.String CALLBACK_DESCRIPTOR
private static final java.lang.String METAFACTORY_DESCRIPTOR
private static final org.objectweb.asm.Type INDEX_TO_INVOKER_METHOD_TYPE
private static final org.objectweb.asm.Type RAW_INVOKER_METHOD_TYPE
private static final org.objectweb.asm.Type INVOKER_METHOD_TYPE
private final java.util.Map<java.lang.reflect.Method,java.lang.reflect.Method> bridgeDelegates
private final java.lang.String checkcastToProxy
Enhancer(java.lang.Class<?> hostClass, java.util.Map<java.lang.reflect.Method,java.lang.reflect.Method> bridgeDelegates)
protected byte[] generateGlue(java.util.Collection<java.lang.reflect.Executable> members)
AbstractGlueGenerator
generateGlue
in class AbstractGlueGenerator
private void setupInvokerTable(org.objectweb.asm.ClassWriter cw)
private void enhanceConstructor(org.objectweb.asm.ClassWriter cw, java.lang.reflect.Constructor<?> constructor)
private void enhanceMethod(org.objectweb.asm.ClassWriter cw, java.lang.reflect.Method method, int methodIndex)
protected void generateConstructorInvoker(org.objectweb.asm.MethodVisitor mv, java.lang.reflect.Constructor<?> constructor)
AbstractGlueGenerator
generateConstructorInvoker
in class AbstractGlueGenerator
protected void generateMethodInvoker(org.objectweb.asm.MethodVisitor mv, java.lang.reflect.Method method)
AbstractGlueGenerator
generateMethodInvoker
in class AbstractGlueGenerator
private void generateVirtualBridge(org.objectweb.asm.ClassWriter cw, java.lang.reflect.Method bridge, java.lang.reflect.Method target)
protected java.lang.invoke.MethodHandle lookupInvokerTable(java.lang.Class<?> glueClass) throws java.lang.Throwable
AbstractGlueGenerator
lookupInvokerTable
in class AbstractGlueGenerator
java.lang.Throwable
private static java.lang.String[] exceptionNames(java.lang.reflect.Executable member)