public final class ClassUtil extends Object
Modifier and Type | Class and Description |
---|---|
static class |
ClassUtil.Ctor
Value class used for caching Constructor declarations; used because
caching done by JDK appears to be somewhat inefficient for some use cases.
|
Constructor and Description |
---|
ClassUtil() |
Modifier and Type | Method and Description |
---|---|
static String |
backticked(String text)
Returns either `text` or [null].
|
static String |
canBeABeanType(Class<?> type) |
static void |
checkAndFixAccess(Member member)
Deprecated.
Since 2.7 call variant that takes boolean flag.
|
static void |
checkAndFixAccess(Member member,
boolean force)
Method that is called if a
Member may need forced access,
to force a field, method or constructor to be accessible: this
is done by calling AccessibleObject.setAccessible(boolean) . |
static String |
classNameOf(Object inst)
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 Class<?> |
classOf(Object inst) |
static void |
closeOnFailAndThrowAsIOE(JsonGenerator g,
Closeable toClose,
Exception fail)
Helper method that encapsulate logic in trying to close given
Closeable
in case of failure; useful mostly in forcing flush()ing as otherwise
error conditions tend to be hard to diagnose. |
static void |
closeOnFailAndThrowAsIOE(JsonGenerator g,
Exception fail)
Helper method that encapsulate logic in trying to close output generator
in case of failure; useful mostly in forcing flush()ing as otherwise
error conditions tend to be hard to diagnose.
|
static <T> T |
createInstance(Class<T> cls,
boolean canFixAccess)
Method that can be called to try to create an instantiate of
specified type.
|
static Object |
defaultValue(Class<?> cls)
Helper method used to get default value for wrappers used for primitive types
(0 for Integer etc)
|
static <T> Iterator<T> |
emptyIterator() |
static String |
exceptionMessage(Throwable t)
Helper method that returns
Throwable.getMessage() for all other exceptions
except for JsonProcessingException , for which getOriginalMessage() is
returned instead. |
static Annotation[] |
findClassAnnotations(Class<?> cls) |
static <T> Constructor<T> |
findConstructor(Class<T> cls,
boolean forceAccess) |
static Class<? extends Enum<?>> |
findEnumType(Class<?> cls)
Helper method that can be used to dynamically figure out formal
enumeration type (class) for given class of an enumeration value.
|
static Class<? extends Enum<?>> |
findEnumType(Enum<?> en)
Helper method that can be used to dynamically figure out formal
enumeration type (class) for given enumeration.
|
static Class<? extends Enum<?>> |
findEnumType(EnumMap<?,?> m)
Helper method that can be used to dynamically figure out
enumeration type of given
EnumSet , without having
access to its declaration. |
static Class<? extends Enum<?>> |
findEnumType(EnumSet<?> s)
Helper method that can be used to dynamically figure out
enumeration type of given
EnumSet , without having
access to its declaration. |
static <T extends Annotation> |
findFirstAnnotatedEnumValue(Class<Enum<?>> enumClass,
Class<T> annotationClass)
A method that will look for the first Enum value annotated with the given Annotation.
|
static List<Class<?>> |
findRawSuperTypes(Class<?> cls,
Class<?> endBefore,
boolean addClassItself) |
static List<Class<?>> |
findSuperClasses(Class<?> cls,
Class<?> endBefore,
boolean addClassItself)
Method for finding all super classes (but not super interfaces) of given class,
starting with the immediate super class and ending in the most distant one.
|
static List<Class<?>> |
findSuperTypes(Class<?> cls,
Class<?> endBefore)
Deprecated.
|
static List<Class<?>> |
findSuperTypes(Class<?> cls,
Class<?> endBefore,
List<Class<?>> result)
Deprecated.
|
static List<JavaType> |
findSuperTypes(JavaType type,
Class<?> endBefore,
boolean addClassItself)
Method that will find all sub-classes and implemented interfaces
of a given class or interface.
|
static String |
getClassDescription(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 Method[] |
getClassMethods(Class<?> cls)
Helper method that gets methods declared in given class; usually a simple thing,
but sometimes (as per [databind#785]) more complicated, depending on classloader
setup.
|
static ClassUtil.Ctor[] |
getConstructors(Class<?> cls) |
static Field[] |
getDeclaredFields(Class<?> cls) |
static Method[] |
getDeclaredMethods(Class<?> cls) |
static Class<?> |
getDeclaringClass(Class<?> cls) |
static Class<?> |
getEnclosingClass(Class<?> cls) |
static Type[] |
getGenericInterfaces(Class<?> cls) |
static Type |
getGenericSuperclass(Class<?> cls) |
static Class<?> |
getOuterClass(Class<?> type)
Method for finding enclosing class for non-static inner classes
|
static String |
getPackageName(Class<?> cls) |
static Throwable |
getRootCause(Throwable t)
Method that can be used to find the "root cause", innermost
of chained (wrapped) exceptions.
|
static boolean |
hasClass(Object inst,
Class<?> raw) |
static boolean |
hasEnclosingMethod(Class<?> cls) |
static boolean |
hasGetterSignature(Method m)
Deprecated.
Since 2.6 not used; may be removed before 3.x
|
static boolean |
isBogusClass(Class<?> cls) |
static boolean |
isCollectionMapOrArray(Class<?> type) |
static boolean |
isConcrete(Class<?> type)
Helper method that checks if given class is a concrete one;
that is, not an interface or abstract class.
|
static boolean |
isConcrete(Member member) |
static boolean |
isJacksonStdImpl(Class<?> implClass) |
static boolean |
isJacksonStdImpl(Object impl)
Method that can be called to determine if given Object is the default
implementation Jackson uses; as opposed to a custom serializer installed by
a module or calling application.
|
static String |
isLocalType(Class<?> type,
boolean allowNonStatic) |
static boolean |
isNonStaticInnerClass(Class<?> cls) |
static boolean |
isObjectOrPrimitive(Class<?> cls) |
static boolean |
isProxyType(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 String |
nameOf(Class<?> cls)
Returns either `cls.getName()` (if `cls` not null),
or "[null]" if `cls` is null.
|
static String |
nameOf(Named named)
Returns either backtick-quoted `named.getName()` (if `named` not null),
or "[null]" if `named` is null.
|
static <T> T |
nonNull(T valueOrNull,
T defaultValue) |
static String |
nonNullString(String str) |
static String |
nullOrToString(Object value) |
static Class<?> |
primitiveType(Class<?> type)
Method that can be used to find primitive type for given class if (but only if)
it is either wrapper type or primitive type; returns `null` if type is neither.
|
static String |
quotedOr(Object str,
String forNull)
Returns either quoted value (with double-quotes) -- if argument non-null
String -- or String NULL (no quotes) (if null).
|
static Class<?> |
rawClass(JavaType t) |
static void |
throwAsIAE(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(Throwable t,
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 <T> T |
throwAsMappingException(DeserializationContext ctxt,
IOException e0) |
static Throwable |
throwIfError(Throwable t)
Helper method that will check if argument is an
Error ,
and if so, (re)throw it; otherwise just return |
static Throwable |
throwIfIOE(Throwable t)
Helper method that will check if argument is an
IOException ,
and if so, (re)throw it; otherwise just return |
static Throwable |
throwIfRTE(Throwable t)
Helper method that will check if argument is an
RuntimeException ,
and if so, (re)throw it; otherwise just return |
static Throwable |
throwRootCauseIfIOE(Throwable t)
Method that works like by calling
getRootCause(java.lang.Throwable) and then
either throwing it (if instanceof IOException ), or
return. |
static void |
unwrapAndThrowAsIAE(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(Throwable t,
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 void |
verifyMustOverride(Class<?> expType,
Object instance,
String method) |
static Class<?> |
wrapperType(Class<?> primitiveType)
Helper method for finding wrapper type for given primitive type (why isn't
there one in JDK?)
|
public static <T> Iterator<T> emptyIterator()
public static List<JavaType> findSuperTypes(JavaType type, Class<?> endBefore, boolean addClassItself)
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 List<Class<?>> findRawSuperTypes(Class<?> cls, Class<?> endBefore, boolean addClassItself)
public static List<Class<?>> findSuperClasses(Class<?> cls, Class<?> endBefore, boolean addClassItself)
addClassItself
is true.@Deprecated public static List<Class<?>> findSuperTypes(Class<?> cls, Class<?> endBefore)
@Deprecated public static List<Class<?>> findSuperTypes(Class<?> cls, Class<?> endBefore, List<Class<?>> result)
public static String canBeABeanType(Class<?> type)
public static Class<?> getOuterClass(Class<?> type)
public static boolean isProxyType(Class<?> type)
public static boolean isConcrete(Class<?> type)
public static boolean isConcrete(Member member)
public static boolean isCollectionMapOrArray(Class<?> type)
public static boolean isBogusClass(Class<?> cls)
public static boolean isNonStaticInnerClass(Class<?> cls)
public static boolean isObjectOrPrimitive(Class<?> cls)
public static void verifyMustOverride(Class<?> expType, Object instance, String method)
@Deprecated public static boolean hasGetterSignature(Method m)
public static Throwable throwIfError(Throwable t)
Error
,
and if so, (re)throw it; otherwise just returnpublic static Throwable throwIfRTE(Throwable t)
RuntimeException
,
and if so, (re)throw it; otherwise just returnpublic static Throwable throwIfIOE(Throwable t) throws IOException
IOException
,
and if so, (re)throw it; otherwise just returnIOException
public static Throwable getRootCause(Throwable t)
public static Throwable throwRootCauseIfIOE(Throwable t) throws IOException
getRootCause(java.lang.Throwable)
and then
either throwing it (if instanceof IOException
), or
return.IOException
public static void throwAsIAE(Throwable t)
IllegalArgumentException
if it
is a checked exception; otherwise (runtime exception or error) throw as ispublic static void throwAsIAE(Throwable t, String msg)
IllegalArgumentException
(and with
specified message) if it
is a checked exception; otherwise (runtime exception or error) throw as ispublic static <T> T throwAsMappingException(DeserializationContext ctxt, IOException e0) throws JsonMappingException
JsonMappingException
public static void unwrapAndThrowAsIAE(Throwable t)
IllegalArgumentException
if it
is a checked exception; otherwise (runtime exception or error) throw as ispublic static void unwrapAndThrowAsIAE(Throwable t, String msg)
IllegalArgumentException
if it
is a checked exception; otherwise (runtime exception or error) throw as ispublic static void closeOnFailAndThrowAsIOE(JsonGenerator g, Exception fail) throws IOException
IOException
public static void closeOnFailAndThrowAsIOE(JsonGenerator g, Closeable toClose, Exception fail) throws IOException
Closeable
in case of failure; useful mostly in forcing flush()ing as otherwise
error conditions tend to be hard to diagnose. However, it is often the
case that output state may be corrupt so we need to be prepared for
secondary exception without masking original one.IOException
public static <T> T createInstance(Class<T> cls, boolean canFixAccess) throws 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.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> Constructor<T> findConstructor(Class<T> cls, boolean forceAccess) throws IllegalArgumentException
IllegalArgumentException
public static <T> T nonNull(T valueOrNull, T defaultValue)
public static String quotedOr(Object str, String forNull)
public static String getClassDescription(Object classOrInstance)
public static String classNameOf(Object inst)
public static String nameOf(Class<?> cls)
public static String nameOf(Named named)
public static String exceptionMessage(Throwable t)
Throwable.getMessage()
for all other exceptions
except for JsonProcessingException
, for which getOriginalMessage()
is
returned instead.
Method is used to avoid accidentally including trailing location information twice
in message when wrapping exceptions.public static Object defaultValue(Class<?> cls)
public static Class<?> wrapperType(Class<?> primitiveType)
public static Class<?> primitiveType(Class<?> type)
@Deprecated public static void checkAndFixAccess(Member member)
checkAndFixAccess(member, false);
public static void checkAndFixAccess(Member member, boolean force)
Member
may need forced access,
to force a field, method or constructor to be accessible: this
is done by calling AccessibleObject.setAccessible(boolean)
.member
- Accessor to call setAccessible()
on.force
- Whether to always try to make accessor accessible (true),
or only if needed as per access rights (false)public static Class<? extends Enum<?>> findEnumType(EnumSet<?> s)
EnumSet
, without having
access to its declaration.
Code is needed to work around design flaw in JDK.public static Class<? extends Enum<?>> findEnumType(EnumMap<?,?> m)
EnumSet
, without having
access to its declaration.
Code is needed to work around design flaw in JDK.public static Class<? extends Enum<?>> findEnumType(Enum<?> en)
public static Class<? extends Enum<?>> findEnumType(Class<?> cls)
public static <T extends Annotation> Enum<?> findFirstAnnotatedEnumValue(Class<Enum<?>> enumClass, Class<T> annotationClass)
If there's more than one value annotated, the first one found will be returned. Which one exactly is used is undetermined.
enumClass
- The Enum class to scan for a value with the given annotationannotationClass
- The annotation to look for.null
if none is found.IllegalArgumentException
- if there's a reflection issue accessing the Enumpublic static boolean isJacksonStdImpl(Object impl)
JacksonStdImpl
annotation on handler (serializer, deserializer etc)
class.
NOTE: passing `null` is legal, and will result in true
being returned.
public static boolean isJacksonStdImpl(Class<?> implClass)
public static boolean hasEnclosingMethod(Class<?> cls)
public static Annotation[] findClassAnnotations(Class<?> cls)
public static Method[] getClassMethods(Class<?> cls)
public static ClassUtil.Ctor[] getConstructors(Class<?> cls)
Copyright © 2008–2019 FasterXML. All rights reserved.