| 1 19 20 package jode.obfuscator.modules; 21 import jode.obfuscator.*; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.lang.UnsupportedOperationException ; 25 26 public class StrongRenamer implements Renamer, OptionHandler { 27 static String [] idents = { 28 "Package", "Class", "Field", "Method", "Local" 29 }; 30 static String [] parts = { 31 "Start", "Part" 32 }; 33 String charsets[][]; 34 35 String 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 [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 option, Collection values) { 55 if (option.startsWith("charset")) { 56 Object value = values.iterator().next(); 57 if (values.size() != 1 || !(value instanceof String )) 58 throw new IllegalArgumentException  59 ("Only string parameter are supported."); 60 String set = (String ) value; 61 String 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 ("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 ("Invalid option `" 95 +option+"'"); 96 } 97 98 public Iterator 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 (ident.getClass().getName()); 112 final String [] theCharset = charsets[identType]; 113 114 return new Iterator () { 115 char[] name = null; 116 int headIndex; 117 118 public boolean hasNext() { 119 return true; 120 } 121 public Object next() { 122 if (name == null) { 123 name = new char[] { theCharset[0].charAt(0) }; 124 headIndex = 0; 125 return new String (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 (name); 132 } 133 headIndex = 0; 134 name[0] = theCharset[0].charAt(0); 135 136 String 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 (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 next = new String (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 (); 163 } 164 }; 165 } 166 } 167 168 | Popular Tags |