~ubuntu-branches/ubuntu/breezy/proguard/breezy

« back to all changes in this revision

Viewing changes to src/proguard/obfuscate/ClassFileObfuscator.java

  • Committer: Bazaar Package Importer
  • Author(s): Sam Clegg
  • Date: 2005-06-17 14:25:24 UTC
  • Revision ID: james.westby@ubuntu.com-20050617142524-thz2yfa3vemy3j9d
Tags: upstream-3.2
ImportĀ upstreamĀ versionĀ 3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: ClassFileObfuscator.java,v 1.23 2004/11/01 21:17:51 eric Exp $
 
2
 *
 
3
 * ProGuard -- shrinking, optimization, and obfuscation of Java class files.
 
4
 *
 
5
 * Copyright (c) 2002-2004 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.obfuscate;
 
22
 
 
23
import proguard.classfile.*;
 
24
import proguard.classfile.util.*;
 
25
import proguard.classfile.visitor.*;
 
26
 
 
27
import java.util.*;
 
28
 
 
29
 
 
30
/**
 
31
 * This <code>ClassFileVisitor</code> comes up with obfuscated names for the
 
32
 * class files it visits, and for their class members. The actual renaming is
 
33
 * done afterward.
 
34
 *
 
35
 * @see ClassFileRenamer
 
36
 *
 
37
 * @author Eric Lafortune
 
38
 */
 
39
public class ClassFileObfuscator
 
40
  implements ClassFileVisitor
 
41
{
 
42
    private boolean useMixedCaseClassNames;
 
43
    private String  defaultPackageName;
 
44
 
 
45
    // Map: [package name - class name factory]
 
46
    private final Map         packageMap = new HashMap();
 
47
    private final NameFactory defaultPackageClassNameFactory;
 
48
    private final Set         namesToAvoid = new HashSet();
 
49
 
 
50
 
 
51
    /**
 
52
     * Creates a new ClassFileObfuscator.
 
53
     * @param programClassPool   the class pool in which class names have to be
 
54
     *                           unique.
 
55
     * @param defaultPackageName the package in which all classes that don't
 
56
     *                           have fixed names will be put, or <code>null</code>,
 
57
     *                           if all classes can remain in their original
 
58
     *                           packages.
 
59
     * @param allowAggressiveOverloading a flag that specifies whether class
 
60
     *                           members can be overloaded aggressively.
 
61
     */
 
62
    public ClassFileObfuscator(ClassPool programClassPool,
 
63
                               String    defaultPackageName,
 
64
                               boolean   useMixedCaseClassNames)
 
65
    {
 
66
        this.defaultPackageName             = defaultPackageName;
 
67
        this.useMixedCaseClassNames         = useMixedCaseClassNames;
 
68
        this.defaultPackageClassNameFactory = new SimpleNameFactory(useMixedCaseClassNames);
 
69
 
 
70
        // Collect all names that have been taken already.
 
71
        programClassPool.classFilesAccept(new ClassFileVisitor()
 
72
        {
 
73
            public void visitProgramClassFile(ProgramClassFile programClassFile)
 
74
            {
 
75
                String newClassName = newClassName(programClassFile);
 
76
                if (newClassName != null)
 
77
                {
 
78
                    namesToAvoid.add(newClassName);
 
79
                }
 
80
            }
 
81
 
 
82
            public void visitLibraryClassFile(LibraryClassFile libraryClassFile)
 
83
            {
 
84
            }
 
85
        });
 
86
    }
 
87
 
 
88
 
 
89
    // Implementations for ClassFileVisitor.
 
90
 
 
91
    public void visitProgramClassFile(ProgramClassFile programClassFile)
 
92
    {
 
93
        // Does this class file still need a new name?
 
94
        if (newClassName(programClassFile) == null)
 
95
        {
 
96
            // Figure out a new name.
 
97
            String className   = programClassFile.getName();
 
98
            String packageName = ClassUtil.internalPackageName(className);
 
99
 
 
100
            String newPackageName = packageName;
 
101
 
 
102
            // Find the right name factory for this package, or use the default.
 
103
            NameFactory packageClassNameFactory = (NameFactory)packageMap.get(packageName);
 
104
            if (packageClassNameFactory == null)
 
105
            {
 
106
                // Do we have a default package name?
 
107
                if (defaultPackageName == null)
 
108
                {
 
109
                    // We haven't seen this package before. Create a new name factory
 
110
                    // for it.
 
111
                    packageClassNameFactory = new SimpleNameFactory(useMixedCaseClassNames);
 
112
                    packageMap.put(packageName, packageClassNameFactory);
 
113
                }
 
114
                else
 
115
                {
 
116
                    // Fall back on the default package class name factory and name.
 
117
                    packageClassNameFactory = defaultPackageClassNameFactory;
 
118
                    newPackageName          = defaultPackageName;
 
119
                }
 
120
            }
 
121
 
 
122
            // Come up with class names until we get an original one.
 
123
            String newClassName;
 
124
            do
 
125
            {
 
126
                // Let the factory produce a class name.
 
127
                newClassName = packageClassNameFactory.nextName();
 
128
 
 
129
                // We may have to add a package part to the class name.
 
130
                if (newPackageName.length() > 0)
 
131
                {
 
132
                    newClassName =
 
133
                        newPackageName +
 
134
                        ClassConstants.INTERNAL_PACKAGE_SEPARATOR +
 
135
                        newClassName;
 
136
                }
 
137
 
 
138
            }
 
139
            while (namesToAvoid.contains(newClassName));
 
140
 
 
141
            setNewClassName(programClassFile, newClassName);
 
142
        }
 
143
    }
 
144
 
 
145
 
 
146
    public void visitLibraryClassFile(LibraryClassFile libraryClassFile)
 
147
    {
 
148
    }
 
149
 
 
150
 
 
151
    // Small utility methods.
 
152
 
 
153
    /**
 
154
     * Assigns a new name to the given class file.
 
155
     * @param classFile the given class file.
 
156
     * @param name      the new name.
 
157
     */
 
158
    static void setNewClassName(ClassFile classFile, String name)
 
159
    {
 
160
        classFile.setVisitorInfo(name);
 
161
    }
 
162
 
 
163
 
 
164
 
 
165
    /**
 
166
     * Retrieves the new name of the given class file.
 
167
     * @param classFile the given class file.
 
168
     * @return the class file's new name, or <code>null</code> if it doesn't
 
169
     *         have one yet.
 
170
     */
 
171
    static String newClassName(ClassFile classFile)
 
172
    {
 
173
        Object visitorInfo = classFile.getVisitorInfo();
 
174
 
 
175
        return visitorInfo instanceof String ?
 
176
            (String)visitorInfo :
 
177
            null;
 
178
    }
 
179
}