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 library 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 library 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 library; 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.ClassConstant;
25: import proguard.classfile.constant.visitor.ConstantVisitor;
26: import proguard.classfile.editor.ConstantPoolEditor;
27: import proguard.classfile.util.SimplifiedVisitor;
28: import proguard.classfile.visitor.*;
29:
30: /**
31: * This <code>ClassVisitor</code> renames the class names and class member
32: * names of the classes it visits, using names previously determined by the
33: * obfuscator.
34: *
35: * @see ClassObfuscator
36: *
37: * @author Eric Lafortune
38: */
39: public class ClassRenamer extends SimplifiedVisitor implements
40: ClassVisitor, MemberVisitor, ConstantVisitor {
41: private final ConstantPoolEditor constantPoolEditor = new ConstantPoolEditor();
42:
43: // Implementations for ClassVisitor.
44:
45: public void visitProgramClass(ProgramClass programClass) {
46: // Rename this class.
47: programClass.constantPoolEntryAccept(programClass.u2this Class,
48: this );
49:
50: // Rename the class members.
51: programClass.fieldsAccept(this );
52: programClass.methodsAccept(this );
53: }
54:
55: public void visitLibraryClass(LibraryClass libraryClass) {
56: libraryClass.this ClassName = ClassObfuscator
57: .newClassName(libraryClass);
58:
59: // Rename the class members.
60: libraryClass.fieldsAccept(this );
61: libraryClass.methodsAccept(this );
62: }
63:
64: // Implementations for MemberVisitor.
65:
66: public void visitProgramMember(ProgramClass programClass,
67: ProgramMember programMember) {
68: // Has the class member name changed?
69: String name = programMember.getName(programClass);
70: String newName = MemberObfuscator.newMemberName(programMember);
71: if (newName != null && !newName.equals(name)) {
72: programMember.u2nameIndex = constantPoolEditor
73: .addUtf8Constant(programClass, newName);
74: }
75: }
76:
77: public void visitLibraryMember(LibraryClass libraryClass,
78: LibraryMember libraryMember) {
79: String newName = MemberObfuscator.newMemberName(libraryMember);
80: if (newName != null) {
81: libraryMember.name = newName;
82: }
83: }
84:
85: // Implementations for ConstantVisitor.
86:
87: public void visitClassConstant(Clazz clazz,
88: ClassConstant classConstant) {
89: // Update the Class entry if required.
90: String newName = ClassObfuscator.newClassName(clazz);
91: if (newName != null) {
92: // Refer to a new Utf8 entry.
93: classConstant.u2nameIndex = constantPoolEditor
94: .addUtf8Constant((ProgramClass) clazz, newName);
95: }
96: }
97: }
|