2
* ProGuard -- shrinking, optimization, obfuscation, and preverification
5
* Copyright (c) 2002-2009 Eric Lafortune (eric@graphics.cornell.edu)
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)
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
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
23
import proguard.classfile.*;
24
import proguard.classfile.util.ClassUtil;
26
import java.io.IOException;
30
* This DataEntryReader delegates to another DataEntryReader, renaming the
31
* data entries based on the renamed classes in the given ClassPool.
33
* @author Eric Lafortune
35
public class DataEntryObfuscator implements DataEntryReader
37
private final ClassPool classPool;
38
private final Map packagePrefixMap;
39
private final DataEntryReader dataEntryReader;
43
* Creates a new DataEntryObfuscator.
44
* @param classPool the class pool that maps from old names to new
46
* @param packagePrefixMap the map from old package prefixes to new package
48
* @param dataEntryReader the DataEntryReader to which calls will be
51
public DataEntryObfuscator(ClassPool classPool,
53
DataEntryReader dataEntryReader)
55
this.classPool = classPool;
56
this.packagePrefixMap = packagePrefixMap;
57
this.dataEntryReader = dataEntryReader;
61
// Implementations for DataEntryReader.
63
public void read(DataEntry dataEntry) throws IOException
65
// Delegate to the actual data entry reader.
66
dataEntryReader.read(renamedDataEntry(dataEntry));
71
* Create a renamed data entry, if possible.
73
private DataEntry renamedDataEntry(DataEntry dataEntry)
75
String dataEntryName = dataEntry.getName();
77
// Try to find a corresponding class name by removing increasingly
79
for (int suffixIndex = dataEntryName.length() - 1;
83
char c = dataEntryName.charAt(suffixIndex);
84
if (!Character.isLetterOrDigit(c))
86
// Chop off the suffix.
87
String className = dataEntryName.substring(0, suffixIndex);
89
// Did we get to the package separator?
90
if (c == ClassConstants.INTERNAL_PACKAGE_SEPARATOR)
95
// Is there a class corresponding to the data entry?
96
Clazz clazz = classPool.getClass(className);
99
// Did the class get a new name?
100
String newClassName = clazz.getName();
101
if (!className.equals(newClassName))
103
// Return a renamed data entry.
104
String newDataEntryName =
105
newClassName + dataEntryName.substring(suffixIndex);
107
return new RenamedDataEntry(dataEntry, newDataEntryName);
110
// Otherwise stop looking.
116
// Did the package get a new name?
117
String packagePrefix = ClassUtil.internalPackagePrefix(dataEntryName);
118
String newPackagePrefix = (String)packagePrefixMap.get(packagePrefix);
119
if (newPackagePrefix != null &&
120
!packagePrefix.equals(newPackagePrefix))
122
// Return a renamed data entry.
123
String newDataEntryName =
124
newPackagePrefix + dataEntryName.substring(packagePrefix.length());
126
return new RenamedDataEntry(dataEntry, newDataEntryName);