1 8 9 package mx4j.util; 10 11 import java.lang.reflect.Array ; 12 import java.lang.reflect.Method ; 13 14 19 public class Utils 20 { 21 27 public static Class loadClass(ClassLoader loader, String name) throws ClassNotFoundException 28 { 29 if (name == null) throw new ClassNotFoundException ("null"); 30 31 name = name.trim(); 32 if (name.equals("boolean")) 33 return boolean.class; 34 else if (name.equals("byte")) 35 return byte.class; 36 else if (name.equals("char")) 37 return char.class; 38 else if (name.equals("short")) 39 return short.class; 40 else if (name.equals("int")) 41 return int.class; 42 else if (name.equals("long")) 43 return long.class; 44 else if (name.equals("float")) 45 return float.class; 46 else if (name.equals("double")) 47 return double.class; 48 else if (name.equals("java.lang.String")) 49 return String .class; 50 else if (name.equals("java.lang.Object")) 51 return Object .class; 52 else if (name.startsWith("[")) 53 { 54 int dimension = 0; 56 while (name.charAt(dimension) == '[') 57 { 58 ++dimension; 59 } 60 char type = name.charAt(dimension); 61 Class cls = null; 62 switch (type) 63 { 64 case 'Z': 65 cls = boolean.class; 66 break; 67 case 'B': 68 cls = byte.class; 69 break; 70 case 'C': 71 cls = char.class; 72 break; 73 case 'S': 74 cls = short.class; 75 break; 76 case 'I': 77 cls = int.class; 78 break; 79 case 'J': 80 cls = long.class; 81 break; 82 case 'F': 83 cls = float.class; 84 break; 85 case 'D': 86 cls = double.class; 87 break; 88 case 'L': 89 String n = name.substring(dimension + 1, name.length() - 1); 91 cls = loadClass(loader, n); 92 break; 93 } 94 95 if (cls == null) 96 { 97 throw new ClassNotFoundException (name); 98 } 99 else 100 { 101 int[] dim = new int[dimension]; 102 return Array.newInstance(cls, dim).getClass(); 103 } 104 } 105 else 106 { 107 if (loader != null) 108 return loader.loadClass(name); 109 else 110 return Class.forName(name, false, null); 111 } 112 } 113 114 118 public static Class [] loadClasses(ClassLoader loader, String [] names) throws ClassNotFoundException 119 { 120 int n = names.length; 121 Class [] cls = new Class [n]; 122 for (int i = 0; i < n; ++i) 123 { 124 String name = names[i]; 125 cls[i] = loadClass(loader, name); 126 } 127 return cls; 128 } 129 130 133 public static boolean isAttributeGetter(Method m) 134 { 135 if (m == null) return false; 136 137 String name = m.getName(); 138 Class retType = m.getReturnType(); 139 Class [] params = m.getParameterTypes(); 140 if (retType != Void.TYPE && params.length == 0) 141 { 142 if (name.startsWith("get") && name.length() > 3) 143 return true; 144 else if (name.startsWith("is") && name.length() > 2 && retType == Boolean.TYPE) return true; 145 } 146 return false; 147 } 148 149 152 public static boolean isAttributeSetter(Method m) 153 { 154 if (m == null) return false; 155 156 String name = m.getName(); 157 Class retType = m.getReturnType(); 158 Class [] params = m.getParameterTypes(); 159 if (retType == Void.TYPE && params.length == 1 && name.startsWith("set") && name.length() > 3) 160 { 161 return true; 162 } 163 return false; 164 } 165 166 public static boolean wildcardMatch(String pattern, String string) 167 { 168 int stringLength = string.length(); 169 int stringIndex = 0; 170 for (int patternIndex = 0; patternIndex < pattern.length(); ++patternIndex) 171 { 172 char c = pattern.charAt(patternIndex); 173 if (c == '*') 174 { 175 while (stringIndex < stringLength) 178 { 179 if (wildcardMatch(pattern.substring(patternIndex + 1), string.substring(stringIndex))) 180 { 181 return true; 182 } 183 ++stringIndex; 185 } 186 } 187 else if (c == '?') 188 { 189 ++stringIndex; 191 if (stringIndex > stringLength) 192 { 193 return false; 194 } 195 } 196 else 197 { 198 if (stringIndex >= stringLength || c != string.charAt(stringIndex)) 200 { 201 return false; 202 } 203 ++stringIndex; 204 } 205 } 206 207 return stringIndex == stringLength; 209 } 210 211 public static boolean arrayEquals(Object [] arr1, Object [] arr2) 212 { 213 if (arr1 == null && arr2 == null) return true; 214 if (arr1 == null ^ arr2 == null) return false; 215 if (!arr1.getClass().equals(arr2.getClass())) return false; 216 if (arr1.length != arr2.length) return false; 217 218 for (int i = 0; i < arr1.length; ++i) 219 { 220 Object obj1 = arr1[i]; 221 Object obj2 = arr2[i]; 222 if (obj1 == null ^ obj2 == null) return false; 223 if (obj1 != null && !obj1.equals(obj2)) return false; 224 } 225 return true; 226 } 227 228 public static boolean arrayEquals(byte[] arr1, byte[] arr2) 229 { 230 if (arr1 == null && arr2 == null) return true; 231 if (arr1 == null ^ arr2 == null) return false; 232 if (!arr1.getClass().equals(arr2.getClass())) return false; 233 if (arr1.length != arr2.length) return false; 234 235 for (int i = 0; i < arr1.length; ++i) 236 { 237 byte b1 = arr1[i]; 238 byte b2 = arr2[i]; 239 if (b1 != b2) return false; 240 } 241 return true; 242 } 243 244 public static int arrayHashCode(Object [] arr) 245 { 246 int hash = 0; 247 if (arr != null) 248 { 249 hash ^= arr.getClass().hashCode(); 251 for (int i = 0; i < arr.length; ++i) 252 { 253 hash ^= arr[i] == null ? 0 : arr[i].hashCode(); 254 } 255 } 256 return hash; 257 } 258 259 public static int arrayHashCode(byte[] arr) 260 { 261 int hash = 0; 262 if (arr != null) 263 { 264 hash ^= arr.getClass().hashCode(); 266 for (int i = 0; i < arr.length; ++i) 267 { 268 hash ^= arr[i]; 269 } 270 } 271 return hash; 272 } 273 274 public static char[] arrayCopy(char[] chars) 275 { 276 if (chars == null) return null; 277 char[] copy = new char[chars.length]; 278 System.arraycopy(chars, 0, copy, 0, chars.length); 279 return copy; 280 } 281 } 282 | Popular Tags |