1 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 } else { 38 int index = context.getIndex(method); 39 indexes.add(new Integer (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 )it.next()).intValue(); 54 55 String 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 |