01: /* NameSwapper Copyright (C) 1999-2002 Jochen Hoenicke.
02: *
03: * This program is free software; you can redistribute it and/or modify
04: * it under the terms of the GNU General Public License as published by
05: * the Free Software Foundation; either version 2, or (at your option)
06: * any later version.
07: *
08: * This program is distributed in the hope that it will be useful,
09: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11: * GNU General Public License for more details.
12: *
13: * You should have received a copy of the GNU General Public License
14: * along with this program; see the file COPYING. If not, write to
15: * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16: *
17: * $Id: NameSwapper.java.in,v 1.1.2.1 2002/05/28 17:34:17 hoenicke Exp $
18: */
19:
20: package jode.obfuscator.modules;
21:
22: import jode.obfuscator.*;
23:
24: import java.util.Collection;
25: import java.util.Set;
26: import java.util.HashSet;
27: import java.util.Iterator;
28: import java.util.Random;
29: import java.lang.UnsupportedOperationException;
30:
31: public class NameSwapper implements Renamer {
32: private Random rand;
33: private Set packs, clazzes, methods, fields, locals;
34:
35: public NameSwapper(boolean swapAll, long seed) {
36: if (swapAll) {
37: packs = clazzes = methods = fields = locals = new HashSet();
38: } else {
39: packs = new HashSet();
40: clazzes = new HashSet();
41: methods = new HashSet();
42: fields = new HashSet();
43: locals = new HashSet();
44: }
45: }
46:
47: public NameSwapper(boolean swapAll) {
48: this (swapAll, System.currentTimeMillis());
49: }
50:
51: private class NameGenerator implements Iterator {
52: Collection pool;
53:
54: NameGenerator(Collection c) {
55: pool = c;
56: }
57:
58: public boolean hasNext() {
59: return true;
60: }
61:
62: public Object next() {
63: int pos = rand.nextInt(pool.size());
64: Iterator i = pool.iterator();
65: while (pos > 0)
66: i.next();
67: return (String) i.next();
68: }
69:
70: public void remove() {
71: throw new UnsupportedOperationException();
72: }
73: }
74:
75: public final Collection getCollection(Identifier ident) {
76: if (ident instanceof PackageIdentifier)
77: return packs;
78: else if (ident instanceof ClassIdentifier)
79: return clazzes;
80: else if (ident instanceof MethodIdentifier)
81: return methods;
82: else if (ident instanceof FieldIdentifier)
83: return fields;
84: else if (ident instanceof LocalIdentifier)
85: return locals;
86: else
87: throw new IllegalArgumentException(ident.getClass()
88: .getName());
89: }
90:
91: public final void addIdentifierName(Identifier ident) {
92: getCollection(ident).add(ident.getName());
93: }
94:
95: public Iterator generateNames(Identifier ident) {
96: return new NameGenerator(getCollection(ident));
97: }
98: }
|