KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > obfuscator > modules > NameSwapper


1 /* NameSwapper Copyright (C) 1999-2002 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * 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 import jode.obfuscator.*;
22
23 import java.util.Collection JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Random JavaDoc;
28 import java.lang.UnsupportedOperationException JavaDoc;
29
30
31 public class NameSwapper implements Renamer {
32     private Random JavaDoc rand;
33     private Set JavaDoc packs, clazzes, methods, fields, locals;
34
35     public NameSwapper(boolean swapAll, long seed) {
36     if (swapAll) {
37         packs = clazzes = methods = fields = locals = new HashSet JavaDoc();
38     } else {
39         packs = new HashSet JavaDoc();
40         clazzes = new HashSet JavaDoc();
41         methods = new HashSet JavaDoc();
42         fields = new HashSet JavaDoc();
43         locals = new HashSet JavaDoc();
44     }
45     }
46
47     public NameSwapper(boolean swapAll) {
48     this(swapAll, System.currentTimeMillis());
49     }
50
51     private class NameGenerator implements Iterator JavaDoc {
52     Collection JavaDoc pool;
53     
54     NameGenerator(Collection JavaDoc c) {
55         pool = c;
56     }
57
58     public boolean hasNext() {
59         return true;
60     }
61
62     public Object JavaDoc next() {
63         int pos = rand.nextInt(pool.size());
64         Iterator JavaDoc i = pool.iterator();
65         while (pos > 0)
66         i.next();
67         return (String JavaDoc) i.next();
68     }
69
70     public void remove() {
71         throw new UnsupportedOperationException JavaDoc();
72     }
73     }
74
75     public final Collection JavaDoc 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 JavaDoc(ident.getClass().getName());
88     }
89
90     public final void addIdentifierName(Identifier ident) {
91     getCollection(ident).add(ident.getName());
92     }
93
94     public Iterator JavaDoc generateNames(Identifier ident) {
95     return new NameGenerator(getCollection(ident));
96     }
97 }
98
99
100
Popular Tags