1 16 package net.sf.cglib.transform.impl; 17 18 import net.sf.cglib.transform.*; 19 import net.sf.cglib.core.*; 20 import org.objectweb.asm.ClassVisitor; 21 import org.objectweb.asm.MethodVisitor; 22 import org.objectweb.asm.Attribute; 23 import org.objectweb.asm.Type; 24 25 public class AccessFieldTransformer extends ClassEmitterTransformer { 26 private Callback callback; 27 28 public AccessFieldTransformer(Callback callback) { 29 this.callback = callback; 30 } 31 32 public interface Callback { 33 String getPropertyName(Type owner, String fieldName); 34 } 35 36 public void declare_field(int access, final String name, Type type, Object value) { 37 super.declare_field(access, name, type, value); 38 39 String property = TypeUtils.upperFirst(callback.getPropertyName(getClassType(), name)); 40 if (property != null) { 41 CodeEmitter e; 42 e = begin_method(Constants.ACC_PUBLIC, 43 new Signature("get" + property, 44 type, 45 Constants.TYPES_EMPTY), 46 null); 47 e.load_this(); 48 e.getfield(name); 49 e.return_value(); 50 e.end_method(); 51 52 e = begin_method(Constants.ACC_PUBLIC, 53 new Signature("set" + property, 54 Type.VOID_TYPE, 55 new Type[]{ type }), 56 null); 57 e.load_this(); 58 e.load_arg(0); 59 e.putfield(name); 60 e.return_value(); 61 e.end_method(); 62 } 63 } 64 } 65 | Popular Tags |