一、前言
关于cayenne包中org.apache.commons.lang.ClassUtils类处理工具类,通过class名称加载类对象为序列集(convertClassNamesToClasses)、将class对象序列集转为className集(convertClassesToClassNames)、获取指定class对象所有接口(getAllInterfaces)、获取指定class对象父类(getAllSuperclasses)、获取指定class类的路径包名(getPackageName)、获取class的class简称名称(getShortClassName)、判断是否内部类(isInnerClass)。
二、源码说明
package org.apache.commons.lang;@b@@b@import java.util.ArrayList;@b@import java.util.HashMap;@b@import java.util.Iterator;@b@import java.util.List;@b@import java.util.Map;@b@@b@public class ClassUtils@b@{@b@ public static final char PACKAGE_SEPARATOR_CHAR = 46;@b@ public static final String PACKAGE_SEPARATOR = String.valueOf('.');@b@ public static final char INNER_CLASS_SEPARATOR_CHAR = 36;@b@ public static final String INNER_CLASS_SEPARATOR = String.valueOf('$');@b@ private static Map primitiveWrapperMap = new HashMap();@b@@b@ static@b@ {@b@ primitiveWrapperMap.put(Boolean.TYPE, Boolean.class);@b@ primitiveWrapperMap.put(Byte.TYPE, Byte.class);@b@ primitiveWrapperMap.put(Character.TYPE, Character.class);@b@ primitiveWrapperMap.put(Short.TYPE, Short.class);@b@ primitiveWrapperMap.put(Integer.TYPE, Integer.class);@b@ primitiveWrapperMap.put(Long.TYPE, Long.class);@b@ primitiveWrapperMap.put(Double.TYPE, Double.class);@b@ primitiveWrapperMap.put(Float.TYPE, Float.class);@b@ }@b@@b@ public static List convertClassNamesToClasses(List classNames)@b@ {@b@ if (classNames == null)@b@ return null;@b@@b@ List classes = new ArrayList(classNames.size());@b@ for (Iterator it = classNames.iterator(); it.hasNext(); ) {@b@ String className = (String)it.next();@b@ try {@b@ classes.add(Class.forName(className));@b@ } catch (Exception localException) {@b@ classes.add(null);@b@ }@b@ }@b@ return classes;@b@ }@b@@b@ public static List convertClassesToClassNames(List classes)@b@ {@b@ if (classes == null)@b@ return null;@b@@b@ List classNames = new ArrayList(classes.size());@b@ for (Iterator it = classes.iterator(); it.hasNext(); ) {@b@ Class cls = (Class)it.next();@b@ if (cls == null) {@b@ classNames.add(null);@b@@b@ break label66:@b@ }@b@@b@ label66: classNames.add(cls.getName());@b@ }@b@@b@ return classNames;@b@ }@b@@b@ public static List getAllInterfaces(Class cls)@b@ {@b@ if (cls == null)@b@ return null;@b@@b@ List list = new ArrayList();@b@ while (cls != null) {@b@ Class[] interfaces = cls.getInterfaces();@b@ for (int i = 0; i < interfaces.length; ++i) {@b@ if (list.contains(interfaces[i]) == false)@b@ list.add(interfaces[i]);@b@@b@ List superInterfaces = getAllInterfaces(interfaces[i]);@b@ for (Iterator it = superInterfaces.iterator(); it.hasNext(); ) {@b@ Class intface = (Class)it.next();@b@ if (list.contains(intface) != false) break label103;@b@ label103: list.add(intface);@b@ }@b@ }@b@@b@ cls = cls.getSuperclass();@b@ }@b@ return list;@b@ }@b@@b@ public static List getAllSuperclasses(Class cls)@b@ {@b@ if (cls == null)@b@ return null;@b@@b@ List classes = new ArrayList();@b@ Class superclass = cls.getSuperclass();@b@ while (superclass != null) {@b@ classes.add(superclass);@b@ superclass = superclass.getSuperclass();@b@ }@b@ return classes;@b@ }@b@@b@ public static String getPackageName(Class cls)@b@ {@b@ if (cls == null)@b@ return "";@b@@b@ return getPackageName(cls.getName());@b@ }@b@@b@ public static String getPackageName(Object object, String valueIfNull)@b@ {@b@ if (object == null)@b@ return valueIfNull;@b@@b@ return getPackageName(object.getClass().getName());@b@ }@b@@b@ public static String getPackageName(String className)@b@ {@b@ if (className == null)@b@ return "";@b@@b@ int i = className.lastIndexOf(46);@b@ if (i == -1)@b@ return "";@b@@b@ return className.substring(0, i);@b@ }@b@@b@ public static String getShortClassName(Class cls)@b@ {@b@ if (cls == null)@b@ return "";@b@@b@ return getShortClassName(cls.getName());@b@ }@b@@b@ public static String getShortClassName(Object object, String valueIfNull)@b@ {@b@ if (object == null)@b@ return valueIfNull;@b@@b@ return getShortClassName(object.getClass().getName());@b@ }@b@@b@ public static String getShortClassName(String className)@b@ {@b@ if (className == null)@b@ return "";@b@@b@ if (className.length() == 0)@b@ return "";@b@@b@ char[] chars = className.toCharArray();@b@ int lastDot = 0;@b@ for (int i = 0; i < chars.length; ++i)@b@ if (chars[i] == '.')@b@ lastDot = i + 1;@b@ else if (chars[i] == '$')@b@ chars[i] = '.';@b@@b@@b@ return new String(chars, lastDot, chars.length - lastDot);@b@ }@b@@b@ public static boolean isAssignable(Class cls, Class toClass)@b@ {@b@ if (toClass == null) {@b@ return false;@b@ }@b@@b@ if (cls == null)@b@ return (toClass.isPrimitive() ^ true);@b@@b@ if (cls.equals(toClass))@b@ return true;@b@@b@ if (cls.isPrimitive()) {@b@ if (toClass.isPrimitive() == false)@b@ return false;@b@@b@ if (Integer.TYPE.equals(cls))@b@ return ((Long.TYPE.equals(toClass)) || @b@ (Float.TYPE.equals(toClass)) || @b@ (Double.TYPE.equals(toClass)));@b@@b@ if (Long.TYPE.equals(cls))@b@ return ((Float.TYPE.equals(toClass)) || @b@ (Double.TYPE.equals(toClass)));@b@@b@ if (Boolean.TYPE.equals(cls))@b@ return false;@b@@b@ if (Double.TYPE.equals(cls))@b@ return false;@b@@b@ if (Float.TYPE.equals(cls))@b@ return Double.TYPE.equals(toClass);@b@@b@ if (Character.TYPE.equals(cls))@b@ return ((Integer.TYPE.equals(toClass)) || @b@ (Long.TYPE.equals(toClass)) || @b@ (Float.TYPE.equals(toClass)) || @b@ (Double.TYPE.equals(toClass)));@b@@b@ if (Short.TYPE.equals(cls))@b@ return ((Integer.TYPE.equals(toClass)) || @b@ (Long.TYPE.equals(toClass)) || @b@ (Float.TYPE.equals(toClass)) || @b@ (Double.TYPE.equals(toClass)));@b@@b@ if (Byte.TYPE.equals(cls)) {@b@ return ((Short.TYPE.equals(toClass)) || @b@ (Integer.TYPE.equals(toClass)) || @b@ (Long.TYPE.equals(toClass)) || @b@ (Float.TYPE.equals(toClass)) || @b@ (Double.TYPE.equals(toClass)));@b@ }@b@@b@ return false;@b@ }@b@ return toClass.isAssignableFrom(cls);@b@ }@b@@b@ public static boolean isAssignable(Class[] classArray, Class[] toClassArray)@b@ {@b@ if (ArrayUtils.isSameLength(classArray, toClassArray) == false)@b@ return false;@b@@b@ if (classArray == null)@b@ classArray = ArrayUtils.EMPTY_CLASS_ARRAY;@b@@b@ if (toClassArray == null)@b@ toClassArray = ArrayUtils.EMPTY_CLASS_ARRAY;@b@@b@ for (int i = 0; i < classArray.length; ++i)@b@ if (isAssignable(classArray[i], toClassArray[i]) == false)@b@ return false;@b@@b@@b@ return true;@b@ }@b@@b@ public static boolean isInnerClass(Class cls)@b@ {@b@ if (cls == null)@b@ return false;@b@@b@ return (cls.getName().indexOf(36) >= 0);@b@ }@b@@b@ public static Class primitiveToWrapper(Class cls)@b@ {@b@ Class convertedClass = cls;@b@ if ((cls != null) && (cls.isPrimitive()))@b@ convertedClass = (Class)primitiveWrapperMap.get(cls);@b@@b@ return convertedClass;@b@ }@b@@b@ public static Class[] primitivesToWrappers(Class[] classes)@b@ {@b@ if (classes == null) {@b@ return null;@b@ }@b@@b@ if (classes.length == 0) {@b@ return classes;@b@ }@b@@b@ Class[] convertedClasses = new Class[classes.length];@b@ for (int i = 0; i < classes.length; ++i)@b@ convertedClasses[i] = primitiveToWrapper(classes[i]);@b@@b@ return convertedClasses;@b@ }@b@}