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 |
apostrophed(String text)
Returns either
'text' (single-quoted) or [null] . |
static String |
backticked(String text)
Returns either
`text` (backtick-quoted) 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 evenIfAlreadyPublic)
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 (a) JacksonException , for which getOriginalMessage() is
returned, and (b) InvocationTargetException , for which the cause's message
is returned, if available. |
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)
Deprecated.
since 2.11 (just call Class method directly)
|
static Method[] |
getDeclaredMethods(Class<?> cls)
Deprecated.
since 2.11 (just call Class method directly)
|
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)
Deprecated.
Since 2.12 (just call methods directly or check class name)
|
static Throwable |
getRootCause(Throwable t)
Method that can be used to find the "root cause", innermost
of chained (wrapped) exceptions.
|
static String |
getTypeDescription(JavaType fullType)
Helper method to create and return "backticked" description of given
resolved type (or,
"null" if null passed), similar
to return vaue of getClassDescription(Object) . |
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 |
isEnumType(Class<?> rawType)
Helper method that encapsulates reliable check on whether
given raw type "is an Enum", that is, is or extends
Enum . |
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 boolean |
isJDKClass(Class<?> rawType)
Accessor for checking whether given
Class is under Java package
of java.* or javax.* (including all sub-packages). |
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 boolean |
isRecordType(Class<?> cls)
Helper method for detecting Java14-added new
Record types |
static String |
name(PropertyName name)
Returns either single-quoted (apostrophe)
'name' (if name not null),
or "[null]" if name is null. |
static String |
name(String name)
Returns either single-quoted (apostrophe)
'name' (if name not null),
or "[null]" if name is null. |
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 single-quoted (apostrophe)
'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.
NOTE: mostly/only called to resolve mix-ins as that's where we do not care about fully-resolved types, just associated annotations.
@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 isRecordType(Class<?> cls)
Record
typespublic 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 getTypeDescription(JavaType fullType)
"null"
if null
passed), similar
to return vaue of getClassDescription(Object)
.fullType
- Fully resolved type or nullpublic static String classNameOf(Object inst)
public static String nameOf(Class<?> cls)
public static String nameOf(Named named)
'named.getName()'
(if named
not null),
or "[null]" if named
is null.
NOTE: before 2.12 returned "backticked" version instead of single-quoted name; changed to be compatible with most existing quoting usage within databind
public static String name(String name)
'name'
(if name
not null),
or "[null]" if name
is null.public static String name(PropertyName name)
'name'
(if name
not null),
or "[null]" if name
is null.public static String backticked(String text)
`text`
(backtick-quoted) or [null]
.public static String apostrophed(String text)
'text'
(single-quoted) or [null]
.public static String exceptionMessage(Throwable t)
Throwable.getMessage()
for all other exceptions
except for (a) JacksonException
, for which getOriginalMessage()
is
returned, and (b) InvocationTargetException
, for which the cause's message
is returned, if available.
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)
IllegalArgumentException
if given type is NOT primitive
type (caller has to check).public static Class<?> primitiveType(Class<?> type)
null
if type is neither.@Deprecated public static void checkAndFixAccess(Member member)
checkAndFixAccess(member, false);
public static void checkAndFixAccess(Member member, boolean evenIfAlreadyPublic)
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.evenIfAlreadyPublic
- Whether to always try to make accessor
accessible, even if public
(true),
or only if needed to force by-pass of non-public
access (false)public static boolean isEnumType(Class<?> rawType)
Enum
.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 isJDKClass(Class<?> rawType)
Class
is under Java package
of java.*
or javax.*
(including all sub-packages).
Added since some aspects of handling need to be changed for JDK types (and
possibly some extensions under javax.
?): for example, forcing of access
will not work well for future JDKs (12 and later).
Note: in Jackson 2.11 only returned true for java.*
(and not javax.*
);
was changed in 2.12.
public static boolean isNonStaticInnerClass(Class<?> cls)
@Deprecated public static String getPackageName(Class<?> cls)
public static boolean hasEnclosingMethod(Class<?> cls)
@Deprecated public static Field[] getDeclaredFields(Class<?> cls)
@Deprecated public static Method[] getDeclaredMethods(Class<?> cls)
public static Annotation[] findClassAnnotations(Class<?> cls)
public static Method[] getClassMethods(Class<?> cls)
public static ClassUtil.Ctor[] getConstructors(Class<?> cls)
Copyright © 2008–2022 FasterXML. All rights reserved.