1 30 package org.objectweb.asm; 31 32 import org.objectweb.asm.ClassAdapter; 33 import org.objectweb.asm.ClassReader; 34 import org.objectweb.asm.ClassVisitor; 35 import org.objectweb.asm.ClassWriter; 36 import org.objectweb.asm.MethodAdapter; 37 import org.objectweb.asm.MethodVisitor; 38 import org.objectweb.asm.Opcodes; 39 40 import java.io.InputStream ; 41 42 45 public class ASMPerfTest extends ALLPerfTest { 46 47 final static Integer ONE = new Integer (1); 48 49 public static void main(final String args[]) throws Exception { 50 System.out.println("ASM PERFORMANCES\n"); 51 new ASMPerfTest().perfs(args); 52 } 53 54 ALLPerfTest newInstance() { 55 return new ASMPerfTest(); 56 } 57 58 byte[] nullAdaptClass(final InputStream is, final String name) 59 throws Exception 60 { 61 ClassReader cr = new ClassReader(is); 62 ClassWriter cw = new ClassWriter(compute); 63 ClassAdapter ca = new ClassAdapter(cw); 64 cr.accept(ca, skipDebug); 65 return cw.toByteArray(); 66 } 67 68 byte[] counterAdaptClass(final InputStream is, final String name) 69 throws Exception 70 { 71 ClassReader cr = new ClassReader(is); 72 ClassWriter cw = new ClassWriter(false); 73 ClassAdapter ca = new CounterClassAdapter(cw); 74 cr.accept(ca, false); 75 return cw.toByteArray(); 76 } 77 78 static class CounterClassAdapter extends ClassAdapter implements Opcodes { 79 80 private String owner; 81 82 CounterClassAdapter(ClassVisitor cv) { 83 super(cv); 84 } 85 86 public void visit( 87 int version, 88 int access, 89 String name, 90 String signature, 91 String superName, 92 String [] interfaces) 93 { 94 super.visit(version, access, name, signature, superName, interfaces); 95 if ((access & ACC_INTERFACE) == 0) { 96 cv.visitField(ACC_PUBLIC, "_counter", "I", null, null); 97 } 98 owner = name; 99 } 100 101 public MethodVisitor visitMethod( 102 int access, 103 String name, 104 String desc, 105 String signature, 106 String [] exceptions) 107 { 108 MethodVisitor mv = super.visitMethod(access, 109 name, 110 desc, 111 signature, 112 exceptions); 113 if (!name.equals("<init>") 114 && (access & (ACC_STATIC + ACC_NATIVE + ACC_ABSTRACT)) == 0) 115 { 116 mv.visitVarInsn(ALOAD, 0); 117 mv.visitVarInsn(ALOAD, 0); 118 mv.visitFieldInsn(GETFIELD, owner, "_counter", "I"); 119 mv.visitLdcInsn(ONE); 120 mv.visitInsn(IADD); 121 mv.visitFieldInsn(PUTFIELD, owner, "_counter", "I"); 122 return new CounterCodeAdapter(mv); 123 } 124 return mv; 125 } 126 } 127 128 static class CounterCodeAdapter extends MethodAdapter { 129 130 CounterCodeAdapter(MethodVisitor mv) { 131 super(mv); 132 } 133 134 public void visitMaxs(int maxStack, int maxLocals) { 135 super.visitMaxs(Math.max(maxStack, 2), maxLocals); 136 } 137 } 138 } 139 | Popular Tags |