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

This page was automatically generated by Maven