com.fasterxml.jackson.databind.util

## Class ClassUtil

- java.lang.Object
- - com.fasterxml.jackson.databind.util.ClassUtil

-     public final class ClassUtil
      extends java.lang.Object

- - ### Nested Class Summary

    Nested Classes
    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 Summary

    Constructors
    Constructor and Description
    `ClassUtil()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `static java.lang.String` | `canBeABeanType(java.lang.Class<?> type)`
    `static void` | `checkAndFixAccess(java.lang.reflect.Member member)` Deprecated. Since 2.7 call variant that takes boolean flag.
    `static void` | `checkAndFixAccess(java.lang.reflect.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 void` | `closeOnFailAndThrowAsIAE(com.fasterxml.jackson.core.JsonGenerator g, java.io.Closeable toClose, java.lang.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` | `closeOnFailAndThrowAsIAE(com.fasterxml.jackson.core.JsonGenerator g, java.lang.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(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 <T> java.util.Iterator<T>` | `emptyIterator()`
    `static java.lang.Class<?>` | `findClass(java.lang.String className)` Deprecated. Since 2.6, use method in `TypeFactory`.
    `static java.lang.annotation.Annotation[]` | `findClassAnnotations(java.lang.Class<?> cls)`
    `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 <T extends java.lang.annotation.Annotation> java.lang.Enum<?>` | `findFirstAnnotatedEnumValue(java.lang.Class<java.lang.Enum<?>> enumClass, java.lang.Class<T> annotationClass)` A method that will look for the first Enum value annotated with the given Annotation.
    `static java.util.List<java.lang.Class<?>>` | `findRawSuperTypes(java.lang.Class<?> cls, java.lang.Class<?> endBefore, boolean addClassItself)`
    `static java.util.List<java.lang.Class<?>>` | `findSuperClasses(java.lang.Class<?> cls, java.lang.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 java.util.List<java.lang.Class<?>>` | `findSuperTypes(java.lang.Class<?> cls, java.lang.Class<?> endBefore)` Deprecated.
    `static java.util.List<java.lang.Class<?>>` | `findSuperTypes(java.lang.Class<?> cls, java.lang.Class<?> endBefore, java.util.List<java.lang.Class<?>> result)` Deprecated.
    `static java.util.List<JavaType>` | `findSuperTypes(JavaType type, java.lang.Class<?> endBefore, boolean addClassItself)` Method that will find all sub-classes and implemented interfaces of a given class or interface.
    `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 ClassUtil.Ctor[]` | `getConstructors(java.lang.Class<?> cls)`
    `static java.lang.reflect.Field[]` | `getDeclaredFields(java.lang.Class<?> cls)`
    `static java.lang.reflect.Method[]` | `getDeclaredMethods(java.lang.Class<?> cls)`
    `static java.lang.Class<?>` | `getDeclaringClass(java.lang.Class<?> cls)`
    `static java.lang.Class<?>` | `getEnclosingClass(java.lang.Class<?> cls)`
    `static java.lang.reflect.Type[]` | `getGenericInterfaces(java.lang.Class<?> cls)`
    `static java.lang.reflect.Type` | `getGenericSuperclass(java.lang.Class<?> cls)`
    `static java.lang.Class<?>` | `getOuterClass(java.lang.Class<?> type)` Method for finding enclosing class for non-static inner classes
    `static java.lang.String` | `getPackageName(java.lang.Class<?> cls)`
    `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` | `hasEnclosingMethod(java.lang.Class<?> cls)`
    `static boolean` | `hasGetterSignature(java.lang.reflect.Method m)` Deprecated. Since 2.6 not used; may be removed before 3.x
    `static boolean` | `isBogusClass(java.lang.Class<?> cls)`
    `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 boolean` | `isJacksonStdImpl(java.lang.Class<?> implClass)`
    `static boolean` | `isJacksonStdImpl(java.lang.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 java.lang.String` | `isLocalType(java.lang.Class<?> type, boolean allowNonStatic)`
    `static boolean` | `isNonStaticInnerClass(java.lang.Class<?> cls)`
    `static boolean` | `isObjectOrPrimitive(java.lang.Class<?> cls)`
    `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 java.lang.Class<?>` | `primitiveType(java.lang.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 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 java.lang.Throwable` | `throwRootCauseIfIOE(java.lang.Throwable t)` Method that works like `throwRootCause(java.lang.Throwable)` if (and only if) root cause is an `IOException`; otherwise returns root cause
    `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?)

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### ClassUtil

          public ClassUtil()

  - ### Method Detail

    - #### emptyIterator

          public static <T> java.util.Iterator<T> emptyIterator()

      Since:
        2.7

    - #### findSuperTypes

          public static java.util.List<JavaType> findSuperTypes(JavaType type,
                                                java.lang.Class<?> endBefore,
                                                boolean addClassItself)

      Method that will find all sub-classes and implemented interfaces of a given class or interface. Classes are listed in order of precedence, starting with the immediate super-class, followed by interfaces class directly declares to implemented, and then recursively followed by parent of super-class and so forth. Note that `Object.class` is not included in the list regardless of whether `endBefore` argument is defined or not.
      Parameters:
        `endBefore` - Super-type to NOT include in results, if any; when encountered, will be ignored (and no super types are checked).
      Since:
        2.7

    - #### findRawSuperTypes

          public static java.util.List<java.lang.Class<?>> findRawSuperTypes(java.lang.Class<?> cls,
                                                             java.lang.Class<?> endBefore,
                                                             boolean addClassItself)

      Since:
        2.7

    - #### findSuperClasses

          public static java.util.List<java.lang.Class<?>> findSuperClasses(java.lang.Class<?> cls,
                                                            java.lang.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.
      Since:
        2.7

    - #### findSuperTypes

          @Deprecated
          public static java.util.List<java.lang.Class<?>> findSuperTypes(java.lang.Class<?> cls,
                                                                     java.lang.Class<?> endBefore)

      Deprecated.

    - #### findSuperTypes

          @Deprecated
          public static java.util.List<java.lang.Class<?>> findSuperTypes(java.lang.Class<?> cls,
                                                                     java.lang.Class<?> endBefore,
                                                                     java.util.List<java.lang.Class<?>> result)

      Deprecated.

    - #### canBeABeanType

          public static java.lang.String canBeABeanType(java.lang.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 java.lang.String isLocalType(java.lang.Class<?> type,
                                     boolean allowNonStatic)

    - #### getOuterClass

          public static java.lang.Class<?> getOuterClass(java.lang.Class<?> type)

      Method for finding enclosing class for non-static inner classes

    - #### isProxyType

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

    - #### isConcrete

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

    - #### isConcrete

          public static boolean isConcrete(java.lang.reflect.Member member)

    - #### isCollectionMapOrArray

          public static boolean isCollectionMapOrArray(java.lang.Class<?> type)

    - #### getClassDescription

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

    - #### findClass

          @Deprecated
          public static java.lang.Class<?> findClass(java.lang.String className)
                                              throws java.lang.ClassNotFoundException

      Deprecated. Since 2.6, use method in `TypeFactory`.
      Throws:
        `java.lang.ClassNotFoundException`

    - #### hasGetterSignature

          @Deprecated
          public static boolean hasGetterSignature(java.lang.reflect.Method m)

      Deprecated. Since 2.6 not used; may be removed before 3.x

    - #### getRootCause

          public static java.lang.Throwable getRootCause(java.lang.Throwable t)

      Method that can be used to find the "root cause", innermost of chained (wrapped) exceptions.

    - #### throwRootCause

          public static void throwRootCause(java.lang.Throwable t)
                                     throws java.lang.Exception

      Method that will unwrap root causes of given Throwable, and throw the innermost `Exception` or `Error` as is. This is useful in cases where mandatory wrapping is added, which is often done by Reflection API.
      Throws:
        `java.lang.Exception`

    - #### throwRootCauseIfIOE

          public static java.lang.Throwable throwRootCauseIfIOE(java.lang.Throwable t)
                                                         throws java.io.IOException

      Method that works like `throwRootCause(java.lang.Throwable)` if (and only if) root cause is an `IOException`; otherwise returns root cause
      Throws:
        `java.io.IOException`
      Since:
        2.8

    - #### throwAsIAE

          public 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

    - #### throwAsIAE

          public 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

    - #### unwrapAndThrowAsIAE

          public 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

    - #### unwrapAndThrowAsIAE

          public 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

    - #### closeOnFailAndThrowAsIAE

          public static void closeOnFailAndThrowAsIAE(com.fasterxml.jackson.core.JsonGenerator g,
                                      java.lang.Exception fail)
                                               throws java.io.IOException

      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.
      Throws:
        `java.io.IOException`
      Since:
        2.8

    - #### closeOnFailAndThrowAsIAE

          public static void closeOnFailAndThrowAsIAE(com.fasterxml.jackson.core.JsonGenerator g,
                                      java.io.Closeable toClose,
                                      java.lang.Exception fail)
                                               throws java.io.IOException

      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:
        `java.io.IOException`
      Since:
        2.8

    - #### createInstance

          public static <T> T createInstance(java.lang.Class<T> cls,
                             boolean canFixAccess)
                                  throws java.lang.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:
        `java.lang.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> java.lang.reflect.Constructor<T> findConstructor(java.lang.Class<T> cls,
                                                             boolean canFixAccess)
                                                                  throws java.lang.IllegalArgumentException

      Throws:
        `java.lang.IllegalArgumentException`

    - #### defaultValue

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

    - #### wrapperType

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

    - #### primitiveType

          public static java.lang.Class<?> primitiveType(java.lang.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

          @Deprecated
          public static void checkAndFixAccess(java.lang.reflect.Member member)

      Deprecated. Since 2.7 call variant that takes boolean flag.
      Equivalent to call:

             checkAndFixAccess(member, false);

    - #### checkAndFixAccess

          public static void checkAndFixAccess(java.lang.reflect.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)`.
      Parameters:
        `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)
      Since:
        2.7

    - #### findEnumType

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

    - #### findEnumType

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

    - #### findEnumType

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

    - #### findEnumType

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

    - #### findFirstAnnotatedEnumValue

          public static <T extends java.lang.annotation.Annotation> java.lang.Enum<?> findFirstAnnotatedEnumValue(java.lang.Class<java.lang.Enum<?>> enumClass,
                                                                                                  java.lang.Class<T> annotationClass)

      A method that will look for the first Enum value annotated with the given Annotation.

      If there's more than one value annotated, the first one found will be returned. Which one exactly is used is undetermined.

      Parameters:
        `enumClass` - The Enum class to scan for a value with the given annotation
        `annotationClass` - The annotation to look for.
      Returns:
        the Enum value annotated with the given Annotation or `null` if none is found.
      Throws:
        `java.lang.IllegalArgumentException` - if there's a reflection issue accessing the Enum
      Since:
        2.8

    - #### isJacksonStdImpl

          public static boolean isJacksonStdImpl(java.lang.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.

    - #### isJacksonStdImpl

          public static boolean isJacksonStdImpl(java.lang.Class<?> implClass)

    - #### isBogusClass

          public static boolean isBogusClass(java.lang.Class<?> cls)

    - #### isNonStaticInnerClass

          public static boolean isNonStaticInnerClass(java.lang.Class<?> cls)

    - #### isObjectOrPrimitive

          public static boolean isObjectOrPrimitive(java.lang.Class<?> cls)

      Since:
        2.7

    - #### getPackageName

          public static java.lang.String getPackageName(java.lang.Class<?> cls)

      Since:
        2.7

    - #### hasEnclosingMethod

          public static boolean hasEnclosingMethod(java.lang.Class<?> cls)

      Since:
        2.7

    - #### getDeclaredFields

          public static java.lang.reflect.Field[] getDeclaredFields(java.lang.Class<?> cls)

      Since:
        2.7

    - #### getDeclaredMethods

          public static java.lang.reflect.Method[] getDeclaredMethods(java.lang.Class<?> cls)

      Since:
        2.7

    - #### findClassAnnotations

          public static java.lang.annotation.Annotation[] findClassAnnotations(java.lang.Class<?> cls)

      Since:
        2.7

    - #### getConstructors

          public static ClassUtil.Ctor[] getConstructors(java.lang.Class<?> cls)

      Since:
        2.7

    - #### getDeclaringClass

          public static java.lang.Class<?> getDeclaringClass(java.lang.Class<?> cls)

      Since:
        2.7

    - #### getGenericSuperclass

          public static java.lang.reflect.Type getGenericSuperclass(java.lang.Class<?> cls)

      Since:
        2.7

    - #### getGenericInterfaces

          public static java.lang.reflect.Type[] getGenericInterfaces(java.lang.Class<?> cls)

      Since:
        2.7

    - #### getEnclosingClass

          public static java.lang.Class<?> getEnclosingClass(java.lang.Class<?> cls)

      Since:
        2.7