Class ClassUtils
- Author:
- Yury Molchan
-
Method Summary
Modifier and TypeMethodDescriptionstatic Stringcapitalize(String name) static Class<?> commonType(Class<?> a, Class<?> b) static Stringdecapitalize(String name) static Objectstatic ClassLoadergetCurrentClassLoader(Class<?> clazz) static StringgetPackageName(Class<?> cls) Gets the package name of aClass.static StringgetShortClassName(Class<?> cls) Gets the class name minus the package name from aClass.static Stringstatic Class<?> static booleanisAssignable(Class<?> cls, Class<?> toClass) Checks if oneClasscan be assigned to a variable of anotherClass.static booleanisValidPackageName(String packageName) static Class<?> primitiveToWrapper(Class<?> cls) Converts the specified primitive Class object to its corresponding wrapper Class object.static voidstatic Stringstatic StringtoFieldName(String name) static Class<?> wrapperToPrimitive(Class<?> cls) Converts the specified wrapper class to its corresponding primitive class.
-
Method Details
-
getCurrentClassLoader
-
primitiveToWrapper
Converts the specified primitive Class object to its corresponding wrapper Class object.
- Parameters:
cls- the class to convert, may be null- Returns:
- the wrapper class for
clsorclsifclsis not a primitive.nullif null input.
-
wrapperToPrimitive
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.TYPEforInteger.class). For other classes, or if the parameter is null, the return value is null.- Parameters:
cls- the class to convert, may be null- Returns:
- the corresponding primitive type if
clsis a wrapper class, null otherwise
-
getShortClassName
Gets the class name minus the package name from a
Class.Consider using the Java 5 API
Class.getSimpleName()instead. The one known difference is that this code will return"Map.Entry"while thejava.lang.Classvariant will simply return"Entry".- Parameters:
cls- the class to get the short name for.- Returns:
- the class name without the package name or an empty string
-
getPackageName
Gets the package name of a
Class.- Parameters:
cls- the class to get the package name for, may benull.- Returns:
- the package name or an empty string
-
isAssignable
Checks if one
Classcan be assigned to a variable of anotherClass.Unlike the
Class.isAssignableFrom(java.lang.Class)method, this method takes into account widenings of primitive classes andnulls.Primitive widenings allow an int to be assigned to a long, float or double. This method returns the correct result for these cases.
Nullmay be assigned to any reference type. This method will returntrueifnullis passed in and the toClass is non-primitive.Specifically, this method tests whether the type represented by the specified
Classparameter can be converted to the type represented by thisClassobject 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 details.- Parameters:
cls- the Class to check, may be nulltoClass- the Class to try to assign into- Returns:
trueif assignment possible
-
commonType
-
capitalize
-
decapitalize
-
getter
-
setter
-
toFieldName
-
isValidPackageName
-
set
- Throws:
Exception
-
get
- Throws:
Exception
-
getType
-