1 25 26 package org.objectweb.easybeans.tests.common.enhancer; 27 28 import java.util.List ; 29 30 import org.objectweb.asm.ClassReader; 31 import org.objectweb.asm.ClassVisitor; 32 import org.objectweb.asm.Opcodes; 33 import org.objectweb.asm.Type; 34 import org.objectweb.asm.tree.AbstractInsnNode; 35 import org.objectweb.asm.tree.ClassNode; 36 import org.objectweb.asm.tree.MethodNode; 37 import org.objectweb.asm.tree.analysis.Analyzer; 38 import org.objectweb.asm.tree.analysis.AnalyzerException; 39 import org.objectweb.asm.tree.analysis.Frame; 40 import org.objectweb.asm.tree.analysis.SimpleVerifier; 41 import org.objectweb.asm.util.CheckClassAdapter; 42 import org.objectweb.asm.util.TraceMethodVisitor; 43 44 48 public class JCheckClassAdapter extends CheckClassAdapter { 49 50 53 private static final int START_INT = 100000; 54 55 59 public JCheckClassAdapter(final ClassVisitor cv) { 60 super(cv); 61 } 62 63 68 public static void checkClass(final byte[] bytes) throws AnalyzerException { 69 ClassReader cr = new ClassReader(bytes); 70 ClassNode cn = new ClassNode(); 71 cr.accept(new CheckClassAdapter(cn), true); 72 List methods = cn.methods; 73 for (int i = 0; i < methods.size(); ++i) { 74 MethodNode method = (MethodNode) methods.get(i); 75 if (method.instructions.size() > 0) { 76 Analyzer a = new Analyzer(new SimpleVerifier(Type.getType("L" + cn.name + ";"), Type.getType("L" + cn.superName 77 + ";"), (cn.access & Opcodes.ACC_INTERFACE) != 0)); 78 AnalyzerException throwE = null; 79 try { 80 a.analyze(cn.name, method); 81 continue; 82 } catch (AnalyzerException e) { 83 throwE = e; 84 } 85 final Frame[] frames = a.getFrames(); 86 87 if (throwE != null) { 88 System.out.println(method.name + method.desc); 89 90 TraceMethodVisitor mv = new TraceMethodVisitor() { 91 92 @Override 93 public void visitMaxs(final int maxStack, final int maxLocals) { 94 for (int i = 0; i < text.size(); ++i) { 95 String s; 96 if (frames[i] == null) { 97 s = "null"; 98 } else { 99 s = frames[i].toString(); 100 } 101 while (s.length() < maxStack + maxLocals + 1) { 102 s += " "; 103 } 104 System.out.print(Integer.toString(i + START_INT).substring(1)); 105 System.out.print(" " + s + " : " + text.get(i)); 106 } 107 System.out.println(); 108 } 109 }; 110 for (int j = 0; j < method.instructions.size(); ++j) { 111 ((AbstractInsnNode) method.instructions.get(j)).accept(mv); 112 } 113 mv.visitMaxs(method.maxStack, method.maxLocals); 114 throw throwE; 115 } 116 } 117 } 118 } 119 120 } 121 | Popular Tags |