Class ClassMethods
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 StringReturns a prettified version of the fully-qualified class name.static StringReturns a prettified version of the specified object's fully-qualified class name.static intcountAncestors(Class<?> type) Returns the number of classes in the class hierarchy of the specified class.static StringReturns a short description of the argument.getAllInterfaces(Class<?> type) Returns the entire interface hierarchy, both "horizontal" and "vertical", associated with specified class or interface.getAncestors(Class<?> type) Returns the class hierarchy of the specified class up to, and includingObject.class.static <T> TgetTypeDefault(Class<T> type) Returns zero, cast to the appropriate type, for primitive types;nullfor any other type.static booleanhasPrimitiveElements(Class<?> type) Returnstrueif the specified type represents an array with a primitive type as its innermost component type.static booleanAlias fortype.isInstance(obj).static booleanisAutoBoxedAs(Class<?> type, Class<?> wrapperType) Returnstrueif first argument is a primitive type and the second argument is the corresponding wrapper class.static booleanisAutoUnboxedAs(Class<?> type, Class<?> primitiveType) Returnstrueif first argument is a primitive wrapper (likeInteger) and the second argument is the corresponding primitive type (int).static booleanisPrimitiveArray(Class<?> type) Returnstrueif the specifiedClassobject represents an array of a primitive type.static booleanisPrimitiveArray(Object obj) Returnstrueif the specified object is an array of a primitive type or aClassobject representing an array of a primitive type.static booleanisPrimitiveNumber(Class<?> type) Returnstrueif the specified class is one of the primitive number classes.static booleanisSameOrSubtype(Class<?> type0, Class<?> type1) Tests whether the first class is the same as, or a subtype of the second class.static booleanisSameOrSupertype(Class<?> type0, Class<?> type1) Tests whether the first class is the same as, or a supertype of the second class.static booleanTests whether the first class is a subtype of the second class.static booleanisSupertype(Class<?> type0, Class<?> type1) Tests whether the first class is a supertype of the second class.static booleanReturnstrueif the specified class is one of the primitive wrapper classes.static StringsimpleClassName(Class<?> clazz) Returns a prettified version of the simple class name.static StringsimpleClassName(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. This method may be useful when bumping up against the limits of generics:List<CharSequence> list1 = List.of("Hello", "world"); // WON'T COMPILE: List<String> list2 = list1; List<String> list2 = ClassMethods.cast(list1);Handle with care, however, as it completely bypasses the Java type system:
String s = ClassMethods.cast(new File("/tmp/foo.txt")); // Compiles! But will throw a ClassCastException at runtime.- 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
-
isSameOrSubtype
Tests whether the first class is the same as, or a subtype of the second class. In other words, whethertype0extends or implementstype1.- Parameters:
type0- the class or interface you are interested intype1- the class or interface to compare it against- Returns:
trueif the first class is the same, or a subtype of the second class;falseotherwise- See Also:
-
isSubtype
Tests whether the first class is a subtype of the second class. In other words, whetherclass0extends or implementsclass1.- Parameters:
type0- the class or interface you are interested intype1- the class or interface to compare it against- Returns:
trueif the first class is the same, or a subtype of the second class;falseotherwise- See Also:
-
isSameOrSupertype
Tests whether the first class is the same as, or a supertype of the second class. In other words, whethertype0is extended or implemented bytype1.- Parameters:
type0- the class or interface you are interested intype1- the class or interface to compare it against- Returns:
trueif the first class is a supertype of the second class;falseotherwise- See Also:
-
isSupertype
Tests whether the first class is a supertype of the second class. In other words, whethertype0is extended or implemented bytype1.- Parameters:
type0- the class or interface you are interested intype1- the class or interface to compare it against- Returns:
trueif the first class is a supertype of the second class;falseotherwise- See Also:
-
isPrimitiveNumber
-
isPrimitiveArray
Returnstrueif the specified object is an array of a primitive type or aClassobject representing an array of a primitive type.- Parameters:
obj- the object to test- Returns:
trueif the specified object is an array of a primitive type, or aClassobject representing an array of a primitive type
-
isPrimitiveArray
Returnstrueif the specifiedClassobject represents an array of a primitive type.- Parameters:
type- the class to test- Returns:
trueif the specifiedClassobject represents an array of a primitive type
-
hasPrimitiveElements
Returnstrueif the specified type represents an array with a primitive type as its innermost component type. So this method will returntruenot just forint[], but also forint[][],int[][][], etc.- Parameters:
type- the class to test- Returns:
trueif the specified type represents an array with a primitive type as its deepest-level component type- See Also:
-
isWrapper
Returnstrueif the specified class is one of the primitive wrapper classes.- Parameters:
type- the class to test- Returns:
trueif the specified class is one of the primitive wrapper classes
-
isAutoUnboxedAs
Returnstrueif first argument is a primitive wrapper (likeInteger) and the second argument is the corresponding primitive type (int). If the first argument is not a wrapper class, this method returnsfalse.- Parameters:
type- the class to testprimitiveType- 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
Returnstrueif 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), this method returnsfalse.- Parameters:
type- the class to testwrapperType- 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
-
unbox
-
getAncestors
-
countAncestors
Returns the number of classes in the class hierarchy of the specified class.- Parameters:
type- 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:
type- theClassobject for which to retrieve the interface hierarchy- Returns:
- The interface hierarchy for the specified
Classobject
-
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:
type- 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
Classobject, 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;nullfor 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
-