1 16 17 package net.sf.cglib.transform.impl; 18 19 import net.sf.cglib.transform.*; 20 import net.sf.cglib.core.*; 21 22 import java.util.*; 23 24 28 public class TransformDemo { 29 30 public static void register(Class cls){ 31 System.out.println("register " + cls); 32 } 33 34 35 public static void start(){ 36 37 MA ma = new MA(); 38 makePersistent(ma); 39 ma.setCharP('A'); 40 ma.getCharP(); 41 ma.setDoubleP(554); 42 ma.setDoubleP(1.2); 43 ma.getFloatP(); 44 ma.setName("testName"); 45 ma.publicField = "set value"; 46 ma.publicField = ma.publicField + " append value"; 47 ma.setBaseTest("base test field"); 48 ma.getBaseTest(); 49 50 } 51 52 public static void makePersistent(Object obj){ 53 System.out.println( "makePersistent " + obj.getClass() + " " + Arrays.asList(obj.getClass().getInterfaces()) ); 54 InterceptFieldEnabled t = (InterceptFieldEnabled)obj; 55 t.setInterceptFieldCallback( new StateManager()); 56 FieldProvider provider = (FieldProvider)obj; 57 System.out.println("Field Names " + Arrays.asList(provider.getFieldNames()) ); 58 System.out.println("Field Types " + Arrays.asList(provider.getFieldTypes()) ); 59 PersistenceCapable pc = (PersistenceCapable)obj; 60 pc.setPersistenceManager("Manager"); 61 62 } 63 64 public static void main( String args [] )throws Exception { 65 66 ClassTransformerFactory transformation = 67 68 new ClassTransformerFactory (){ 69 70 public ClassTransformer newInstance(){ 71 try{ 72 InterceptFieldTransformer t1 = new InterceptFieldTransformer( new Filter() ); 73 74 75 AddStaticInitTransformer t2 = new AddStaticInitTransformer( 76 TransformDemo.class.getMethod("register",new Class []{Class .class}) 77 ); 78 79 80 AddDelegateTransformer t3 = new AddDelegateTransformer( 81 new Class []{PersistenceCapable.class}, 82 PersistenceCapableImpl.class 83 ); 84 85 FieldProviderTransformer t4 = new FieldProviderTransformer(); 86 87 return new ClassTransformerChain( new ClassTransformer[]{t4,t1,t2,t3} ); 88 89 }catch(Exception e){ 90 throw new CodeGenerationException(e); 91 } 92 } 93 }; 94 95 TransformingClassLoader loader = new TransformingClassLoader( 96 TransformDemo.class.getClassLoader(), 97 new ClassFilter(){ 98 public boolean accept(String name){ 99 System.out.println("load : " + name); 100 boolean f = 101 Base.class.getName().equals(name) || 102 MA.class.getName().equals(name) || 103 TransformDemo.class.getName().equals(name); 104 if(f){ 105 System.out.println("transforming " + name); 106 } 107 return f; 108 } 109 }, 110 transformation 111 ); 112 113 114 loader.loadClass(TransformDemo.class.getName()).getMethod("start",new Class []{}).invoke(null,null); 115 116 } 117 118 119 public static class Filter implements InterceptFieldFilter{ 120 121 122 public boolean acceptRead(org.objectweb.asm.Type owner, String name) { 123 return true; 124 } 125 126 public boolean acceptWrite(org.objectweb.asm.Type owner, String name) { 127 return true; 128 } 129 130 }; 131 132 133 public static class StateManager implements InterceptFieldCallback{ 134 135 136 public boolean readBoolean(Object _this, String name, boolean oldValue) { 137 System.out.println("read " + name + " = " + oldValue); 138 return oldValue; 139 } 140 141 public byte readByte(Object _this, String name, byte oldValue) { 142 System.out.println("read " + name + " = " + oldValue); 143 return oldValue; 144 } 145 146 public char readChar(Object _this, String name, char oldValue) { 147 System.out.println("read " + name + " = " + oldValue); 148 return oldValue; 149 } 150 151 public double readDouble(Object _this, String name, double oldValue) { 152 System.out.println("read " + name + " = " + oldValue); 153 return oldValue; 154 } 155 156 public float readFloat(Object _this, String name, float oldValue) { 157 System.out.println("read " + name + " = " + oldValue); 158 return oldValue; 159 } 160 161 public int readInt(Object _this, String name, int oldValue) { 162 System.out.println("read " + name + " = " + oldValue); 163 return oldValue; 164 } 165 166 public long readLong(Object _this, String name, long oldValue) { 167 System.out.println("read " + name + " = " + oldValue); 168 return oldValue; 169 } 170 171 public Object readObject(Object _this, String name, Object oldValue) { 172 System.out.println("read " + name + " = " + oldValue); 173 return oldValue; 174 } 175 176 public short readShort(Object _this, String name, short oldValue) { 177 System.out.println("read " + name + " = " + oldValue); 178 return oldValue; 179 } 180 181 public boolean writeBoolean(Object _this, String name, boolean oldValue, boolean newValue) { 182 System.out.println( "write " + name + " = " + newValue); 183 return newValue; 184 } 185 186 public byte writeByte(Object _this, String name, byte oldValue, byte newValue) { 187 System.out.println( "write " + name + " = " + newValue); 188 return newValue; 189 } 190 191 public char writeChar(Object _this, String name, char oldValue, char newValue) { 192 System.out.println( "write " + name + " = " + newValue); 193 return newValue; 194 } 195 196 public double writeDouble(Object _this, String name, double oldValue, double newValue) { 197 System.out.println( "write " + name + " = " + newValue); 198 return newValue; 199 } 200 201 public float writeFloat(Object _this, String name, float oldValue, float newValue) { 202 System.out.println( "write " + name + " = " + newValue); 203 return newValue; 204 } 205 206 public int writeInt(Object _this, String name, int oldValue, int newValue) { 207 System.out.println( "write " + name + " = " + newValue); 208 return newValue; 209 } 210 211 public long writeLong(Object _this, String name, long oldValue, long newValue) { 212 System.out.println( "write " + name + " = " + newValue); 213 return newValue; 214 } 215 216 public Object writeObject(Object _this, String name, Object oldValue, Object newValue) { 217 System.out.println( "write " + name + " = " + newValue); 218 return newValue; 219 } 220 221 public short writeShort(Object _this, String name, short oldValue, short newValue) { 222 System.out.println( "write " + name + " = " + newValue); 223 return newValue; 224 } 225 226 } 227 228 229 230 } 231 | Popular Tags |