01: /* ===========================================================================
02: * $RCSfile: NameMaker.java,v $
03: * ===========================================================================
04: *
05: * RetroGuard -- an obfuscation package for Java classfiles.
06: *
07: * Copyright (c) 1998-2006 Mark Welsh (markw@retrologic.com)
08: *
09: * This program can be redistributed and/or modified under the terms of the
10: * Version 2 of the GNU General Public License as published by the Free
11: * Software Foundation.
12: *
13: * This program is distributed in the hope that it will be useful,
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: * GNU General Public License for more details.
17: *
18: */
19:
20: package COM.rl.obf;
21:
22: import java.io.*;
23: import java.util.*;
24:
25: /**
26: * Base class for name generators for a given namespace.
27: * The base class gathers statistics for name mappings.
28: *
29: * @author Mark Welsh
30: */
31: abstract public class NameMaker {
32: // Fields ----------------------------------------------------------------
33: protected static Hashtable frequency = new Hashtable(); // Used for logging frequency of generated names
34:
35: // Class Methods ---------------------------------------------------------
36: /** Get an enumeration of distinct obfuscated names for all namespaces. */
37: public static Enumeration getNames() {
38: return frequency.keys();
39: }
40:
41: /** Get an enumeration of use count for distinct obfuscated names for all namespaces. */
42: public static Enumeration getUseCounts() {
43: return frequency.elements();
44: }
45:
46: // Instance Methods ------------------------------------------------------
47: /** Return the next unique name for this namespace, differing only for identical arg-lists. */
48: public String nextName(String descriptor) throws Exception {
49: // Log the name usage globally across all namespaces
50: String name = getNextName(descriptor);
51: Integer intCount = (Integer) frequency.get(name);
52: if (intCount == null) {
53: frequency.put(name, new Integer(0));
54: } else {
55: frequency.remove(name);
56: frequency.put(name, new Integer(intCount.intValue() + 1));
57: }
58: return name;
59: }
60:
61: /** Return the next unique name for this namespace, differing only for identical arg-lists. */
62: abstract protected String getNextName(String descriptor)
63: throws Exception;
64: }
|