KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > obfuscate > NameMarker


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.visitor.*;
25
26
27 /**
28  * This <code>ClassVisitor</code> and <code>MemberVisitor</code>
29  * marks names of the classes and class members it visits. The marked names
30  * will remain unchanged in the obfuscation step.
31  *
32  * @see ClassObfuscator
33  * @see MemberObfuscator
34  *
35  * @author Eric Lafortune
36  */

37 class NameMarker
38 implements ClassVisitor,
39            MemberVisitor
40 {
41     // Implementations for ClassVisitor.
42

43     public void visitProgramClass(ProgramClass programClass)
44     {
45         keepClassName(programClass);
46     }
47
48
49     public void visitLibraryClass(LibraryClass libraryClass)
50     {
51         keepClassName(libraryClass);
52     }
53
54
55     // Implementations for MemberVisitor.
56

57     public void visitProgramField(ProgramClass programClass, ProgramField programField)
58     {
59         keepFieldName(programClass, programField);
60     }
61
62
63     public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
64     {
65         keepMethodName(programClass, programMethod);
66     }
67
68
69     public void visitLibraryField(LibraryClass libraryClass, LibraryField libraryField)
70     {
71         keepFieldName(libraryClass, libraryField);
72     }
73
74
75     public void visitLibraryMethod(LibraryClass libraryClass, LibraryMethod libraryMethod)
76     {
77         keepMethodName(libraryClass, libraryMethod);
78     }
79
80
81     // Small utility method.
82

83     /**
84      * Ensures the name of the given class name will be kept.
85      */

86     public void keepClassName(Clazz clazz)
87     {
88         ClassObfuscator.setNewClassName(clazz,
89                                         clazz.getName());
90     }
91
92
93     /**
94      * Ensures the name of the given field name will be kept.
95      */

96     private void keepFieldName(Clazz clazz, Field field)
97     {
98         MemberObfuscator.setFixedNewMemberName(field,
99                                                field.getName(clazz));
100     }
101
102
103     /**
104      * Ensures the name of the given method name will be kept.
105      */

106     private void keepMethodName(Clazz clazz, Method method)
107     {
108         String JavaDoc name = method.getName(clazz);
109
110         if (!name.equals(ClassConstants.INTERNAL_METHOD_NAME_CLINIT) &&
111             !name.equals(ClassConstants.INTERNAL_METHOD_NAME_INIT))
112         {
113             MemberObfuscator.setFixedNewMemberName(method,
114                                                    method.getName(clazz));
115         }
116     }
117 }
118
Popular Tags