Class ClassMethods

java.lang.Object
org.klojang.util.ClassMethods

public final class ClassMethods extends Object
Methods for inspecting types.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Class<?>
    box(Class<?> clazz)
    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
    className(Class<?> clazz)
    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
    Returns the number of classes in the class hierarchy of the specified class.
    static String
    Returns a short description of the argument.
    static Set<Class<?>>
    Returns the entire interface hierarchy, both "horizontal" and "vertical", associated with specified class or interface.
    static List<Class<?>>
    getAncestors(Class<?> clazz)
    Returns the class hierarchy of the specified class up to, and including Object.class.
    static <T> T
    Returns zero, cast to the appropriate type, for primitive types; null for any other type.
    static boolean
    isA(Object obj, Class<?> type)
    static boolean
    isAutoBoxedAs(Class<?> classToTest, Class<?> wrapperClass)
    Returns true if first argument is a primitive type and the second argument is the corresponding wrapper class.
    static boolean
    isAutoUnboxedAs(Class<?> classToTest, Class<?> primitiveClass)
    Returns true if first argument is a primitive wrapper type and the second argument is the corresponding primitive type.
    static boolean
    Returns true if the specified type represents an array with a primitive type as its deepest-level component type.
    static boolean
    Returns true if the specified Class object represents an array of a primitive type.
    static boolean
    Returns true if the specified object is an array of a primitive type or a Class object representing an array of a primitive type.
    static boolean
    Returns true if the specified class is one of the primitive number classes.
    static boolean
    isSubtype(Class<?> class0, Class<?> class1)
    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
    isWrapper(Class<?> clazz)
    Returns true if the specified class is one of the primitive wrapper classes.
    static String
    Returns a prettified version of the simple class name.
    static String
    Returns a prettified version of an object's simple class name.
    static Class<?>
    unbox(Class<?> clazz)
    Returns the primitive type corresponding to the specified class if it is a wrapper class; else the class itself is returned.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 be null.
      Type Parameters:
      T - The type of the object
      R - The type to case it to
      Parameters:
      obj - The object whose type to cast
      Returns:
      An instance of type <R>
    • isA

      public static boolean isA(Object obj, Class<?> type)
      Parameters:
      obj - The object to test
      type - The class or interface to test the object against
      Returns:
      whether the 1st argument is an instance of the 2nd argument
    • isSubtype

      public static boolean isSubtype(Class<?> class0, Class<?> class1)
      Tests whether the first class is the same as, or a subtype of the second class. In other words, whether class0 extends or implements class1. In case you keep forgetting what "assignable from" means. Equivalent to class1.isAssignableFrom(class0).
      Parameters:
      class0 - The class or interface you are interested in
      class1 - 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:
      • CommonChecks.subtypeOf()
    • isSupertype

      public static boolean isSupertype(Class<?> class0, Class<?> class1)
      Tests whether the first class is the same as, or a supertype of the second class. In other words, whether class0 is extended or implemented by class1. Equivalent to class0.isAssignableFrom(class1).
      Parameters:
      class0 - The class or interface you are interested in
      class1 - 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:
      • CommonChecks.supertypeOf()
    • isPrimitiveNumber

      public static boolean isPrimitiveNumber(Class<?> clazz)
      Returns true if the specified class is one of the primitive number classes. Note that this does not include char.class — just like Character does not extend Number.
      Parameters:
      clazz - the class to test
      Returns:
      whether the specified class is one of the primitive number classes
    • isPrimitiveArray

      public static boolean isPrimitiveArray(Object obj)
      Returns true if the specified object is an array of a primitive type or a Class 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 a Class object representing an array of a primitive type
    • isPrimitiveArray

      public static boolean isPrimitiveArray(Class<?> clazz)
      Returns true if the specified Class object represents an array of a primitive type.
      Parameters:
      clazz - the class to test
      Returns:
      true if the specified Class object represents an array of a primitive type
    • isDeeplyPrimitiveArray

      public static boolean isDeeplyPrimitiveArray(Class<?> clazz)
      Returns true if the specified type represents an array with a primitive type as its deepest-level component type. So this method will return true not just for int[], but also for int[][], 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

      public static boolean isWrapper(Class<?> clazz)
      Returns true 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

      public static boolean isAutoUnboxedAs(Class<?> classToTest, Class<?> primitiveClass)
      Returns true 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 (like Integer.class), or the second class is not a primitive type (like int.class), this method returns false. No exception is thrown!
      Parameters:
      classToTest - the class to test
      primitiveClass - 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

      public static boolean isAutoBoxedAs(Class<?> classToTest, Class<?> wrapperClass)
      Returns true 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 (like int.class), or the second class is not a wrapper class (like Integer.class), this method returns false. No exception is thrown!
      Parameters:
      classToTest - the class to test
      wrapperClass - 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

      public static Class<?> box(Class<?> clazz)
      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

      public static Class<?> unbox(Class<?> clazz)
      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

      public static List<Class<?>> getAncestors(Class<?> clazz)
      Returns the class hierarchy of the specified class up to, and including Object.class.
      Parameters:
      clazz - the class for which to get the class hierarchy.
      Returns:
      The superclasses of the specified class.
    • countAncestors

      public static int countAncestors(Class<?> clazz)
      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

      public static Set<Class<?>> getAllInterfaces(Class<?> clazz)
      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 - the Class object for which to retrieve the interface hierarchy
      Returns:
      The interface hierarchy for the specified Class object
    • className

      public static String className(Object obj)
      Returns a prettified version of the specified object's fully-qualified class name. Equivalent to className(obj.getClass()).
      Parameters:
      obj - The object whose class name to return
      Returns:
      The class name
    • className

      public static String className(Class<?> clazz)
      Returns a prettified version of the fully-qualified class name. If the provided type is a non-array type, this method simply forwards to Class.getName(); otherwise it is equivalent to ArrayType.forClass(clazz).arrayClassName().
      Parameters:
      clazz - the class whose name to return
      Returns:
      The class name
    • simpleClassName

      public static String simpleClassName(Object obj)
      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 to ArrayType.forClass(clazz).toString().
      Parameters:
      obj - The object whose class name to return
      Returns:
      The class name
    • simpleClassName

      public static String simpleClassName(Class<?> clazz)
      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 to ArrayType.forClass(clazz).toString().
      Parameters:
      clazz - the class whose ame to return
      Returns:
      The class name
    • describe

      public static String describe(Object obj)
      Returns a short description of the argument. Unless the argument is null, 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:
    • getTypeDefault

      public static <T> T getTypeDefault(Class<T> type)
      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