KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > obfuscate > ClassRenamer


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 library 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 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.*;
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
40 extends SimplifiedVisitor
41 implements ClassVisitor,
42              MemberVisitor,
43              ConstantVisitor
44 {
45     private ConstantPoolEditor constantPoolEditor = new ConstantPoolEditor();
46
47
48     // Implementations for ClassVisitor.
49

50     public void visitProgramClass(ProgramClass programClass)
51     {
52         // Rename this class.
53
programClass.constantPoolEntryAccept(programClass.u2thisClass, this);
54
55         // Rename the class members.
56
programClass.fieldsAccept(this);
57         programClass.methodsAccept(this);
58     }
59
60
61     public void visitLibraryClass(LibraryClass libraryClass)
62     {
63         libraryClass.thisClassName = ClassObfuscator.newClassName(libraryClass);
64
65         // Rename the class members.
66
libraryClass.fieldsAccept(this);
67         libraryClass.methodsAccept(this);
68     }
69
70
71     // Implementations for MemberVisitor.
72

73     public void visitProgramMember(ProgramClass programClass,
74                                      ProgramMember programMember)
75     {
76         // Has the class member name changed?
77
String JavaDoc name = programMember.getName(programClass);
78         String JavaDoc newName = MemberObfuscator.newMemberName(programMember);
79         if (newName != null &&
80             !newName.equals(name))
81         {
82             programMember.u2nameIndex =
83                 constantPoolEditor.addUtf8Constant(programClass, newName);
84         }
85     }
86
87     public void visitLibraryMember(LibraryClass libraryClass,
88                                      LibraryMember libraryMember)
89     {
90         String JavaDoc newName = MemberObfuscator.newMemberName(libraryMember);
91         if (newName != null)
92         {
93             libraryMember.name = newName;
94         }
95     }
96
97
98     // Implementations for ConstantVisitor.
99

100     public void visitClassConstant(Clazz clazz, ClassConstant classConstant)
101     {
102         // Update the Class entry if required.
103
String JavaDoc newName = ClassObfuscator.newClassName(clazz);
104         if (newName != null)
105         {
106             // Refer to a new Utf8 entry.
107
classConstant.u2nameIndex =
108                 constantPoolEditor.addUtf8Constant((ProgramClass)clazz,
109                                                    newName);
110         }
111     }
112 }
113
Popular Tags