KickJava   Java API By Example, From Geeks To Geeks.

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


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.Label;
21 import org.objectweb.asm.Type;
22
23 class LazyLoaderGenerator implements CallbackGenerator {
24     public static final LazyLoaderGenerator INSTANCE = new LazyLoaderGenerator();
25
26     private static final Signature LOAD_OBJECT =
27       TypeUtils.parseSignature("Object loadObject()");
28     private static final Type LAZY_LOADER =
29       TypeUtils.parseType("net.sf.cglib.proxy.LazyLoader");
30
31     public void generate(ClassEmitter ce, Context context, List methods) {
32         Set indexes = new HashSet();
33         for (Iterator it = methods.iterator(); it.hasNext();) {
34             MethodInfo method = (MethodInfo)it.next();
35             if (TypeUtils.isProtected(method.getModifiers())) {
36                 // ignore protected methods
37
} else {
38                 int index = context.getIndex(method);
39                 indexes.add(new Integer JavaDoc(index));
40                 CodeEmitter e = context.beginMethod(ce, method);
41                 e.load_this();
42                 e.dup();
43                 e.invoke_virtual_this(loadMethod(index));
44                 e.checkcast(method.getClassInfo().getType());
45                 e.load_args();
46                 e.invoke(method);
47                 e.return_value();
48                 e.end_method();
49             }
50         }
51
52         for (Iterator it = indexes.iterator(); it.hasNext();) {
53             int index = ((Integer JavaDoc)it.next()).intValue();
54
55             String JavaDoc delegate = "CGLIB$LAZY_LOADER_" + index;
56             ce.declare_field(Constants.ACC_PRIVATE, delegate, Constants.TYPE_OBJECT, null);
57
58             CodeEmitter e = ce.begin_method(Constants.ACC_PRIVATE |
59                                             Constants.ACC_SYNCHRONIZED |
60                                             Constants.ACC_FINAL,
61                                             loadMethod(index),
62                                             null);
63             e.load_this();
64             e.getfield(delegate);
65             e.dup();
66             Label end = e.make_label();
67             e.ifnonnull(end);
68             e.pop();
69             e.load_this();
70             context.emitCallback(e, index);
71             e.invoke_interface(LAZY_LOADER, LOAD_OBJECT);
72             e.dup_x1();
73             e.putfield(delegate);
74             e.mark(end);
75             e.return_value();
76             e.end_method();
77             
78         }
79     }
80
81     private Signature loadMethod(int index) {
82         return new Signature("CGLIB$LOAD_PRIVATE_" + index,
83                              Constants.TYPE_OBJECT,
84                              Constants.TYPES_EMPTY);
85     }
86
87     public void generateStatic(CodeEmitter e, Context context, List methods) { }
88 }
89
Popular Tags