Refactored getClass() to support spaces in class names.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@345284 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
502f7f4a19
commit
39c9476337
|
@ -23,44 +23,42 @@ import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Operates on classes without using reflection.</p>
|
* <p>Operates on classes without using reflection.</p>
|
||||||
*
|
* <p/>
|
||||||
* <p>This class handles invalid <code>null</code> inputs as best it can.
|
* <p>This class handles invalid <code>null</code> inputs as best it can. Each method documents its behaviour in more
|
||||||
* Each method documents its behaviour in more detail.</p>
|
* detail.</p>
|
||||||
*
|
*
|
||||||
* @author Stephen Colebourne
|
* @author Stephen Colebourne
|
||||||
* @author Gary Gregory
|
* @author Gary Gregory
|
||||||
* @author Norm Deane
|
* @author Norm Deane
|
||||||
* @author Alban Peignier
|
* @author Alban Peignier
|
||||||
* @since 2.0
|
|
||||||
* @version $Id$
|
* @version $Id$
|
||||||
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
public class ClassUtils {
|
public class ClassUtils
|
||||||
|
{
|
||||||
/**
|
/**
|
||||||
* <p>The package separator character: <code>'.' == {@value}</code>.</p>
|
* <p>The package separator character: <code>'.' == {@value}</code>.</p>
|
||||||
*/
|
*/
|
||||||
public static final char PACKAGE_SEPARATOR_CHAR = '.';
|
public static final char PACKAGE_SEPARATOR_CHAR = '.';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>The package separator String: <code>"."</code>.</p>
|
* <p>The package separator String: <code>"."</code>.</p>
|
||||||
*/
|
*/
|
||||||
public static final String PACKAGE_SEPARATOR = String.valueOf( PACKAGE_SEPARATOR_CHAR );
|
public static final String PACKAGE_SEPARATOR = String.valueOf( PACKAGE_SEPARATOR_CHAR );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>The inner class separator character: <code>'$' == {@value}</code>.</p>
|
* <p>The inner class separator character: <code>'$' == {@value}</code>.</p>
|
||||||
*/
|
*/
|
||||||
public static final char INNER_CLASS_SEPARATOR_CHAR = '$';
|
public static final char INNER_CLASS_SEPARATOR_CHAR = '$';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>The inner class separator String: <code>"$"</code>.</p>
|
* <p>The inner class separator String: <code>"$"</code>.</p>
|
||||||
*/
|
*/
|
||||||
public static final String INNER_CLASS_SEPARATOR = String.valueOf( INNER_CLASS_SEPARATOR_CHAR );
|
public static final String INNER_CLASS_SEPARATOR = String.valueOf( INNER_CLASS_SEPARATOR_CHAR );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Maps primitive <code>Class</code>es to their corresponding wrapper <code>Class</code>.
|
* Maps primitive <code>Class</code>es to their corresponding wrapper <code>Class</code>.
|
||||||
*/
|
*/
|
||||||
private static Map primitiveWrapperMap = new HashMap();
|
private static Map primitiveWrapperMap = new HashMap();
|
||||||
static {
|
|
||||||
|
static
|
||||||
|
{
|
||||||
primitiveWrapperMap.put( Boolean.TYPE, Boolean.class );
|
primitiveWrapperMap.put( Boolean.TYPE, Boolean.class );
|
||||||
primitiveWrapperMap.put( Byte.TYPE, Byte.class );
|
primitiveWrapperMap.put( Byte.TYPE, Byte.class );
|
||||||
primitiveWrapperMap.put( Character.TYPE, Character.class );
|
primitiveWrapperMap.put( Character.TYPE, Character.class );
|
||||||
|
@ -73,19 +71,36 @@ public class ClassUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>ClassUtils instances should NOT be constructed in standard programming.
|
* Maps a primitive class name to its corresponding abbreviation used in array class names.
|
||||||
* Instead, the class should be used as
|
|
||||||
* <code>ClassUtils.getShortClassName(cls)</code>.</p>
|
|
||||||
*
|
|
||||||
* <p>This constructor is public to permit tools that require a JavaBean
|
|
||||||
* instance to operate.</p>
|
|
||||||
*/
|
*/
|
||||||
public ClassUtils() {
|
private static Map abbreviationMap = new HashMap();
|
||||||
|
|
||||||
|
static
|
||||||
|
{
|
||||||
|
abbreviationMap.put( "int", "I" );
|
||||||
|
abbreviationMap.put( "boolean", "Z" );
|
||||||
|
abbreviationMap.put( "float", "F" );
|
||||||
|
abbreviationMap.put( "long", "J" );
|
||||||
|
abbreviationMap.put( "short", "S" );
|
||||||
|
abbreviationMap.put( "byte", "B" );
|
||||||
|
abbreviationMap.put( "double", "D" );
|
||||||
|
abbreviationMap.put( "char", "C" );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>ClassUtils instances should NOT be constructed in standard programming. Instead, the class should be used as
|
||||||
|
* <code>ClassUtils.getShortClassName(cls)</code>.</p>
|
||||||
|
* <p/>
|
||||||
|
* <p>This constructor is public to permit tools that require a JavaBean instance to operate.</p>
|
||||||
|
*/
|
||||||
|
public ClassUtils()
|
||||||
|
{
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Short class name
|
// Short class name
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Gets the class name minus the package name for an <code>Object</code>.</p>
|
* <p>Gets the class name minus the package name for an <code>Object</code>.</p>
|
||||||
*
|
*
|
||||||
|
@ -93,8 +108,10 @@ public class ClassUtils {
|
||||||
* @param valueIfNull the value to return if null
|
* @param valueIfNull the value to return if null
|
||||||
* @return the class name of the object without the package name, or the null value
|
* @return the class name of the object without the package name, or the null value
|
||||||
*/
|
*/
|
||||||
public static String getShortClassName(Object object, String valueIfNull) {
|
public static String getShortClassName( Object object, String valueIfNull )
|
||||||
if (object == null) {
|
{
|
||||||
|
if( object == null )
|
||||||
|
{
|
||||||
return valueIfNull;
|
return valueIfNull;
|
||||||
}
|
}
|
||||||
return getShortClassName( object.getClass().getName() );
|
return getShortClassName( object.getClass().getName() );
|
||||||
|
@ -106,8 +123,10 @@ public class ClassUtils {
|
||||||
* @param cls the class to get the short name for.
|
* @param cls the class to get the short name for.
|
||||||
* @return the class name without the package name or an empty string
|
* @return the class name without the package name or an empty string
|
||||||
*/
|
*/
|
||||||
public static String getShortClassName(Class cls) {
|
public static String getShortClassName( Class cls )
|
||||||
if (cls == null) {
|
{
|
||||||
|
if( cls == null )
|
||||||
|
{
|
||||||
return StringUtils.EMPTY;
|
return StringUtils.EMPTY;
|
||||||
}
|
}
|
||||||
return getShortClassName( cls.getName() );
|
return getShortClassName( cls.getName() );
|
||||||
|
@ -115,25 +134,32 @@ public class ClassUtils {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Gets the class name minus the package name from a String.</p>
|
* <p>Gets the class name minus the package name from a String.</p>
|
||||||
*
|
* <p/>
|
||||||
* <p>The string passed in is assumed to be a class name - it is not checked.</p>
|
* <p>The string passed in is assumed to be a class name - it is not checked.</p>
|
||||||
*
|
*
|
||||||
* @param className the className to get the short name for
|
* @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
|
* @return the class name of the class without the package name or an empty string
|
||||||
*/
|
*/
|
||||||
public static String getShortClassName(String className) {
|
public static String getShortClassName( String className )
|
||||||
if (className == null) {
|
{
|
||||||
|
if( className == null )
|
||||||
|
{
|
||||||
return StringUtils.EMPTY;
|
return StringUtils.EMPTY;
|
||||||
}
|
}
|
||||||
if (className.length() == 0) {
|
if( className.length() == 0 )
|
||||||
|
{
|
||||||
return StringUtils.EMPTY;
|
return StringUtils.EMPTY;
|
||||||
}
|
}
|
||||||
char[] chars = className.toCharArray();
|
char[] chars = className.toCharArray();
|
||||||
int lastDot = 0;
|
int lastDot = 0;
|
||||||
for (int i = 0; i < chars.length; i++) {
|
for( int i = 0; i < chars.length; i++ )
|
||||||
if (chars[i] == PACKAGE_SEPARATOR_CHAR) {
|
{
|
||||||
|
if( chars[i] == PACKAGE_SEPARATOR_CHAR )
|
||||||
|
{
|
||||||
lastDot = i + 1;
|
lastDot = i + 1;
|
||||||
} else if (chars[i] == INNER_CLASS_SEPARATOR_CHAR) { // handle inner classes
|
}
|
||||||
|
else if( chars[i] == INNER_CLASS_SEPARATOR_CHAR )
|
||||||
|
{ // handle inner classes
|
||||||
chars[i] = PACKAGE_SEPARATOR_CHAR;
|
chars[i] = PACKAGE_SEPARATOR_CHAR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -142,6 +168,7 @@ public class ClassUtils {
|
||||||
|
|
||||||
// Package name
|
// Package name
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Gets the package name of an <code>Object</code>.</p>
|
* <p>Gets the package name of an <code>Object</code>.</p>
|
||||||
*
|
*
|
||||||
|
@ -149,8 +176,10 @@ public class ClassUtils {
|
||||||
* @param valueIfNull the value to return if null
|
* @param valueIfNull the value to return if null
|
||||||
* @return the package name of the object, or the null value
|
* @return the package name of the object, or the null value
|
||||||
*/
|
*/
|
||||||
public static String getPackageName(Object object, String valueIfNull) {
|
public static String getPackageName( Object object, String valueIfNull )
|
||||||
if (object == null) {
|
{
|
||||||
|
if( object == null )
|
||||||
|
{
|
||||||
return valueIfNull;
|
return valueIfNull;
|
||||||
}
|
}
|
||||||
return getPackageName( object.getClass().getName() );
|
return getPackageName( object.getClass().getName() );
|
||||||
|
@ -162,8 +191,10 @@ public class ClassUtils {
|
||||||
* @param cls the class to get the package name for, may be <code>null</code>.
|
* @param cls the class to get the package name for, may be <code>null</code>.
|
||||||
* @return the package name or an empty string
|
* @return the package name or an empty string
|
||||||
*/
|
*/
|
||||||
public static String getPackageName(Class cls) {
|
public static String getPackageName( Class cls )
|
||||||
if (cls == null) {
|
{
|
||||||
|
if( cls == null )
|
||||||
|
{
|
||||||
return StringUtils.EMPTY;
|
return StringUtils.EMPTY;
|
||||||
}
|
}
|
||||||
return getPackageName( cls.getName() );
|
return getPackageName( cls.getName() );
|
||||||
|
@ -171,19 +202,22 @@ public class ClassUtils {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Gets the package name from a <code>String</code>.</p>
|
* <p>Gets the package name from a <code>String</code>.</p>
|
||||||
*
|
* <p/>
|
||||||
* <p>The string passed in is assumed to be a class name - it is not checked.</p>
|
* <p>The string passed in is assumed to be a class name - it is not checked.</p> <p>If the class is unpackaged,
|
||||||
* <p>If the class is unpackaged, return an empty string.</p>
|
* return an empty string.</p>
|
||||||
*
|
*
|
||||||
* @param className the className to get the package name for, may be <code>null</code>
|
* @param className the className to get the package name for, may be <code>null</code>
|
||||||
* @return the package name or an empty string
|
* @return the package name or an empty string
|
||||||
*/
|
*/
|
||||||
public static String getPackageName(String className) {
|
public static String getPackageName( String className )
|
||||||
if (className == null) {
|
{
|
||||||
|
if( className == null )
|
||||||
|
{
|
||||||
return StringUtils.EMPTY;
|
return StringUtils.EMPTY;
|
||||||
}
|
}
|
||||||
int i = className.lastIndexOf( PACKAGE_SEPARATOR_CHAR );
|
int i = className.lastIndexOf( PACKAGE_SEPARATOR_CHAR );
|
||||||
if (i == -1) {
|
if( i == -1 )
|
||||||
|
{
|
||||||
return StringUtils.EMPTY;
|
return StringUtils.EMPTY;
|
||||||
}
|
}
|
||||||
return className.substring( 0, i );
|
return className.substring( 0, i );
|
||||||
|
@ -191,20 +225,23 @@ public class ClassUtils {
|
||||||
|
|
||||||
// Superclasses/Superinterfaces
|
// Superclasses/Superinterfaces
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Gets a <code>List</code> of superclasses for the given class.</p>
|
* <p>Gets a <code>List</code> of superclasses for the given class.</p>
|
||||||
*
|
*
|
||||||
* @param cls the class to look up, may be <code>null</code>
|
* @param cls the class to look up, may be <code>null</code>
|
||||||
* @return the <code>List</code> of superclasses in order going up from this one
|
* @return the <code>List</code> of superclasses in order going up from this one <code>null</code> if null input
|
||||||
* <code>null</code> if null input
|
|
||||||
*/
|
*/
|
||||||
public static List getAllSuperclasses(Class cls) {
|
public static List getAllSuperclasses( Class cls )
|
||||||
if (cls == null) {
|
{
|
||||||
|
if( cls == null )
|
||||||
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
List classes = new ArrayList();
|
List classes = new ArrayList();
|
||||||
Class superclass = cls.getSuperclass();
|
Class superclass = cls.getSuperclass();
|
||||||
while (superclass != null) {
|
while( superclass != null )
|
||||||
|
{
|
||||||
classes.add( superclass );
|
classes.add( superclass );
|
||||||
superclass = superclass.getSuperclass();
|
superclass = superclass.getSuperclass();
|
||||||
}
|
}
|
||||||
|
@ -212,33 +249,37 @@ public class ClassUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Gets a <code>List</code> of all interfaces implemented by the given
|
* <p>Gets a <code>List</code> of all interfaces implemented by the given class and its superclasses.</p>
|
||||||
* class and its superclasses.</p>
|
* <p/>
|
||||||
*
|
* <p>The order is determined by looking through each interface in turn as declared in the source file and following
|
||||||
* <p>The order is determined by looking through each interface in turn as
|
* its hierarchy up. Then each superclass is considered in the same way. Later duplicates are ignored, so the order
|
||||||
* declared in the source file and following its hierarchy up. Then each
|
* is maintained.</p>
|
||||||
* 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>
|
* @param cls the class to look up, may be <code>null</code>
|
||||||
* @return the <code>List</code> of interfaces in order,
|
* @return the <code>List</code> of interfaces in order, <code>null</code> if null input
|
||||||
* <code>null</code> if null input
|
|
||||||
*/
|
*/
|
||||||
public static List getAllInterfaces(Class cls) {
|
public static List getAllInterfaces( Class cls )
|
||||||
if (cls == null) {
|
{
|
||||||
|
if( cls == null )
|
||||||
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
List list = new ArrayList();
|
List list = new ArrayList();
|
||||||
while (cls != null) {
|
while( cls != null )
|
||||||
|
{
|
||||||
Class[] interfaces = cls.getInterfaces();
|
Class[] interfaces = cls.getInterfaces();
|
||||||
for (int i = 0; i < interfaces.length; i++) {
|
for( int i = 0; i < interfaces.length; i++ )
|
||||||
if (list.contains(interfaces[i]) == false) {
|
{
|
||||||
|
if( list.contains( interfaces[i] ) == false )
|
||||||
|
{
|
||||||
list.add( interfaces[i] );
|
list.add( interfaces[i] );
|
||||||
}
|
}
|
||||||
List superInterfaces = getAllInterfaces( interfaces[i] );
|
List superInterfaces = getAllInterfaces( interfaces[i] );
|
||||||
for (Iterator it = superInterfaces.iterator(); it.hasNext();) {
|
for( Iterator it = superInterfaces.iterator(); it.hasNext(); )
|
||||||
|
{
|
||||||
Class intface = ( Class ) it.next();
|
Class intface = ( Class ) it.next();
|
||||||
if (list.contains(intface) == false) {
|
if( list.contains( intface ) == false )
|
||||||
|
{
|
||||||
list.add( intface );
|
list.add( intface );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -250,28 +291,34 @@ public class ClassUtils {
|
||||||
|
|
||||||
// Convert list
|
// Convert list
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Given a <code>List</code> of class names, this method converts them into classes.</p>
|
* <p>Given a <code>List</code> of class names, this method converts them into classes.</p>
|
||||||
*
|
* <p/>
|
||||||
* <p>A new <code>List</code> is returned. If the class name cannot be found, <code>null</code>
|
* <p>A new <code>List</code> is returned. If the class name cannot be found, <code>null</code> is stored in the
|
||||||
* is stored in the <code>List</code>. If the class name in the <code>List</code> is
|
* <code>List</code>. If the class name in the <code>List</code> is <code>null</code>, <code>null</code> is stored
|
||||||
* <code>null</code>, <code>null</code> is stored in the output <code>List</code>.</p>
|
* in the output <code>List</code>.</p>
|
||||||
*
|
*
|
||||||
* @param classNames the classNames to change
|
* @param classNames the classNames to change
|
||||||
* @return a <code>List</code> of Class objects corresponding to the class names,
|
* @return a <code>List</code> of Class objects corresponding to the class names, <code>null</code> if null input
|
||||||
* <code>null</code> if null input
|
|
||||||
* @throws ClassCastException if classNames contains a non String entry
|
* @throws ClassCastException if classNames contains a non String entry
|
||||||
*/
|
*/
|
||||||
public static List convertClassNamesToClasses(List classNames) {
|
public static List convertClassNamesToClasses( List classNames )
|
||||||
if (classNames == null) {
|
{
|
||||||
|
if( classNames == null )
|
||||||
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
List classes = new ArrayList( classNames.size() );
|
List classes = new ArrayList( classNames.size() );
|
||||||
for (Iterator it = classNames.iterator(); it.hasNext();) {
|
for( Iterator it = classNames.iterator(); it.hasNext(); )
|
||||||
|
{
|
||||||
String className = ( String ) it.next();
|
String className = ( String ) it.next();
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
classes.add( Class.forName( className ) );
|
classes.add( Class.forName( className ) );
|
||||||
} catch (Exception ex) {
|
}
|
||||||
|
catch( Exception ex )
|
||||||
|
{
|
||||||
classes.add( null );
|
classes.add( null );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -279,27 +326,31 @@ public class ClassUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Given a <code>List</code> of <code>Class</code> objects, this method converts
|
* <p>Given a <code>List</code> of <code>Class</code> objects, this method converts them into class names.</p>
|
||||||
* them into class names.</p>
|
* <p/>
|
||||||
*
|
* <p>A new <code>List</code> is returned. <code>null</code> objects will be copied into the returned list as
|
||||||
* <p>A new <code>List</code> is returned. <code>null</code> objects will be copied into
|
* <code>null</code>.</p>
|
||||||
* the returned list as <code>null</code>.</p>
|
|
||||||
*
|
*
|
||||||
* @param classes the classes to change
|
* @param classes the classes to change
|
||||||
* @return a <code>List</code> of class names corresponding to the Class objects,
|
* @return a <code>List</code> of class names corresponding to the Class objects, <code>null</code> if null input
|
||||||
* <code>null</code> if null input
|
|
||||||
* @throws ClassCastException if <code>classes</code> contains a non-<code>Class</code> entry
|
* @throws ClassCastException if <code>classes</code> contains a non-<code>Class</code> entry
|
||||||
*/
|
*/
|
||||||
public static List convertClassesToClassNames(List classes) {
|
public static List convertClassesToClassNames( List classes )
|
||||||
if (classes == null) {
|
{
|
||||||
|
if( classes == null )
|
||||||
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
List classNames = new ArrayList( classes.size() );
|
List classNames = new ArrayList( classes.size() );
|
||||||
for (Iterator it = classes.iterator(); it.hasNext();) {
|
for( Iterator it = classes.iterator(); it.hasNext(); )
|
||||||
|
{
|
||||||
Class cls = ( Class ) it.next();
|
Class cls = ( Class ) it.next();
|
||||||
if (cls == null) {
|
if( cls == null )
|
||||||
|
{
|
||||||
classNames.add( null );
|
classNames.add( null );
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
classNames.add( cls.getName() );
|
classNames.add( cls.getName() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -308,49 +359,50 @@ public class ClassUtils {
|
||||||
|
|
||||||
// Is assignable
|
// Is assignable
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Checks if an array of Classes can be assigned to another array of Classes.</p>
|
* <p>Checks if an array of Classes can be assigned to another array of Classes.</p>
|
||||||
*
|
* <p/>
|
||||||
* <p>This method calls {@link #isAssignable(Class, Class) isAssignable} for each
|
* <p>This method calls {@link #isAssignable(Class, Class) isAssignable} for each Class pair in the input arrays. It
|
||||||
* Class pair in the input arrays. It can be used to check if a set of arguments
|
* can be used to check if a set of arguments (the first parameter) are suitably compatible with a set of method
|
||||||
* (the first parameter) are suitably compatible with a set of method parameter types
|
* parameter types (the second parameter).</p>
|
||||||
* (the second parameter).</p>
|
* <p/>
|
||||||
*
|
* <p>Unlike the {@link Class#isAssignableFrom(Class)} method, this method takes into account widenings of primitive
|
||||||
* <p>Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method, this
|
* classes and <code>null</code>s.</p>
|
||||||
* method takes into account widenings of primitive classes and
|
* <p/>
|
||||||
* <code>null</code>s.</p>
|
* <p>Primitive widenings allow an int to be assigned to a <code>long</code>, <code>float</code> or
|
||||||
*
|
* <code>double</code>. This method returns the correct result for these cases.</p>
|
||||||
* <p>Primitive widenings allow an int to be assigned to a <code>long</code>,
|
* <p/>
|
||||||
* <code>float</code> or <code>double</code>. This method returns the correct
|
* <p><code>Null</code> may be assigned to any reference type. This method will return <code>true</code> if
|
||||||
* result for these cases.</p>
|
* <code>null</code> is passed in and the toClass is non-primitive.</p>
|
||||||
*
|
* <p/>
|
||||||
* <p><code>Null</code> may be assigned to any reference type. This method will
|
* <p>Specifically, this method tests whether the type represented by the specified <code>Class</code> parameter can
|
||||||
* return <code>true</code> if <code>null</code> is passed in and the toClass is
|
* be converted to the type represented by this <code>Class</code> object via an identity conversion widening
|
||||||
* non-primitive.</p>
|
* 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>
|
||||||
* <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 classArray the array of Classes to check, may be <code>null</code>
|
* @param classArray the array of Classes to check, may be <code>null</code>
|
||||||
* @param toClassArray the array of Classes to try to assign into, may be <code>null</code>
|
* @param toClassArray the array of Classes to try to assign into, may be <code>null</code>
|
||||||
* @return <code>true</code> if assignment possible
|
* @return <code>true</code> if assignment possible
|
||||||
*/
|
*/
|
||||||
public static boolean isAssignable(Class[] classArray, Class[] toClassArray) {
|
public static boolean isAssignable( Class[] classArray, Class[] toClassArray )
|
||||||
if (ArrayUtils.isSameLength(classArray, toClassArray) == false) {
|
{
|
||||||
|
if( ArrayUtils.isSameLength( classArray, toClassArray ) == false )
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (classArray == null) {
|
if( classArray == null )
|
||||||
|
{
|
||||||
classArray = ArrayUtils.EMPTY_CLASS_ARRAY;
|
classArray = ArrayUtils.EMPTY_CLASS_ARRAY;
|
||||||
}
|
}
|
||||||
if (toClassArray == null) {
|
if( toClassArray == null )
|
||||||
|
{
|
||||||
toClassArray = ArrayUtils.EMPTY_CLASS_ARRAY;
|
toClassArray = ArrayUtils.EMPTY_CLASS_ARRAY;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < classArray.length; i++) {
|
for( int i = 0; i < classArray.length; i++ )
|
||||||
if (isAssignable(classArray[i], toClassArray[i]) == false) {
|
{
|
||||||
|
if( isAssignable( classArray[i], toClassArray[i] ) == false )
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -358,77 +410,86 @@ public class ClassUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Checks if one <code>Class</code> can be assigned to a variable of
|
* <p>Checks if one <code>Class</code> can be assigned to a variable of another <code>Class</code>.</p>
|
||||||
* another <code>Class</code>.</p>
|
* <p/>
|
||||||
*
|
* <p>Unlike the {@link Class#isAssignableFrom(Class)} method, this method takes into account widenings of primitive
|
||||||
* <p>Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method,
|
* classes and <code>null</code>s.</p>
|
||||||
* this method takes into account widenings of primitive classes and
|
* <p/>
|
||||||
* <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>Primitive widenings allow an int to be assigned to a long, float or
|
* <p/>
|
||||||
* 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><code>Null</code> may be assigned to any reference type. This method
|
* <p/>
|
||||||
* will return <code>true</code> if <code>null</code> is passed in and the
|
* <p>Specifically, this method tests whether the type represented by the specified <code>Class</code> parameter can
|
||||||
* toClass is non-primitive.</p>
|
* 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
|
||||||
* <p>Specifically, this method tests whether the type represented by the
|
* Language Specification</a></em>, sections 5.1.1, 5.1.2 and 5.1.4 for details.</p>
|
||||||
* 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 cls the Class to check, may be null
|
||||||
* @param toClass the Class to try to assign into, returns false if null
|
* @param toClass the Class to try to assign into, returns false if null
|
||||||
* @return <code>true</code> if assignment possible
|
* @return <code>true</code> if assignment possible
|
||||||
*/
|
*/
|
||||||
public static boolean isAssignable(Class cls, Class toClass) {
|
public static boolean isAssignable( Class cls, Class toClass )
|
||||||
if (toClass == null) {
|
{
|
||||||
|
if( toClass == null )
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// have to check for null, as isAssignableFrom doesn't
|
// have to check for null, as isAssignableFrom doesn't
|
||||||
if (cls == null) {
|
if( cls == null )
|
||||||
|
{
|
||||||
return !( toClass.isPrimitive() );
|
return !( toClass.isPrimitive() );
|
||||||
}
|
}
|
||||||
if (cls.equals(toClass)) {
|
if( cls.equals( toClass ) )
|
||||||
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (cls.isPrimitive()) {
|
if( cls.isPrimitive() )
|
||||||
if (toClass.isPrimitive() == false) {
|
{
|
||||||
|
if( toClass.isPrimitive() == false )
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (Integer.TYPE.equals(cls)) {
|
if( Integer.TYPE.equals( cls ) )
|
||||||
|
{
|
||||||
return Long.TYPE.equals( toClass )
|
return Long.TYPE.equals( toClass )
|
||||||
|| Float.TYPE.equals( toClass )
|
|| Float.TYPE.equals( toClass )
|
||||||
|| Double.TYPE.equals( toClass );
|
|| Double.TYPE.equals( toClass );
|
||||||
}
|
}
|
||||||
if (Long.TYPE.equals(cls)) {
|
if( Long.TYPE.equals( cls ) )
|
||||||
|
{
|
||||||
return Float.TYPE.equals( toClass )
|
return Float.TYPE.equals( toClass )
|
||||||
|| Double.TYPE.equals( toClass );
|
|| Double.TYPE.equals( toClass );
|
||||||
}
|
}
|
||||||
if (Boolean.TYPE.equals(cls)) {
|
if( Boolean.TYPE.equals( cls ) )
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (Double.TYPE.equals(cls)) {
|
if( Double.TYPE.equals( cls ) )
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (Float.TYPE.equals(cls)) {
|
if( Float.TYPE.equals( cls ) )
|
||||||
|
{
|
||||||
return Double.TYPE.equals( toClass );
|
return Double.TYPE.equals( toClass );
|
||||||
}
|
}
|
||||||
if (Character.TYPE.equals(cls)) {
|
if( Character.TYPE.equals( cls ) )
|
||||||
|
{
|
||||||
return Integer.TYPE.equals( toClass )
|
return Integer.TYPE.equals( toClass )
|
||||||
|| Long.TYPE.equals( toClass )
|
|| Long.TYPE.equals( toClass )
|
||||||
|| Float.TYPE.equals( toClass )
|
|| Float.TYPE.equals( toClass )
|
||||||
|| Double.TYPE.equals( toClass );
|
|| Double.TYPE.equals( toClass );
|
||||||
}
|
}
|
||||||
if (Short.TYPE.equals(cls)) {
|
if( Short.TYPE.equals( cls ) )
|
||||||
|
{
|
||||||
return Integer.TYPE.equals( toClass )
|
return Integer.TYPE.equals( toClass )
|
||||||
|| Long.TYPE.equals( toClass )
|
|| Long.TYPE.equals( toClass )
|
||||||
|| Float.TYPE.equals( toClass )
|
|| Float.TYPE.equals( toClass )
|
||||||
|| Double.TYPE.equals( toClass );
|
|| Double.TYPE.equals( toClass );
|
||||||
}
|
}
|
||||||
if (Byte.TYPE.equals(cls)) {
|
if( Byte.TYPE.equals( cls ) )
|
||||||
|
{
|
||||||
return Short.TYPE.equals( toClass )
|
return Short.TYPE.equals( toClass )
|
||||||
|| Integer.TYPE.equals( toClass )
|
|| Integer.TYPE.equals( toClass )
|
||||||
|| Long.TYPE.equals( toClass )
|
|| Long.TYPE.equals( toClass )
|
||||||
|
@ -442,46 +503,47 @@ public class ClassUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Converts the specified primitive Class object to its corresponding
|
* <p>Converts the specified primitive Class object to its corresponding wrapper Class object.</p>
|
||||||
* wrapper Class object.</p>
|
* <p/>
|
||||||
*
|
* <p>NOTE: From v2.2, this method handles <code>Void.TYPE</code>, returning <code>Void.TYPE</code>.</p>
|
||||||
* <p>NOTE: From v2.2, this method handles <code>Void.TYPE</code>,
|
|
||||||
* returning <code>Void.TYPE</code>.</p>
|
|
||||||
*
|
*
|
||||||
* @param cls the class to convert, may be null
|
* @param cls the class to convert, may be null
|
||||||
* @return the wrapper class for <code>cls</code> or <code>cls</code> if
|
* @return the wrapper class for <code>cls</code> or <code>cls</code> if <code>cls</code> is not a primitive.
|
||||||
* <code>cls</code> is not a primitive. <code>null</code> if null input.
|
* <code>null</code> if null input.
|
||||||
* @since 2.1
|
* @since 2.1
|
||||||
*/
|
*/
|
||||||
public static Class primitiveToWrapper(Class cls) {
|
public static Class primitiveToWrapper( Class cls )
|
||||||
|
{
|
||||||
Class convertedClass = cls;
|
Class convertedClass = cls;
|
||||||
if (cls != null && cls.isPrimitive()) {
|
if( cls != null && cls.isPrimitive() )
|
||||||
|
{
|
||||||
convertedClass = ( Class ) primitiveWrapperMap.get( cls );
|
convertedClass = ( Class ) primitiveWrapperMap.get( cls );
|
||||||
}
|
}
|
||||||
return convertedClass;
|
return convertedClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Converts the specified array of primitive Class objects to an array of
|
* <p>Converts the specified array of primitive Class objects to an array of its corresponding wrapper Class
|
||||||
* its corresponding wrapper Class objects.</p>
|
* objects.</p>
|
||||||
*
|
*
|
||||||
* @param classes the class array to convert, may be null or empty
|
* @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
|
* @return an array which contains for each given class, the wrapper class or the original class if class is not a
|
||||||
* the original class if class is not a primitive. <code>null</code> if null input.
|
* primitive. <code>null</code> if null input. Empty array if an empty array passed in.
|
||||||
* Empty array if an empty array passed in.
|
|
||||||
* @since 2.1
|
* @since 2.1
|
||||||
*/
|
*/
|
||||||
public static Class[] primitivesToWrappers(Class[] classes) {
|
public static Class[] primitivesToWrappers( Class[] classes )
|
||||||
if (classes == null) {
|
{
|
||||||
|
if( classes == null )
|
||||||
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
if( classes.length == 0 )
|
||||||
if (classes.length == 0) {
|
{
|
||||||
return classes;
|
return classes;
|
||||||
}
|
}
|
||||||
|
|
||||||
Class[] convertedClasses = new Class[classes.length];
|
Class[] convertedClasses = new Class[classes.length];
|
||||||
for (int i=0; i < classes.length; i++) {
|
for( int i = 0; i < classes.length; i++ )
|
||||||
|
{
|
||||||
convertedClasses[i] = primitiveToWrapper( classes[i] );
|
convertedClasses[i] = primitiveToWrapper( classes[i] );
|
||||||
}
|
}
|
||||||
return convertedClasses;
|
return convertedClasses;
|
||||||
|
@ -489,18 +551,120 @@ public class ClassUtils {
|
||||||
|
|
||||||
// Inner class
|
// Inner class
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Is the specified class an inner class or static nested class.</p>
|
* <p>Is the specified class an inner class or static nested class.</p>
|
||||||
*
|
*
|
||||||
* @param cls the class to check, may be null
|
* @param cls the class to check, may be null
|
||||||
* @return <code>true</code> if the class is an inner or static nested class,
|
* @return <code>true</code> if the class is an inner or static nested class, false if not or <code>null</code>
|
||||||
* false if not or <code>null</code>
|
|
||||||
*/
|
*/
|
||||||
public static boolean isInnerClass(Class cls) {
|
public static boolean isInnerClass( Class cls )
|
||||||
if (cls == null) {
|
{
|
||||||
|
if( cls == null )
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return cls.getName().indexOf( INNER_CLASS_SEPARATOR_CHAR ) >= 0;
|
return cls.getName().indexOf( INNER_CLASS_SEPARATOR_CHAR ) >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the class represented by <code>className</code> using the <code>classLoader</code>. This implementation
|
||||||
|
* supports names like "<code>java.lang.String[]</code>" as well as "<code>[Ljava.lang.String;</code>".
|
||||||
|
*
|
||||||
|
* @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 <code>className</code> using the <code>classLoader</code>
|
||||||
|
* @throws ClassNotFoundException if the class is not found
|
||||||
|
*/
|
||||||
|
public static Class getClass( ClassLoader classLoader, String className, boolean initialize )
|
||||||
|
throws ClassNotFoundException
|
||||||
|
{
|
||||||
|
Class clazz;
|
||||||
|
if( abbreviationMap.containsKey( className ) )
|
||||||
|
{
|
||||||
|
clazz = Class.forName( "[" + abbreviationMap.get( className ), initialize, classLoader ).getComponentType();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
clazz = Class.forName( toProperClassName( className ), initialize, classLoader );
|
||||||
|
}
|
||||||
|
return clazz;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the (initialized) class represented by <code>className</code> using the <code>classLoader</code>. This
|
||||||
|
* implementation supports names like "<code>java.lang.String[]</code>" as well as
|
||||||
|
* "<code>[Ljava.lang.String;</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 );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the (initialized )class represented by <code>className</code> using the current thread's context class
|
||||||
|
* loader. This implementation supports names like "<code>java.lang.String[]</code>" as well as
|
||||||
|
* "<code>[Ljava.lang.String;</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( Thread.currentThread().getContextClassLoader() == null ? ClassUtils.class.getClassLoader() :
|
||||||
|
Thread.currentThread().getContextClassLoader(), className, true );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the class represented by <code>className</code> using the current thread's context class loader. This
|
||||||
|
* implementation supports names like "<code>java.lang.String[]</code>" as well as
|
||||||
|
* "<code>[Ljava.lang.String;</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
|
||||||
|
{
|
||||||
|
return getClass( Thread.currentThread().getContextClassLoader() == null ? ClassUtils.class.getClassLoader() :
|
||||||
|
Thread.currentThread().getContextClassLoader(), className, initialize );
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String toProperClassName( String className )
|
||||||
|
{
|
||||||
|
className = StringUtils.deleteWhitespace( className );
|
||||||
|
if( className == null )
|
||||||
|
{
|
||||||
|
throw new NullArgumentException( "className" );
|
||||||
|
}
|
||||||
|
else if( className.endsWith( "[]" ) )
|
||||||
|
{
|
||||||
|
final StringBuffer classNameBuffer = new StringBuffer();
|
||||||
|
while( className.endsWith( "[]" ) )
|
||||||
|
{
|
||||||
|
className = className.substring( 0, className.length() - 2 );
|
||||||
|
classNameBuffer.append( "[" );
|
||||||
|
}
|
||||||
|
final String abbreviation = ( String ) abbreviationMap.get( className );
|
||||||
|
if( abbreviation != null )
|
||||||
|
{
|
||||||
|
classNameBuffer.append( abbreviation );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
classNameBuffer.append( "L" ).append( className ).append( ";" );
|
||||||
|
}
|
||||||
|
className = classNameBuffer.toString();
|
||||||
|
|
||||||
|
}
|
||||||
|
return className;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -392,6 +392,110 @@ public class ClassUtilsTest extends TestCase {
|
||||||
assertNotSame("unmodified", noPrimitives, ClassUtils.primitivesToWrappers(noPrimitives));
|
assertNotSame("unmodified", noPrimitives, ClassUtils.primitivesToWrappers(noPrimitives));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testGetClassClassNotFound() throws Exception {
|
||||||
|
assertGetClassThrowsClassNotFound( "bool" );
|
||||||
|
assertGetClassThrowsClassNotFound( "bool[]" );
|
||||||
|
assertGetClassThrowsClassNotFound( "integer[]" );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGetClassInvalidArguments() throws Exception {
|
||||||
|
assertGetClassThrowsIllegalArgument( null );
|
||||||
|
assertGetClassThrowsClassNotFound( "[][][]" );
|
||||||
|
assertGetClassThrowsClassNotFound( "[[]" );
|
||||||
|
assertGetClassThrowsClassNotFound( "[" );
|
||||||
|
assertGetClassThrowsClassNotFound( "java.lang.String][" );
|
||||||
|
assertGetClassThrowsClassNotFound( ".hello.world" );
|
||||||
|
assertGetClassThrowsClassNotFound( "hello..world" );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testWithInterleavingWhitespace() throws ClassNotFoundException {
|
||||||
|
assertEquals( int[].class, ClassUtils.getClass( " int [ ] " ) );
|
||||||
|
assertEquals( long[].class, ClassUtils.getClass( "\rlong\t[\n]\r" ) );
|
||||||
|
assertEquals( short[].class, ClassUtils.getClass( "\tshort \t\t[]" ) );
|
||||||
|
assertEquals( byte[].class, ClassUtils.getClass( "byte[\t\t\n\r] " ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGetClassByNormalNameArrays() throws ClassNotFoundException {
|
||||||
|
assertEquals( int[].class, ClassUtils.getClass( "int[]" ) );
|
||||||
|
assertEquals( long[].class, ClassUtils.getClass( "long[]" ) );
|
||||||
|
assertEquals( short[].class, ClassUtils.getClass( "short[]" ) );
|
||||||
|
assertEquals( byte[].class, ClassUtils.getClass( "byte[]" ) );
|
||||||
|
assertEquals( char[].class, ClassUtils.getClass( "char[]" ) );
|
||||||
|
assertEquals( float[].class, ClassUtils.getClass( "float[]" ) );
|
||||||
|
assertEquals( double[].class, ClassUtils.getClass( "double[]" ) );
|
||||||
|
assertEquals( boolean[].class, ClassUtils.getClass( "boolean[]" ) );
|
||||||
|
assertEquals( String[].class, ClassUtils.getClass( "java.lang.String[]" ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGetClassByNormalNameArrays2D() throws ClassNotFoundException {
|
||||||
|
assertEquals( int[][].class, ClassUtils.getClass( "int[][]" ) );
|
||||||
|
assertEquals( long[][].class, ClassUtils.getClass( "long[][]" ) );
|
||||||
|
assertEquals( short[][].class, ClassUtils.getClass( "short[][]" ) );
|
||||||
|
assertEquals( byte[][].class, ClassUtils.getClass( "byte[][]" ) );
|
||||||
|
assertEquals( char[][].class, ClassUtils.getClass( "char[][]" ) );
|
||||||
|
assertEquals( float[][].class, ClassUtils.getClass( "float[][]" ) );
|
||||||
|
assertEquals( double[][].class, ClassUtils.getClass( "double[][]" ) );
|
||||||
|
assertEquals( boolean[][].class, ClassUtils.getClass( "boolean[][]" ) );
|
||||||
|
assertEquals( String[][].class, ClassUtils.getClass( "java.lang.String[][]" ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGetClassWithArrayClasses2D() throws Exception {
|
||||||
|
assertGetClassReturnsClass( String[][].class );
|
||||||
|
assertGetClassReturnsClass( int[][].class );
|
||||||
|
assertGetClassReturnsClass( long[][].class );
|
||||||
|
assertGetClassReturnsClass( short[][].class );
|
||||||
|
assertGetClassReturnsClass( byte[][].class );
|
||||||
|
assertGetClassReturnsClass( char[][].class );
|
||||||
|
assertGetClassReturnsClass( float[][].class );
|
||||||
|
assertGetClassReturnsClass( double[][].class );
|
||||||
|
assertGetClassReturnsClass( boolean[][].class );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGetClassWithArrayClasses() throws Exception {
|
||||||
|
assertGetClassReturnsClass( String[].class );
|
||||||
|
assertGetClassReturnsClass( int[].class );
|
||||||
|
assertGetClassReturnsClass( long[].class );
|
||||||
|
assertGetClassReturnsClass( short[].class );
|
||||||
|
assertGetClassReturnsClass( byte[].class );
|
||||||
|
assertGetClassReturnsClass( char[].class );
|
||||||
|
assertGetClassReturnsClass( float[].class );
|
||||||
|
assertGetClassReturnsClass( double[].class );
|
||||||
|
assertGetClassReturnsClass( boolean[].class );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGetClassRawPrimitives() throws ClassNotFoundException {
|
||||||
|
assertEquals( int.class, ClassUtils.getClass( "int" ) );
|
||||||
|
assertEquals( long.class, ClassUtils.getClass( "long" ) );
|
||||||
|
assertEquals( short.class, ClassUtils.getClass( "short" ) );
|
||||||
|
assertEquals( byte.class, ClassUtils.getClass( "byte" ) );
|
||||||
|
assertEquals( char.class, ClassUtils.getClass( "char" ) );
|
||||||
|
assertEquals( float.class, ClassUtils.getClass( "float" ) );
|
||||||
|
assertEquals( double.class, ClassUtils.getClass( "double" ) );
|
||||||
|
assertEquals( boolean.class, ClassUtils.getClass( "boolean" ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertGetClassReturnsClass( Class c ) throws Exception {
|
||||||
|
assertEquals( c, ClassUtils.getClass( c.getName() ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertGetClassThrowsException( String className, Class exceptionType ) throws Exception {
|
||||||
|
try {
|
||||||
|
ClassUtils.getClass( className );
|
||||||
|
fail( "ClassUtils.getClass() should fail with an exception of type " + exceptionType.getName() + " when given class name \"" + className + "\"." );
|
||||||
|
}
|
||||||
|
catch( Exception e ) {
|
||||||
|
assertTrue( exceptionType.isAssignableFrom( e.getClass() ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertGetClassThrowsIllegalArgument( String className ) throws Exception {
|
||||||
|
assertGetClassThrowsException( className, IllegalArgumentException.class );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertGetClassThrowsClassNotFound( String className ) throws Exception {
|
||||||
|
assertGetClassThrowsException( className, ClassNotFoundException.class );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance of URLClassLoader with the system class loader's URLs and a <code>null</code> parent
|
* Creates a new instance of URLClassLoader with the system class loader's URLs and a <code>null</code> parent
|
||||||
* class loader.
|
* class loader.
|
||||||
|
|
Loading…
Reference in New Issue