java.lang.Object
org.klojang.util.ClassMethods
Methods for inspecting types.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Class
<?> Returns the wrapper class corresponding to the specified class if it is a primitive type; else the class itself is returned.static <T,
R> R cast
(T obj) Performs a brute-force cast to<R>
of the specified object.static String
Returns a prettified version of the fully-qualified class name.static String
Returns a prettified version of the specified object's fully-qualified class name.static int
countAncestors
(Class<?> clazz) Returns the number of classes in the class hierarchy of the specified class.static String
Returns a short description of the argument.getAllInterfaces
(Class<?> clazz) Returns the entire interface hierarchy, both "horizontal" and "vertical", associated with specified class or interface.getAncestors
(Class<?> clazz) Returns the class hierarchy of the specified class up to, and includingObject.class
.static <T> T
getTypeDefault
(Class<T> type) Returns zero, cast to the appropriate type, for primitive types;null
for any other type.static boolean
Alias fortype.isInstance(obj)
.static boolean
isAutoBoxedAs
(Class<?> classToTest, Class<?> wrapperClass) Returnstrue
if first argument is a primitive type and the second argument is the corresponding wrapper class.static boolean
isAutoUnboxedAs
(Class<?> classToTest, Class<?> primitiveClass) Returnstrue
if first argument is a primitive wrapper type and the second argument is the corresponding primitive type.static boolean
isDeeplyPrimitiveArray
(Class<?> clazz) Returnstrue
if the specified type represents an array with a primitive type as its deepest-level component type.static boolean
isPrimitiveArray
(Class<?> clazz) Returnstrue
if the specifiedClass
object represents an array of a primitive type.static boolean
isPrimitiveArray
(Object obj) Returnstrue
if the specified object is an array of a primitive type or aClass
object representing an array of a primitive type.static boolean
isPrimitiveNumber
(Class<?> clazz) Returnstrue
if the specified class is one of the primitive number classes.static boolean
Tests whether the first class is the same as, or a subtype of the second class.static boolean
isSupertype
(Class<?> class0, Class<?> class1) Tests whether the first class is the same as, or a supertype of the second class.static boolean
Returnstrue
if the specified class is one of the primitive wrapper classes.static String
simpleClassName
(Class<?> clazz) Returns a prettified version of the simple class name.static String
simpleClassName
(Object obj) Returns a prettified version of an object's simple class name.static Class
<?> Returns the primitive type corresponding to the specified class if it is a wrapper class; else the class itself is returned.
-
Method Details
-
cast
public static <T,R> R cast(T obj) Performs a brute-force cast to<R>
of the specified object. Handle with care as it lets you completely bypass the Java type system. The argument is allowed to benull
.- Type Parameters:
T
- The type of the objectR
- The type to case it to- Parameters:
obj
- The object whose type to cast- Returns:
- An instance of type
<R>
-
isA
Alias fortype.isInstance(obj)
.- Parameters:
obj
- The object to testtype
- The class or interface to test the object against- Returns:
- whether the 1st argument is an instance of the 2nd argument
-
isSubtype
Tests whether the first class is the same as, or a subtype of the second class. In other words, whetherclass0
extends or implementsclass1
. In case you keep forgetting what "assignable from" means. Equivalent toclass1.isAssignableFrom(class0)
.- Parameters:
class0
- The class or interface you are interested inclass1
- The class or interface to compare it against- Returns:
true
if the first class is a subtype of the second class;false
otherwise- See Also:
-
isSupertype
Tests whether the first class is the same as, or a supertype of the second class. In other words, whetherclass0
is extended or implemented byclass1
. Equivalent toclass0.isAssignableFrom(class1)
.- Parameters:
class0
- The class or interface you are interested inclass1
- The class or interface to compare it against- Returns:
true
if the first class is a supertype of the second class;false
otherwise- See Also:
-
isPrimitiveNumber
Returnstrue
if the specified class is one of the primitive number classes. Note that this does not includechar.class
— just likeCharacter
does not extendNumber
.- Parameters:
clazz
- the class to test- Returns:
- whether the specified class is one of the primitive number classes
-
isPrimitiveArray
Returnstrue
if the specified object is an array of a primitive type or aClass
object representing an array of a primitive type.- Parameters:
obj
- The object to test- Returns:
true
if the specified object is an array of a primitive type, or aClass
object representing an array of a primitive type
-
isPrimitiveArray
Returnstrue
if the specifiedClass
object represents an array of a primitive type.- Parameters:
clazz
- the class to test- Returns:
true
if the specifiedClass
object represents an array of a primitive type
-
isDeeplyPrimitiveArray
Returnstrue
if the specified type represents an array with a primitive type as its deepest-level component type. So this method will returntrue
not just forint[]
, but also forint[][]
,int[][][]
, etc.- Parameters:
clazz
- the class to test- Returns:
true
if the specified type represents an array with a primitive type as its deepest-level component type- See Also:
-
isWrapper
Returnstrue
if the specified class is one of the primitive wrapper classes.- Parameters:
clazz
- the class to test- Returns:
true
if the specified class is one of the primitive wrapper classes
-
isAutoUnboxedAs
Returnstrue
if first argument is a primitive wrapper type and the second argument is the corresponding primitive type. If the first class is not a wrapper class (likeInteger.class
), or the second class is not a primitive type (likeint.class
), this method returnsfalse
. No exception is thrown!- Parameters:
classToTest
- the class to testprimitiveClass
- the class to compare it with (supposedly, but not necessarily, a primitive type)- Returns:
- whether instances of the first class will be auto-unboxed into instances of the second class
-
isAutoBoxedAs
Returnstrue
if first argument is a primitive type and the second argument is the corresponding wrapper class. If the first class is not a primitive type (likeint.class
), or the second class is not a wrapper class (likeInteger.class
), this method returnsfalse
. No exception is thrown!- Parameters:
classToTest
- the class to testwrapperClass
- the class to compare it with (supposedly, but not necessarily, a primitive wrapper type)- Returns:
- whether instances of the first class will be auto-unboxed into instances of the second class
-
box
Returns the wrapper class corresponding to the specified class if it is a primitive type; else the class itself is returned.- Parameters:
clazz
- the (primitive) class- Returns:
- The corresponding wrapper class
-
unbox
Returns the primitive type corresponding to the specified class if it is a wrapper class; else the class itself is returned.- Parameters:
clazz
- the (wrapper) class- Returns:
- The corresponding primitive class
-
getAncestors
Returns the class hierarchy of the specified class up to, and includingObject.class
.- Parameters:
clazz
- the class for which to get the class hierarchy.- Returns:
- The superclasses of the specified class.
-
countAncestors
Returns the number of classes in the class hierarchy of the specified class.- Parameters:
clazz
- the class for which to count the number of classes in its class hierarchy- Returns:
- the number of classes in the class hierarchy of the specified class
-
getAllInterfaces
Returns the entire interface hierarchy, both "horizontal" and "vertical", associated with specified class or interface. Returns an empty set if the argument is a top-level interface, or if the class is a regular class that does not implement any interface (directly, or indirectly via its superclass).- Parameters:
clazz
- theClass
object for which to retrieve the interface hierarchy- Returns:
- The interface hierarchy for the specified
Class
object
-
className
Returns a prettified version of the specified object's fully-qualified class name. Equivalent toclassName(obj.getClass())
.- Parameters:
obj
- The object whose class name to return- Returns:
- The class name
-
className
Returns a prettified version of the fully-qualified class name. If the provided type is a non-array type, this method simply forwards toClass.getName()
; otherwise it is equivalent toArrayType.forClass(clazz).arrayClassName()
.- Parameters:
clazz
- the class whose name to return- Returns:
- The class name
-
simpleClassName
Returns a prettified version of an object's simple class name. If the type is a non-array type this method simply forwards to Class.getSimpleName(); otherwise it is equivalent toArrayType.forClass(clazz).toString()
.- Parameters:
obj
- The object whose class name to return- Returns:
- The class name
-
simpleClassName
Returns a prettified version of the simple class name. If the provided type is a non-array type this method simply forwards to Class.getSimpleName(); otherwise it is equivalent toArrayType.forClass(clazz).toString()
.- Parameters:
clazz
- the class whose ame to return- Returns:
- The class name
-
describe
Returns a short description of the argument. Unless the argument isnull
, the description will at least contain the simple class name of the argument.- If the argument is
null
, the string "null" is returned. - If the argument is a
Collection
, the returned string will contain the collection's simple class name and its size. For example: "ArrayList[113]
" - If the argument is a
Map
, the returned string will contain the map's simple class name and its size. For example: "TreeMap[97]
" - If the argument is an array, the returned string will contain the simple
class name of the array's innermost
component type and its length. For
example: "
String[42][][]
" - If the argument is a
Class
object, the returned string will look like this: "FileOutputStream.class
" (with ".class" appended to the simple class name) - Otherwise, the simple class name of the argument is returned.
- Parameters:
obj
- the object to describe- Returns:
- a description of the object
- See Also:
- If the argument is
-
getTypeDefault
Returns zero, cast to the appropriate type, for primitive types;null
for any other type.- Type Parameters:
T
- The type of the class- Parameters:
type
- The class for which to retrieve the default value- Returns:
- The default value
-