KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* KeywordRenamer 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: KeywordRenamer.java.in,v 1.2.2.1 2002/05/28 17:34:17 hoenicke Exp $
18  */

19
20 package jode.obfuscator.modules;
21 import jode.obfuscator.*;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.lang.UnsupportedOperationException JavaDoc;
25
26 public class KeywordRenamer implements Renamer, OptionHandler {
27     String JavaDoc keywords[];
28     Renamer backup;
29
30     public KeywordRenamer() {
31     keywords = new String JavaDoc[] {
32         "if", "else", "for", "while", "throw", "return",
33         "class", "interface", "implements", "extends",
34         "instanceof", "new",
35         "int", "boolean", "long", "float", "double", "short",
36         "public", "protected", "private",
37         "static", "synchronized", "strict", "transient", "abstract",
38         "volatile", "final",
39         /* Not really keywords, but very confusing anyway. */
40         "Object", "String", "Thread", "Runnable", "StringBuffer", "Vector"
41     };
42     backup = new StrongRenamer();
43     }
44
45     public void setOption(String JavaDoc option, Collection JavaDoc values) {
46     if (option.startsWith("keywords")) {
47         keywords = (String JavaDoc[]) values.toArray(new String JavaDoc[values.size()]);
48     } else if (option.startsWith("backup")) {
49         if (values.size() != 1)
50         throw new IllegalArgumentException JavaDoc
51             ("Only one backup is allowed");
52         backup = (Renamer) values.iterator().next();
53     } else
54         throw new IllegalArgumentException JavaDoc("Invalid option `"+option+"'");
55     }
56
57     public Iterator JavaDoc generateNames(final Identifier ident) {
58     return new Iterator JavaDoc() {
59         int pos = 0;
60         Iterator JavaDoc backing = null;
61
62         public boolean hasNext() {
63         return true;
64         }
65         public Object JavaDoc next() {
66         if (pos < keywords.length)
67             return keywords[pos++];
68         
69         if (backing == null)
70             backing = backup.generateNames(ident);
71
72         return backing.next();
73         }
74
75         public void remove() {
76         throw new UnsupportedOperationException JavaDoc();
77         }
78     };
79     }
80 }
81
Popular Tags