KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > classfile > editor > ComparableConstant


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

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 /**
30  * This class is a <code>Comparable</code> wrapper of <code>Constant</code>
31  * objects. It can store an index, in order to identify the constant pool
32  * entry after it has been sorted. The comparison is primarily based on the
33  * types of the constant pool entries, and secondarily on the contents of
34  * the constant pool entries.
35  *
36  * @author Eric Lafortune
37  */

38 class ComparableConstant
39 extends SimplifiedVisitor
40 implements Comparable JavaDoc, 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     // Implementations for Comparable.
86

87     public int compareTo(Object JavaDoc other)
88     {
89         ComparableConstant otherComparableConstant = (ComparableConstant)other;
90
91         otherConstant = otherComparableConstant.thisConstant;
92
93         // Compare based on the original indices, if the actual constant pool
94
// entries are the same.
95
if (thisConstant == otherConstant)
96         {
97             int otherIndex = otherComparableConstant.thisIndex;
98
99             return thisIndex < otherIndex ? -1 :
100                    thisIndex == otherIndex ? 0 :
101                    1;
102         }
103
104         // Compare based on the tags, if they are different.
105
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         // Otherwise compare based on the contents of the Constant objects.
114
thisConstant.accept(clazz, this);
115
116         return result;
117     }
118
119
120     // Implementations for ConstantVisitor.
121

122     public void visitIntegerConstant(Clazz clazz, IntegerConstant integerConstant)
123     {
124         // In JDK 1.4, we can use Integer.compare(a,b).
125
result = new Integer JavaDoc(integerConstant.getValue()).compareTo(new Integer JavaDoc(((IntegerConstant)otherConstant).getValue()));
126     }
127
128     public void visitLongConstant(Clazz clazz, LongConstant longConstant)
129     {
130         // In JDK 1.4, we can use Long.compare(a,b).
131
result = new Long JavaDoc(longConstant.getValue()).compareTo(new Long JavaDoc(((LongConstant)otherConstant).getValue()));
132     }
133
134     public void visitFloatConstant(Clazz clazz, FloatConstant floatConstant)
135     {
136         // In JDK 1.4, we can use Float.compare(a,b).
137
result = new Float JavaDoc(floatConstant.getValue()).compareTo(new Float JavaDoc(((FloatConstant)otherConstant).getValue()));
138     }
139
140     public void visitDoubleConstant(Clazz clazz, DoubleConstant doubleConstant)
141     {
142         // In JDK 1.4, we can use Double.compare(a,b).
143
result = new Double JavaDoc(doubleConstant.getValue()).compareTo(new Double JavaDoc(((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