1 16 package net.sf.cglib.transform.impl; 17 18 import net.sf.cglib.transform.*; 19 import java.util.*; 20 import net.sf.cglib.core.TypeUtils; 21 import org.objectweb.asm.Type; 22 23 public class ExampleTask extends AbstractTransformTask { 24 private List properties = new ArrayList(); 25 private String fieldSuffix = ""; 26 private ClassTransformer transformer; 27 28 protected ClassTransformer getClassTransformer(String name[]) { 29 return transformer; 30 } 31 32 protected void beforeExecute() { 33 ClassTransformer t1 = new AccessFieldTransformer(new AccessFieldTransformer.Callback() { 34 public String getPropertyName(Type owner, String fieldName) { 35 return fieldName + fieldSuffix; 36 } 37 }); 38 int size = properties.size(); 39 String [] names = new String [size]; 40 Type[] types = new Type[size]; 41 for (int i = 0; i < size; i++) { 42 NewProperty p = (NewProperty)properties.get(i); 43 names[i] = p.name; 44 types[i] = TypeUtils.parseType(p.type); 45 } 46 ClassTransformer t2 = new AddPropertyTransformer(names, types); 47 transformer = new ClassTransformerChain(new ClassTransformer[]{ t1, t2 }); 48 } 49 50 public static class NewProperty { 51 private String name; 52 private String type; 53 54 public void setName(String name) { 55 this.name = name; 56 } 57 58 public void setType(String type) { 59 this.type = type; 60 } 61 } 62 63 public void addNewproperty(NewProperty prop) { 64 properties.add(prop); 65 } 66 67 public void setFieldsuffix(String fieldSuffix) { 68 this.fieldSuffix = fieldSuffix; 69 } 70 } 71 | Popular Tags |