KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > yworks > yguard > obf > PkCl


1 /**
2  * YGuard -- an obfuscation library for Java(TM) classfiles.
3  *
4  * Original Copyright (c) 1999 Mark Welsh (markw@retrologic.com)
5  * Modifications Copyright (c) 2002 yWorks GmbH (yguard@yworks.com)
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * The author may be contacted at yguard@yworks.com
22  *
23  * Java and all Java-based marks are trademarks or registered
24  * trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
25  */

26 package com.yworks.yguard.obf;
27
28 import com.yworks.yguard.obf.classfile.Logger;
29
30 import java.io.*;
31 import java.lang.reflect.*;
32 import java.util.*;
33
34 /**
35  * Base to package and class tree item.
36  *
37  * @author Mark Welsh
38  */

39 abstract public class PkCl extends TreeItem
40 {
41     // Constants -------------------------------------------------------------
42

43
44     // Fields ----------------------------------------------------------------
45
/** Owns a list of classes. */
46     protected Hashtable cls = new Hashtable();
47
48
49     // Class Methods ---------------------------------------------------------
50

51
52     // Instance Methods ------------------------------------------------------
53
/** Ctor. */
54     public PkCl(TreeItem parent, String JavaDoc name)
55     {
56         super(parent, name);
57     }
58
59     /** Get a class by name. */
60     public Cl getClass(String JavaDoc name) {return (Cl)cls.get(name);}
61
62     /** Get an Enumeration of classes directly beneath this PkCl. */
63     public Enumeration getClassEnum() {return cls.elements();}
64
65     /** Get an Enumeration of all classes (outer and inner) in the tree beneath this PkCl. */
66     public Enumeration getAllClassEnum()
67     {
68         Vector allClasses = new Vector();
69         addAllClasses(allClasses);
70         return allClasses.elements();
71     }
72     /** List classes and recursively compose a list of all inner classes. */
73     protected void addAllClasses(Vector allClasses)
74     {
75         for (Enumeration enumeration = cls.elements(); enumeration.hasMoreElements(); )
76         {
77             Cl cl = (Cl)enumeration.nextElement();
78             allClasses.addElement(cl);
79             cl.addAllClasses(allClasses);
80         }
81     }
82
83     /** Return number of classes. */
84     public int getClassCount() {return cls.size();}
85
86     /** Add a class to the list of owned classes. */
87     abstract public Cl addClass(String JavaDoc name, String JavaDoc superName, String JavaDoc[] interfaceNames, int modifiers) ;
88
89     
90     /** Add a class to the list of owned classes. */
91     public Cl addClass(boolean isInnerClass, String JavaDoc name, String JavaDoc superName, String JavaDoc[] interfaceNames, int modifiers)
92     {
93         Cl cl = getClass(name);
94
95         // Remove placeholder if present
96
PlaceholderCl plClassItem = null;
97         if (cl instanceof PlaceholderCl)
98         {
99             plClassItem = (PlaceholderCl)cl;
100             cls.remove(name);
101             cl = null;
102         }
103
104         // Add the class, if not already present
105
if (cl == null)
106         {
107             cl = new Cl(this, isInnerClass, name, superName, interfaceNames, modifiers);
108             cls.put(name, cl);
109         }
110
111         // Copy over the inner class data from the placeholder, if any
112
if (plClassItem != null)
113         {
114             for (Enumeration enumeration = plClassItem.getClassEnum(); enumeration.hasMoreElements(); )
115             {
116                 Cl innerCl = (Cl)enumeration.nextElement();
117                 innerCl.setParent(cl);
118                 cl.addClass(innerCl);
119             }
120         }
121         return cl;
122     }
123
124     /** Add a placeholder class to our list of owned classes, to be replaced later by the full class. */
125     abstract public Cl addPlaceholderClass(String JavaDoc name) ;
126
127     /** Add a placeholder class to our list of owned classes, to be replaced later by the full class. */
128     public Cl addPlaceholderClass(boolean isInnerClass, String JavaDoc name)
129     {
130         Cl cl = getClass(name);
131         if (cl == null)
132         {
133             cl = new PlaceholderCl(this, isInnerClass, name);
134             cls.put(name, cl);
135         }
136         return cl;
137     }
138
139     /** Generate unique obfuscated names for this namespace. */
140     public void generateNames()
141     {
142         generateNames(cls);
143     }
144
145     /** Generate unique obfuscated names for a given namespace. */
146     protected void generateNames(Hashtable hash)
147     {
148         Vector vec = new Vector();
149         for (Enumeration enumeration = hash.elements(); enumeration.hasMoreElements(); )
150         {
151             TreeItem ti = (TreeItem)enumeration.nextElement();
152             if (ti.isFixed())
153             {
154                 vec.addElement(ti.getOutName());
155             }
156         }
157         String JavaDoc[] noObfNames = new String JavaDoc[vec.size()];
158         for (int i = 0; i < noObfNames.length; i++)
159         {
160             noObfNames[i] = (String JavaDoc)vec.elementAt(i);
161         }
162         NameMakerFactory nmf = NameMakerFactory.getInstance();
163         for (Enumeration enumeration = hash.elements(); enumeration.hasMoreElements(); )
164         {
165             TreeItem ti = (TreeItem)enumeration.nextElement();
166             if (!ti.isFixed())
167             {
168                 if (ti instanceof Cl && ((Cl)ti).isInnerClass()){
169                   NameMaker innerClassNameMaker = nmf.getInnerClassNameMaker(noObfNames, getFullInName());
170                   ti.setOutName(innerClassNameMaker.nextName(null));
171                 } else if (ti instanceof Pk){
172                   NameMaker packageNameMaker = nmf.getPackageNameMaker(noObfNames, getFullInName());
173                   ti.setOutName(packageNameMaker.nextName(null));
174                 } else {
175                   NameMaker classNameMaker = nmf.getClassNameMaker(noObfNames, getFullInName());
176                   boolean newNameFound = true;
177                   Cl.ClassResolver resolver = Cl.getClassResolver();
178                   do {
179                     ti.setOutName(classNameMaker.nextName(null));
180                     String JavaDoc newName = ti.getFullOutName();
181                     try{
182                       resolver.resolve(newName);
183                       newNameFound = false;
184                     } catch (ClassNotFoundException JavaDoc cnfe){
185                       newNameFound = true;
186                     }
187                   } while (!newNameFound);
188                 }
189             }
190         }
191     }
192 }
193
194
Popular Tags