KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > javaguard > PkCl


1 /**
2  * JavaGuard -- an obfuscation package for Java classfiles.
3  *
4  * Copyright (c) 1999 Mark Welsh (markw@retrologic.com)
5  * Copyright (c) 2002 Thorsten Heit (theit@gmx.de)
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 theit@gmx.de.
22  *
23  *
24  * $Id: PkCl.java,v 1.5 2002/05/13 17:22:55 glurk Exp $
25  */

26 package net.sf.javaguard;
27
28
29 import java.util.*;
30
31
32 /** Base to package and class tree item.
33  *
34  * @author <a HREF="mailto:markw@retrologic.com">Mark Welsh</a>
35  * @author <a HREF="mailto:theit@gmx.de">Thorsten Heit</a>
36  */

37 abstract public class PkCl extends TreeItem {
38   /** Stores a list of subclasses. */
39   protected Map cls;
40   
41   
42   
43   
44   /** Constructor that initializes the tree item.
45    * @param parent the tree parent for the current element
46    * @param name the name of the current element
47    */

48   public PkCl(TreeItem parent, String JavaDoc name) {
49     super(parent, name);
50     cls = new TreeMap();
51   }
52   
53   
54   
55   
56   /** Get a class by name.
57    * @param name the class name to find
58    * @return the class element with the given name
59    */

60   public Cl getClass(String JavaDoc name) {
61     return (Cl) cls.get(name);
62   }
63   
64   
65   
66   
67   /** Get an iteration for all classes directly beneath this tree item.
68    * @return iterator for all classes beneath the current tree item
69    */

70   public Iterator getClassIterator() {
71     return cls.values().iterator();
72   }
73   
74   
75   
76   
77   /** Get an iteration of all classes (both outer and inner) in the tree
78    * beneath this tree item.
79    * @return iterator for all subclasses of the current tree item
80    */

81   public Iterator getAllClassIterator() {
82     Vector allClasses = new Vector();
83     addAllClasses(allClasses);
84     return allClasses.iterator();
85   }
86   
87   
88   /** List classes and recursively compose a list of all inner classes.
89    * @param allClasses a vector that will hold all subclasses of the current
90    * tree item
91    */

92   protected void addAllClasses(Vector allClasses) {
93     for (Iterator iter = getClassIterator(); iter.hasNext(); ) {
94       Cl cl = (Cl) iter.next();
95       allClasses.addElement(cl);
96       cl.addAllClasses(allClasses);
97     }
98   }
99   
100   
101   
102   
103   /** Return the number of classes beneath the current tree item.
104    * @return number of classes beneath the tree item
105    */

106   public int getClassCount() {
107     return cls.size();
108   }
109   
110   
111   
112   
113   /** Add a class to the list of owned classes.
114    * @param name the name of the class
115    * @param superName the name of the super class
116    * @param interfaceNames array with implemented interfaces
117    */

118   abstract public Cl addClass(String JavaDoc name, String JavaDoc superName, String JavaDoc[] interfaceNames);
119   
120   
121   /** Add a class to the list of owned classes.
122    * @param name the name of the class
123    * @param isInnerClass true if the class specifies an inner class; false else
124    * @param superName the name of the super class
125    * @param interfaceNames array with implemented interfaces
126    */

127   protected Cl addClass(String JavaDoc name, boolean isInnerClass, String JavaDoc superName, String JavaDoc[] interfaceNames) {
128     Cl cl = getClass(name);
129     
130     // Remove placeholder if present
131
PlaceholderCl plClassItem = null;
132     if (cl instanceof PlaceholderCl) {
133       plClassItem = (PlaceholderCl)cl;
134       cls.remove(name);
135       cl = null;
136     }
137     
138     // Add the class, if not already present
139
if (null == cl) {
140       cl = new Cl(this, isInnerClass, name, superName, interfaceNames);
141       cls.put(name, cl);
142     }
143     
144     // Copy over the inner class data from the placeholder, if any
145
if (plClassItem != null) {
146       for (Iterator iter = plClassItem.getClassIterator(); iter.hasNext(); ) {
147         Cl innerCl = (Cl) iter.next();
148         innerCl.setParent(cl);
149         cl.addClass(innerCl);
150       }
151     }
152     return cl;
153   }
154   
155   
156   
157   
158   /** Add a placeholder class to our list of owned classes that will be replaced
159    * later by the full class.
160    * @param name the name of the placeholder class
161    */

162   abstract public Cl addPlaceholderClass(String JavaDoc name);
163   
164   
165   /** Add a placeholder class to our list of owned classes that will be replaced
166    * later by the full class.
167    * @param name the name of the placeholder class
168    * @param isInnerClass true if the placeholder class specifies an inner class;
169    * false else
170    */

171   protected Cl addPlaceholderClass(String JavaDoc name, boolean isInnerClass) {
172     Cl cl = getClass(name);
173     if (cl == null) {
174       cl = new PlaceholderCl(this, isInnerClass, name);
175       cls.put(name, cl);
176     }
177     return cl;
178   }
179   
180   
181   
182   
183   /** Generate unique obfuscated names for this namespace.
184    */

185   public void generateNames() {
186     generateNames(cls);
187   }
188   
189   
190   
191   
192   /** Generate unique obfuscated names for a given namespace.
193    * @param map the namespace
194    */

195   protected void generateNames(Map map) {
196     Vector vec = new Vector();
197     for (Iterator iter = map.values().iterator(); iter.hasNext(); ) {
198       TreeItem ti = (TreeItem) iter.next();
199       // elements that are already fixed in some way or that should be ignored
200
// must be excluded from the name generation step
201
if (ti.isFixed() || ti.canIgnoreElement()) {
202         vec.addElement(ti.getOutName());
203       }
204     }
205     String JavaDoc[] noObfNames = new String JavaDoc[vec.size()];
206     for (int i = 0; i < noObfNames.length; i++) {
207       noObfNames[i] = (String JavaDoc)vec.elementAt(i);
208     }
209     NameMaker nm = new KeywordNameMaker(noObfNames, false, true);
210     for (Iterator iter = map.values().iterator(); iter.hasNext(); ) {
211       TreeItem ti = (TreeItem) iter.next();
212       if (!ti.isFixed() && !ti.canIgnoreElement()) {
213         ti.setOutName(nm.nextName(null));
214       }
215     }
216   }
217 }
218
Popular Tags