1 49 50 package org.jfree.util; 51 52 import java.io.IOException ; 53 import java.io.InputStream ; 54 import java.lang.reflect.InvocationTargetException ; 55 import java.lang.reflect.Method ; 56 import java.lang.reflect.Modifier ; 57 import java.net.URL ; 58 import java.util.Collection ; 59 import java.util.Iterator ; 60 import java.util.ArrayList ; 61 import java.util.StringTokenizer ; 62 63 69 public final class ObjectUtilities { 70 71 74 public static final String THREAD_CONTEXT = "ThreadContext"; 75 78 public static final String CLASS_CONTEXT = "ClassContext"; 79 80 83 private static String classLoaderSource = THREAD_CONTEXT; 84 87 private static ClassLoader classLoader; 88 89 92 private ObjectUtilities() { 93 } 94 95 101 public static String getClassLoaderSource() { 102 return classLoaderSource; 103 } 104 105 115 public static void setClassLoaderSource(final String classLoaderSource) { 116 ObjectUtilities.classLoaderSource = classLoaderSource; 117 } 118 119 127 public static boolean equal(final Object o1, final Object o2) { 128 if (o1 == o2) { 129 return true; 130 } 131 if (o1 != null) { 132 return o1.equals(o2); 133 } 134 else { 135 return false; 136 } 137 } 138 139 147 public static int hashCode(final Object object) { 148 int result = 0; 149 if (object != null) { 150 result = object.hashCode(); 151 } 152 return result; 153 } 154 155 163 public static Object clone(final Object object) 164 throws CloneNotSupportedException { 165 if (object == null) { 166 throw new IllegalArgumentException ("Null 'object' argument."); 167 } 168 if (object instanceof PublicCloneable) { 169 final PublicCloneable pc = (PublicCloneable) object; 170 return pc.clone(); 171 } 172 else { 173 try { 174 final Method method = object.getClass().getMethod("clone", 175 (Class []) null); 176 if (Modifier.isPublic(method.getModifiers())) { 177 return method.invoke(object, (Object []) null); 178 } 179 } 180 catch (NoSuchMethodException e) { 181 Log.warn("Object without clone() method is impossible."); 182 } 183 catch (IllegalAccessException e) { 184 Log.warn("Object.clone(): unable to call method."); 185 } 186 catch (InvocationTargetException e) { 187 Log.warn("Object without clone() method is impossible."); 188 } 189 } 190 throw new CloneNotSupportedException ("Failed to clone."); 191 } 192 193 203 public static Collection deepClone(final Collection collection) 204 throws CloneNotSupportedException { 205 206 if (collection == null) { 207 throw new IllegalArgumentException ("Null 'collection' argument."); 208 } 209 final Collection result 213 = (Collection ) ObjectUtilities.clone(collection); 214 result.clear(); 215 final Iterator iterator = collection.iterator(); 216 while (iterator.hasNext()) { 217 final Object item = iterator.next(); 218 if (item != null) { 219 result.add(clone(item)); 220 } 221 else { 222 result.add(null); 223 } 224 } 225 return result; 226 } 227 228 233 public synchronized static void setClassLoader( 234 final ClassLoader classLoader) { 235 ObjectUtilities.classLoader = classLoader; 236 } 237 238 243 public static ClassLoader getClassLoader() { 244 return classLoader; 245 } 246 247 257 public synchronized static ClassLoader getClassLoader(final Class c) { 258 if (classLoader != null) { 259 return classLoader; 260 } 261 if ("ThreadContext".equals(classLoaderSource)) { 262 final ClassLoader threadLoader 263 = Thread.currentThread().getContextClassLoader(); 264 if (threadLoader != null) { 265 return threadLoader; 266 } 267 } 268 269 final ClassLoader applicationCL = c.getClassLoader(); 271 if (applicationCL == null) { 272 return ClassLoader.getSystemClassLoader(); 273 } 274 else { 275 return applicationCL; 276 } 277 } 278 279 280 287 public static URL getResource(final String name, final Class c) { 288 final ClassLoader cl = getClassLoader(c); 289 if (cl == null) { 290 return null; 291 } 292 return cl.getResource(name); 293 } 294 295 302 public static URL getResourceRelative(final String name, final Class c) { 303 final ClassLoader cl = getClassLoader(c); 304 final String cname = convertName(name, c); 305 if (cl == null) { 306 return null; 307 } 308 return cl.getResource(cname); 309 } 310 311 321 private static String convertName(final String name, Class c) { 322 if (name.startsWith("/")) { 323 return name.substring(1); 325 } 326 327 while (c.isArray()) { 329 c = c.getComponentType(); 330 } 331 final String baseName = c.getName(); 333 final int index = baseName.lastIndexOf('.'); 334 if (index == -1) { 335 return name; 336 } 337 338 final String pkgName = baseName.substring(0, index); 339 return pkgName.replace('.', '/') + "/" + name; 340 } 341 342 350 public static InputStream getResourceAsStream(final String name, 351 final Class context) { 352 final URL url = getResource(name, context); 353 if (url == null) { 354 return null; 355 } 356 357 try { 358 return url.openStream(); 359 } 360 catch (IOException e) { 361 return null; 362 } 363 } 364 365 373 public static InputStream getResourceRelativeAsStream 374 (final String name, final Class context) { 375 final URL url = getResourceRelative(name, context); 376 if (url == null) { 377 return null; 378 } 379 380 try { 381 return url.openStream(); 382 } 383 catch (IOException e) { 384 return null; 385 } 386 } 387 388 396 public static Object loadAndInstantiate(final String className, 397 final Class source) { 398 try { 399 final ClassLoader loader = getClassLoader(source); 400 final Class c = loader.loadClass(className); 401 return c.newInstance(); 402 } 403 catch (Exception e) { 404 return null; 405 } 406 } 407 408 418 public static Object loadAndInstantiate(final String className, 419 final Class source, 420 final Class type) { 421 try { 422 final ClassLoader loader = getClassLoader(source); 423 final Class c = loader.loadClass(className); 424 if (type.isAssignableFrom(c)) { 425 return c.newInstance(); 426 } 427 } 428 catch (Exception e) { 429 return null; 430 } 431 return null; 432 } 433 434 435 public static boolean isJDK14() { 436 final ClassLoader loader = getClassLoader(ObjectUtilities.class); 437 if (loader != null) { 438 try { 439 loader.loadClass("java.util.RandomAccess"); 440 return true; 441 } 442 catch (ClassNotFoundException e) { 443 return false; 444 } 445 catch(Exception e) { 446 } 448 } 449 try { 452 final String version = System.getProperty 453 ("java.vm.specification.version"); 454 if (version == null) { 456 return false; 457 } 458 459 String [] versions = parseVersions(version); 460 String [] target = new String []{ "1", "4" }; 461 return (ArrayUtilities.compareVersionArrays(versions, target) >= 0); 462 } 463 catch(Exception e) { 464 return false; 465 } 466 } 467 468 private static String [] parseVersions (String version) 469 { 470 if (version == null) 471 { 472 return new String [0]; 473 } 474 475 final ArrayList versions = new ArrayList (); 476 StringTokenizer strtok = new StringTokenizer (version, "."); 477 while (strtok.hasMoreTokens()) 478 { 479 versions.add (strtok.nextToken()); 480 } 481 return (String []) versions.toArray(new String [versions.size()]); 482 } 483 } 484 | Popular Tags |