All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.apache.commons.lang.ClassUtils Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in com.fitburpliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.com.fitbur/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fitbur.apache.com.fitburmons.lang;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.fitbur.apache.com.fitburmons.lang.text.StrBuilder;

/**
 * 

Operates on classes without using reflection.

* *

This class handles invalid null inputs as best it can. * Each method documents its behaviour in more com.fitburtail.

* *

The notion of a canonical name includes the human * readable name for the type, for example int[]. The * non-canonical method variants work with the JVM names, such as * [I.

* * @author Apache Software Foundation * @author Gary Gregory * @author Norm Deane * @author Alban Peignier * @author Tomasz Blachowicz * @since 2.0 * @version $Id: ClassUtils.java 1057072 2011-01-10 01:55:57Z niallp $ */ public class ClassUtils { /** *

The package separator character: '.' == {@value}.

*/ public static final char PACKAGE_SEPARATOR_CHAR = '.'; /** *

The package separator String: ".".

*/ public static final String PACKAGE_SEPARATOR = String.valueOf(PACKAGE_SEPARATOR_CHAR); /** *

The inner class separator character: '$' == {@value}.

*/ public static final char INNER_CLASS_SEPARATOR_CHAR = '$'; /** *

The inner class separator String: "$".

*/ public static final String INNER_CLASS_SEPARATOR = String.valueOf(INNER_CLASS_SEPARATOR_CHAR); /** * Maps primitive Classes to their corresponding wrapper Class. */ private static final Map primitiveWrapperMap = new HashMap(); static { primitiveWrapperMap.put(Boolean.TYPE, Boolean.class); primitiveWrapperMap.put(Byte.TYPE, Byte.class); primitiveWrapperMap.put(Character.TYPE, Character.class); primitiveWrapperMap.put(Short.TYPE, Short.class); primitiveWrapperMap.put(Integer.TYPE, Integer.class); primitiveWrapperMap.put(Long.TYPE, Long.class); primitiveWrapperMap.put(Double.TYPE, Double.class); primitiveWrapperMap.put(Float.TYPE, Float.class); primitiveWrapperMap.put(Void.TYPE, Void.TYPE); } /** * Maps wrapper Classes to their corresponding primitive types. */ private static final Map wrapperPrimitiveMap = new HashMap(); static { for (Iterator it = primitiveWrapperMap.keySet().iterator(); it.hasNext();) { Class primitiveClass = (Class) it.next(); Class wrapperClass = (Class) primitiveWrapperMap.get(primitiveClass); if (!primitiveClass.equals(wrapperClass)) { wrapperPrimitiveMap.put(wrapperClass, primitiveClass); } } } /** * Maps a primitive class name to its corresponding abbreviation used in array class names. */ private static final Map abbreviationMap = new HashMap(); /** * Maps an abbreviation used in array class names to corresponding primitive class name. */ private static final Map reverseAbbreviationMap = new HashMap(); /** * Add primitive type abbreviation to maps of abbreviations. * * @param primitive Canonical name of primitive type * @param abbreviation Corresponding abbreviation of primitive type */ private static void addAbbreviation(String primitive, String abbreviation) { abbreviationMap.put(primitive, abbreviation); reverseAbbreviationMap.put(abbreviation, primitive); } /** * Feed abbreviation maps */ static { addAbbreviation("int", "I"); addAbbreviation("boolean", "Z"); addAbbreviation("float", "F"); addAbbreviation("long", "J"); addAbbreviation("short", "S"); addAbbreviation("byte", "B"); addAbbreviation("double", "D"); addAbbreviation("char", "C"); } /** *

ClassUtils instances should NOT be constructed in standard programming. * Instead, the class should be used as * ClassUtils.getShortClassName(cls).

* *

This constructor is public to permit tools that require a JavaBean * instance to operate.

*/ public ClassUtils() { super(); } // Short class name // ---------------------------------------------------------------------- /** *

Gets the class name minus the package name for an Object.

* * @param object the class to get the short name for, may be null * @param valueIfNull the value to return if null * @return the class name of the object without the package name, or the null value */ public static String getShortClassName(Object object, String valueIfNull) { if (object == null) { return valueIfNull; } return getShortClassName(object.getClass()); } /** *

Gets the class name minus the package name from a Class.

* * @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()); } /** *

Gets the class name minus the package name from a String.

* *

The string passed in is assumed to be a class name - it is not checked.

* * @param className the className to get the short name for * @return the class name of the class without the package name or an empty string */ public static String getShortClassName(String className) { if (className == null) { return StringUtils.EMPTY; } if (className.length() == 0) { return StringUtils.EMPTY; } StrBuilder arrayPrefix = new StrBuilder(); // Handle array encoding if (className.startsWith("[")) { while (className.charAt(0) == '[') { className = className.substring(1); arrayPrefix.append("[]"); } // Strip Object type encoding if (className.charAt(0) == 'L' && className.charAt(className.length() - 1) == ';') { className = className.substring(1, className.length() - 1); } } if (reverseAbbreviationMap.containsKey(className)) { className = (String)reverseAbbreviationMap.get(className); } int lastDotIdx = className.lastIndexOf(PACKAGE_SEPARATOR_CHAR); int innerIdx = className.indexOf( INNER_CLASS_SEPARATOR_CHAR, lastDotIdx == -1 ? 0 : lastDotIdx + 1); String out = className.substring(lastDotIdx + 1); if (innerIdx != -1) { out = out.replace(INNER_CLASS_SEPARATOR_CHAR, PACKAGE_SEPARATOR_CHAR); } return out + arrayPrefix; } // Package name // ---------------------------------------------------------------------- /** *

Gets the package name of an Object.

* * @param object the class to get the package name for, may be null * @param valueIfNull the value to return if null * @return the package name of the object, or the null value */ public static String getPackageName(Object object, String valueIfNull) { if (object == null) { return valueIfNull; } return getPackageName(object.getClass()); } /** *

Gets the package name of a Class.

* * @param cls the class to get the package name for, may be null. * @return the package name or an empty string */ public static String getPackageName(Class cls) { if (cls == null) { return StringUtils.EMPTY; } return getPackageName(cls.getName()); } /** *

Gets the package name from a String.

* *

The string passed in is assumed to be a class name - it is not checked.

*

If the class is unpackaged, return an empty string.

* * @param className the className to get the package name for, may be null * @return the package name or an empty string */ public static String getPackageName(String className) { if (className == null || className.length() == 0) { return StringUtils.EMPTY; } // Strip array encoding while (className.charAt(0) == '[') { className = className.substring(1); } // Strip Object type encoding if (className.charAt(0) == 'L' && className.charAt(className.length() - 1) == ';') { className = className.substring(1); } int i = className.lastIndexOf(PACKAGE_SEPARATOR_CHAR); if (i == -1) { return StringUtils.EMPTY; } return className.substring(0, i); } // Superclasses/Superinterfaces // ---------------------------------------------------------------------- /** *

Gets a List of superclasses for the given class.

* * @param cls the class to look up, may be null * @return the List of superclasses in order going up from this one * null if null input */ public static List getAllSuperclasses(Class cls) { if (cls == null) { return null; } List classes = new ArrayList(); Class superclass = cls.getSuperclass(); while (superclass != null) { classes.add(superclass); superclass = superclass.getSuperclass(); } return classes; } /** *

Gets a List of all interfaces implemented by the given * class and its superclasses.

* *

The order is com.fitburtermined by looking through each interface in turn as * com.fitburclared 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.

* * @param cls the class to look up, may be null * @return the List of interfaces in order, * null if null input */ public static List getAllInterfaces(Class cls) { if (cls == null) { return null; } List interfacesFound = new ArrayList(); getAllInterfaces(cls, interfacesFound); return interfacesFound; } /** * Get the interfaces for the specified class. * * @param cls the class to look up, may be null * @param interfacesFound the Set of interfaces for the class */ private static void getAllInterfaces(Class cls, List interfacesFound) { while (cls != null) { Class[] interfaces = cls.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { if (!interfacesFound.contains(interfaces[i])) { interfacesFound.add(interfaces[i]); getAllInterfaces(interfaces[i], interfacesFound); } } cls = cls.getSuperclass(); } } // Convert list // ---------------------------------------------------------------------- /** *

Given a List of class names, this method converts them into classes.

* *

A new List is returned. If the class name cannot be found, null * is stored in the List. If the class name in the List is * null, null is stored in the output List.

* * @param classNames the classNames to change * @return a List of Class objects corresponding to the class names, * null if null input * @throws ClassCastException if classNames contains a non String entry */ public static List convertClassNamesToClasses(List classNames) { if (classNames == null) { return null; } List classes = new ArrayList(classNames.size()); for (Iterator it = classNames.iterator(); it.hasNext();) { String className = (String) it.next(); try { classes.add(Class.forName(className)); } catch (Exception ex) { classes.add(null); } } return classes; } /** *

Given a List of Class objects, this method converts * them into class names.

* *

A new List is returned. null objects will be copied into * the returned list as null.

* * @param classes the classes to change * @return a List of class names corresponding to the Class objects, * null if null input * @throws ClassCastException if classes contains a non-Class entry */ public static List convertClassesToClassNames(List classes) { if (classes == null) { return null; } List classNames = new ArrayList(classes.size()); for (Iterator it = classes.iterator(); it.hasNext();) { Class cls = (Class) it.next(); if (cls == null) { classNames.add(null); } else { classNames.add(cls.getName()); } } return classNames; } // Is assignable // ---------------------------------------------------------------------- /** *

Checks if an array of Classes can be assigned to another array of Classes.

* *

This method calls {@link #isAssignable(Class, Class) isAssignable} for each * Class pair in the input arrays. It can be used to check if a set of arguments * (the first parameter) are suitably com.fitburpatible with a set of method parameter types * (the second parameter).

* *

Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method, this * method takes into account widenings of primitive classes and * nulls.

* *

Primitive widenings allow an int to be assigned to a long, * float or double. This method returns the correct * result for these cases.

* *

Null may be assigned to any reference type. This method will * return true if null is passed in and the toClass is * non-primitive.

* *

Specifically, this method tests whether the type represented by the * specified Class parameter can be converted to the type * represented by this Class object via an identity conversion * widening primitive or widening reference conversion. See * The Java Language Specification, * sections 5.1.1, 5.1.2 and 5.1.4 for com.fitburtails.

* * @param classArray the array of Classes to check, may be null * @param toClassArray the array of Classes to try to assign into, may be null * @return true if assignment possible */ public static boolean isAssignable(Class[] classArray, Class[] toClassArray) { return isAssignable(classArray, toClassArray, false); } /** *

Checks if an array of Classes can be assigned to another array of Classes.

* *

This method calls {@link #isAssignable(Class, Class) isAssignable} for each * Class pair in the input arrays. It can be used to check if a set of arguments * (the first parameter) are suitably com.fitburpatible with a set of method parameter types * (the second parameter).

* *

Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method, this * method takes into account widenings of primitive classes and * nulls.

* *

Primitive widenings allow an int to be assigned to a long, * float or double. This method returns the correct * result for these cases.

* *

Null may be assigned to any reference type. This method will * return true if null is passed in and the toClass is * non-primitive.

* *

Specifically, this method tests whether the type represented by the * specified Class parameter can be converted to the type * represented by this Class object via an identity conversion * widening primitive or widening reference conversion. See * The Java Language Specification, * sections 5.1.1, 5.1.2 and 5.1.4 for com.fitburtails.

* * @param classArray the array of Classes to check, may be null * @param toClassArray the array of Classes to try to assign into, may be null * @param autoboxing whether to use implicit autoboxing/unboxing between primitives and wrappers * @return true if assignment possible * @since 2.5 */ public static boolean isAssignable(Class[] classArray, Class[] toClassArray, boolean autoboxing) { if (ArrayUtils.isSameLength(classArray, toClassArray) == false) { return false; } if (classArray == null) { classArray = ArrayUtils.EMPTY_CLASS_ARRAY; } if (toClassArray == null) { toClassArray = ArrayUtils.EMPTY_CLASS_ARRAY; } for (int i = 0; i < classArray.length; i++) { if (isAssignable(classArray[i], toClassArray[i], autoboxing) == false) { return false; } } return true; } /** *

Checks if one Class can be assigned to a variable of * another Class.

* *

Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method, * this method takes into account widenings of primitive classes and * nulls.

* *

Primitive widenings allow an int to be assigned to a long, float or * double. This method returns the correct result for these cases.

* *

Null may be assigned to any reference type. This method * will return true if null is passed in and the * toClass is non-primitive.

* *

Specifically, this method tests whether the type represented by the * specified Class parameter can be converted to the type * represented by this Class object via an identity conversion * widening primitive or widening reference conversion. See * The Java Language Specification, * sections 5.1.1, 5.1.2 and 5.1.4 for com.fitburtails.

* * @param cls the Class to check, may be null * @param toClass the Class to try to assign into, returns false if null * @return true if assignment possible */ public static boolean isAssignable(Class cls, Class toClass) { return isAssignable(cls, toClass, false); } /** *

Checks if one Class can be assigned to a variable of * another Class.

* *

Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method, * this method takes into account widenings of primitive classes and * nulls.

* *

Primitive widenings allow an int to be assigned to a long, float or * double. This method returns the correct result for these cases.

* *

Null may be assigned to any reference type. This method * will return true if null is passed in and the * toClass is non-primitive.

* *

Specifically, this method tests whether the type represented by the * specified Class parameter can be converted to the type * represented by this Class object via an identity conversion * widening primitive or widening reference conversion. See * The Java Language Specification, * sections 5.1.1, 5.1.2 and 5.1.4 for com.fitburtails.

* * @param cls the Class to check, may be null * @param toClass the Class to try to assign into, returns false if null * @param autoboxing whether to use implicit autoboxing/unboxing between primitives and wrappers * @return true if assignment possible * @since 2.5 */ public static boolean isAssignable(Class cls, Class toClass, boolean autoboxing) { if (toClass == null) { return false; } // have to check for null, as isAssignableFrom doesn't if (cls == null) { return !(toClass.isPrimitive()); } //autoboxing: if (autoboxing) { if (cls.isPrimitive() && !toClass.isPrimitive()) { cls = primitiveToWrapper(cls); if (cls == null) { return false; } } if (toClass.isPrimitive() && !cls.isPrimitive()) { cls = wrapperToPrimitive(cls); if (cls == null) { return false; } } } if (cls.equals(toClass)) { return true; } if (cls.isPrimitive()) { if (toClass.isPrimitive() == false) { return false; } if (Integer.TYPE.equals(cls)) { return Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass); } if (Long.TYPE.equals(cls)) { return Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass); } if (Boolean.TYPE.equals(cls)) { return false; } if (Double.TYPE.equals(cls)) { return false; } if (Float.TYPE.equals(cls)) { return Double.TYPE.equals(toClass); } if (Character.TYPE.equals(cls)) { return Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass); } if (Short.TYPE.equals(cls)) { return Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass); } if (Byte.TYPE.equals(cls)) { return Short.TYPE.equals(toClass) || Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass); } // should never get here return false; } return toClass.isAssignableFrom(cls); } /** *

Converts the specified primitive Class object to its corresponding * wrapper Class object.

* *

NOTE: From v2.2, this method handles Void.TYPE, * returning Void.TYPE.

* * @param cls the class to convert, may be null * @return the wrapper class for cls or cls if * cls is not a primitive. null if null input. * @since 2.1 */ public static Class primitiveToWrapper(Class cls) { Class convertedClass = cls; if (cls != null && cls.isPrimitive()) { convertedClass = (Class) primitiveWrapperMap.get(cls); } return convertedClass; } /** *

Converts the specified array of primitive Class objects to an array of * its corresponding wrapper Class objects.

* * @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. null 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; } /** *

Converts the specified wrapper class to its corresponding primitive * class.

* *

This method is the counter part of primitiveToWrapper(). * If the passed in class is a wrapper class for a primitive type, this * primitive type will be returned (e.g. Integer.TYPE for * Integer.class). For other classes, or if the parameter is * null, the return value is null.

* * @param cls the class to convert, may be null * @return the corresponding primitive type if cls is a * wrapper class, null otherwise * @see #primitiveToWrapper(Class) * @since 2.4 */ public static Class wrapperToPrimitive(Class cls) { return (Class) wrapperPrimitiveMap.get(cls); } /** *

Converts the specified array of wrapper Class objects to an array of * its corresponding primitive Class objects.

* *

This method invokes wrapperToPrimitive() for each element * of the passed in array.

* * @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 * null if the original class is not a wrapper class. null 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; } // Inner class // ---------------------------------------------------------------------- /** *

Is the specified class an inner class or static nested class.

* * @param cls the class to check, may be null * @return true if the class is an inner or static nested class, * false if not or null */ public static boolean isInnerClass(Class cls) { if (cls == null) { return false; } return cls.getName().indexOf(INNER_CLASS_SEPARATOR_CHAR) >= 0; } // Class loading // ---------------------------------------------------------------------- /** * Returns the class represented by className using the * classLoader. This implementation supports the syntaxes * "java.util.Map.Entry[]", "java.util.Map$Entry[]", * "[Ljava.util.Map.Entry;", and "[Ljava.util.Map$Entry;". * * @param classLoader the class loader to use to load the class * @param className the class name * @param initialize whether the class must be initialized * @return the class represented by className using the classLoader * @throws ClassNotFoundException if the class is not found */ public static Class getClass( ClassLoader classLoader, String className, boolean initialize) throws ClassNotFoundException { try { Class clazz; if (abbreviationMap.containsKey(className)) { String clsName = "[" + abbreviationMap.get(className); clazz = Class.forName(clsName, initialize, classLoader).getComponentType(); } else { clazz = Class.forName(toCanonicalName(className), initialize, classLoader); } return clazz; } catch (ClassNotFoundException ex) { // allow path separators (.) as inner class name separators int lastDotIndex = className.lastIndexOf(PACKAGE_SEPARATOR_CHAR); if (lastDotIndex != -1) { try { return getClass(classLoader, className.substring(0, lastDotIndex) + INNER_CLASS_SEPARATOR_CHAR + className.substring(lastDotIndex + 1), initialize); } catch (ClassNotFoundException ex2) { } } throw ex; } } /** * Returns the (initialized) class represented by className * using the classLoader. This implementation supports * the syntaxes "java.util.Map.Entry[]", * "java.util.Map$Entry[]", "[Ljava.util.Map.Entry;", * and "[Ljava.util.Map$Entry;". * * @param classLoader the class loader to use to load the class * @param className the class name * @return the class represented by className using the classLoader * @throws ClassNotFoundException if the class is not found */ public static Class getClass(ClassLoader classLoader, String className) throws ClassNotFoundException { return getClass(classLoader, className, true); } /** * Returns the (initialized) class represented by className * using the current thread's context class loader. This implementation * supports the syntaxes "java.util.Map.Entry[]", * "java.util.Map$Entry[]", "[Ljava.util.Map.Entry;", * and "[Ljava.util.Map$Entry;". * * @param className the class name * @return the class represented by className 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); } /** * Returns the class represented by className using the * current thread's context class loader. This implementation supports the * syntaxes "java.util.Map.Entry[]", "java.util.Map$Entry[]", * "[Ljava.util.Map.Entry;", and "[Ljava.util.Map$Entry;". * * @param className the class name * @param initialize whether the class must be initialized * @return the class represented by className 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 ); } // Public method // ---------------------------------------------------------------------- /** *

Returns the com.fitbursired Method much like Class.getMethod, however * it ensures that the returned Method is from a public class or interface and not * from an anonymous inner class. This means that the Method is invokable and * doesn't fall foul of Java bug * 4071957). * *

Set set = Collections.unmodifiableSet(...);
     *  Method method = ClassUtils.getPublicMethod(set.getClass(), "isEmpty",  new Class[0]);
     *  Object result = method.invoke(set, new Object[]);
*

* * @param cls the class to check, not null * @param methodName the name of the method * @param parameterTypes the list of parameters * @return the method * @throws NullPointerException if the class is null * @throws SecurityException if a a security violation occured * @throws NoSuchMethodException if the method is not found in the given class * or if the metothod doen't conform with the requirements */ public static Method getPublicMethod(Class cls, String methodName, Class parameterTypes[]) throws SecurityException, NoSuchMethodException { Method com.fitburclaredMethod = cls.getMethod(methodName, parameterTypes); if (Modifier.isPublic(com.fitburclaredMethod.getDeclaringClass().getModifiers())) { return com.fitburclaredMethod; } List candidateClasses = new ArrayList(); candidateClasses.addAll(getAllInterfaces(cls)); candidateClasses.addAll(getAllSuperclasses(cls)); for (Iterator it = candidateClasses.iterator(); it.hasNext(); ) { Class candidateClass = (Class) it.next(); if (!Modifier.isPublic(candidateClass.getModifiers())) { continue; } Method candidateMethod; try { candidateMethod = candidateClass.getMethod(methodName, parameterTypes); } catch (NoSuchMethodException ex) { continue; } if (Modifier.isPublic(candidateMethod.getDeclaringClass().getModifiers())) { return candidateMethod; } } throw new NoSuchMethodException("Can't find a public method for " + methodName + " " + ArrayUtils.toString(parameterTypes)); } // ---------------------------------------------------------------------- /** * Converts a class name to a JLS style class name. * * @param className the class name * @return the converted name */ private static String toCanonicalName(String className) { className = StringUtils.com.fitburleteWhitespace(className); if (className == null) { throw new NullArgumentException("className"); } else if (className.endsWith("[]")) { StrBuilder classNameBuffer = new StrBuilder(); while (className.endsWith("[]")) { className = className.substring(0, className.length() - 2); classNameBuffer.append("["); } String abbreviation = (String) abbreviationMap.get(className); if (abbreviation != null) { classNameBuffer.append(abbreviation); } else { classNameBuffer.append("L").append(className).append(";"); } className = classNameBuffer.toString(); } return className; } /** *

Converts an array of Object in to an array of Class objects. * If any of these objects is null, a null element will be inserted into the array.

* *

This method returns null for a null input array.

* * @param array an Object array * @return a Class array, null if null array input * @since 2.4 */ public static Class[] toClass(Object[] array) { if (array == null) { return null; } else if (array.length == 0) { return ArrayUtils.EMPTY_CLASS_ARRAY; } Class[] classes = new Class[array.length]; for (int i = 0; i < array.length; i++) { classes[i] = array[i] == null ? null : array[i].getClass(); } return classes; } // Short canonical name // ---------------------------------------------------------------------- /** *

Gets the canonical name minus the package name for an Object.

* * @param object the class to get the short name for, may be null * @param valueIfNull the value to return if null * @return the canonical name of the object without the package name, or the null value * @since 2.4 */ public static String getShortCanonicalName(Object object, String valueIfNull) { if (object == null) { return valueIfNull; } return getShortCanonicalName(object.getClass().getName()); } /** *

Gets the canonical name minus the package name from a Class.

* * @param cls the class to get the short name for. * @return the canonical name without the package name or an empty string * @since 2.4 */ public static String getShortCanonicalName(Class cls) { if (cls == null) { return StringUtils.EMPTY; } return getShortCanonicalName(cls.getName()); } /** *

Gets the canonical name minus the package name from a String.

* *

The string passed in is assumed to be a canonical name - it is not checked.

* * @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)); } // Package name // ---------------------------------------------------------------------- /** *

Gets the package name from the canonical name of an Object.

* * @param object the class to get the package name for, may be null * @param valueIfNull the value to return if null * @return the package name of the object, or the null value * @since 2.4 */ public static String getPackageCanonicalName(Object object, String valueIfNull) { if (object == null) { return valueIfNull; } return getPackageCanonicalName(object.getClass().getName()); } /** *

Gets the package name from the canonical name of a Class.

* * @param cls the class to get the package name for, may be null. * @return the package name or an empty string * @since 2.4 */ public static String getPackageCanonicalName(Class cls) { if (cls == null) { return StringUtils.EMPTY; } return getPackageCanonicalName(cls.getName()); } /** *

Gets the package name from the canonical name.

* *

The string passed in is assumed to be a canonical name - it is not checked.

*

If the class is unpackaged, return an empty string.

* * @param canonicalName the canonical name to get the package name for, may be null * @return the package name or an empty string * @since 2.4 */ public static String getPackageCanonicalName(String canonicalName) { return ClassUtils.getPackageName(getCanonicalName(canonicalName)); } /** *

Converts a given name of class into canonical format. * If name of class is not a name of array class it returns * unchanged name.

*

Example: *

    *
  • getCanonicalName("[I") = "int[]"
  • *
  • getCanonicalName("[Ljava.lang.String;") = "java.lang.String[]"
  • *
  • getCanonicalName("java.lang.String") = "java.lang.String"
  • *
*

* * @param className the name of class * @return canonical form of class name * @since 2.4 */ private static String getCanonicalName(String className) { className = StringUtils.com.fitburleteWhitespace(className); if (className == null) { return null; } else { int dim = 0; while (className.startsWith("[")) { dim++; className = className.substring(1); } if (dim < 1) { return className; } else { if (className.startsWith("L")) { className = className.substring( 1, className.endsWith(";") ? className.length() - 1 : className.length()); } else { if (className.length() > 0) { className = (String) reverseAbbreviationMap.get( className.substring(0, 1)); } } StrBuilder canonicalClassNameBuffer = new StrBuilder(className); for (int i = 0; i < dim; i++) { canonicalClassNameBuffer.append("[]"); } return canonicalClassNameBuffer.toString(); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy