001: /*
002: * ProGuard -- shrinking, optimization, obfuscation, and preverification
003: * of Java bytecode.
004: *
005: * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the Free
009: * Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful, but WITHOUT
013: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
015: * more details.
016: *
017: * You should have received a copy of the GNU General Public License along
018: * with this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: package proguard.obfuscate;
022:
023: import proguard.classfile.*;
024: import proguard.classfile.util.*;
025:
026: /**
027: * This MappingKeeper applies the mappings that it receives to its class pool,
028: * so these mappings are ensured in a subsequent obfuscation step.
029: *
030: * @author Eric Lafortune
031: */
032: public class MappingKeeper implements MappingProcessor {
033: private final ClassPool classPool;
034: private final WarningPrinter warningPrinter;
035:
036: // A field acting as a parameter.
037: private Clazz clazz;
038:
039: /**
040: * Creates a new MappingKeeper.
041: * @param classPool the class pool in which class names and class
042: * member names have to be mapped.
043: * @param warningPrinter the optional warning printer to which warnings
044: * can be printed.
045: */
046: public MappingKeeper(ClassPool classPool,
047: WarningPrinter warningPrinter) {
048: this .classPool = classPool;
049: this .warningPrinter = warningPrinter;
050: }
051:
052: // Implementations for MappingProcessor.
053:
054: public boolean processClassMapping(String className,
055: String newClassName) {
056: // Find the class.
057: String name = ClassUtil.internalClassName(className);
058:
059: clazz = classPool.getClass(name);
060: if (clazz != null) {
061: String newName = ClassUtil.internalClassName(newClassName);
062:
063: // Print out a warning if the mapping conflicts with a name that
064: // was set before.
065: if (warningPrinter != null) {
066: String currentNewName = ClassObfuscator
067: .newClassName(clazz);
068: if (currentNewName != null
069: && !currentNewName.equals(newName)) {
070: warningPrinter.print("Warning: "
071: + className
072: + " is not being kept as '"
073: + ClassUtil
074: .externalClassName(currentNewName)
075: + "', but remapped to '" + newClassName
076: + "'");
077: }
078: }
079:
080: ClassObfuscator.setNewClassName(clazz, newName);
081:
082: // The class members have to be kept as well.
083: return true;
084: }
085:
086: return false;
087: }
088:
089: public void processFieldMapping(String className, String fieldType,
090: String fieldName, String newFieldName) {
091: if (clazz != null) {
092: // Find the field.
093: String name = fieldName;
094: String descriptor = ClassUtil.internalType(fieldType);
095:
096: Field field = clazz.findField(name, descriptor);
097: if (field != null) {
098: // Print out a warning if the mapping conflicts with a name that
099: // was set before.
100: if (warningPrinter != null) {
101: String currentNewName = MemberObfuscator
102: .newMemberName(field);
103: if (currentNewName != null
104: && !currentNewName.equals(newFieldName)) {
105: warningPrinter.print("Warning: " + className
106: + ": field '" + fieldType + " "
107: + fieldName
108: + "' is not being kept as '"
109: + currentNewName
110: + "', but remapped to '" + newFieldName
111: + "'");
112: }
113: }
114:
115: // Make sure the mapping name will be kept.
116: MemberObfuscator.setFixedNewMemberName(field,
117: newFieldName);
118: }
119: }
120: }
121:
122: public void processMethodMapping(String className,
123: int firstLineNumber, int lastLineNumber,
124: String methodReturnType, String methodNameAndArguments,
125: String newMethodName) {
126: if (clazz != null) {
127: // Find the method.
128: String name = ClassUtil
129: .externalMethodName(methodNameAndArguments);
130: String descriptor = ClassUtil.internalMethodDescriptor(
131: methodReturnType, methodNameAndArguments);
132:
133: Method method = clazz.findMethod(name, descriptor);
134: if (method != null) {
135: // Print out a warning if the mapping conflicts with a name that
136: // was set before.
137: if (warningPrinter != null) {
138: String currentNewName = MemberObfuscator
139: .newMemberName(method);
140: if (currentNewName != null
141: && !currentNewName.equals(newMethodName)) {
142: warningPrinter.print("Warning: " + className
143: + ": method '" + methodReturnType + " "
144: + methodNameAndArguments
145: + "' is not being kept as '"
146: + currentNewName
147: + "', but remapped to '"
148: + newMethodName + "'");
149: }
150: }
151:
152: // Make sure the mapping name will be kept.
153: MemberObfuscator.setFixedNewMemberName(method,
154: newMethodName);
155: }
156: }
157: }
158: }
|