1 30 package org.objectweb.asm.commons; 31 32 import java.io.IOException ; 33 import java.lang.reflect.InvocationTargetException ; 34 import java.lang.reflect.Method ; 35 36 import org.objectweb.asm.AbstractTest; 37 import org.objectweb.asm.ClassAdapter; 38 import org.objectweb.asm.ClassReader; 39 import org.objectweb.asm.ClassVisitor; 40 import org.objectweb.asm.ClassWriter; 41 import org.objectweb.asm.MethodVisitor; 42 import org.objectweb.asm.Opcodes; 43 44 49 public class AdviceAdapterUnitTest extends AbstractTest { 50 51 public void test() throws Exception { 52 Class c = getClass(); 53 String name = c.getName(); 54 AdvisingClassLoader cl = new AdvisingClassLoader(name + "$"); 55 Class cc = cl.loadClass(name + "$B"); 56 Method m = cc.getMethod("run", new Class [] { Integer.TYPE }); 57 try { 58 m.invoke(null, new Object [] { new Integer (0) }); 59 } catch (InvocationTargetException e) { 60 throw (Exception ) e.getTargetException(); 61 } 62 } 63 64 private static class AdvisingClassLoader extends ClassLoader { 65 private String prefix; 66 67 public AdvisingClassLoader(final String prefix) throws IOException { 68 this.prefix = prefix; 69 } 70 71 public Class loadClass(final String name) throws ClassNotFoundException 72 { 73 if (name.startsWith(prefix)) { 74 try { 75 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); 76 ClassReader cr = new ClassReader(getClass().getResourceAsStream("/" 77 + name.replace('.', '/') + ".class")); 78 cr.accept(new AdviceClassAdapter(cw), 79 ClassReader.EXPAND_FRAMES); 80 byte[] bytecode = cw.toByteArray(); 81 return super.defineClass(name, bytecode, 0, bytecode.length); 82 } catch (IOException ex) { 83 throw new ClassNotFoundException ("Load error: " 84 + ex.toString(), ex); 85 } 86 } 87 return super.loadClass(name); 88 } 89 90 } 91 92 private static int n = 0; 94 95 public static void enter(final String msg) { 96 System.err.println(off().append("enter ").append(msg).toString()); 97 n++; 98 } 99 100 public static void exit(final String msg) { 101 n--; 102 System.err.println(off().append("<").toString()); 103 } 104 105 private static StringBuffer off() { 106 StringBuffer sb = new StringBuffer (); 107 for (int i = 0; i < n; i++) { 108 sb.append(" "); 109 } 110 return sb; 111 } 112 113 static class AdviceClassAdapter extends ClassAdapter implements Opcodes { 114 private String cname; 115 116 public AdviceClassAdapter(final ClassVisitor cv) { 117 super(cv); 118 } 119 120 public void visit( 121 final int version, 122 final int access, 123 final String name, 124 final String signature, 125 final String superName, 126 final String [] interfaces) 127 { 128 this.cname = name; 129 super.visit(version, access, name, signature, superName, interfaces); 130 } 131 132 public MethodVisitor visitMethod( 133 final int access, 134 final String name, 135 final String desc, 136 final String signature, 137 final String [] exceptions) 138 { 139 MethodVisitor mv = cv.visitMethod(access, 140 name, 141 desc, 142 signature, 143 exceptions); 144 145 if (mv == null 146 || (access & (Opcodes.ACC_ABSTRACT | Opcodes.ACC_NATIVE)) > 0) 147 { 148 return mv; 149 } 150 151 return new AdviceAdapter(mv, access, name, desc) { 152 protected void onMethodEnter() { 153 mv.visitLdcInsn(cname + "." + name + desc); 154 mv.visitMethodInsn(INVOKESTATIC, 155 "org/objectweb/asm/commons/AdviceAdapterUnitTest", 156 "enter", 157 "(Ljava/lang/String;)V"); 158 } 159 160 protected void onMethodExit(final int opcode) { 161 mv.visitLdcInsn(cname + "." + name + desc); 162 mv.visitMethodInsn(INVOKESTATIC, 163 "org/objectweb/asm/commons/AdviceAdapterUnitTest", 164 "exit", 165 "(Ljava/lang/String;)V"); 166 } 167 168 }; 169 } 170 } 171 172 174 public static class A { 175 final String s; 176 177 public A(final String s) { 178 this.s = s; 179 } 180 181 public A(final A a) { 182 this.s = a.s; 183 } 184 } 185 186 public static class B extends A { 187 188 public B() { 189 super(new B("")); 190 test(this); 191 } 192 193 public B(final A a) { 194 super(a); 195 test(this); 196 } 197 198 public B(final String s) { 199 super(s == null ? new A("") : new A(s)); 200 test(this); 201 } 202 203 private static A aa; 204 205 public B(final String s, final A a) { 206 this(s == null ? aa = new A(s) : a); 207 A aa = new A(""); 208 test(aa); 209 } 210 211 public B(final String s, final String s1) { 212 super(s != null ? new A(getA(s1).s) : new A(s)); 213 test(this); 214 } 215 216 private void test(final Object b) { 217 } 218 219 private static A getA(final String s) { 220 return new A(s); 221 } 222 223 public static void run(final int n) { 225 new B(); 226 new B(new A("")); 227 new B(new B()); 228 new B("", new A("")); 229 new B("", ""); 230 } 231 232 } 233 } 234 | Popular Tags |