1 26 package net.sf.javaguard; 27 28 29 import java.io.*; 30 import java.util.Hashtable ; 31 import java.util.Vector ; 32 33 34 41 public class KeywordNameMaker extends NameMaker { 42 private static final String DUMMY_ARG_LIST = "dummy"; 44 45 46 private int skipped = 0; private Vector namesToDate = new Vector (); 49 private Hashtable argCount = new Hashtable (); 50 private String [] noObfNames = null; private String [] keywordsToUse; 52 private String [] keywordsToExclude; 53 private String [] firstLetter; 54 private String [] nextLetter; 55 private String [] noKeywords = {}; 56 private String [] someKeywords = { 57 "a", "if", "do", "for", "int", "new", "try", "byte", "case", "char", 58 "else", "goto", "long", "null", "void" 59 }; 60 private String [] allKeywords = { 61 "if", "do", "for", "int", "new", "try", "byte", "case", "char", 62 "else", "goto", "long", "null", "this", "void", "true", "false", 63 "break", "catch", "class", "const", "float", "final", "short", 64 "super", "throw", "while", "double", "import", "native", "public", 65 "return", "static", "switch", "throws", "boolean", "default", 66 "extends", "finally", "package", "private", "abstract", "continue", 67 "volatile", "interface", "protected", "transient", "implements", 68 "instanceof", "synchronized" 69 }; 70 private String [] firstLetterLower = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", 71 "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", 72 "y", "z"}; 73 private String [] nextLetterLower = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", 74 "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", 75 "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; 76 private String [] firstLetterAll = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", 77 "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", 78 "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 79 "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", 80 "W", "X", "Y", "Z"}; 81 private String [] nextLetterAll = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", 82 "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", 83 "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 84 "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", 85 "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; 86 87 88 89 90 91 public KeywordNameMaker() { 92 this(null); 93 } 94 95 96 97 public KeywordNameMaker(String [] noObfNames) { 98 this(noObfNames, true); 99 } 100 101 102 103 public KeywordNameMaker(String [] noObfNames, boolean useKeywords) { 104 this(noObfNames, true, false); 105 } 106 107 108 109 public KeywordNameMaker(String [] noObfNames, boolean useKeywords, boolean lowerCaseOnly) { 110 this.noObfNames = noObfNames == null ? new String [0] : noObfNames; 111 if (useKeywords) { 112 keywordsToUse = someKeywords; 113 keywordsToExclude = someKeywords; 114 } else { 115 keywordsToUse = noKeywords; 116 keywordsToExclude = allKeywords; 117 } 118 if (lowerCaseOnly) { 119 firstLetter = firstLetterLower; 120 nextLetter = nextLetterLower; 121 } else { 122 firstLetter = firstLetterAll; 123 nextLetter = nextLetterAll; 124 } 125 } 126 127 128 129 protected String getNextName(String descriptor) { 130 String argList = DUMMY_ARG_LIST; 132 if (null != descriptor) { 133 argList = getArgList(descriptor); 134 } 135 Integer intCount = (Integer )argCount.get(argList); 136 int theCount = 0; 137 if (null == intCount) { 138 argCount.put(argList, new Integer (theCount)); 139 } else { 140 theCount = intCount.intValue() + 1; 141 argCount.remove(argList); 142 argCount.put(argList, new Integer (theCount)); 143 } 144 return getName(theCount); 145 } 146 147 148 private String getArgList(String descriptor) { 150 int pos = descriptor.indexOf(')'); 151 return descriptor.substring(1, pos); 152 } 153 154 155 private String getName(int index) { 157 String name = null; 159 if (index < namesToDate.size()) { 160 name = (String )namesToDate.elementAt(index); 161 } else { 162 for (;;) { 164 name = getNewName(index + skipped); 165 if (!Tools.isInArray(name, noObfNames) && 166 (index + skipped < keywordsToUse.length || 167 !Tools.isInArray(name, keywordsToExclude))) { 168 break; 169 } 170 skipped++; 171 } 172 namesToDate.addElement(name); 173 } 174 return name; 175 } 176 177 178 private String getNewName(int index) { 180 String name = null; 181 182 if (index < keywordsToUse.length) { 184 name = keywordsToUse[index]; 185 } else { 186 index -= keywordsToUse.length; 188 if (index < firstLetter.length) { 189 name = firstLetter[index]; 190 } else { 191 index -= firstLetter.length; 193 int nextLetters = 1; 194 int subspaceSize = nextLetter.length; 195 while (index >= firstLetter.length * subspaceSize) { 196 index -= firstLetter.length * subspaceSize; 197 nextLetters++; 198 subspaceSize *= nextLetter.length; 199 } 200 201 StringBuffer sb = new StringBuffer (firstLetter[index / subspaceSize]); 203 while (subspaceSize != 1) { 204 index %= subspaceSize; 205 subspaceSize /= nextLetter.length; 206 sb.append(nextLetter[index / subspaceSize]); 207 } 208 209 name = sb.toString(); 211 } 212 } 213 return name; 214 } 215 } 216 | Popular Tags |