java.lang.Object
tools.jackson.databind.util.ClassUtil
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classValue class used for containing information about discovered Constructors -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Stringapostrophed(String text) Returns either'text'(single-quoted) or[null].static Stringbackticked(String text) Returns either`text`(backtick-quoted) or[null].static StringcanBeABeanType(Class<?> type) static voidcheckAndFixAccess(Member member, boolean evenIfAlreadyPublic) Method that is called if aMembermay need forced access, to force a field, method or constructor to be accessible: this is done by callingAccessibleObject.setAccessible(boolean).static StringclassNameOf(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<?>static voidcloseOnFailAndThrowAsJacksonE(JsonGenerator g, AutoCloseable toClose, Exception fail) Helper method that encapsulate logic in trying to close givenCloseablein case of failure; useful mostly in forcing flush()ing as otherwise error conditions tend to be hard to diagnose.static voidHelper 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> TcreateInstance(Class<T> cls, boolean canFixAccess) Method that can be called to try to create an instantiate of specified type.static ObjectdefaultValue(Class<?> cls) Helper method used to get default value for wrappers used for primitive types (0 for Integer etc)static <T> Iterator<T>static <T> Stream<T>static StringHelper method that returnsThrowable.getMessage()for all other exceptions except for (a)JacksonException, for whichgetOriginalMessage()is returned, and (b)InvocationTargetException, for which the cause's message is returned, if available.static <T> Constructor<T>findConstructor(Class<T> cls, boolean forceAccess) findEnumType(Class<?> cls) Helper method that can be used to dynamically figure out formal enumeration type (class) for given class of an enumeration value.findEnumType(Enum<?> en) Helper method that can be used to dynamically figure out formal enumeration type (class) for given enumeration.findEnumType(EnumMap<?, ?> m) Helper method that can be used to dynamically figure out enumeration type of givenEnumSet, without having access to its declaration.findEnumType(EnumSet<?> s) Helper method that can be used to dynamically figure out enumeration type of givenEnumSet, without having access to its declaration.findRawSuperTypes(Class<?> cls, Class<?> endBefore, boolean addClassItself) 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 StringgetClassDescription(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 Class<?>getDeclaringClass(Class<?> cls) static Class<?>getEnclosingClass(Class<?> cls) static intstatic Class<?>getOuterClass(Class<?> type) Method for finding enclosing class for non-static inner classesstatic ThrowableMethod that can be used to find the "root cause", innermost of chained (wrapped) exceptions.static StringgetTypeDescription(JavaType fullType) Helper method to create and return "backticked" description of given resolved type (or,"null"ifnullpassed), similar to return vaue ofgetClassDescription(Object).static booleanstatic booleanhasEnclosingMethod(Class<?> cls) static booleanisBogusClass(Class<?> cls) static booleanisCollectionMapOrArray(Class<?> type) static booleanisConcrete(Class<?> type) Helper method that checks if given class is a concrete one; that is, not an interface or abstract class.static booleanisConcrete(Member member) static booleanisEnumType(Class<?> rawType) Helper method that encapsulates reliable check on whether given raw type "is an Enum", that is, is or extendsEnum.static booleanisJacksonStdImpl(Class<?> implClass) static booleanisJacksonStdImpl(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 booleanConvenience method for:static booleanisJDKClass(Class<?> rawType) Accessor for checking whether givenClassis under Java package ofjava.*,javax.*orsun.*(including all sub-packages).static booleanisJDKCoreClass(Class<?> rawType) Similar toisJDKClass(Class), but for JDK core classes: those in packages underjava.*but NOT underjavax.*(orsun.*).static StringisLocalType(Class<?> type, boolean allowNonStatic) static booleanisNonStaticInnerClass(Class<?> cls) static booleanisObjectOrPrimitive(Class<?> cls) static booleanisProxyType(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 booleanisRecordType(Class<?> cls) Helper method for detecting Java14-addedRecordtypesstatic StringReturns either single-quoted (apostrophe)'name'(ifnamenot null), or "[null]" ifnameis null.static Stringname(PropertyName name) Returns either single-quoted (apostrophe)'name'(ifnamenot null), or "[null]" ifnameis null.static StringReturns either `cls.getName()` (if `cls` not null), or "[null]" if `cls` is null.static StringReturns either single-quoted (apostrophe)'named.getName()'(ifnamednot null), or "[null]" ifnamedis null.static <T> TnonNull(T valueOrNull, T defaultValue) static StringnonNullString(String str) static StringnullOrToString(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; returnsnullif type is neither.static Stringstatic Class<?>static <E extends Throwable>
RuntimeExceptionsneakyThrow(Throwable throwable) Throw an exception even if it's checked and not declared.static voidMethod that will wrap 't' as anIllegalArgumentExceptionif it is a checked exception; otherwise (runtime exception or error) throw as isstatic voidthrowAsIAE(Throwable t, String msg) Method that will wrap 't' as anIllegalArgumentException(and with specified message) if it is a checked exception; otherwise (runtime exception or error) throw as isstatic ThrowableHelper method that will check if argument is anError, and if so, (re)throw it; otherwise just returnstatic ThrowableHelper method that will check if argument is anIOException, and if so, (re)throw it; otherwise just returnstatic ThrowableHelper method that will check if argument is anRuntimeException, and if so, (re)throw it; otherwise just returnstatic ThrowableMethod that works like by callinggetRootCause(java.lang.Throwable)and then either throwing it (if instanceofIOException), or return.static voidMethod that will locate the innermost exception for given Throwable; and then wrap it as anIllegalArgumentExceptionif it is a checked exception; otherwise (runtime exception or error) throw as isstatic voidunwrapAndThrowAsIAE(Throwable t, String msg) Method that will locate the innermost exception for given Throwable; and then wrap it as anIllegalArgumentExceptionif it is a checked exception; otherwise (runtime exception or error) throw as isstatic voidverifyMustOverride(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?).
-
Constructor Details
-
ClassUtil
public ClassUtil()
-
-
Method Details
-
emptyIterator
-
emptyStream
- Since:
- 2.19
-
findRawSuperTypes
-
findSuperClasses
public 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. Class itself is included ifaddClassItselfis true.NOTE: mostly/only called to resolve mix-ins as that's where we do not care about fully-resolved types, just associated annotations.
-
canBeABeanType
- Returns:
- Null if class might be a bean; type String (that identifies why it's not a bean) if not
-
isLocalType
-
getOuterClass
Method for finding enclosing class for non-static inner classes -
isProxyType
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. -
isConcrete
Helper method that checks if given class is a concrete one; that is, not an interface or abstract class. -
isConcrete
-
isCollectionMapOrArray
-
isBogusClass
-
isRecordType
Helper method for detecting Java14-addedRecordtypes -
isObjectOrPrimitive
-
hasClass
-
verifyMustOverride
-
throwIfError
Helper method that will check if argument is anError, and if so, (re)throw it; otherwise just return -
throwIfRTE
Helper method that will check if argument is anRuntimeException, and if so, (re)throw it; otherwise just return -
throwIfJacksonE
Helper method that will check if argument is anIOException, and if so, (re)throw it; otherwise just return- Throws:
JacksonException
-
getRootCause
Method that can be used to find the "root cause", innermost of chained (wrapped) exceptions. -
throwRootCauseIfJacksonE
Method that works like by callinggetRootCause(java.lang.Throwable)and then either throwing it (if instanceofIOException), or return.- Throws:
JacksonException
-
throwAsIAE
Method that will wrap 't' as anIllegalArgumentExceptionif it is a checked exception; otherwise (runtime exception or error) throw as is -
throwAsIAE
Method that will wrap 't' as anIllegalArgumentException(and with specified message) if it is a checked exception; otherwise (runtime exception or error) throw as is -
unwrapAndThrowAsIAE
Method that will locate the innermost exception for given Throwable; and then wrap it as anIllegalArgumentExceptionif it is a checked exception; otherwise (runtime exception or error) throw as is -
unwrapAndThrowAsIAE
Method that will locate the innermost exception for given Throwable; and then wrap it as anIllegalArgumentExceptionif it is a checked exception; otherwise (runtime exception or error) throw as is -
sneakyThrow
Throw an exception even if it's checked and not declared.- Throws:
E extends Throwable
-
closeOnFailAndThrowAsJacksonE
public static void closeOnFailAndThrowAsJacksonE(JsonGenerator g, Exception fail) throws JacksonException 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. 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.Note that exception is thrown as-is if unchecked (likely case); if it is checked, however,
RuntimeExceptionis thrown (except forIOExceptionwhich will be wrapped asJacksonIOException.- Throws:
JacksonException- Since:
- 3.0 (with this name)
-
closeOnFailAndThrowAsJacksonE
public static void closeOnFailAndThrowAsJacksonE(JsonGenerator g, AutoCloseable toClose, Exception fail) throws JacksonException Helper method that encapsulate logic in trying to close givenCloseablein 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.- Throws:
JacksonException
-
createInstance
public static <T> T createInstance(Class<T> cls, boolean canFixAccess) throws IllegalArgumentException Method that can be called to try to create an instantiate of specified type. Instantiation is done using default no-argument constructor.- Parameters:
canFixAccess- Whether it is possible to try to change access rights of the default constructor (in case it is not publicly accessible) or not.- Throws:
IllegalArgumentException- If instantiation fails for any reason; except for cases where constructor throws an unchecked exception (which will be passed as is)
-
findConstructor
public static <T> Constructor<T> findConstructor(Class<T> cls, boolean forceAccess) throws IllegalArgumentException - Throws:
IllegalArgumentException
-
classOf
-
rawClass
-
nonNull
public static <T> T nonNull(T valueOrNull, T defaultValue) -
nullOrToString
-
nonNullString
-
quotedOr
-
getClassDescription
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. -
getTypeDescription
Helper method to create and return "backticked" description of given resolved type (or,"null"ifnullpassed), similar to return vaue ofgetClassDescription(Object).- Parameters:
fullType- Fully resolved type or null- Returns:
- String description of type including generic type parameters, surrounded by backticks, if type passed; or string "null" if {code null} passed
-
classNameOf
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. -
nameOf
Returns either `cls.getName()` (if `cls` not null), or "[null]" if `cls` is null. -
nameOf
Returns either single-quoted (apostrophe)'named.getName()'(ifnamednot null), or "[null]" ifnamedis null. -
name
Returns either single-quoted (apostrophe)'name'(ifnamenot null), or "[null]" ifnameis null. -
name
Returns either single-quoted (apostrophe)'name'(ifnamenot null), or "[null]" ifnameis null. -
backticked
Returns either`text`(backtick-quoted) or[null]. -
apostrophed
Returns either'text'(single-quoted) or[null]. -
exceptionMessage
Helper method that returnsThrowable.getMessage()for all other exceptions except for (a)JacksonException, for whichgetOriginalMessage()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. -
defaultValue
Helper method used to get default value for wrappers used for primitive types (0 for Integer etc) -
wrapperType
Helper method for finding wrapper type for given primitive type (why isn't there one in JDK?). NOTE: throwsIllegalArgumentExceptionif given type is NOT primitive type (caller has to check). -
primitiveType
Method that can be used to find primitive type for given class if (but only if) it is either wrapper type or primitive type; returnsnullif type is neither.- Since:
- 2.7
-
checkAndFixAccess
Method that is called if aMembermay need forced access, to force a field, method or constructor to be accessible: this is done by callingAccessibleObject.setAccessible(boolean).- Parameters:
member- Accessor to callsetAccessible()on.evenIfAlreadyPublic- Whether to always try to make accessor accessible, even ifpublic(true), or only if needed to force by-pass of non-publicaccess (false)
-
isEnumType
Helper method that encapsulates reliable check on whether given raw type "is an Enum", that is, is or extendsEnum. -
findEnumType
Helper method that can be used to dynamically figure out enumeration type of givenEnumSet, without having access to its declaration. Code is needed to work around design flaw in JDK. -
findEnumType
Helper method that can be used to dynamically figure out enumeration type of givenEnumSet, without having access to its declaration. Code is needed to work around design flaw in JDK. -
findEnumType
Helper method that can be used to dynamically figure out formal enumeration type (class) for given enumeration. This is either class of enum instance (for "simple" enumerations), or its superclass (for enums with instance fields or methods) -
findEnumType
Helper method that can be used to dynamically figure out formal enumeration type (class) for given class of an enumeration value. This is either class of enum instance (for "simple" enumerations), or its superclass (for enums with instance fields or methods) -
isJacksonStdImpl
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. Determination is done usingJacksonStdImplannotation on handler (serializer, deserializer etc) class.NOTE: passing `null` is legal, and will result in
truebeing returned. -
isJacksonStdImpl
-
isJDKClass
Accessor for checking whether givenClassis under Java package ofjava.*,javax.*orsun.*(including all sub-packages).Added since some aspects of handling need to be changed for JDK types (and possibly some extensions under
javax. and sun.: for example, forcing of access will not work well for future JDKs. -
isJDKCoreClass
Similar toisJDKClass(Class), but for JDK core classes: those in packages underjava.*but NOT underjavax.*(orsun.*).- Since:
- 2.20
-
isJDK17OrAbove
public static boolean isJDK17OrAbove()Convenience method for:return getJDKMajorVersion() >= 17
that also catches any possible exceptions so it is safe to call from static contexts.- Returns:
Trueif we can determine that the code is running on JDK 17 or above;falseotherwise.- Since:
- 2.15
-
getJDKMajorVersion
public static int getJDKMajorVersion()- Returns:
- Major version of JDK we are running on
- Throws:
IllegalStateException- If JDK version information cannot be determined- Since:
- 2.15
-
isNonStaticInnerClass
-
hasEnclosingMethod
-
getClassMethods
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.- Since:
- 2.9
-
getConstructors
- Since:
- 2.7
-
getDeclaringClass
-
getEnclosingClass
-