1 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Set ; 34 35 import org.objectweb.asm.ClassReader; 36 import org.objectweb.asm.Opcodes; 37 import org.objectweb.asm.tree.AbstractInsnNode; 38 import org.objectweb.asm.tree.ClassNode; 39 import org.objectweb.asm.tree.IincInsnNode; 40 import org.objectweb.asm.tree.MethodNode; 41 import org.objectweb.asm.tree.VarInsnNode; 42 import org.objectweb.asm.tree.analysis.Analyzer; 43 import org.objectweb.asm.tree.analysis.BasicVerifier; 44 import org.objectweb.asm.tree.analysis.SourceInterpreter; 45 import org.objectweb.asm.tree.analysis.SourceValue; 46 import org.objectweb.asm.tree.analysis.Frame; 47 import org.objectweb.asm.util.TraceMethodVisitor; 48 49 52 public class Analysis implements Opcodes { 53 54 public static void main(final String [] args) throws Exception { 55 ClassReader cr = new ClassReader("Analysis"); 56 ClassNode cn = new ClassNode(); 57 cr.accept(cn, ClassReader.SKIP_DEBUG); 58 59 List methods = cn.methods; 60 for (int i = 0; i < methods.size(); ++i) { 61 MethodNode method = (MethodNode) methods.get(i); 62 if (method.instructions.size() > 0) { 63 if (!analyze(cn, method)) { 64 Analyzer a = new Analyzer(new BasicVerifier()); 65 try { 66 a.analyze(cn.name, method); 67 } catch (Exception ignored) { 68 } 69 final Frame[] frames = a.getFrames(); 70 71 TraceMethodVisitor mv = new TraceMethodVisitor() { 72 public void visitMaxs( 73 final int maxStack, 74 final int maxLocals) 75 { 76 for (int i = 0; i < text.size(); ++i) { 77 String s = frames[i] == null 78 ? "null" 79 : frames[i].toString(); 80 while (s.length() < Math.max(20, maxStack 81 + maxLocals + 1)) 82 { 83 s += " "; 84 } 85 System.err.print(Integer.toString(i + 1000) 86 .substring(1) 87 + " " + s + " : " + text.get(i)); 88 } 89 System.err.println(); 90 } 91 }; 92 for (int j = 0; j < method.instructions.size(); ++j) { 93 Object insn = method.instructions.get(j); 94 ((AbstractInsnNode) insn).accept(mv); 95 } 96 mv.visitMaxs(0, 0); 97 } 98 } 99 } 100 } 101 102 107 public static boolean analyze(final ClassNode c, final MethodNode m) 108 throws Exception 109 { 110 Analyzer a = new Analyzer(new SourceInterpreter()); 111 Frame[] frames = a.analyze(c.name, m); 112 113 Set stores = new HashSet (); 117 for (int i = 0; i < m.instructions.size(); ++i) { 118 Object insn = m.instructions.get(i); 119 int opcode = ((AbstractInsnNode) insn).getOpcode(); 120 if ((opcode >= ILOAD && opcode <= ALOAD) || opcode == IINC) { 121 int var = opcode == IINC 122 ? ((IincInsnNode) insn).var 123 : ((VarInsnNode) insn).var; 124 Frame f = frames[i]; 125 if (f != null) { 126 Set s = ((SourceValue) f.getLocal(var)).insns; 127 Iterator j = s.iterator(); 128 while (j.hasNext()) { 129 insn = j.next(); 130 if (insn instanceof VarInsnNode) { 131 stores.add(insn); 132 } 133 } 134 } 135 } 136 } 137 138 boolean ok = true; 140 for (int i = 0; i < m.instructions.size(); ++i) { 141 Object insn = m.instructions.get(i); 142 if (insn instanceof AbstractInsnNode) { 143 int opcode = ((AbstractInsnNode) insn).getOpcode(); 144 if (opcode >= ISTORE && opcode <= ASTORE) { 145 if (!stores.contains(insn)) { 146 ok = false; 147 System.err.println("method " + m.name 148 + ", instruction " + i 149 + ": useless store instruction"); 150 } 151 } 152 } 153 } 154 return ok; 155 } 156 157 160 public int test(int i, int j) { 161 i = i + 1; 163 if (j == 0) { 164 j = 1; } else { 166 try { 167 j = j - 1; int k = 0; 169 if (i > 0) { 170 k = i - 1; 171 } 172 return k; 173 } catch (Exception e) { j = j + 1; } 176 } 177 178 return 0; 179 } 180 } 181
| Popular Tags
|