1 21 package proguard.classfile.editor; 22 23 import proguard.classfile.*; 24 import proguard.classfile.util.SimplifiedVisitor; 25 import proguard.classfile.attribute.*; 26 import proguard.classfile.attribute.visitor.AttributeVisitor; 27 import proguard.classfile.attribute.annotation.*; 28 29 35 public class VariableEditor 36 extends SimplifiedVisitor 37 implements AttributeVisitor 38 { 39 private boolean modified; 40 41 private boolean[] deleted = new boolean[ClassConstants.TYPICAL_VARIABLES_SIZE]; 42 private int[] variableMap = new int[ClassConstants.TYPICAL_VARIABLES_SIZE]; 43 44 private VariableRemapper variableRemapper = new VariableRemapper(); 45 46 47 52 public void reset(int maxLocals) 53 { 54 if (deleted.length < maxLocals) 56 { 57 deleted = new boolean[maxLocals]; 58 } 59 else 60 { 61 for (int index = 0; index < maxLocals; index++) 62 { 63 deleted[index] = false; 64 } 65 } 66 67 modified = false; 68 } 69 70 71 75 public void deleteVariable(int variableIndex) 76 { 77 deleted[variableIndex] = true; 78 79 modified = true; 80 } 81 82 83 86 public boolean isDeleted(int instructionOffset) 87 { 88 return deleted[instructionOffset]; 89 } 90 91 92 94 public void visitAnyAttribute(Clazz clazz, Attribute attribute) {} 95 96 97 public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute) 98 { 99 if (!modified) 101 { 102 return; 103 } 104 105 int oldMaxLocals = codeAttribute.u2maxLocals; 106 107 if (variableMap.length < oldMaxLocals) 109 { 110 variableMap = new int[oldMaxLocals]; 111 } 112 113 int newVariableIndex = 0; 115 for (int oldVariableIndex = 0; oldVariableIndex < oldMaxLocals; oldVariableIndex++) 116 { 117 if (oldVariableIndex >= deleted.length || 118 !deleted[oldVariableIndex]) 119 { 120 variableMap[oldVariableIndex] = newVariableIndex++; 121 } 122 else 123 { 124 variableMap[oldVariableIndex] = -1; 125 } 126 } 127 128 variableRemapper.setVariableMap(variableMap); 130 131 variableRemapper.visitCodeAttribute(clazz, method, codeAttribute); 133 134 codeAttribute.u2maxLocals = newVariableIndex; 136 } 137 } 138 | Popular Tags |