// Initialize an array whose elements can be compared.
int sortLength = 0;
for (int oldIndex = 1; oldIndex < constantPoolCount; oldIndex++)
{
Constant constant = programClass.constantPool[oldIndex];
if (constant != null)
{
comparableConstantPool[sortLength++] =
new ComparableConstant(programClass, oldIndex, constant);
}
}
// Sort the array.
Arrays.sort(comparableConstantPool, 0, sortLength);
// Save the sorted elements.
int newLength = 1;
int newIndex = 1;
ComparableConstant previousComparableConstant = null;
for (int sortIndex = 0; sortIndex < sortLength; sortIndex++)
{
ComparableConstant comparableConstant = comparableConstantPool[sortIndex];
// Isn't this a duplicate of the previous constant?
if (!comparableConstant.equals(previousComparableConstant))
{
// Remember the index of the new entry.
newIndex = newLength;
// Copy the sorted constant pool entry over to the constant pool.
Constant constant = comparableConstant.getConstant();
newConstantPool[newLength++] = constant;
// Long entries take up two slots, the second of which is null.
int tag = constant.getTag();
if (tag == ClassConstants.CONSTANT_Long ||
tag == ClassConstants.CONSTANT_Double)
{
newConstantPool[newLength++] = null;
}