KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > obfuscate > MappingKeeper


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 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
26
27 /**
28  * This MappingKeeper applies the mappings that it receives to its class pool,
29  * so these mappings are ensured in a subsequent obfuscation step.
30  *
31  * @author Eric Lafortune
32  */

33 public class MappingKeeper implements MappingProcessor
34 {
35     private ClassPool classPool;
36     private WarningPrinter warningPrinter;
37
38     // A field acting as a parameter.
39
private Clazz clazz;
40
41
42     /**
43      * Creates a new MappingKeeper.
44      * @param classPool the class pool in which class names and class
45      * member names have to be mapped.
46      * @param warningPrinter the optional warning printer to which warnings
47      * can be printed.
48      */

49     public MappingKeeper(ClassPool classPool,
50                          WarningPrinter warningPrinter)
51     {
52         this.classPool = classPool;
53         this.warningPrinter = warningPrinter;
54     }
55
56
57     // Implementations for MappingProcessor.
58

59     public boolean processClassMapping(String JavaDoc className,
60                                        String JavaDoc newClassName)
61     {
62         // Find the class.
63
String JavaDoc name = ClassUtil.internalClassName(className);
64
65         clazz = classPool.getClass(name);
66         if (clazz != null)
67         {
68             String JavaDoc newName = ClassUtil.internalClassName(newClassName);
69
70             // Print out a warning if the mapping conflicts with a name that
71
// was set before.
72
if (warningPrinter != null)
73             {
74                 String JavaDoc currentNewName = ClassObfuscator.newClassName(clazz);
75                 if (currentNewName != null &&
76                     !currentNewName.equals(newName))
77                 {
78                     warningPrinter.print("Warning: " +
79                                          className +
80                                          " is not being kept as '" +
81                                          ClassUtil.externalClassName(currentNewName) +
82                                          "', but remapped to '" +
83                                          newClassName + "'");
84                 }
85             }
86
87             ClassObfuscator.setNewClassName(clazz, newName);
88
89             // The class members have to be kept as well.
90
return true;
91         }
92
93         return false;
94     }
95
96
97     public void processFieldMapping(String JavaDoc className,
98                                     String JavaDoc fieldType,
99                                     String JavaDoc fieldName,
100                                     String JavaDoc newFieldName)
101     {
102         if (clazz != null)
103         {
104             // Find the field.
105
String JavaDoc name = fieldName;
106             String JavaDoc descriptor = ClassUtil.internalType(fieldType);
107
108             Field field = clazz.findField(name, descriptor);
109             if (field != null)
110             {
111                 // Print out a warning if the mapping conflicts with a name that
112
// was set before.
113
if (warningPrinter != null)
114                 {
115                     String JavaDoc currentNewName = MemberObfuscator.newMemberName(field);
116                     if (currentNewName != null &&
117                         !currentNewName.equals(newFieldName))
118                     {
119                         warningPrinter.print("Warning: " +
120                                              className +
121                                              ": field '" + fieldType + " " + fieldName +
122                                              "' is not being kept as '" + currentNewName +
123                                              "', but remapped to '" + newFieldName + "'");
124                     }
125                 }
126
127                 // Make sure the mapping name will be kept.
128
MemberObfuscator.setFixedNewMemberName(field, newFieldName);
129             }
130         }
131     }
132
133
134     public void processMethodMapping(String JavaDoc className,
135                                      int firstLineNumber,
136                                      int lastLineNumber,
137                                      String JavaDoc methodReturnType,
138                                      String JavaDoc methodNameAndArguments,
139                                      String JavaDoc newMethodName)
140     {
141         if (clazz != null)
142         {
143             // Find the method.
144
String JavaDoc name = ClassUtil.externalMethodName(methodNameAndArguments);
145             String JavaDoc descriptor = ClassUtil.internalMethodDescriptor(methodReturnType,
146                                                                    methodNameAndArguments);
147
148             Method method = clazz.findMethod(name, descriptor);
149             if (method != null)
150             {
151                 // Print out a warning if the mapping conflicts with a name that
152
// was set before.
153
if (warningPrinter != null)
154                 {
155                     String JavaDoc currentNewName = MemberObfuscator.newMemberName(method);
156                     if (currentNewName != null &&
157                         !currentNewName.equals(newMethodName))
158                     {
159                         warningPrinter.print("Warning: " +
160                                              className +
161                                              ": method '" + methodReturnType + " " + methodNameAndArguments +
162                                              "' is not being kept as '" + currentNewName +
163                                              "', but remapped to '" + newMethodName + "'");
164                     }
165                 }
166
167                 // Make sure the mapping name will be kept.
168
MemberObfuscator.setFixedNewMemberName(method, newMethodName);
169             }
170         }
171     }
172 }
173
Popular Tags