一、前言
关于jcommon包(1.0.9)定义org.jfree.util.ObjectUtilities对象工具类(依赖org.jfree.util.ArrayUtilities类参考),进行深度克隆deepClone复制、类资源加载初始化及指定名称类型获取资源数据流等。
二、源码说明
package org.jfree.util;@b@@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.lang.reflect.InvocationTargetException;@b@import java.lang.reflect.Method;@b@import java.lang.reflect.Modifier;@b@import java.net.URL;@b@import java.util.ArrayList;@b@import java.util.Collection;@b@import java.util.Iterator;@b@import java.util.StringTokenizer;@b@@b@public final class ObjectUtilities@b@{@b@ public static final String THREAD_CONTEXT = "ThreadContext";@b@ public static final String CLASS_CONTEXT = "ClassContext";@b@ private static String classLoaderSource = "ThreadContext";@b@ private static ClassLoader classLoader;@b@@b@ public static String getClassLoaderSource()@b@ {@b@ return classLoaderSource;@b@ }@b@@b@ public static void setClassLoaderSource(String classLoaderSource)@b@ {@b@ classLoaderSource = classLoaderSource;@b@ }@b@@b@ public static boolean equal(Object o1, Object o2)@b@ {@b@ if (o1 == o2)@b@ return true;@b@@b@ if (o1 != null) {@b@ return o1.equals(o2);@b@ }@b@@b@ return false;@b@ }@b@@b@ public static int hashCode(Object object)@b@ {@b@ int result = 0;@b@ if (object != null)@b@ result = object.hashCode();@b@@b@ return result;@b@ }@b@@b@ public static Object clone(Object object)@b@ throws CloneNotSupportedException@b@ {@b@ if (object == null)@b@ throw new IllegalArgumentException("Null 'object' argument.");@b@@b@ if (object instanceof PublicCloneable) {@b@ PublicCloneable pc = (PublicCloneable)object;@b@ return pc.clone();@b@ }@b@ try@b@ {@b@ Method method = object.getClass().getMethod("clone", (Class[])null);@b@@b@ if (Modifier.isPublic(method.getModifiers()))@b@ return method.invoke(object, (Object[])null);@b@ }@b@ catch (NoSuchMethodException e)@b@ {@b@ Log.warn("Object without clone() method is impossible.");@b@ }@b@ catch (IllegalAccessException e) {@b@ Log.warn("Object.clone(): unable to call method.");@b@ }@b@ catch (InvocationTargetException e) {@b@ Log.warn("Object without clone() method is impossible.");@b@ }@b@@b@ throw new CloneNotSupportedException("Failed to clone.");@b@ }@b@@b@ public static Collection deepClone(Collection collection)@b@ throws CloneNotSupportedException@b@ {@b@ if (collection == null) {@b@ throw new IllegalArgumentException("Null 'collection' argument.");@b@ }@b@@b@ Collection result = (Collection)clone(collection);@b@@b@ result.clear();@b@ Iterator iterator = collection.iterator();@b@ while (iterator.hasNext()) {@b@ Object item = iterator.next();@b@ if (item != null) {@b@ result.add(clone(item));@b@ }@b@ else@b@ result.add(null);@b@ }@b@@b@ return result;@b@ }@b@@b@ public static synchronized void setClassLoader(ClassLoader classLoader)@b@ {@b@ classLoader = classLoader;@b@ }@b@@b@ public static ClassLoader getClassLoader()@b@ {@b@ return classLoader;@b@ }@b@@b@ public static synchronized ClassLoader getClassLoader(Class c)@b@ {@b@ if (classLoader != null)@b@ return classLoader;@b@@b@ if ("ThreadContext".equals(classLoaderSource)) {@b@ ClassLoader threadLoader = Thread.currentThread().getContextClassLoader();@b@@b@ if (threadLoader != null) {@b@ return threadLoader;@b@ }@b@@b@ }@b@@b@ ClassLoader applicationCL = c.getClassLoader();@b@ if (applicationCL == null) {@b@ return ClassLoader.getSystemClassLoader();@b@ }@b@@b@ return applicationCL;@b@ }@b@@b@ public static URL getResource(String name, Class c)@b@ {@b@ ClassLoader cl = getClassLoader(c);@b@ if (cl == null)@b@ return null;@b@@b@ return cl.getResource(name);@b@ }@b@@b@ public static URL getResourceRelative(String name, Class c)@b@ {@b@ ClassLoader cl = getClassLoader(c);@b@ String cname = convertName(name, c);@b@ if (cl == null)@b@ return null;@b@@b@ return cl.getResource(cname);@b@ }@b@@b@ private static String convertName(String name, Class c)@b@ {@b@ if (name.startsWith("/"))@b@ {@b@ return name.substring(1);@b@ }@b@@b@ while (c.isArray()) {@b@ c = c.getComponentType();@b@ }@b@@b@ String baseName = c.getName();@b@ int index = baseName.lastIndexOf(46);@b@ if (index == -1) {@b@ return name;@b@ }@b@@b@ String pkgName = baseName.substring(0, index);@b@ return pkgName.replace('.', '/') + "/" + name;@b@ }@b@@b@ public static InputStream getResourceAsStream(String name, Class context)@b@ {@b@ URL url = getResource(name, context);@b@ if (url == null)@b@ return null;@b@@b@ try@b@ {@b@ return url.openStream();@b@ } catch (IOException e) {@b@ }@b@ return null;@b@ }@b@@b@ public static InputStream getResourceRelativeAsStream(String name, Class context)@b@ {@b@ URL url = getResourceRelative(name, context);@b@ if (url == null)@b@ return null;@b@@b@ try@b@ {@b@ return url.openStream();@b@ } catch (IOException e) {@b@ }@b@ return null;@b@ }@b@@b@ public static Object loadAndInstantiate(String className, Class source)@b@ {@b@ ClassLoader loader;@b@ try@b@ {@b@ loader = getClassLoader(source);@b@ Class c = loader.loadClass(className);@b@ return c.newInstance();@b@ } catch (Exception e) {@b@ }@b@ return null;@b@ }@b@@b@ public static Object loadAndInstantiate(String className, Class source, Class type)@b@ {@b@ ClassLoader loader;@b@ try@b@ {@b@ loader = getClassLoader(source);@b@ Class c = loader.loadClass(className);@b@ if (type.isAssignableFrom(c))@b@ return c.newInstance();@b@ }@b@ catch (Exception e)@b@ {@b@ return null;@b@ }@b@ return null;@b@ }@b@@b@ public static boolean isJDK14()@b@ {@b@ ClassLoader loader = getClassLoader(ObjectUtilities.class);@b@ if (loader != null);@b@ try {@b@ loader.loadClass("java.util.RandomAccess");@b@ return true;@b@ }@b@ catch (ClassNotFoundException e) {@b@ return false;@b@ }@b@ catch (Exception version)@b@ {@b@ try@b@ {@b@ String version = System.getProperty("java.vm.specification.version");@b@@b@ if (version == null) {@b@ return false;@b@ }@b@@b@ String[] versions = parseVersions(version);@b@ String[] target = { "1", "4" };@b@ return (ArrayUtilities.compareVersionArrays(versions, target) >= 0); } catch (Exception version) {@b@ }@b@ }@b@ return false;@b@ }@b@@b@ private static String[] parseVersions(String version)@b@ {@b@ if (version == null)@b@ {@b@ return new String[0];@b@ }@b@@b@ ArrayList versions = new ArrayList();@b@ StringTokenizer strtok = new StringTokenizer(version, ".");@b@ while (strtok.hasMoreTokens())@b@ {@b@ versions.add(strtok.nextToken());@b@ }@b@ return ((String[])(String[])versions.toArray(new String[versions.size()]));@b@ }@b@}