public final class ClassUtil
extends java.lang.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 java.lang.String |
backticked(java.lang.String text)
Returns either `text` or [null].
|
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 java.lang.String |
classNameOf(java.lang.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 java.lang.Class<?> |
classOf(java.lang.Object inst) |
static void |
closeOnFailAndThrowAsIOE(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 |
closeOnFailAndThrowAsIOE(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.String |
exceptionMessage(java.lang.Throwable t)
Helper method that returns
Throwable.getMessage() for all other exceptions
except for JsonProcessingException , for which getOriginalMessage() is
returned instead. |
static java.lang.annotation.Annotation[] |
findClassAnnotations(java.lang.Class<?> cls) |
static <T> java.lang.reflect.Constructor<T> |
findConstructor(java.lang.Class<T> cls,
boolean forceAccess) |
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> |
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 java.lang.reflect.Method[] |
getClassMethods(java.lang.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(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 java.lang.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(java.lang.Object inst,
java.lang.Class<?> raw) |
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 |
isEnumType(java.lang.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(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.String |
nameOf(java.lang.Class<?> cls)
Returns either `cls.getName()` (if `cls` not null),
or "[null]" if `cls` is null.
|
static java.lang.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 java.lang.String |
nonNullString(java.lang.String str) |
static java.lang.String |
nullOrToString(java.lang.Object value) |
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 java.lang.String |
quotedOr(java.lang.Object str,
java.lang.String forNull)
Returns either quoted value (with double-quotes) -- if argument non-null
String -- or String NULL (no quotes) (if null).
|
static java.lang.Class<?> |
rawClass(JavaType t) |
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 <T> T |
throwAsMappingException(DeserializationContext ctxt,
java.io.IOException e0) |
static java.lang.Throwable |
throwIfError(java.lang.Throwable t)
Helper method that will check if argument is an
Error ,
and if so, (re)throw it; otherwise just return |
static java.lang.Throwable |
throwIfIOE(java.lang.Throwable t)
Helper method that will check if argument is an
IOException ,
and if so, (re)throw it; otherwise just return |
static java.lang.Throwable |
throwIfRTE(java.lang.Throwable t)
Helper method that will check if argument is an
RuntimeException ,
and if so, (re)throw it; otherwise just return |
static java.lang.Throwable |
throwRootCauseIfIOE(java.lang.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(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 void |
verifyMustOverride(java.lang.Class<?> expType,
java.lang.Object instance,
java.lang.String method) |
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?)
|
public static <T> java.util.Iterator<T> emptyIterator()
public static java.util.List<JavaType> findSuperTypes(JavaType type, java.lang.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 java.util.List<java.lang.Class<?>> findRawSuperTypes(java.lang.Class<?> cls, java.lang.Class<?> endBefore, boolean addClassItself)
public static java.util.List<java.lang.Class<?>> findSuperClasses(java.lang.Class<?> cls, java.lang.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 java.util.List<java.lang.Class<?>> findSuperTypes(java.lang.Class<?> cls, java.lang.Class<?> endBefore)
@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)
public static java.lang.String canBeABeanType(java.lang.Class<?> type)
public static java.lang.String isLocalType(java.lang.Class<?> type, boolean allowNonStatic)
public static java.lang.Class<?> getOuterClass(java.lang.Class<?> type)
public static boolean isProxyType(java.lang.Class<?> type)
public static boolean isConcrete(java.lang.Class<?> type)
public static boolean isConcrete(java.lang.reflect.Member member)
public static boolean isCollectionMapOrArray(java.lang.Class<?> type)
public static boolean isBogusClass(java.lang.Class<?> cls)
public static boolean isNonStaticInnerClass(java.lang.Class<?> cls)
public static boolean isObjectOrPrimitive(java.lang.Class<?> cls)
public static boolean hasClass(java.lang.Object inst, java.lang.Class<?> raw)
public static void verifyMustOverride(java.lang.Class<?> expType, java.lang.Object instance, java.lang.String method)
@Deprecated public static boolean hasGetterSignature(java.lang.reflect.Method m)
public static java.lang.Throwable throwIfError(java.lang.Throwable t)
Error
,
and if so, (re)throw it; otherwise just returnpublic static java.lang.Throwable throwIfRTE(java.lang.Throwable t)
RuntimeException
,
and if so, (re)throw it; otherwise just returnpublic static java.lang.Throwable throwIfIOE(java.lang.Throwable t) throws java.io.IOException
IOException
,
and if so, (re)throw it; otherwise just returnjava.io.IOException
public static java.lang.Throwable getRootCause(java.lang.Throwable t)
public static java.lang.Throwable throwRootCauseIfIOE(java.lang.Throwable t) throws java.io.IOException
getRootCause(java.lang.Throwable)
and then
either throwing it (if instanceof IOException
), or
return.java.io.IOException
public static void throwAsIAE(java.lang.Throwable t)
IllegalArgumentException
if it
is a checked exception; otherwise (runtime exception or error) throw as ispublic static void throwAsIAE(java.lang.Throwable t, java.lang.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, java.io.IOException e0) throws JsonMappingException
JsonMappingException
public static void unwrapAndThrowAsIAE(java.lang.Throwable t)
IllegalArgumentException
if it
is a checked exception; otherwise (runtime exception or error) throw as ispublic static void unwrapAndThrowAsIAE(java.lang.Throwable t, java.lang.String msg)
IllegalArgumentException
if it
is a checked exception; otherwise (runtime exception or error) throw as ispublic static void closeOnFailAndThrowAsIOE(JsonGenerator g, java.lang.Exception fail) throws java.io.IOException
java.io.IOException
public static void closeOnFailAndThrowAsIOE(JsonGenerator g, java.io.Closeable toClose, java.lang.Exception fail) throws java.io.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.java.io.IOException
public static <T> T createInstance(java.lang.Class<T> cls, boolean canFixAccess) throws java.lang.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.java.lang.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> java.lang.reflect.Constructor<T> findConstructor(java.lang.Class<T> cls, boolean forceAccess) throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
public static java.lang.Class<?> classOf(java.lang.Object inst)
public static java.lang.Class<?> rawClass(JavaType t)
public static <T> T nonNull(T valueOrNull, T defaultValue)
public static java.lang.String nullOrToString(java.lang.Object value)
public static java.lang.String nonNullString(java.lang.String str)
public static java.lang.String quotedOr(java.lang.Object str, java.lang.String forNull)
public static java.lang.String getClassDescription(java.lang.Object classOrInstance)
public static java.lang.String getTypeDescription(JavaType fullType)
"null"
if null
passed), similar
to return vaue of getClassDescription(Object)
.fullType
- Fully resolved type or nullpublic static java.lang.String classNameOf(java.lang.Object inst)
public static java.lang.String nameOf(java.lang.Class<?> cls)
public static java.lang.String nameOf(Named named)
public static java.lang.String backticked(java.lang.String text)
public static java.lang.String exceptionMessage(java.lang.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 java.lang.Object defaultValue(java.lang.Class<?> cls)
public static java.lang.Class<?> wrapperType(java.lang.Class<?> primitiveType)
public static java.lang.Class<?> primitiveType(java.lang.Class<?> type)
@Deprecated public static void checkAndFixAccess(java.lang.reflect.Member member)
checkAndFixAccess(member, false);
public static void checkAndFixAccess(java.lang.reflect.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 boolean isEnumType(java.lang.Class<?> rawType)
Enum
.public static java.lang.Class<? extends java.lang.Enum<?>> findEnumType(java.util.EnumSet<?> s)
EnumSet
, without having
access to its declaration.
Code is needed to work around design flaw in JDK.public static java.lang.Class<? extends java.lang.Enum<?>> findEnumType(java.util.EnumMap<?,?> m)
EnumSet
, without having
access to its declaration.
Code is needed to work around design flaw in JDK.public static java.lang.Class<? extends java.lang.Enum<?>> findEnumType(java.lang.Enum<?> en)
public static java.lang.Class<? extends java.lang.Enum<?>> findEnumType(java.lang.Class<?> cls)
public static <T extends java.lang.annotation.Annotation> java.lang.Enum<?> findFirstAnnotatedEnumValue(java.lang.Class<java.lang.Enum<?>> enumClass, java.lang.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.java.lang.IllegalArgumentException
- if there's a reflection issue accessing the Enumpublic static boolean isJacksonStdImpl(java.lang.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(java.lang.Class<?> implClass)
public static java.lang.String getPackageName(java.lang.Class<?> cls)
public static boolean hasEnclosingMethod(java.lang.Class<?> cls)
public static java.lang.reflect.Field[] getDeclaredFields(java.lang.Class<?> cls)
public static java.lang.reflect.Method[] getDeclaredMethods(java.lang.Class<?> cls)
public static java.lang.annotation.Annotation[] findClassAnnotations(java.lang.Class<?> cls)
public static java.lang.reflect.Method[] getClassMethods(java.lang.Class<?> cls)
public static ClassUtil.Ctor[] getConstructors(java.lang.Class<?> cls)
public static java.lang.Class<?> getDeclaringClass(java.lang.Class<?> cls)
public static java.lang.reflect.Type getGenericSuperclass(java.lang.Class<?> cls)
public static java.lang.reflect.Type[] getGenericInterfaces(java.lang.Class<?> cls)
public static java.lang.Class<?> getEnclosingClass(java.lang.Class<?> cls)
Copyright © 2010 - 2020 Adobe. All Rights Reserved