001: /**
002: * JavaGuard -- an obfuscation package for Java classfiles.
003: *
004: * Copyright (c) 1999 Mark Welsh (markw@retrologic.com)
005: * Copyright (c) 2002 Thorsten Heit (theit@gmx.de)
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * The author may be contacted at theit@gmx.de.
022: *
023: *
024: * $Id: PkCl.java,v 1.5 2002/05/13 17:22:55 glurk Exp $
025: */package net.sf.javaguard;
026:
027: import java.util.*;
028:
029: /** Base to package and class tree item.
030: *
031: * @author <a href="mailto:markw@retrologic.com">Mark Welsh</a>
032: * @author <a href="mailto:theit@gmx.de">Thorsten Heit</a>
033: */
034: abstract public class PkCl extends TreeItem {
035: /** Stores a list of subclasses. */
036: protected Map cls;
037:
038: /** Constructor that initializes the tree item.
039: * @param parent the tree parent for the current element
040: * @param name the name of the current element
041: */
042: public PkCl(TreeItem parent, String name) {
043: super (parent, name);
044: cls = new TreeMap();
045: }
046:
047: /** Get a class by name.
048: * @param name the class name to find
049: * @return the class element with the given name
050: */
051: public Cl getClass(String name) {
052: return (Cl) cls.get(name);
053: }
054:
055: /** Get an iteration for all classes directly beneath this tree item.
056: * @return iterator for all classes beneath the current tree item
057: */
058: public Iterator getClassIterator() {
059: return cls.values().iterator();
060: }
061:
062: /** Get an iteration of all classes (both outer and inner) in the tree
063: * beneath this tree item.
064: * @return iterator for all subclasses of the current tree item
065: */
066: public Iterator getAllClassIterator() {
067: Vector allClasses = new Vector();
068: addAllClasses(allClasses);
069: return allClasses.iterator();
070: }
071:
072: /** List classes and recursively compose a list of all inner classes.
073: * @param allClasses a vector that will hold all subclasses of the current
074: * tree item
075: */
076: protected void addAllClasses(Vector allClasses) {
077: for (Iterator iter = getClassIterator(); iter.hasNext();) {
078: Cl cl = (Cl) iter.next();
079: allClasses.addElement(cl);
080: cl.addAllClasses(allClasses);
081: }
082: }
083:
084: /** Return the number of classes beneath the current tree item.
085: * @return number of classes beneath the tree item
086: */
087: public int getClassCount() {
088: return cls.size();
089: }
090:
091: /** Add a class to the list of owned classes.
092: * @param name the name of the class
093: * @param superName the name of the super class
094: * @param interfaceNames array with implemented interfaces
095: */
096: abstract public Cl addClass(String name, String super Name,
097: String[] interfaceNames);
098:
099: /** Add a class to the list of owned classes.
100: * @param name the name of the class
101: * @param isInnerClass true if the class specifies an inner class; false else
102: * @param superName the name of the super class
103: * @param interfaceNames array with implemented interfaces
104: */
105: protected Cl addClass(String name, boolean isInnerClass,
106: String super Name, String[] interfaceNames) {
107: Cl cl = getClass(name);
108:
109: // Remove placeholder if present
110: PlaceholderCl plClassItem = null;
111: if (cl instanceof PlaceholderCl) {
112: plClassItem = (PlaceholderCl) cl;
113: cls.remove(name);
114: cl = null;
115: }
116:
117: // Add the class, if not already present
118: if (null == cl) {
119: cl = new Cl(this , isInnerClass, name, super Name,
120: interfaceNames);
121: cls.put(name, cl);
122: }
123:
124: // Copy over the inner class data from the placeholder, if any
125: if (plClassItem != null) {
126: for (Iterator iter = plClassItem.getClassIterator(); iter
127: .hasNext();) {
128: Cl innerCl = (Cl) iter.next();
129: innerCl.setParent(cl);
130: cl.addClass(innerCl);
131: }
132: }
133: return cl;
134: }
135:
136: /** Add a placeholder class to our list of owned classes that will be replaced
137: * later by the full class.
138: * @param name the name of the placeholder class
139: */
140: abstract public Cl addPlaceholderClass(String name);
141:
142: /** Add a placeholder class to our list of owned classes that will be replaced
143: * later by the full class.
144: * @param name the name of the placeholder class
145: * @param isInnerClass true if the placeholder class specifies an inner class;
146: * false else
147: */
148: protected Cl addPlaceholderClass(String name, boolean isInnerClass) {
149: Cl cl = getClass(name);
150: if (cl == null) {
151: cl = new PlaceholderCl(this , isInnerClass, name);
152: cls.put(name, cl);
153: }
154: return cl;
155: }
156:
157: /** Generate unique obfuscated names for this namespace.
158: */
159: public void generateNames() {
160: generateNames(cls);
161: }
162:
163: /** Generate unique obfuscated names for a given namespace.
164: * @param map the namespace
165: */
166: protected void generateNames(Map map) {
167: Vector vec = new Vector();
168: for (Iterator iter = map.values().iterator(); iter.hasNext();) {
169: TreeItem ti = (TreeItem) iter.next();
170: // elements that are already fixed in some way or that should be ignored
171: // must be excluded from the name generation step
172: if (ti.isFixed() || ti.canIgnoreElement()) {
173: vec.addElement(ti.getOutName());
174: }
175: }
176: String[] noObfNames = new String[vec.size()];
177: for (int i = 0; i < noObfNames.length; i++) {
178: noObfNames[i] = (String) vec.elementAt(i);
179: }
180: NameMaker nm = new KeywordNameMaker(noObfNames, false, true);
181: for (Iterator iter = map.values().iterator(); iter.hasNext();) {
182: TreeItem ti = (TreeItem) iter.next();
183: if (!ti.isFixed() && !ti.canIgnoreElement()) {
184: ti.setOutName(nm.nextName(null));
185: }
186: }
187: }
188: }
|