1 26 package net.sf.javaguard; 27 28 29 import java.util.Vector ; 30 31 32 37 public class Tools { 38 43 public static boolean isInArray(String s, String [] list) { 44 for (int i = 0; i < list.length; i++) { 45 if (s.equals(list[i])) return true; 46 } 47 return false; 48 } 49 50 51 57 public static boolean isInArrayIgnoreCase(String s, String [] list) { 58 for (int i = 0; i < list.length; i++) { 59 if (s.equalsIgnoreCase(list[i])) return true; 60 } 61 return false; 62 } 63 64 65 66 67 75 public static String [] parseDescriptor(String descriptor) 76 throws IllegalStateException { 77 return parseDescriptor(descriptor, false); 78 } 79 80 81 92 public static String [] parseDescriptor(String descriptor, boolean isDisplay) 93 throws IllegalStateException { 94 String [] names = null; 96 if (descriptor.charAt(0) != '(') { 97 names = new String [1]; 98 names[0] = descriptor; 99 } else { 100 Vector namesVec = new Vector (); 102 descriptor = descriptor.substring(1); 103 String type = ""; 104 while (descriptor.length() > 0) { 105 switch (descriptor.charAt(0)) { 106 case '[': 107 type = type + "["; 108 descriptor = descriptor.substring(1); 109 break; 110 111 case 'B': 112 case 'C': 113 case 'D': 114 case 'F': 115 case 'I': 116 case 'J': 117 case 'S': 118 case 'Z': 119 case 'V': 120 namesVec.addElement(type + descriptor.substring(0, 1)); 121 descriptor = descriptor.substring(1); 122 type = ""; 123 break; 124 125 case ')': 126 descriptor = descriptor.substring(1); 127 break; 128 129 case 'L': { 130 int pos = descriptor.indexOf(';') + 1; 131 namesVec.addElement(type + descriptor.substring(0, pos)); 132 descriptor = descriptor.substring(pos); 133 type = ""; 134 } 135 break; 136 137 default: 138 throw new IllegalStateException ("Illegal field or method descriptor: " + descriptor); 139 } 140 } 141 names = new String [namesVec.size()]; 142 for (int i = 0; i < names.length; i++) { 143 names[i] = (String ) namesVec.elementAt(i); 144 } 145 } 146 147 String [] translatedNames = new String [names.length]; 149 for (int i = 0; i < names.length; i++) { 150 translatedNames[i] = translateType(names[i], isDisplay); 151 } 152 return translatedNames; 153 } 154 155 156 157 158 167 public static String translateType(String inName, boolean isDisplay) 168 throws IllegalStateException { 169 String outName = null; 170 switch (inName.charAt(0)) { 171 case '[': 172 if (!isDisplay) { 175 outName = translate(inName); 177 } else { 178 outName = translateType(inName.substring(1), true) + "[]"; 180 } 181 break; 182 183 case 'B': 184 outName = Byte.TYPE.getName(); 185 break; 186 187 case 'C': 188 outName = Character.TYPE.getName(); 189 break; 190 191 case 'D': 192 outName = Double.TYPE.getName(); 193 break; 194 195 case 'F': 196 outName = Float.TYPE.getName(); 197 break; 198 199 case 'I': 200 outName = Integer.TYPE.getName(); 201 break; 202 203 case 'J': 204 outName = Long.TYPE.getName(); 205 break; 206 207 case 'S': 208 outName = Short.TYPE.getName(); 209 break; 210 211 case 'Z': 212 outName = Boolean.TYPE.getName(); 213 break; 214 215 case 'V': 216 outName = Void.TYPE.getName(); 217 break; 218 219 case 'L': { 220 int pos = inName.indexOf(';'); 221 outName = translate(inName.substring(1, inName.indexOf(';'))); 222 } 223 break; 224 225 default: 226 throw new IllegalStateException ("Illegal type: " + inName); 227 } 228 return outName; 229 } 230 231 232 233 234 239 public static String translate(String name) { 240 return name.replace('/', '.'); 241 } 242 } 243 | Popular Tags |