1 16 package net.sf.cglib; 17 18 import java.io.*; 19 import java.io.File ; 20 import java.io.InputStream ; 21 import java.net.URL ; 22 import java.net.URLClassLoader ; 23 import java.util.*; 24 import java.util.ArrayList ; 25 import java.util.StringTokenizer ; 26 27 import net.sf.cglib.core.KeyFactory; 28 import net.sf.cglib.core.Signature; 29 import net.sf.cglib.proxy.*; 30 import net.sf.cglib.proxy.Callback; 31 import net.sf.cglib.proxy.Factory; 32 import net.sf.cglib.reflect.FastClass; 33 34 import junit.framework.*; 35 36 40 abstract public class CodeGenTestCase extends TestCase { 41 public CodeGenTestCase(String testName) { 42 super(testName); 43 } 44 45 public abstract void perform(ClassLoader loader)throws Throwable ; 46 47 48 public boolean leaks()throws Throwable { 49 50 List classPath = new ArrayList (); 51 52 for( StringTokenizer tokenizer = new StringTokenizer (System.getProperty("java.class.path"),File.pathSeparator); tokenizer.hasMoreElements(); ){ 53 54 classPath.add( new File (tokenizer.nextToken()).toURL() ); 55 56 } 57 58 59 final Set coreClasses = new HashSet(); 60 coreClasses.add(Factory.class.getName()); 61 coreClasses.add(Callback.class.getName()); 62 coreClasses.add(MethodInterceptor.class.getName()); 63 coreClasses.add(Mixin.class.getName()); 64 coreClasses.add(KeyFactory.class.getName()); 65 coreClasses.add(FastClass.class.getName()); 66 coreClasses.add(Signature.class.getName()); 67 68 69 70 71 72 73 URLClassLoader loader = new URLClassLoader ((URL []) classPath.toArray(new URL [classPath.size()])){ 74 75 public Class loadClass(String name) throws ClassNotFoundException { 76 77 String res = name.replace('.','/') + ".class"; 78 79 if(name.startsWith("java") || name.startsWith("org.objectweb.asm")){ 80 return super.loadClass(name); 81 } 82 83 84 85 if( coreClasses.contains(name) ){ 86 return super.loadClass(name); 87 } 88 89 90 try{ 91 92 93 InputStream is = getResourceAsStream(res); 94 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 95 try{ 96 int b; 97 while( (b = is.read()) != -1 ){ 98 bout.write((byte)b); 99 } 100 101 }finally{ 102 is.close(); 103 } 104 byte data [] = bout.toByteArray(); 105 return defineClass(name,data,0,data.length ); 106 }catch(Exception e){ 107 throw new ClassNotFoundException ( name + ":" + e.toString()); 108 } 109 } 110 111 }; 112 113 perform(loader); 114 115 java.lang.ref.Reference ref = new java.lang.ref.WeakReference (loader); 116 117 loader = null; 118 java.util.List list = new java.util.ArrayList (); 119 120 for(int i = 0; i < 512; i++ ){ 121 122 System.gc(); 123 System.gc(); 124 125 if(ref.get() == null ){ 126 127 return false; 128 129 } 130 131 byte[] garbage = new byte[ (i + 1)*1004 ]; 132 list.add(garbage); 133 134 135 } 136 137 return true; 138 139 140 } 141 142 143 144 } 145 146 | Popular Tags |