1 16 package net.sf.cglib.transform.impl; 17 18 import net.sf.cglib.transform.*; 19 import net.sf.cglib.core.Constants; 20 import net.sf.cglib.core.ReflectUtils; 21 import net.sf.cglib.beans.*; 22 import java.util.*; 23 import java.lang.reflect.Method ; 24 import junit.framework.*; 25 import org.objectweb.asm.Type; 26 27 30 public class TestTransformingLoader extends net.sf.cglib.CodeGenTestCase { 31 32 private static final ClassFilter TEST_FILTER = new ClassFilter() { 33 public boolean accept(String name) { 34 System.err.println("Loading " + name); 35 return name.startsWith("net.sf.cglib."); 36 } 37 }; 38 39 private ClassTransformer getExampleTransformer(String name, Type type) { 40 return new AddPropertyTransformer(new String []{ name }, new Type[]{ type }); 41 } 42 43 public void testExample() throws Exception { 44 ClassTransformer t1 = getExampleTransformer("herby", Constants.TYPE_STRING); 45 ClassTransformer t2 = getExampleTransformer("derby", Type.DOUBLE_TYPE); 46 ClassTransformer chain = new ClassTransformerChain(new ClassTransformer[]{ t1, t2 }); 47 Class loaded = loadHelper(chain, Example.class); 48 Object obj = loaded.newInstance(); 49 String value = "HELLO"; 50 loaded.getMethod("setHerby", new Class []{ String .class }).invoke(obj, new Object []{ value }); 51 assertTrue(value.equals(loaded.getMethod("getHerby", null).invoke(obj, null))); 52 53 loaded.getMethod("setDerby", new Class []{ Double.TYPE }).invoke(obj, new Object []{ new Double (1.23456789d) }); 54 } 55 56 private static Class inited; 57 58 public static void initStatic(Class foo) { 59 System.err.println("INITING: " + foo); 60 } 61 62 public void testAddStatic() throws Exception { 63 Method m = ReflectUtils.findMethod("net.sf.cglib.transform.impl.TestTransformingLoader.initStatic(Class)"); 64 ClassTransformer t = new AddStaticInitTransformer(m); 65 Class loaded = loadHelper(t, Example.class); 67 Object obj = loaded.newInstance(); 68 } 70 71 public void testInterceptField() throws Exception { 72 ClassTransformer t = new InterceptFieldTransformer(new InterceptFieldFilter() { 73 public boolean acceptRead(Type owner, String name) { 74 return true; 75 } 76 public boolean acceptWrite(Type owner, String name) { 77 return true; 78 } 79 }); 80 Class loaded = loadHelper(t, Example.class); 81 } 83 84 public void testFieldProvider() throws Exception { 85 ClassTransformer t = new FieldProviderTransformer(); 86 Class loaded = loadHelper(t, Example.class); 87 } 98 99 private static Class loadHelper( final ClassTransformer t, Class target) throws ClassNotFoundException { 100 ClassLoader parent = TestTransformingLoader.class.getClassLoader(); 101 TransformingClassLoader loader = new TransformingClassLoader(parent, TEST_FILTER, 102 103 new ClassTransformerFactory(){ 104 public ClassTransformer newInstance(){ 105 return t; 106 } 107 } 108 109 ); 110 return loader.loadClass(target.getName()); 111 } 112 113 public TestTransformingLoader(String testName) { 114 super(testName); 115 } 116 117 public static void main(String [] args) { 118 junit.textui.TestRunner.run(suite()); 119 } 120 121 public static Test suite() { 122 return new TestSuite(TestTransformingLoader.class); 123 } 124 125 public void perform(ClassLoader loader) throws Throwable { 126 } 127 128 public void testFailOnMemoryLeak() throws Throwable { 129 } 130 131 } 132 | Popular Tags |