public final class ClassUtil
extends java.lang.Object
Constructor and Description |
---|
ClassUtil() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
canBeABeanType(java.lang.Class<?> type) |
static void |
checkAndFixAccess(java.lang.reflect.Member member)
Method called to check if we can use the passed method or constructor
(wrt access restriction -- public methods can be called, others
usually not); and if not, if there is a work-around for
the problem.
|
static <T> T |
createInstance(java.lang.Class<T> cls,
boolean canFixAccess)
Method that can be called to try to create an instantiate of
specified type.
|
static java.lang.Object |
defaultValue(java.lang.Class<?> cls)
Helper method used to get default value for wrappers used for primitive types
(0 for Integer etc)
|
static java.lang.Class<?> |
findClass(java.lang.String className) |
static <T> java.lang.reflect.Constructor<T> |
findConstructor(java.lang.Class<T> cls,
boolean canFixAccess) |
static java.lang.Class<? extends java.lang.Enum<?>> |
findEnumType(java.lang.Class<?> cls)
Helper method that can be used to dynamically figure out formal
enumeration type (class) for given class of an enumeration value.
|
static java.lang.Class<? extends java.lang.Enum<?>> |
findEnumType(java.lang.Enum<?> en)
Helper method that can be used to dynamically figure out formal
enumeration type (class) for given enumeration.
|
static java.lang.Class<? extends java.lang.Enum<?>> |
findEnumType(java.util.EnumMap<?,?> m)
Helper method that can be used to dynamically figure out
enumeration type of given
EnumSet , without having
access to its declaration. |
static java.lang.Class<? extends java.lang.Enum<?>> |
findEnumType(java.util.EnumSet<?> s)
Helper method that can be used to dynamically figure out
enumeration type of given
EnumSet , without having
access to its declaration. |
static java.util.List<java.lang.Class<?>> |
findSuperTypes(java.lang.Class<?> cls,
java.lang.Class<?> endBefore)
Method that will find all sub-classes and implemented interfaces
of a given class or interface.
|
static java.util.List<java.lang.Class<?>> |
findSuperTypes(java.lang.Class<?> cls,
java.lang.Class<?> endBefore,
java.util.List<java.lang.Class<?>> result) |
static java.lang.String |
getClassDescription(java.lang.Object classOrInstance)
Helper method used to construct appropriate description
when passed either type (Class) or an instance; in latter
case, class of instance is to be used.
|
static java.lang.Class<?> |
getOuterClass(java.lang.Class<?> type)
Method for finding enclosing class for non-static inner classes
|
static java.lang.Throwable |
getRootCause(java.lang.Throwable t)
Method that can be used to find the "root cause", innermost
of chained (wrapped) exceptions.
|
static boolean |
hasGetterSignature(java.lang.reflect.Method m) |
static boolean |
isCollectionMapOrArray(java.lang.Class<?> type) |
static boolean |
isConcrete(java.lang.Class<?> type)
Helper method that checks if given class is a concrete one;
that is, not an interface or abstract class.
|
static boolean |
isConcrete(java.lang.reflect.Member member) |
static java.lang.String |
isLocalType(java.lang.Class<?> type)
Deprecated.
since 1.9, use variant that takes second argument
|
static java.lang.String |
isLocalType(java.lang.Class<?> type,
boolean allowNonStatic) |
static boolean |
isProxyType(java.lang.Class<?> type)
Helper method used to weed out dynamic Proxy types; types that do
not expose concrete method API that we could use to figure out
automatic Bean (property) based serialization.
|
static void |
throwAsIAE(java.lang.Throwable t)
Method that will wrap 't' as an
IllegalArgumentException if it
is a checked exception; otherwise (runtime exception or error) throw as is |
static void |
throwAsIAE(java.lang.Throwable t,
java.lang.String msg)
Method that will wrap 't' as an
IllegalArgumentException (and with
specified message) if it
is a checked exception; otherwise (runtime exception or error) throw as is |
static void |
throwRootCause(java.lang.Throwable t)
Method that will unwrap root causes of given Throwable, and throw
the innermost
Exception or Error as is. |
static void |
unwrapAndThrowAsIAE(java.lang.Throwable t)
Method that will locate the innermost exception for given Throwable;
and then wrap it as an
IllegalArgumentException if it
is a checked exception; otherwise (runtime exception or error) throw as is |
static void |
unwrapAndThrowAsIAE(java.lang.Throwable t,
java.lang.String msg)
Method that will locate the innermost exception for given Throwable;
and then wrap it as an
IllegalArgumentException if it
is a checked exception; otherwise (runtime exception or error) throw as is |
static java.lang.Class<?> |
wrapperType(java.lang.Class<?> primitiveType)
Helper method for finding wrapper type for given primitive type (why isn't
there one in JDK?)
|
public static java.util.List<java.lang.Class<?>> findSuperTypes(java.lang.Class<?> cls, java.lang.Class<?> endBefore)
Object.class
is not included in the list
regardless of whether endBefore
argument is defined or not.endBefore
- Super-type to NOT include in results, if any; when
encountered, will be ignored (and no super types are checked).public static java.util.List<java.lang.Class<?>> findSuperTypes(java.lang.Class<?> cls, java.lang.Class<?> endBefore, java.util.List<java.lang.Class<?>> result)
public static java.lang.String canBeABeanType(java.lang.Class<?> type)
@Deprecated public static java.lang.String isLocalType(java.lang.Class<?> type)
public static java.lang.String isLocalType(java.lang.Class<?> type, boolean allowNonStatic)
public static java.lang.Class<?> getOuterClass(java.lang.Class<?> type)
public static boolean isProxyType(java.lang.Class<?> type)
public static boolean isConcrete(java.lang.Class<?> type)
public static boolean isConcrete(java.lang.reflect.Member member)
public static boolean isCollectionMapOrArray(java.lang.Class<?> type)
public static java.lang.String getClassDescription(java.lang.Object classOrInstance)
public static java.lang.Class<?> findClass(java.lang.String className) throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundException
public static boolean hasGetterSignature(java.lang.reflect.Method m)
public static java.lang.Throwable getRootCause(java.lang.Throwable t)
public static void throwRootCause(java.lang.Throwable t) throws java.lang.Exception
Exception
or Error
as is.
This is useful in cases where mandatory wrapping is added, which
is often done by Reflection API.java.lang.Exception
public static void throwAsIAE(java.lang.Throwable t)
IllegalArgumentException
if it
is a checked exception; otherwise (runtime exception or error) throw as ispublic static void throwAsIAE(java.lang.Throwable t, java.lang.String msg)
IllegalArgumentException
(and with
specified message) if it
is a checked exception; otherwise (runtime exception or error) throw as ispublic static void unwrapAndThrowAsIAE(java.lang.Throwable t)
IllegalArgumentException
if it
is a checked exception; otherwise (runtime exception or error) throw as ispublic static void unwrapAndThrowAsIAE(java.lang.Throwable t, java.lang.String msg)
IllegalArgumentException
if it
is a checked exception; otherwise (runtime exception or error) throw as ispublic static <T> T createInstance(java.lang.Class<T> cls, boolean canFixAccess) throws java.lang.IllegalArgumentException
canFixAccess
- Whether it is possible to try to change access
rights of the default constructor (in case it is not publicly
accessible) or not.java.lang.IllegalArgumentException
- If instantiation fails for any reason;
except for cases where constructor throws an unchecked exception
(which will be passed as is)public static <T> java.lang.reflect.Constructor<T> findConstructor(java.lang.Class<T> cls, boolean canFixAccess) throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
public static java.lang.Object defaultValue(java.lang.Class<?> cls)
public static java.lang.Class<?> wrapperType(java.lang.Class<?> primitiveType)
public static void checkAndFixAccess(java.lang.reflect.Member member)
public static java.lang.Class<? extends java.lang.Enum<?>> findEnumType(java.util.EnumSet<?> s)
EnumSet
, without having
access to its declaration.
Code is needed to work around design flaw in JDK.public static java.lang.Class<? extends java.lang.Enum<?>> findEnumType(java.util.EnumMap<?,?> m)
EnumSet
, without having
access to its declaration.
Code is needed to work around design flaw in JDK.public static java.lang.Class<? extends java.lang.Enum<?>> findEnumType(java.lang.Enum<?> en)
public static java.lang.Class<? extends java.lang.Enum<?>> findEnumType(java.lang.Class<?> cls)