1 10 11 package org.mule.util; 12 13 import java.io.IOException ; 14 import java.lang.reflect.Constructor ; 15 import java.lang.reflect.InvocationTargetException ; 16 import java.lang.reflect.Method ; 17 import java.lang.reflect.Modifier ; 18 import java.net.URL ; 19 import java.security.AccessController ; 20 import java.security.PrivilegedAction ; 21 import java.util.ArrayList ; 22 import java.util.Arrays ; 23 import java.util.Collections ; 24 import java.util.Enumeration ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 33 public class ClassUtils extends org.apache.commons.lang.ClassUtils 35 { 36 public static final Object [] NO_ARGS = new Object []{}; 37 38 public static boolean isConcrete(Class clazz) 39 { 40 if (clazz == null) 41 { 42 throw new NullPointerException ("clazz"); 43 } 44 return !(clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())); 45 } 46 47 61 public static URL getResource(final String resourceName, final Class callingClass) 62 { 63 URL url = (URL )AccessController.doPrivileged(new PrivilegedAction () 64 { 65 public Object run() 66 { 67 return Thread.currentThread().getContextClassLoader().getResource(resourceName); 68 } 69 }); 70 71 if (url == null) 72 { 73 url = (URL )AccessController.doPrivileged(new PrivilegedAction () 74 { 75 public Object run() 76 { 77 return ClassUtils.class.getClassLoader().getResource(resourceName); 78 } 79 }); 80 } 81 82 if (url == null) 83 { 84 url = (URL )AccessController.doPrivileged(new PrivilegedAction () 85 { 86 public Object run() 87 { 88 return callingClass.getClassLoader().getResource(resourceName); 89 } 90 }); 91 } 92 93 return url; 94 } 95 96 public static Enumeration getResources(final String resourceName, final Class callingClass) 97 { 98 Enumeration enumeration = (Enumeration )AccessController.doPrivileged(new PrivilegedAction () 99 { 100 public Object run() 101 { 102 try 103 { 104 return Thread.currentThread().getContextClassLoader().getResources(resourceName); 105 } 106 catch (IOException e) 107 { 108 return null; 109 } 110 } 111 }); 112 113 if (enumeration == null) 114 { 115 enumeration = (Enumeration )AccessController.doPrivileged(new PrivilegedAction () 116 { 117 public Object run() 118 { 119 try 120 { 121 return ClassUtils.class.getClassLoader().getResources(resourceName); 122 } 123 catch (IOException e) 124 { 125 return null; 126 } 127 } 128 }); 129 } 130 131 if (enumeration == null) 132 { 133 enumeration = (Enumeration )AccessController.doPrivileged(new PrivilegedAction () 134 { 135 public Object run() 136 { 137 try 138 { 139 return callingClass.getClassLoader().getResources(resourceName); 140 } 141 catch (IOException e) 142 { 143 return null; 144 } 145 } 146 }); 147 } 148 149 return enumeration; 150 } 151 152 168 public static Class loadClass(final String className, final Class callingClass) 169 throws ClassNotFoundException 170 { 171 Class clazz = (Class )AccessController.doPrivileged(new PrivilegedAction () 172 { 173 public Object run() 174 { 175 try 176 { 177 return Thread.currentThread().getContextClassLoader().loadClass(className); 178 } 179 catch (ClassNotFoundException e) 180 { 181 return null; 182 } 183 } 184 }); 185 186 if (clazz == null) 187 { 188 clazz = (Class )AccessController.doPrivileged(new PrivilegedAction () 189 { 190 public Object run() 191 { 192 try 193 { 194 return Class.forName(className); 195 } 196 catch (ClassNotFoundException e) 197 { 198 return null; 199 } 200 } 201 }); 202 } 203 204 if (clazz == null) 205 { 206 clazz = (Class )AccessController.doPrivileged(new PrivilegedAction () 207 { 208 public Object run() 209 { 210 try 211 { 212 return ClassUtils.class.getClassLoader().loadClass(className); 213 } 214 catch (ClassNotFoundException e) 215 { 216 return null; 217 } 218 } 219 }); 220 } 221 222 if (clazz == null) 223 { 224 clazz = (Class )AccessController.doPrivileged(new PrivilegedAction () 225 { 226 public Object run() 227 { 228 try 229 { 230 return callingClass.getClassLoader().loadClass(className); 231 } 232 catch (ClassNotFoundException e) 233 { 234 return null; 235 } 236 } 237 }); 238 } 239 240 if (clazz == null) 241 { 242 throw new ClassNotFoundException (className); 243 } 244 245 return clazz; 246 } 247 248 251 public static void printClassLoader() 252 { 253 System.out.println("ClassLoaderUtils.printClassLoader"); 254 printClassLoader(Thread.currentThread().getContextClassLoader()); 255 } 256 257 261 public static void printClassLoader(ClassLoader cl) 262 { 263 System.out.println("ClassLoaderUtils.printClassLoader(cl = " + cl + ")"); 264 265 if (cl != null) 266 { 267 printClassLoader(cl.getParent()); 268 } 269 } 270 271 public static Object instanciateClass(Class clazz, Object [] constructorArgs) 272 throws SecurityException , NoSuchMethodException , IllegalArgumentException , InstantiationException , 273 IllegalAccessException , InvocationTargetException 274 { 275 Class [] args; 276 if (constructorArgs != null) 277 { 278 args = new Class [constructorArgs.length]; 279 for (int i = 0; i < constructorArgs.length; i++) 280 { 281 if (constructorArgs[i] == null) 282 { 283 args[i] = null; 284 } 285 else 286 { 287 args[i] = constructorArgs[i].getClass(); 288 } 289 } 290 } 291 else 292 { 293 args = new Class [0]; 294 } 295 Constructor ctor = getConstructor(clazz, args); 296 if (ctor == null) 297 { 298 StringBuffer argsString = new StringBuffer (100); 299 for (int i = 0; i < args.length; i++) 300 { 301 argsString.append(args[i].getName()).append(", "); 302 } 303 throw new NoSuchMethodException ("could not find constructor with matching arg params: " 304 + argsString); 305 } 306 307 return ctor.newInstance(constructorArgs); 308 } 309 310 public static Object instanciateClass(String name, Object [] constructorArgs) 311 throws ClassNotFoundException , SecurityException , NoSuchMethodException , IllegalArgumentException , 312 InstantiationException , IllegalAccessException , InvocationTargetException 313 { 314 Class clazz = loadClass(name, ClassUtils.class); 315 return instanciateClass(clazz, constructorArgs); 316 317 } 318 319 public static Object instanciateClass(String name, Object [] constructorArgs, Class callingClass) 320 throws ClassNotFoundException , SecurityException , NoSuchMethodException , IllegalArgumentException , 321 InstantiationException , IllegalAccessException , InvocationTargetException 322 { 323 Class clazz = loadClass(name, callingClass); 324 return instanciateClass(clazz, constructorArgs); 325 326 } 327 328 public static Class [] getParameterTypes(Object bean, String methodName) 329 { 330 if (!methodName.startsWith("set")) 331 { 332 methodName = "set" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1); 333 } 334 Method methods[] = bean.getClass().getMethods(); 335 336 for (int i = 0; i < methods.length; i++) 337 { 338 if (methods[i].getName().equals(methodName)) 339 { 340 return methods[i].getParameterTypes(); 341 } 342 } 343 return new Class []{}; 344 } 345 346 356 public static Method getMethod(String name, Class [] parameterTypes, Class clazz) 357 { 358 Method [] methods = clazz.getMethods(); 359 for (int i = 0; i < methods.length; i++) 360 { 361 if (methods[i].getName().equals(name)) 362 { 363 if (parameterTypes == null) 364 { 365 return methods[i]; 366 } 367 else if (Arrays.equals(methods[i].getParameterTypes(), parameterTypes)) 368 { 369 return methods[i]; 370 } 371 } 372 } 373 return null; 374 } 375 376 public static Constructor getConstructor(Class clazz, Class [] paramTypes) 377 { 378 Constructor [] ctors = clazz.getConstructors(); 379 for (int i = 0; i < ctors.length; i++) 380 { 381 if (ctors[i].getParameterTypes().length == paramTypes.length) 382 { 383 Class [] types = ctors[i].getParameterTypes(); 384 boolean match = true; 385 for (int x = 0; x < types.length; x++) 386 { 387 if (paramTypes[x] == null) 388 { 389 match = true; 390 } 391 else 392 { 393 match = types[x].isAssignableFrom(paramTypes[x]); 394 } 395 } 396 if (match) 397 { 398 return ctors[i]; 399 } 400 } 401 } 402 return null; 403 } 404 405 419 public static List getSatisfiableMethods(Class implementation, 420 Class [] parameterTypes, 421 boolean voidOk, 422 boolean matchOnObject, 423 String [] ignoreMethods) 424 { 425 List result = new ArrayList (); 426 List ignore = (ignoreMethods == null ? Collections.EMPTY_LIST : Arrays.asList(ignoreMethods)); 427 List methods = Arrays.asList(implementation.getMethods()); 428 for (Iterator iterator = methods.iterator(); iterator.hasNext();) 429 { 430 Method method = (Method )iterator.next(); 431 Class [] methodParams = method.getParameterTypes(); 432 433 if (compare(methodParams, parameterTypes, matchOnObject)) 434 { 435 if (!ignore.contains(method.getName())) 436 { 437 if ((method.getReturnType().getName().equals("void") && voidOk) 438 || !method.getReturnType().getName().equals("void")) 439 { 440 result.add(method); 441 } 442 } 443 } 444 } 445 return result; 446 } 447 448 456 public static boolean isClassOnPath(String className, Class currentClass) 457 { 458 try 459 { 460 return (loadClass(className, currentClass) != null); 461 } 462 catch (ClassNotFoundException e) 463 { 464 return false; 465 } 466 } 467 468 474 public static Class [] getClassTypes(Object object) 475 { 476 Class [] types; 477 if (object instanceof Object []) 478 { 479 Object [] objects = (Object [])object; 480 types = new Class [objects.length]; 481 for (int i = 0; i < objects.length; i++) 482 { 483 types[i] = objects[i].getClass(); 484 } 485 } 486 else 487 { 488 types = new Class [1]; 489 types[0] = object.getClass(); 490 } 491 return types; 492 } 493 494 public static String getClassName(Class clazz) 495 { 496 if (clazz == null) 497 { 498 return null; 499 } 500 String name = clazz.getName(); 501 return name.substring(name.lastIndexOf(".") + 1); 502 } 503 504 public static boolean compare(Class [] c1, Class [] c2, boolean matchOnObject) 505 { 506 if (c1.length != c2.length) 507 { 508 return false; 509 } 510 for (int i = 0; i < c1.length; i++) 511 { 512 if (c1[i].equals(Object .class) && !matchOnObject) 513 { 514 return false; 515 } 516 if (!c1[i].isAssignableFrom(c2[i])) 517 { 518 519 return false; 520 } 521 } 522 return true; 523 } 524 } 525 | Popular Tags |