1 package net.sf.cglib.proxy;
2
3 import net.sf.cglib.core.*;
4 import java.lang.reflect.Method;
5 import java.util.*;
6 import org.objectweb.asm.Type;
7
8 class InvocationHandlerGenerator
9 implements CallbackGenerator
10 {
11 public static final InvocationHandlerGenerator INSTANCE = new InvocationHandlerGenerator();
12
13 private static final Type INVOCATION_HANDLER =
14 TypeUtils.parseType("net.sf.cglib.proxy.InvocationHandler");
15 private static final Type UNDECLARED_THROWABLE_EXCEPTION =
16 TypeUtils.parseType("net.sf.cglib.proxy.UndeclaredThrowableException");
17 private static final Type METHOD =
18 TypeUtils.parseType("java.lang.reflect.Method");
19 private static final Signature INVOKE =
20 TypeUtils.parseSignature("Object invoke(Object, java.lang.reflect.Method, Object[])");
21
22 private String getFieldName(Context context, Method method) {
23 return "CGLIB$$METHOD_" + context.getUniqueName(method);
24 }
25
26 public void generate(ClassEmitter ce, final Context context) {
27 for (Iterator it = context.getMethods(); it.hasNext();) {
28 Method method = (Method)it.next();
29
30 String fieldName = getFieldName(context, method);
31 ce.declare_field(Constants.PRIVATE_FINAL_STATIC, fieldName, METHOD, null, null);
32
33 Type[] exceptions = ReflectUtils.getExceptionTypes(method);
34 CodeEmitter e = ce.begin_method(context.getModifiers(method),
35 ReflectUtils.getSignature(method),
36 exceptions,
37 null);
38 Block handler = e.begin_block();
39 context.emitCallback(e, context.getIndex(method));
40 e.load_this();
41 e.getfield(fieldName);
42 e.create_arg_array();
43 e.invoke_interface(INVOCATION_HANDLER, INVOKE);
44 e.unbox(Type.getType(method.getReturnType()));
45 e.return_value();
46 handler.end();
47 EmitUtils.wrap_undeclared_throwable(e, handler, exceptions, UNDECLARED_THROWABLE_EXCEPTION);
48 e.end_method();
49 }
50 }
51
52 public void generateStatic(CodeEmitter e, final Context context) {
53 for (Iterator it = context.getMethods(); it.hasNext();) {
54 Method method = (Method)it.next();
55 EmitUtils.load_method(e, method);
56 e.putfield(getFieldName(context, method));
57 }
58 }
59 }
This page was automatically generated by Maven