KickJava   Java API By Example, From Geeks To Geeks.

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


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.Constant;
25 import proguard.classfile.visitor.ClassVisitor;
26
27 import java.util.Arrays JavaDoc;
28
29 /**
30  * This ClassVisitor sorts the constant pool entries of the classes that
31  * it visits. The sorting order is based on the types of the constant pool
32  * entries in the first place, and on their contents in the second place.
33  *
34  * @author Eric Lafortune
35  */

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     // Implementations for ClassVisitor.
45

46     public void visitProgramClass(ProgramClass programClass)
47     {
48         // Sort the constant pool and set up an index map.
49
sortConstantPool(programClass,
50                          programClass.constantPool,
51                          programClass.u2constantPoolCount);
52
53         // Remap all constant pool references.
54
constantPoolRemapper.setConstantIndexMap(constantIndexMap);
55         constantPoolRemapper.visitProgramClass(programClass);
56     }
57
58
59     public void visitLibraryClass(LibraryClass libraryClass)
60     {
61     }
62
63
64     // Small utility methods.
65

66     /**
67      * Sorts the given constant pool.
68      */

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         // Initialize an array whose elements can be compared.
78
for (int oldIndex = 1; oldIndex < length; oldIndex++)
79         {
80             Constant constant = constantPool[oldIndex];
81
82             // Long entries take up two entries, the second of which is null.
83
if (constant == null)
84             {
85                 constant = constantPool[oldIndex-1];
86             }
87
88             comparableConstantPool[oldIndex] = new ComparableConstant(clazz,
89                                                                       oldIndex,
90                                                                       constant);
91         }
92
93         // Sort the array.
94
Arrays.sort(comparableConstantPool, 1, length);
95
96         // Save the sorted elements.
97
Constant previousConstant = null;
98         for (int newIndex = 1; newIndex < length; newIndex++)
99         {
100             ComparableConstant comparableConstant = comparableConstantPool[newIndex];
101
102             // Fill out the map array.
103
int oldIndex = comparableConstant.getIndex();
104             constantIndexMap[oldIndex] = newIndex;
105
106             // Copy the sorted constant pool entry over to the constant pool.
107
// Long entries take up two entries, the second of which is null.
108
Constant constant = comparableConstant.getConstant();
109             constantPool[newIndex] = constant != previousConstant ?
110                 constant :
111                 null;
112
113             previousConstant = constant;
114         }
115     }
116 }
117
Popular Tags