KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
19 import net.sf.cglib.core.*;
20 import org.objectweb.asm.Type;
21
22 class DispatcherGenerator implements CallbackGenerator {
23     public static final DispatcherGenerator INSTANCE =
24       new DispatcherGenerator(false);
25     public static final DispatcherGenerator PROXY_REF_INSTANCE =
26       new DispatcherGenerator(true);
27
28     private static final Type DISPATCHER =
29       TypeUtils.parseType("net.sf.cglib.proxy.Dispatcher");
30     private static final Type PROXY_REF_DISPATCHER =
31       TypeUtils.parseType("net.sf.cglib.proxy.ProxyRefDispatcher");
32     private static final Signature LOAD_OBJECT =
33       TypeUtils.parseSignature("Object loadObject()");
34     private static final Signature PROXY_REF_LOAD_OBJECT =
35       TypeUtils.parseSignature("Object loadObject(Object)");
36
37     private boolean proxyRef;
38
39     private DispatcherGenerator(boolean proxyRef) {
40         this.proxyRef = proxyRef;
41     }
42
43     public void generate(ClassEmitter ce, Context context, List methods) {
44         for (Iterator it = methods.iterator(); it.hasNext();) {
45             MethodInfo method = (MethodInfo)it.next();
46             if (!TypeUtils.isProtected(method.getModifiers())) {
47                 CodeEmitter e = context.beginMethod(ce, method);
48                 context.emitCallback(e, context.getIndex(method));
49                 if (proxyRef) {
50                     e.load_this();
51                     e.invoke_interface(PROXY_REF_DISPATCHER, PROXY_REF_LOAD_OBJECT);
52                 } else {
53                     e.invoke_interface(DISPATCHER, LOAD_OBJECT);
54                 }
55                 e.checkcast(method.getClassInfo().getType());
56                 e.load_args();
57                 e.invoke(method);
58                 e.return_value();
59                 e.end_method();
60             }
61         }
62     }
63
64     public void generateStatic(CodeEmitter e, Context context, List methods) { }
65 }
66
Popular Tags