1 30 package org.objectweb.asm; 31 32 import java.lang.instrument.ClassFileTransformer ; 33 import java.lang.instrument.IllegalClassFormatException ; 34 import java.lang.instrument.Instrumentation ; 35 import java.security.ProtectionDomain ; 36 import java.util.HashSet ; 37 38 import org.objectweb.asm.attrs.CodeComment; 39 40 import junit.framework.TestSuite; 41 42 public class ClassWriterResizeInsnsTest extends AbstractTest { 43 44 public static void premain( 45 final String agentArgs, 46 final Instrumentation inst) 47 { 48 inst.addTransformer(new ClassFileTransformer () { 49 public byte[] transform( 50 final ClassLoader loader, 51 final String className, 52 final Class classBeingRedefined, 53 final ProtectionDomain domain, 54 byte[] b) throws IllegalClassFormatException 55 { 56 String n = className.replace('/', '.'); 57 if (agentArgs.length() == 0 || n.indexOf(agentArgs) != -1) { 58 try { 59 b = transformClass(b, ClassWriter.COMPUTE_FRAMES); 60 if (n.equals("pkg.FrameMap")) { 61 transformClass(b, 0); 62 } 63 return b; 64 } catch (Throwable e) { 65 return transformClass(b, 0); 66 } 67 } else { 68 return null; 69 } 70 } 71 }); 72 } 73 74 private static byte[] transformClass(final byte[] clazz, final int flags) { 75 ClassReader cr = new ClassReader(clazz); 76 ClassWriter cw = new ClassWriter(flags) { 77 protected String getCommonSuperClass( 78 final String type1, 79 final String type2) 80 { 81 ClassInfo c, d; 82 try { 83 c = new ClassInfo(type1, getClass().getClassLoader()); 84 d = new ClassInfo(type2, getClass().getClassLoader()); 85 } catch (Throwable e) { 86 throw new RuntimeException (e); 87 } 88 if (c.isAssignableFrom(d)) { 89 return type1; 90 } 91 if (d.isAssignableFrom(c)) { 92 return type2; 93 } 94 if (c.isInterface() || d.isInterface()) { 95 return "java/lang/Object"; 96 } else { 97 do { 98 c = c.getSuperclass(); 99 } while (!c.isAssignableFrom(d)); 100 return c.getType().getInternalName(); 101 } 102 } 103 }; 104 ClassAdapter ca = new ClassAdapter(cw) { 105 106 private boolean transformed = false; 107 108 public void visit( 109 int version, 110 int access, 111 String name, 112 String signature, 113 String superName, 114 String [] interfaces) 115 { 116 if (flags == ClassWriter.COMPUTE_FRAMES) { 117 version = Opcodes.V1_6; 118 } 119 super.visit(version, 120 access, 121 name, 122 signature, 123 superName, 124 interfaces); 125 } 126 127 public MethodVisitor visitMethod( 128 final int access, 129 final String name, 130 final String desc, 131 final String signature, 132 final String [] exceptions) 133 { 134 return new MethodAdapter(cv.visitMethod(access, 135 name, 136 desc, 137 signature, 138 exceptions)) 139 { 140 private HashSet labels = new HashSet (); 141 142 public void visitLabel(final Label label) { 143 super.visitLabel(label); 144 labels.add(label); 145 } 146 147 public void visitJumpInsn( 148 final int opcode, 149 final Label label) 150 { 151 super.visitJumpInsn(opcode, label); 152 if (opcode != Opcodes.GOTO) { 153 if (!transformed && !labels.contains(label)) { 154 transformed = true; 155 for (int i = 0; i < 33000; ++i) { 156 mv.visitInsn(Opcodes.NOP); 157 } 158 } 159 } 160 } 161 }; 162 } 163 }; 164 cr.accept(ca, new Attribute[] { new CodeComment() }, 0); 165 return cw.toByteArray(); 166 } 167 168 public static TestSuite suite() throws Exception { 169 return new ClassWriterResizeInsnsTest().getSuite(); 170 } 171 172 public void test() throws Exception { 173 try { 174 Class.forName(n, true, getClass().getClassLoader()); 175 } catch (NoClassDefFoundError ncdfe) { 176 } catch (UnsatisfiedLinkError ule) { 178 } catch (ClassFormatError cfe) { 180 fail(cfe.getMessage()); 181 } catch (VerifyError ve) { 182 fail(ve.toString()); 183 } 184 } 185 } 186 | Popular Tags |