1 21 package proguard.classfile.editor; 22 23 import proguard.classfile.*; 24 import proguard.classfile.constant.*; 25 import proguard.classfile.constant.visitor.ConstantVisitor; 26 import proguard.classfile.util.SimplifiedVisitor; 27 28 29 38 class ComparableConstant 39 extends SimplifiedVisitor 40 implements Comparable , ConstantVisitor 41 { 42 private static int[] PRIORITIES = new int[13]; 43 static 44 { 45 PRIORITIES[ClassConstants.CONSTANT_Integer] = 0; 46 PRIORITIES[ClassConstants.CONSTANT_Float] = 1; 47 PRIORITIES[ClassConstants.CONSTANT_Long] = 2; 48 PRIORITIES[ClassConstants.CONSTANT_Double] = 3; 49 PRIORITIES[ClassConstants.CONSTANT_String] = 4; 50 PRIORITIES[ClassConstants.CONSTANT_Class] = 5; 51 PRIORITIES[ClassConstants.CONSTANT_Fieldref] = 6; 52 PRIORITIES[ClassConstants.CONSTANT_Methodref] = 7; 53 PRIORITIES[ClassConstants.CONSTANT_InterfaceMethodref] = 8; 54 PRIORITIES[ClassConstants.CONSTANT_NameAndType] = 9; 55 PRIORITIES[ClassConstants.CONSTANT_Utf8] = 10; 56 } 57 58 private Clazz clazz; 59 private int thisIndex; 60 private Constant thisConstant; 61 private Constant otherConstant; 62 private int result; 63 64 65 public ComparableConstant(Clazz clazz, int index, Constant constant) 66 { 67 this.clazz = clazz; 68 this.thisIndex = index; 69 this.thisConstant = constant; 70 } 71 72 73 public int getIndex() 74 { 75 return thisIndex; 76 } 77 78 79 public Constant getConstant() 80 { 81 return thisConstant; 82 } 83 84 85 87 public int compareTo(Object other) 88 { 89 ComparableConstant otherComparableConstant = (ComparableConstant)other; 90 91 otherConstant = otherComparableConstant.thisConstant; 92 93 if (thisConstant == otherConstant) 96 { 97 int otherIndex = otherComparableConstant.thisIndex; 98 99 return thisIndex < otherIndex ? -1 : 100 thisIndex == otherIndex ? 0 : 101 1; 102 } 103 104 int thisTag = thisConstant.getTag(); 106 int otherTag = otherConstant.getTag(); 107 108 if (thisTag != otherTag) 109 { 110 return PRIORITIES[thisTag] < PRIORITIES[otherTag] ? -1 : 1; 111 } 112 113 thisConstant.accept(clazz, this); 115 116 return result; 117 } 118 119 120 122 public void visitIntegerConstant(Clazz clazz, IntegerConstant integerConstant) 123 { 124 result = new Integer (integerConstant.getValue()).compareTo(new Integer (((IntegerConstant)otherConstant).getValue())); 126 } 127 128 public void visitLongConstant(Clazz clazz, LongConstant longConstant) 129 { 130 result = new Long (longConstant.getValue()).compareTo(new Long (((LongConstant)otherConstant).getValue())); 132 } 133 134 public void visitFloatConstant(Clazz clazz, FloatConstant floatConstant) 135 { 136 result = new Float (floatConstant.getValue()).compareTo(new Float (((FloatConstant)otherConstant).getValue())); 138 } 139 140 public void visitDoubleConstant(Clazz clazz, DoubleConstant doubleConstant) 141 { 142 result = new Double (doubleConstant.getValue()).compareTo(new Double (((DoubleConstant)otherConstant).getValue())); 144 } 145 146 public void visitStringConstant(Clazz clazz, StringConstant stringConstant) 147 { 148 result = stringConstant.getString(clazz).compareTo(((StringConstant)otherConstant).getString(clazz)); 149 } 150 151 public void visitUtf8Constant(Clazz clazz, Utf8Constant utf8Constant) 152 { 153 result = utf8Constant.getString().compareTo(((Utf8Constant)otherConstant).getString()); 154 } 155 156 public void visitAnyRefConstant(Clazz clazz, RefConstant refConstant) 157 { 158 RefConstant otherRefConstant = (RefConstant)otherConstant; 159 result = (refConstant.getClassName(clazz) + ' ' + 160 refConstant.getName(clazz) + ' ' + 161 refConstant.getType(clazz)) 162 .compareTo 163 (otherRefConstant.getClassName(clazz) + ' ' + 164 otherRefConstant.getName(clazz) + ' ' + 165 otherRefConstant.getType(clazz)); 166 } 167 168 public void visitClassConstant(Clazz clazz, ClassConstant classConstant) 169 { 170 result = classConstant.getName(clazz).compareTo(((ClassConstant)otherConstant).getName(clazz)); 171 } 172 173 public void visitNameAndTypeConstant(Clazz clazz, NameAndTypeConstant nameAndTypeConstant) 174 { 175 NameAndTypeConstant otherNameAndTypeConstant = (NameAndTypeConstant)otherConstant; 176 result = (nameAndTypeConstant.getName(clazz) + ' ' + 177 nameAndTypeConstant.getType(clazz)) 178 .compareTo 179 (otherNameAndTypeConstant.getName(clazz) + ' ' + 180 otherNameAndTypeConstant.getType(clazz)); 181 } 182 } 183 | Popular Tags |