1 21 package proguard.optimize.peephole; 22 23 import proguard.classfile.*; 24 import proguard.classfile.attribute.*; 25 import proguard.classfile.attribute.visitor.*; 26 import proguard.classfile.instruction.*; 27 import proguard.classfile.util.SimplifiedVisitor; 28 import proguard.optimize.info.ExceptionInstructionChecker; 29 30 36 public class UnreachableExceptionRemover 37 extends SimplifiedVisitor 38 implements AttributeVisitor, 39 ExceptionInfoVisitor 40 { 41 private ExceptionInfoVisitor extraExceptionInfoVisitor; 42 43 44 private ExceptionInstructionChecker exceptionInstructionChecker = new ExceptionInstructionChecker(); 45 46 47 50 public UnreachableExceptionRemover() 51 { 52 this(null); 53 } 54 55 56 61 public UnreachableExceptionRemover(ExceptionInfoVisitor extraExceptionInfoVisitor) 62 { 63 this.extraExceptionInfoVisitor = extraExceptionInfoVisitor; 64 } 65 66 67 69 public void visitAnyAttribute(Clazz clazz, Attribute attribute) {} 70 71 72 public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute) 73 { 74 codeAttribute.exceptionsAccept(clazz, method, this); 76 77 codeAttribute.u2exceptionTableLength = 79 removeEmptyExceptions(codeAttribute.exceptionTable, 80 codeAttribute.u2exceptionTableLength); 81 } 82 83 84 86 public void visitExceptionInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, ExceptionInfo exceptionInfo) 87 { 88 if (!mayThrowExceptions(clazz, 89 method, 90 codeAttribute, 91 exceptionInfo.u2startPC, 92 exceptionInfo.u2endPC)) 93 { 94 exceptionInfo.u2endPC = exceptionInfo.u2startPC; 96 97 if (extraExceptionInfoVisitor != null) 98 { 99 extraExceptionInfoVisitor.visitExceptionInfo(clazz, method, codeAttribute, exceptionInfo); 100 } 101 } 102 } 103 104 105 107 110 private boolean mayThrowExceptions(Clazz clazz, 111 Method method, 112 CodeAttribute codeAttribute, 113 int startOffset, 114 int endOffset) 115 { 116 byte[] code = codeAttribute.code; 117 118 int offset = startOffset; 120 while (offset < endOffset) 121 { 122 Instruction instruction = InstructionFactory.create(code, offset); 124 125 if (exceptionInstructionChecker.mayThrowExceptions(clazz, 127 method, 128 codeAttribute, 129 offset, 130 instruction)) 131 { 132 return true; 133 } 134 135 offset += instruction.length(offset); 137 } 138 139 return false; 140 } 141 142 143 147 private int removeEmptyExceptions(ExceptionInfo[] exceptionInfos, 148 int exceptionInfoCount) 149 { 150 int newIndex = 0; 152 for (int index = 0; index < exceptionInfoCount; index++) 153 { 154 ExceptionInfo exceptionInfo = exceptionInfos[index]; 155 if (exceptionInfo.u2startPC < exceptionInfo.u2endPC) 156 { 157 exceptionInfos[newIndex++] = exceptionInfo; 158 } 159 } 160 161 return newIndex; 162 } 163 } 164 | Popular Tags |