001: /**
002: * YGuard -- an obfuscation library for Java(TM) classfiles.
003: *
004: * Copyright (c) 2002 yWorks GmbH (yguard@yworks.com)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * The author may be contacted at yguard@yworks.com
021: *
022: * Java and all Java-based marks are trademarks or registered
023: * trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
024: */package com.yworks.yguard.obf;
025:
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: /**
030: *
031: * @author muellese
032: */
033: public abstract class NameMakerFactory {
034:
035: /** Holds value of property instance. */
036: private static NameMakerFactory instance = new DefaultNameMakerFactory();
037:
038: /** Creates a new instance of NameMakerFactory */
039: protected NameMakerFactory() {
040: }
041:
042: /** Getter for property instance.
043: * @return Value of property instance.
044: *
045: */
046: public static NameMakerFactory getInstance() {
047: return instance;
048: }
049:
050: /** Setter for property instance.
051: * @param instance New value of property instance.
052: *
053: */
054: protected void setInstance(NameMakerFactory _instance) {
055: instance = _instance;
056: }
057:
058: public abstract NameMaker getPackageNameMaker(
059: String[] reservedNames, String packageName);
060:
061: public abstract NameMaker getClassNameMaker(String[] reservedNames,
062: String packageName);
063:
064: public abstract NameMaker getInnerClassNameMaker(
065: String[] reservedNames, String packageName);
066:
067: public abstract NameMaker getMethodNameMaker(
068: String[] reservedNames, String fqClassName);
069:
070: public abstract NameMaker getFieldNameMaker(String[] reservedNames,
071: String fqClassName);
072:
073: public static class DefaultNameMakerFactory extends
074: NameMakerFactory {
075:
076: private Map classNameMap = new HashMap();
077: private Map fieldNameMap = new HashMap();
078: private Map methodNameMap = new HashMap();
079: private Map innerClassNameMap = new HashMap();
080: private Map packageNameMap = new HashMap();
081:
082: public NameMaker getClassNameMaker(String[] reservedNames,
083: String fqClassName) {
084: NameMaker res = (NameMaker) classNameMap.get(fqClassName);
085: if (res == null) {
086: res = createClassNameMaker(reservedNames, fqClassName);
087: classNameMap.put(fqClassName, res);
088: }
089: return res;
090: }
091:
092: protected NameMaker createClassNameMaker(
093: String[] reservedNames, String fqClassName) {
094: return new KeywordNameMaker(reservedNames);
095: }
096:
097: public NameMaker getFieldNameMaker(String[] reservedNames,
098: String fqClassName) {
099: NameMaker res = (NameMaker) fieldNameMap.get(fqClassName);
100: if (res == null) {
101: res = createFieldNameMaker(reservedNames, fqClassName);
102: fieldNameMap.put(fqClassName, res);
103: }
104: return res;
105: }
106:
107: protected NameMaker createFieldNameMaker(
108: String[] reservedNames, String fqClassName) {
109: return new KeywordNameMaker(reservedNames, false, true);
110: }
111:
112: public NameMaker getInnerClassNameMaker(String[] reservedNames,
113: String fqInnerClassName) {
114: NameMaker res = (NameMaker) innerClassNameMap
115: .get(fqInnerClassName);
116: if (res == null) {
117: res = createInnerClassNameMaker(reservedNames,
118: fqInnerClassName);
119: innerClassNameMap.put(fqInnerClassName, res);
120: }
121: return res;
122: }
123:
124: protected NameMaker createInnerClassNameMaker(
125: final String[] reservedNames, String fqInnerClassName) {
126: final NameMaker inner = new KeywordNameMaker(null);
127: //JBuilder7 incompatability workaround
128: return new NameMaker() {
129: public String nextName(String sig) {
130: while (true) {
131: String name = '_' + inner.nextName(sig);
132: if (reservedNames == null
133: || !Tools
134: .isInArray(name, reservedNames)) {
135: return name;
136: }
137: }
138: }
139: };
140: }
141:
142: public NameMaker getMethodNameMaker(String[] reservedNames,
143: String fqClassName) {
144: NameMaker res = (NameMaker) methodNameMap.get(fqClassName);
145: if (res == null) {
146: res = createMethodNameMaker(reservedNames, fqClassName);
147: methodNameMap.put(fqClassName, res);
148: }
149: return res;
150: }
151:
152: protected NameMaker createMethodNameMaker(
153: String[] reservedNames, String fqClassName) {
154: return new KeywordNameMaker(reservedNames, false, true);
155: }
156:
157: public NameMaker getPackageNameMaker(String[] reservedNames,
158: String packageName) {
159: NameMaker res = (NameMaker) packageNameMap.get(packageName);
160: if (res == null) {
161: res = createPackageNameMaker(reservedNames, packageName);
162: packageNameMap.put(packageName, res);
163: }
164: return res;
165: }
166:
167: protected NameMaker createPackageNameMaker(
168: String[] reservedNames, String packageName) {
169: return new KeywordNameMaker(reservedNames);
170: }
171:
172: public String toString() {
173: return "default";
174: }
175: }
176: }
|