01: /*
02: * ProGuard -- shrinking, optimization, obfuscation, and preverification
03: * of Java bytecode.
04: *
05: * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
06: *
07: * This program is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU General Public License as published by the Free
09: * 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.obfuscate;
22:
23: import proguard.classfile.*;
24: import proguard.classfile.constant.Constant;
25: import proguard.classfile.editor.ConstantPoolRemapper;
26: import proguard.classfile.visitor.ClassVisitor;
27:
28: /**
29: * This ClassVisitor removes NameAndType constant pool entries
30: * that are not marked as being used.
31: *
32: * @see NameAndTypeUsageMarker
33: *
34: * @author Eric Lafortune
35: */
36: public class NameAndTypeShrinker implements ClassVisitor {
37: private int[] constantIndexMap;
38: private final ConstantPoolRemapper constantPoolRemapper = new ConstantPoolRemapper();
39:
40: // Implementations for ClassVisitor.
41:
42: public void visitProgramClass(ProgramClass programClass) {
43: // Shift the used constant pool entries together, filling out the
44: // index map.
45: programClass.u2constantPoolCount = shrinkConstantPool(
46: programClass.constantPool,
47: programClass.u2constantPoolCount);
48:
49: // Remap all constant pool references.
50: constantPoolRemapper.setConstantIndexMap(constantIndexMap);
51: constantPoolRemapper.visitProgramClass(programClass);
52: }
53:
54: public void visitLibraryClass(LibraryClass libraryClass) {
55: }
56:
57: // Small utility methods.
58:
59: /**
60: * Removes all NameAndType entries that are not marked as being used
61: * from the given constant pool.
62: * @return the new number of entries.
63: */
64: private int shrinkConstantPool(Constant[] constantPool, int length) {
65: // Create a new index map, if necessary.
66: if (constantIndexMap == null
67: || constantIndexMap.length < length) {
68: constantIndexMap = new int[length];
69: }
70:
71: int counter = 1;
72: boolean isUsed = false;
73:
74: // Shift the used constant pool entries together.
75: for (int index = 1; index < length; index++) {
76: constantIndexMap[index] = counter;
77:
78: Constant constant = constantPool[index];
79:
80: // Don't update the flag if this is the second half of a long entry.
81: if (constant != null) {
82: isUsed = constant.getTag() != ClassConstants.CONSTANT_NameAndType
83: || NameAndTypeUsageMarker.isUsed(constant);
84: }
85:
86: if (isUsed) {
87: constantPool[counter++] = constant;
88: }
89: }
90:
91: // Clear the remaining constant pool elements.
92: for (int index = counter; index < length; index++) {
93: constantPool[index] = null;
94: }
95:
96: return counter;
97: }
98: }
|