ClassRenamer.java revision db267bc191f906f55eaef21a27110cce2ec57fdf
1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2009 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 */
21package proguard.obfuscate;
22
23import proguard.classfile.*;
24import proguard.classfile.constant.ClassConstant;
25import proguard.classfile.constant.visitor.ConstantVisitor;
26import proguard.classfile.editor.ConstantPoolEditor;
27import proguard.classfile.util.SimplifiedVisitor;
28import 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 * @see MemberObfuscator
37 *
38 * @author Eric Lafortune
39 */
40public class ClassRenamer
41extends      SimplifiedVisitor
42implements   ClassVisitor,
43             MemberVisitor,
44             ConstantVisitor
45{
46    // Implementations for ClassVisitor.
47
48    public void visitProgramClass(ProgramClass programClass)
49    {
50        // Rename this class.
51        programClass.thisClassConstantAccept(this);
52
53        // Rename the class members.
54        programClass.fieldsAccept(this);
55        programClass.methodsAccept(this);
56    }
57
58
59    public void visitLibraryClass(LibraryClass libraryClass)
60    {
61        libraryClass.thisClassName = ClassObfuscator.newClassName(libraryClass);
62
63        // Rename the class members.
64        libraryClass.fieldsAccept(this);
65        libraryClass.methodsAccept(this);
66    }
67
68
69    // Implementations for MemberVisitor.
70
71    public void visitProgramMember(ProgramClass  programClass,
72                                     ProgramMember programMember)
73    {
74        // Has the class member name changed?
75        String name    = programMember.getName(programClass);
76        String newName = MemberObfuscator.newMemberName(programMember);
77        if (newName != null &&
78            !newName.equals(name))
79        {
80            programMember.u2nameIndex =
81                new ConstantPoolEditor(programClass).addUtf8Constant(newName);
82        }
83    }
84
85    public void visitLibraryMember(LibraryClass  libraryClass,
86                                   LibraryMember libraryMember)
87    {
88        String newName = MemberObfuscator.newMemberName(libraryMember);
89        if (newName != null)
90        {
91            libraryMember.name = newName;
92        }
93    }
94
95
96    // Implementations for ConstantVisitor.
97
98    public void visitClassConstant(Clazz clazz, ClassConstant classConstant)
99    {
100        // Update the Class entry if required.
101        String newName = ClassObfuscator.newClassName(clazz);
102        if (newName != null)
103        {
104            // Refer to a new Utf8 entry.
105            classConstant.u2nameIndex =
106                new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newName);
107        }
108    }
109}
110