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 final Map | |
| private static final String | |
| private final String | |
| private static final String | |
| private static final String | |
| private static final String | |
| private static final String | |
| private static final Type | |
| private static final Type | |
| private static final String | |
| private static final String | |
| private static final String | |
| private static final Type |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| private void | enhanceConstructor(ClassWriter cw, Constructor<?> constructor)
Generate enhanced constructor that takes a handler array along with the expected arguments. |
| private void | enhanceMethod(ClassWriter cw, Method method, int methodIndex)
Generate enhanced method that calls the handler with the same index. |
| private static String[] | exceptionNames(Executable member)
Returns internal names of exceptions declared by the given constructor/method. |
| protected void | generateConstructorInvoker(MethodVisitor mv, Constructor<?> constructor)
Implements abstract com. |
| protected byte[] | generateGlue(Collection<Executable> members)
Implements abstract com. |
| protected void | generateMethodInvoker(MethodVisitor mv, Method method)
Implements abstract com. |
| private void | generateVirtualBridge(ClassWriter cw, Method bridge, Method target)
Override the original bridge method and replace it with virtual dispatch to the target. |
| protected MethodHandle | lookupInvokerTable(Class<?> glueClass)
Implements abstract com. |
| private void | setupInvokerTable(ClassWriter cw)
Generate static initializer to setup invoker table based on the trampoline. |
| bridgeDelegates | back to summary |
|---|---|
| private final Map<Method, Method> bridgeDelegates | |
| CALLBACK_DESCRIPTOR | back to summary |
|---|---|
| private static final String CALLBACK_DESCRIPTOR | |
| checkcastToProxy | back to summary |
|---|---|
| private final String checkcastToProxy | |
| HANDLER_ARRAY_TYPE | back to summary |
|---|---|
| private static final String HANDLER_ARRAY_TYPE | |
| HANDLER_TYPE | back to summary |
|---|---|
| private static final String HANDLER_TYPE | |
| HANDLERS_DESCRIPTOR | back to summary |
|---|---|
| private static final String HANDLERS_DESCRIPTOR | |
| HANDLERS_NAME | back to summary |
|---|---|
| private static final String HANDLERS_NAME | |
| INDEX_TO_INVOKER_METHOD_TYPE | back to summary |
|---|---|
| private static final Type INDEX_TO_INVOKER_METHOD_TYPE | |
| INVOKER_METHOD_TYPE | back to summary |
|---|---|
| private static final Type INVOKER_METHOD_TYPE | |
| INVOKERS_DESCRIPTOR | back to summary |
|---|---|
| private static final String INVOKERS_DESCRIPTOR | |
| INVOKERS_NAME | back to summary |
|---|---|
| private static final String INVOKERS_NAME | |
| METAFACTORY_DESCRIPTOR | back to summary |
|---|---|
| private static final String METAFACTORY_DESCRIPTOR | |
| RAW_INVOKER_METHOD_TYPE | back to summary |
|---|---|
| private static final Type RAW_INVOKER_METHOD_TYPE | |
| Enhancer | back to summary |
|---|---|
| pack-priv Enhancer(Class<?> hostClass, Map<Method, Method> bridgeDelegates) | |
| enhanceConstructor | back to summary |
|---|---|
| private void enhanceConstructor(ClassWriter cw, Constructor<?> constructor) Generate enhanced constructor that takes a handler array along with the expected arguments. | |
| enhanceMethod | back to summary |
|---|---|
| private void enhanceMethod(ClassWriter cw, Method method, int methodIndex) Generate enhanced method that calls the handler with the same index. | |
| exceptionNames | back to summary |
|---|---|
| private static String[] exceptionNames(Executable member) Returns internal names of exceptions declared by the given constructor/method. | |
| generateConstructorInvoker | back to summary |
|---|---|
| protected void generateConstructorInvoker(MethodVisitor mv, Constructor<?> constructor) Implements abstract com. Doc from com. Generate invoker that takes a context and an argument array and calls the constructor.
| |
| generateGlue | back to summary |
|---|---|
| protected byte[] generateGlue(Collection<Executable> members) Implements abstract com. Doc from com. Generates enhancer/fast-class bytecode for the given constructors/methods.
| |
| generateMethodInvoker | back to summary |
|---|---|
| protected void generateMethodInvoker(MethodVisitor mv, Method method) Implements abstract com. Doc from com. Generate invoker that takes an instance and an argument array and calls the method.
| |
| generateVirtualBridge | back to summary |
|---|---|
| private void generateVirtualBridge(ClassWriter cw, Method bridge, Method target) Override the original bridge method and replace it with virtual dispatch to the target. | |
| lookupInvokerTable | back to summary |
|---|---|
| protected MethodHandle lookupInvokerTable(Class<?> glueClass) throws Throwable Implements abstract com. Doc from com. Lookup the invoker table; this may be represented by a function or a trampoline.
| |
| setupInvokerTable | back to summary |
|---|---|
| private void setupInvokerTable(ClassWriter cw) Generate static initializer to setup invoker table based on the trampoline. | |