1 30 package org.objectweb.asm.commons; 31 32 import junit.framework.TestSuite; 33 import junit.textui.TestRunner; 34 35 import org.objectweb.asm.AbstractTest; 36 import org.objectweb.asm.ClassAdapter; 37 import org.objectweb.asm.ClassReader; 38 import org.objectweb.asm.ClassVisitor; 39 import org.objectweb.asm.ClassWriter; 40 import org.objectweb.asm.MethodVisitor; 41 import org.objectweb.asm.Opcodes; 42 43 48 public class AdviceAdapterTest extends AbstractTest { 49 50 public static void main(final String [] args) throws Exception { 51 TestRunner.run(AdviceAdapterTest.suite()); 52 } 53 54 public static TestSuite suite() throws Exception { 55 return new AdviceAdapterTest().getSuite(); 56 } 57 58 public void test() throws Exception { 59 ClassReader cr = new ClassReader(is); 60 ClassWriter cw1 = new ClassWriter(0); 61 ClassWriter cw2 = new ClassWriter(0); 62 cr.accept(new ReferenceClassAdapter(cw1), ClassReader.EXPAND_FRAMES); 63 cr.accept(new AdviceClassAdapter(cw2), ClassReader.EXPAND_FRAMES); 64 assertEquals(new ClassReader(cw1.toByteArray()), 65 new ClassReader(cw2.toByteArray())); 66 } 67 68 static class ReferenceClassAdapter extends ClassAdapter { 69 70 public ReferenceClassAdapter(final ClassVisitor cv) { 71 super(cv); 72 } 73 74 public MethodVisitor visitMethod( 75 final int access, 76 final String name, 77 final String desc, 78 final String signature, 79 final String [] exceptions) 80 { 81 MethodVisitor mv = cv.visitMethod(access, 82 name, 83 desc, 84 signature, 85 exceptions); 86 87 if (mv == null 88 || (access & (Opcodes.ACC_ABSTRACT | Opcodes.ACC_NATIVE)) > 0) 89 { 90 return mv; 91 } 92 93 return new LocalVariablesSorter(access, desc, mv); 94 } 95 } 96 97 static class AdviceClassAdapter extends ClassAdapter { 98 99 public AdviceClassAdapter(final ClassVisitor cv) { 100 super(cv); 101 } 102 103 public MethodVisitor visitMethod( 104 final int access, 105 final String name, 106 final String desc, 107 final String signature, 108 final String [] exceptions) 109 { 110 MethodVisitor mv = cv.visitMethod(access, 111 name, 112 desc, 113 signature, 114 exceptions); 115 116 if (mv == null 117 || (access & (Opcodes.ACC_ABSTRACT | Opcodes.ACC_NATIVE)) > 0) 118 { 119 return mv; 120 } 121 122 return new AdviceAdapter(mv, access, name, desc) { 123 protected void onMethodEnter() { 124 } 128 129 protected void onMethodExit(final int opcode) { 130 } 135 }; 136 } 137 } 138 } 139 | Popular Tags |