1 16 package net.sf.cglib.proxy; 17 18 import net.sf.cglib.core.*; 19 import java.util.*; 20 import org.objectweb.asm.Type; 21 22 class InvocationHandlerGenerator 23 implements CallbackGenerator 24 { 25 public static final InvocationHandlerGenerator INSTANCE = new InvocationHandlerGenerator(); 26 27 private static final Type INVOCATION_HANDLER = 28 TypeUtils.parseType("net.sf.cglib.proxy.InvocationHandler"); 29 private static final Type UNDECLARED_THROWABLE_EXCEPTION = 30 TypeUtils.parseType("net.sf.cglib.proxy.UndeclaredThrowableException"); 31 private static final Type METHOD = 32 TypeUtils.parseType("java.lang.reflect.Method"); 33 private static final Signature INVOKE = 34 TypeUtils.parseSignature("Object invoke(Object, java.lang.reflect.Method, Object[])"); 35 36 public void generate(ClassEmitter ce, Context context, List methods) { 37 for (Iterator it = methods.iterator(); it.hasNext();) { 38 MethodInfo method = (MethodInfo)it.next(); 39 Signature impl = context.getImplSignature(method); 40 ce.declare_field(Constants.PRIVATE_FINAL_STATIC, impl.getName(), METHOD, null); 41 42 CodeEmitter e = context.beginMethod(ce, method); 43 Block handler = e.begin_block(); 44 context.emitCallback(e, context.getIndex(method)); 45 e.load_this(); 46 e.getfield(impl.getName()); 47 e.create_arg_array(); 48 e.invoke_interface(INVOCATION_HANDLER, INVOKE); 49 e.unbox(method.getSignature().getReturnType()); 50 e.return_value(); 51 handler.end(); 52 EmitUtils.wrap_undeclared_throwable(e, handler, method.getExceptionTypes(), UNDECLARED_THROWABLE_EXCEPTION); 53 e.end_method(); 54 } 55 } 56 57 public void generateStatic(CodeEmitter e, Context context, List methods) { 58 for (Iterator it = methods.iterator(); it.hasNext();) { 59 MethodInfo method = (MethodInfo)it.next(); 60 EmitUtils.load_method(e, method); 61 e.putfield(context.getImplSignature(method).getName()); 62 } 63 } 64 } 65 | Popular Tags |