/** * <p>Gets the short class name for a class.</p> * * <p>The short class name is the classname excluding * the package name.</p> * * @param cls the <code>Class</code> to get the short name of * @return the short name */ protected String getShortClassName(Class cls) { return ClassUtils.getShortClassName(cls); }
/** * <p>Gets a <code>List</code> of all interfaces implemented by the given * class and its superclasses.</p> * * <p>The order is determined by looking through each interface in turn as * declared in the source file and following its hierarchy up. Then each * superclass is considered in the same way. Later duplicates are ignored, * so the order is maintained.</p> * * @param cls the class to look up, may be <code>null</code> * @return the <code>List</code> of interfaces in order, * <code>null</code> if null input */ public static List getAllInterfaces(Class cls) { if (cls == null) { return null; } List interfacesFound = new ArrayList(); getAllInterfaces(cls, interfacesFound); return interfacesFound; }
/** * <p>Converts the specified array of primitive Class objects to an array of * its corresponding wrapper Class objects.</p> * * @param classes the class array to convert, may be null or empty * @return an array which contains for each given class, the wrapper class or * the original class if class is not a primitive. <code>null</code> if null input. * Empty array if an empty array passed in. * @since 2.1 */ public static Class[] primitivesToWrappers(Class[] classes) { if (classes == null) { return null; } if (classes.length == 0) { return classes; } Class[] convertedClasses = new Class[classes.length]; for (int i = 0; i < classes.length; i++) { convertedClasses[i] = primitiveToWrapper(classes[i]); } return convertedClasses; }
/** * <p>Gets the canonical name minus the package name from a String.</p> * * <p>The string passed in is assumed to be a canonical name - it is not checked.</p> * * @param canonicalName the class name to get the short name for * @return the canonical name of the class without the package name or an empty string * @since 2.4 */ public static String getShortCanonicalName(String canonicalName) { return ClassUtils.getShortClassName(getCanonicalName(canonicalName)); }
clazz = Class.forName(clsName, initialize, classLoader).getComponentType(); } else { clazz = Class.forName(toCanonicalName(className), initialize, classLoader); return getClass(classLoader, className.substring(0, lastDotIndex) + INNER_CLASS_SEPARATOR_CHAR + className.substring(lastDotIndex + 1), initialize);
candidateClasses.addAll(getAllInterfaces(cls)); candidateClasses.addAll(getAllSuperclasses(cls));
/** * Returns the (initialized) class represented by <code>className</code> * using the current thread's context class loader. This implementation * supports the syntaxes "<code>java.util.Map.Entry[]</code>", * "<code>java.util.Map$Entry[]</code>", "<code>[Ljava.util.Map.Entry;</code>", * and "<code>[Ljava.util.Map$Entry;</code>". * * @param className the class name * @return the class represented by <code>className</code> using the current thread's context class loader * @throws ClassNotFoundException if the class is not found */ public static Class getClass(String className) throws ClassNotFoundException { return getClass(className, true); }
/** * <p>Checks if one <code>Class</code> can be assigned to a variable of * another <code>Class</code>.</p> * * <p>Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method, * this method takes into account widenings of primitive classes and * <code>null</code>s.</p> * * <p>Primitive widenings allow an int to be assigned to a long, float or * double. This method returns the correct result for these cases.</p> * * <p><code>Null</code> may be assigned to any reference type. This method * will return <code>true</code> if <code>null</code> is passed in and the * toClass is non-primitive.</p> * * <p>Specifically, this method tests whether the type represented by the * specified <code>Class</code> parameter can be converted to the type * represented by this <code>Class</code> object via an identity conversion * widening primitive or widening reference conversion. See * <em><a href="http://java.sun.com/docs/books/jls/">The Java Language Specification</a></em>, * sections 5.1.1, 5.1.2 and 5.1.4 for details.</p> * * @param cls the Class to check, may be null * @param toClass the Class to try to assign into, returns false if null * @return <code>true</code> if assignment possible */ public static boolean isAssignable(Class cls, Class toClass) { return isAssignable(cls, toClass, false); }
/** * <p>Converts the specified array of wrapper Class objects to an array of * its corresponding primitive Class objects.</p> * * <p>This method invokes <code>wrapperToPrimitive()</code> for each element * of the passed in array.</p> * * @param classes the class array to convert, may be null or empty * @return an array which contains for each given class, the primitive class or * <b>null</b> if the original class is not a wrapper class. <code>null</code> if null input. * Empty array if an empty array passed in. * @see #wrapperToPrimitive(Class) * @since 2.4 */ public static Class[] wrappersToPrimitives(Class[] classes) { if (classes == null) { return null; } if (classes.length == 0) { return classes; } Class[] convertedClasses = new Class[classes.length]; for (int i = 0; i < classes.length; i++) { convertedClasses[i] = wrapperToPrimitive(classes[i]); } return convertedClasses; }
cls = primitiveToWrapper(cls); if (cls == null) { return false; cls = wrapperToPrimitive(cls); if (cls == null) { return false;
String offsetStr = repeat(" ", level); if (p == null || c == String.class || c.isPrimitive() || ClassUtils.wrapperToPrimitive(c) != null) { errorStr.append(offsetStr).append(name + ": [" + p + "]\n"); } else if (ClassUtils.isAssignable(c, Iterable.class)) { errorStr.append(offsetStr).append(name + " is an iterable\n"); Iterator<?> i1 = ((Iterable<?>)p).iterator(); dumpObject(errorStr, name + "[" + i + "]", Array.get(p, i), t, level + 1); } else if (ClassUtils.isAssignable(c, Map.class)) { Map<?,?> c1 = (Map<?,?>)p; errorStr.append(offsetStr).append(name + " is a map\n");
/** * Returns the (initialized) class represented by <code>className</code> * using the <code>classLoader</code>. This implementation supports * the syntaxes "<code>java.util.Map.Entry[]</code>", * "<code>java.util.Map$Entry[]</code>", "<code>[Ljava.util.Map.Entry;</code>", * and "<code>[Ljava.util.Map$Entry;</code>". * * @param classLoader the class loader to use to load the class * @param className the class name * @return the class represented by <code>className</code> using the <code>classLoader</code> * @throws ClassNotFoundException if the class is not found */ public static Class getClass(ClassLoader classLoader, String className) throws ClassNotFoundException { return getClass(classLoader, className, true); }
@SuppressWarnings("unchecked") protected static List<Class<?>> findAllParentClasses( final Class<?> sourceClass) { List<Class<?>> superClasses = new ArrayList<Class<?>>(); superClasses.add(sourceClass); superClasses.addAll(ClassUtils.getAllSuperclasses(sourceClass)); superClasses.addAll(ClassUtils.getAllInterfaces(sourceClass)); return superClasses; }
return isAssignable(classArray, toClassArray, false);
/** * Get the number of steps required to promote a primitive number to another * type. * @param srcClass the (primitive) source class * @param destClass the (primitive) destination class * @return The cost of promoting the primitive */ private static float getPrimitivePromotionCost(final Class srcClass, final Class destClass) { float cost = 0.0f; Class cls = srcClass; if (!cls.isPrimitive()) { // slight unwrapping penalty cost += 0.1f; cls = ClassUtils.wrapperToPrimitive(cls); } for (int i = 0; cls != destClass && i < ORDERED_PRIMITIVE_TYPES.length; i++) { if (cls == ORDERED_PRIMITIVE_TYPES[i]) { cost += 0.1f; if (i < ORDERED_PRIMITIVE_TYPES.length - 1) { cls = ORDERED_PRIMITIVE_TYPES[i + 1]; } } } return cost; }
/** * <p>Gets the canonical name minus the package name from a String.</p> * * <p>The string passed in is assumed to be a canonical name - it is not checked.</p> * * @param canonicalName the class name to get the short name for * @return the canonical name of the class without the package name or an empty string * @since 2.4 */ public static String getShortCanonicalName(String canonicalName) { return ClassUtils.getShortClassName(getCanonicalName(canonicalName)); }
/** * <p>Gets the class name minus the package name from a <code>Class</code>.</p> * * @param cls the class to get the short name for. * @return the class name without the package name or an empty string */ public static String getShortClassName(Class cls) { if (cls == null) { return StringUtils.EMPTY; } return getShortClassName(cls.getName()); }
public UpdateQuerier(DataAccessFactory dataAccessFactory, StatementMetaData metaData) { this.dataAccessFactory = dataAccessFactory; // 转换基本类型 Class<?> returnType = metaData.getReturnType(); if (returnType.isPrimitive()) { returnType = ClassUtils.primitiveToWrapper(returnType); } this.returnType = returnType; this.returnGeneratedKeys = metaData.getReturnGeneratedKeys(); }
/** * Returns the class represented by <code>className</code> using the * current thread's context class loader. This implementation supports the * syntaxes "<code>java.util.Map.Entry[]</code>", "<code>java.util.Map$Entry[]</code>", * "<code>[Ljava.util.Map.Entry;</code>", and "<code>[Ljava.util.Map$Entry;</code>". * * @param className the class name * @param initialize whether the class must be initialized * @return the class represented by <code>className</code> using the current thread's context class loader * @throws ClassNotFoundException if the class is not found */ public static Class getClass(String className, boolean initialize) throws ClassNotFoundException { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); ClassLoader loader = contextCL == null ? ClassUtils.class.getClassLoader() : contextCL; return getClass(loader, className, initialize ); }
private static Class<?>[] getAllInterfaces(Class<?> baseClass) { List interfaces = ClassUtils.getAllInterfaces(baseClass); Class<?>[] result = new Class<?>[interfaces.size()]; int i = 0; for (Object o : interfaces) { result[i++] = (Class<?>)o; } return result; }