Class ClassUtil

java.lang.Object
tools.jackson.databind.util.ClassUtil

public final class ClassUtil extends Object
  • Constructor Details

    • ClassUtil

      public ClassUtil()
  • Method Details

    • emptyIterator

      public static <T> Iterator<T> emptyIterator()
    • emptyStream

      public static <T> Stream<T> emptyStream()
      Since:
      2.19
    • findRawSuperTypes

      public static List<Class<?>> findRawSuperTypes(Class<?> cls, Class<?> endBefore, boolean addClassItself)
    • 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 if 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.

    • canBeABeanType

      public static String canBeABeanType(Class<?> type)
      Returns:
      Null if class might be a bean; type String (that identifies why it's not a bean) if not
    • isLocalType

      public static String isLocalType(Class<?> type, boolean allowNonStatic)
    • getOuterClass

      public static Class<?> getOuterClass(Class<?> type)
      Method for finding enclosing class for non-static inner classes
    • isProxyType

      public 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.
    • isConcrete

      public static boolean isConcrete(Class<?> type)
      Helper method that checks if given class is a concrete one; that is, not an interface or abstract class.
    • isConcrete

      public static boolean isConcrete(Member member)
    • isCollectionMapOrArray

      public static boolean isCollectionMapOrArray(Class<?> type)
    • isBogusClass

      public static boolean isBogusClass(Class<?> cls)
    • isRecordType

      public static boolean isRecordType(Class<?> cls)
      Helper method for detecting Java14-added Record types
    • isObjectOrPrimitive

      public static boolean isObjectOrPrimitive(Class<?> cls)
    • hasClass

      public static boolean hasClass(Object inst, Class<?> raw)
    • verifyMustOverride

      public static void verifyMustOverride(Class<?> expType, Object instance, String method)
    • throwIfError

      public static Throwable throwIfError(Throwable t)
      Helper method that will check if argument is an Error, and if so, (re)throw it; otherwise just return
    • throwIfRTE

      public static Throwable throwIfRTE(Throwable t)
      Helper method that will check if argument is an RuntimeException, and if so, (re)throw it; otherwise just return
    • throwIfJacksonE

      public static Throwable throwIfJacksonE(Throwable t) throws tools.jackson.core.JacksonException
      Helper method that will check if argument is an IOException, and if so, (re)throw it; otherwise just return
      Throws:
      tools.jackson.core.JacksonException
    • getRootCause

      public static Throwable getRootCause(Throwable t)
      Method that can be used to find the "root cause", innermost of chained (wrapped) exceptions.
    • throwRootCauseIfJacksonE

      public static Throwable throwRootCauseIfJacksonE(Throwable t) throws tools.jackson.core.JacksonException
      Method that works like by calling getRootCause(java.lang.Throwable) and then either throwing it (if instanceof IOException), or return.
      Throws:
      tools.jackson.core.JacksonException
    • throwAsIAE

      public 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
    • throwAsIAE

      public 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
    • unwrapAndThrowAsIAE

      public 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
    • unwrapAndThrowAsIAE

      public 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
    • closeOnFailAndThrowAsJacksonE

      public static void closeOnFailAndThrowAsJacksonE(tools.jackson.core.JsonGenerator g, Exception fail) throws tools.jackson.core.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, RuntimeException is thrown (except for IOException which will be wrapped as JacksonIOException.

      Throws:
      tools.jackson.core.JacksonException
      Since:
      3.0 (with this name)
    • closeOnFailAndThrowAsJacksonE

      public static void closeOnFailAndThrowAsJacksonE(tools.jackson.core.JsonGenerator g, AutoCloseable toClose, Exception fail) throws tools.jackson.core.JacksonException
      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. 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:
      tools.jackson.core.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

      public static Class<?> classOf(Object inst)
    • rawClass

      public static Class<?> rawClass(JavaType t)
    • nonNull

      public static <T> T nonNull(T valueOrNull, T defaultValue)
    • nullOrToString

      public static String nullOrToString(Object value)
    • nonNullString

      public static String nonNullString(String str)
    • quotedOr

      public static String quotedOr(Object str, String forNull)
    • getClassDescription

      public 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.
    • getTypeDescription

      public 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).
      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

      public 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.
    • nameOf

      public static String nameOf(Class<?> cls)
      Returns either `cls.getName()` (if `cls` not null), or "[null]" if `cls` is null.
    • nameOf

      public static String nameOf(tools.jackson.core.util.Named named)
      Returns either single-quoted (apostrophe) 'named.getName()' (if named not null), or "[null]" if named is null.
    • name

      public static String name(String name)
      Returns either single-quoted (apostrophe) 'name' (if name not null), or "[null]" if name is null.
    • name

      public static String name(PropertyName name)
      Returns either single-quoted (apostrophe) 'name' (if name not null), or "[null]" if name is null.
    • backticked

      public static String backticked(String text)
      Returns either `text` (backtick-quoted) or [null].
    • apostrophed

      public static String apostrophed(String text)
      Returns either 'text' (single-quoted) or [null].
    • exceptionMessage

      public 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. Method is used to avoid accidentally including trailing location information twice in message when wrapping exceptions.
    • defaultValue

      public static Object defaultValue(Class<?> cls)
      Helper method used to get default value for wrappers used for primitive types (0 for Integer etc)
    • wrapperType

      public static Class<?> wrapperType(Class<?> primitiveType)
      Helper method for finding wrapper type for given primitive type (why isn't there one in JDK?). NOTE: throws IllegalArgumentException if given type is NOT primitive type (caller has to check).
    • primitiveType

      public 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.
      Since:
      2.7
    • checkAndFixAccess

      public 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).
      Parameters:
      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)
    • isEnumType

      public 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.
    • findEnumType

      public 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. Code is needed to work around design flaw in JDK.
    • findEnumType

      public 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. Code is needed to work around design flaw in JDK.
    • findEnumType

      public static Class<? extends Enum<?>> findEnumType(Enum<?> en)
      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

      public 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. This is either class of enum instance (for "simple" enumerations), or its superclass (for enums with instance fields or methods)
    • isJacksonStdImpl

      public 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. Determination is done using JacksonStdImpl annotation on handler (serializer, deserializer etc) class.

      NOTE: passing `null` is legal, and will result in true being returned.

    • isJacksonStdImpl

      public static boolean isJacksonStdImpl(Class<?> implClass)
    • isJDKClass

      public static boolean isJDKClass(Class<?> rawType)
      Accessor for checking whether given Class is under Java package of java.*, javax.* or sun.* (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.

    • 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:
      True if we can determine that the code is running on JDK 17 or above; false otherwise.
      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

      public static boolean isNonStaticInnerClass(Class<?> cls)
    • hasEnclosingMethod

      public static boolean hasEnclosingMethod(Class<?> cls)
    • getClassMethods

      public 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.
      Since:
      2.9
    • getConstructors

      public static ClassUtil.Ctor[] getConstructors(Class<?> cls)
      Since:
      2.7
    • getDeclaringClass

      public static Class<?> getDeclaringClass(Class<?> cls)
    • getEnclosingClass

      public static Class<?> getEnclosingClass(Class<?> cls)