KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > proxy > InvocationHandlerGenerator


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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