KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* StrongRenamer 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: StrongRenamer.java.in,v 1.3.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 StrongRenamer implements Renamer, OptionHandler {
27     static String JavaDoc[] idents = {
28     "Package", "Class", "Field", "Method", "Local"
29     };
30     static String JavaDoc[] parts = {
31     "Start", "Part"
32     };
33     String JavaDoc charsets[][];
34
35     String JavaDoc javaKeywords[] = {
36     "abstract", "default", "if", "private", "throw", "boolean",
37     "do", "implements", "protected", "throws", "break", "double",
38     "import", "public", "transient", "byte", "else", "instanceof",
39     "return", "try", "case", "extends", "int", "short", "void",
40     "catch", "final", "interface", "static", "volatile", "char",
41     "finally", "long", "super", "while", "class", "float",
42     "native", "switch", "const", "for", "new", "synchronized",
43     "continue", "goto", "package", "this", "strictfp", "null",
44     "true", "false"
45     };
46
47     public StrongRenamer() {
48     charsets = new String JavaDoc[idents.length][parts.length];
49     for (int i=0; i< idents.length; i++)
50         for (int j=0; j< parts.length; j++)
51         charsets[i][j] = "abcdefghijklmnopqrstuvwxyz";
52     }
53
54     public void setOption(String JavaDoc option, Collection JavaDoc values) {
55     if (option.startsWith("charset")) {
56         Object JavaDoc value = values.iterator().next();
57         if (values.size() != 1 || !(value instanceof String JavaDoc))
58         throw new IllegalArgumentException JavaDoc
59             ("Only string parameter are supported.");
60         String JavaDoc set = (String JavaDoc) value;
61         String JavaDoc remOpt = option.substring("charset".length());
62         int part = -1, ident = -1;
63         if (remOpt.length() > 0) {
64         for (int i=0; i < idents.length; i++) {
65             if (remOpt.startsWith(idents[i])) {
66             remOpt = remOpt.substring(idents[i].length());
67             ident = i;
68             break;
69             }
70         }
71         }
72         if (remOpt.length() > 0) {
73         for (int j=0; j < parts.length; j++) {
74             if (remOpt.startsWith(parts[j])) {
75             remOpt = remOpt.substring(parts[j].length());
76             part = j;
77             break;
78             }
79         }
80         }
81         if (remOpt.length() > 0)
82         throw new IllegalArgumentException JavaDoc("Invalid charset `"
83                            +option+"'");
84         for (int i = 0; i < idents.length; i++) {
85         if (ident >= 0 && ident != i)
86             continue;
87         for (int j = 0; j < parts.length; j++) {
88             if (part >= 0 && part != j)
89             continue;
90             charsets[i][j] = set;
91         }
92         }
93     } else
94         throw new IllegalArgumentException JavaDoc("Invalid option `"
95                            +option+"'");
96     }
97
98     public Iterator JavaDoc generateNames(Identifier ident) {
99     int identType;
100     if (ident instanceof PackageIdentifier)
101         identType = 0;
102     else if (ident instanceof ClassIdentifier)
103         identType = 1;
104     else if (ident instanceof FieldIdentifier)
105         identType = 2;
106     else if (ident instanceof MethodIdentifier)
107         identType = 3;
108     else if (ident instanceof LocalIdentifier)
109         identType = 4;
110     else
111         throw new IllegalArgumentException JavaDoc(ident.getClass().getName());
112     final String JavaDoc[] theCharset = charsets[identType];
113
114     return new Iterator JavaDoc() {
115         char[] name = null;
116         int headIndex;
117
118         public boolean hasNext() {
119         return true;
120         }
121         public Object JavaDoc next() {
122         if (name == null) {
123             name = new char[] { theCharset[0].charAt(0) };
124             headIndex = 0;
125             return new String JavaDoc(name);
126         }
127         next_name:
128         while (true) {
129             if (++headIndex < theCharset[0].length()) {
130             name[0] = theCharset[0].charAt(headIndex);
131             return new String JavaDoc(name);
132             }
133             headIndex = 0;
134             name[0] = theCharset[0].charAt(0);
135             
136             String JavaDoc charset = theCharset[1];
137             for (int pos = 1; pos < name.length; pos++) {
138             int index = charset.indexOf(name[pos]) + 1;
139             if (index < charset.length()) {
140                 name[pos] = charset.charAt(index);
141                 return new String JavaDoc(name);
142             }
143             name[pos] = charset.charAt(0);
144             }
145         
146             name = new char[name.length+1];
147             name[0] = theCharset[0].charAt(0);
148             char firstCont = theCharset[1].charAt(0);
149             for (int i=1; i <name.length; i++)
150             name[i] = firstCont;
151             
152             String JavaDoc next = new String JavaDoc(name);
153             for (int i = 0; i < javaKeywords.length; i++) {
154             if (next.equals(javaKeywords[i]))
155                 continue next_name;
156             }
157             return next;
158         }
159         }
160
161         public void remove() {
162         throw new UnsupportedOperationException JavaDoc();
163         }
164     };
165     }
166 }
167
168
Popular Tags