1 21 package proguard.classfile.editor; 22 23 import proguard.classfile.*; 24 import proguard.classfile.constant.Constant; 25 import proguard.classfile.visitor.ClassVisitor; 26 27 import java.util.Arrays ; 28 29 36 public class ConstantPoolSorter implements ClassVisitor 37 { 38 private int[] constantIndexMap = new int[ClassConstants.TYPICAL_CONSTANT_POOL_SIZE]; 39 private ComparableConstant[] comparableConstantPool = new ComparableConstant[ClassConstants.TYPICAL_CONSTANT_POOL_SIZE]; 40 41 private ConstantPoolRemapper constantPoolRemapper = new ConstantPoolRemapper(); 42 43 44 46 public void visitProgramClass(ProgramClass programClass) 47 { 48 sortConstantPool(programClass, 50 programClass.constantPool, 51 programClass.u2constantPoolCount); 52 53 constantPoolRemapper.setConstantIndexMap(constantIndexMap); 55 constantPoolRemapper.visitProgramClass(programClass); 56 } 57 58 59 public void visitLibraryClass(LibraryClass libraryClass) 60 { 61 } 62 63 64 66 69 private void sortConstantPool(Clazz clazz, Constant[] constantPool, int length) 70 { 71 if (constantIndexMap.length < length) 72 { 73 constantIndexMap = new int[length]; 74 comparableConstantPool = new ComparableConstant[length]; 75 } 76 77 for (int oldIndex = 1; oldIndex < length; oldIndex++) 79 { 80 Constant constant = constantPool[oldIndex]; 81 82 if (constant == null) 84 { 85 constant = constantPool[oldIndex-1]; 86 } 87 88 comparableConstantPool[oldIndex] = new ComparableConstant(clazz, 89 oldIndex, 90 constant); 91 } 92 93 Arrays.sort(comparableConstantPool, 1, length); 95 96 Constant previousConstant = null; 98 for (int newIndex = 1; newIndex < length; newIndex++) 99 { 100 ComparableConstant comparableConstant = comparableConstantPool[newIndex]; 101 102 int oldIndex = comparableConstant.getIndex(); 104 constantIndexMap[oldIndex] = newIndex; 105 106 Constant constant = comparableConstant.getConstant(); 109 constantPool[newIndex] = constant != previousConstant ? 110 constant : 111 null; 112 113 previousConstant = constant; 114 } 115 } 116 } 117 | Popular Tags |