- java.lang.Object
-
- org.github.gestalt.config.utils.ClassUtils
-
public final class ClassUtils extends java.lang.ObjectOperates on classes without using reflection.This class handles invalid
nullinputs as best it can. Each method documents its behavior in more detail.The notion of a
canonical nameincludes the human readable name for the type, for exampleint[]. The non-canonical method variants work with the JVM names, such as[I.- Since:
- 2.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanisAssignable(java.lang.Class<?> cls, java.lang.Class<?> toClass)Checks if oneClasscan be assigned to a variable of anotherClass.static booleanisAssignable(java.lang.Class<?> cls, java.lang.Class<?> toClass, boolean autoboxing)Checks if oneClasscan be assigned to a variable of anotherClass.static booleanisPrimitiveOrWrapper(java.lang.Class<?> type)Returns whether the giventypeis a primitive or primitive wrapper (Boolean,Byte,Character,Short,Integer,Long,Double,Float).static booleanisPrimitiveWrapper(java.lang.Class<?> type)Returns whether the giventypeis a primitive wrapper (Boolean,Byte,Character,Short,Integer,Long,Double,Float).static java.lang.Class<?>primitiveToWrapper(java.lang.Class<?> cls)Converts the specified primitive Class object to its corresponding wrapper Class object.static java.lang.Class<?>wrapperToPrimitive(java.lang.Class<?> cls)Converts the specified wrapper class to its corresponding primitive class.
-
-
-
Method Detail
-
isAssignable
public static boolean isAssignable(java.lang.Class<?> cls, java.lang.Class<?> toClass)Checks if oneClasscan 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.Since Lang 3.0, this method will default behavior for calculating assignability between primitive and wrapper types corresponding to the running Java version; i.e. autoboxing will be the default behavior in VMs running Java versions > 1.5.
- Parameters:
cls- the Class to check, may be nulltoClass- the Class to try to assign into, returns false if null- Returns:
trueif assignment possible
-
isAssignable
public static boolean isAssignable(java.lang.Class<?> cls, java.lang.Class<?> toClass, boolean autoboxing)Checks if oneClasscan 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 false if nullautoboxing- whether to use implicit autoboxing/unboxing between primitives and wrappers- Returns:
trueif assignment possible
-
primitiveToWrapper
public static java.lang.Class<?> primitiveToWrapper(java.lang.Class<?> cls)
Converts the specified primitive Class object to its corresponding wrapper Class object.NOTE: From v2.2, this method handles
Void.TYPE, returningVoid.TYPE.- Parameters:
cls- the class to convert, may be null- Returns:
- the wrapper class for
clsorclsifclsis not a primitive.nullif null input. - Since:
- 2.1
-
wrapperToPrimitive
public static java.lang.Class<?> wrapperToPrimitive(java.lang.Class<?> cls)
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 - Since:
- 2.4
- See Also:
primitiveToWrapper(Class)
-
isPrimitiveOrWrapper
public static boolean isPrimitiveOrWrapper(java.lang.Class<?> type)
Returns whether the giventypeis a primitive or primitive wrapper (Boolean,Byte,Character,Short,Integer,Long,Double,Float).- Parameters:
type- The class to query or null.- Returns:
- true if the given
typeis a primitive or primitive wrapper (Boolean,Byte,Character,Short,Integer,Long,Double,Float). - Since:
- 3.1
-
isPrimitiveWrapper
public static boolean isPrimitiveWrapper(java.lang.Class<?> type)
Returns whether the giventypeis a primitive wrapper (Boolean,Byte,Character,Short,Integer,Long,Double,Float).- Parameters:
type- The class to query or null.- Returns:
- true if the given
typeis a primitive wrapper (Boolean,Byte,Character,Short,Integer,Long,Double,Float). - Since:
- 3.1
-
-