public final class ObjectHelper extends Object
| Modifier and Type | Method and Description |
|---|---|
static int |
arrayLength(Object[] pojo) |
static String |
asString(Object[] objects)
Turns the given object arrays into a meaningful string
|
static <T> T |
cast(Class<T> toType,
Object value)
Converts the given value to the required type or throw a meaningful exception
|
static String |
classCanonicalName(Object value)
Returns the canonical type name of the given value
|
static String |
className(Object value)
Returns the type name of the given value
|
static Class<?> |
convertPrimitiveTypeToWrapperType(Class<?> type)
Converts primitive types such as int to its wrapper type like
Integer |
static Iterable<Throwable> |
createExceptionIterable(Throwable exception)
Creates an Iterable to walk the exception from the bottom up
(the last caused by going upwards to the root exception).
|
static Iterator<Throwable> |
createExceptionIterator(Throwable exception)
Creates an Iterator to walk the exception from the bottom up
(the last caused by going upwards to the root exception).
|
static boolean |
equal(Object a,
Object b)
A helper method for comparing objects for equality while handling nulls
|
static boolean |
equal(Object a,
Object b,
boolean ignoreCase)
A helper method for comparing objects for equality while handling nulls
|
static boolean |
equalByteArray(byte[] a,
byte[] b)
A helper method for comparing byte arrays for equality while handling
nulls
|
static boolean |
equalIgnoreCase(Object a,
Object b)
A helper method for comparing objects for equality while handling case insensitivity
|
static boolean |
evaluateValuePredicate(Object value)
Evaluate the value as a predicate which attempts to convert the value to
a boolean otherwise true is returned if the value is not null
|
static List<Method> |
findMethodsWithAnnotation(Class<?> type,
Class<? extends Annotation> annotationType)
Returns a list of methods which are annotated with the given annotation
|
static List<Method> |
findMethodsWithAnnotation(Class<?> type,
Class<? extends Annotation> annotationType,
boolean checkMetaAnnotations)
Returns a list of methods which are annotated with the given annotation
|
static Optional<Object> |
firstNotNull(Object... values)
Returns the first non null object null.
|
static <A extends Annotation> |
getAnnotation(Object instance,
Class<A> type)
Gets the annotation from the given instance.
|
static String |
getDefaultCharacterSet()
Helper method to return the default character set name
|
static <T> T |
getException(Class<T> type,
Throwable exception)
Retrieves the given exception type from the exception.
|
static String |
getIdentityHashCode(Object object) |
static String |
getPropertyName(Method method)
Returns the Java Bean property name of the given method, if it is a
setter
|
static boolean |
getSystemProperty(String name,
Boolean defaultValue)
A helper method to access a boolean system property, catching any
security exceptions
|
static String |
getSystemProperty(String name,
String defaultValue)
A helper method to access a system property, catching any security exceptions
|
static boolean |
hasAnnotation(AnnotatedElement elem,
Class<? extends Annotation> annotationType,
boolean checkMetaAnnotations)
Checks if a Class or Method are annotated with the given annotation
|
static boolean |
hasAnnotation(Annotation[] annotations,
Class<?> type)
Returns true if the given collection of annotations matches the given type
|
static boolean |
hasDefaultPublicNoArgConstructor(Class<?> type)
Does the given class have a default public no-arg constructor.
|
static <T> void |
ifNotEmpty(T value,
Consumer<T> consumer)
Tests whether the value is not null, an empty string, an empty collection or a map
|
static boolean |
isAssignableFrom(Class<?> a,
Class<?> b)
Returns true if a class is assignable from another class like the
Class.isAssignableFrom(Class) method but which also includes
coercion between primitive types to deal with Java 5 primitive type
wrapping |
static boolean |
isEmpty(Object value)
Tests whether the value is null or an empty string.
|
static boolean |
isEqualToAny(Object object,
Object... values)
Returns true if the given object is equal to any of the expected value
|
static boolean |
isNaN(Object value)
Is the given value a numeric NaN type
|
static boolean |
isNotEmpty(Object value)
Tests whether the value is not null, an empty string or an empty collection/map.
|
static boolean |
isOverridingMethod(Class<?> inheritingClass,
Method source,
Method target,
boolean exact)
Tests whether the target method overrides the source method from the
inheriting class.
|
static boolean |
isOverridingMethod(Method source,
Method target)
Tests whether the target method overrides the source method.
|
static boolean |
isOverridingMethod(Method source,
Method target,
boolean exact)
Tests whether the target method overrides the source method.
|
static boolean |
isPrimitiveArrayType(Class<?> clazz)
Returns if the given
clazz type is a Java primitive array type. |
static Class<?> |
loadClass(String name)
Attempts to load the given class name using the thread context class
loader or the class loader used to load this class
|
static Class<?> |
loadClass(String name,
ClassLoader loader)
Attempts to load the given class name using the thread context class
loader or the given class loader
|
static Class<?> |
loadClass(String name,
ClassLoader loader,
boolean needToWarn)
Attempts to load the given class name using the thread context class
loader or the given class loader
|
static InputStream |
loadResourceAsStream(String name)
Attempts to load the given resource as a stream using the thread context
class loader or the class loader used to load this class
|
static InputStream |
loadResourceAsStream(String name,
ClassLoader loader)
Attempts to load the given resource as a stream using
first the given class loader, then the thread context
class loader and finally the class loader used to load this class
|
static URL |
loadResourceAsURL(String name)
Attempts to load the given resource as a stream using the thread context
class loader or the class loader used to load this class
|
static URL |
loadResourceAsURL(String name,
ClassLoader loader)
Attempts to load the given resource as a stream using the thread context
class loader or the class loader used to load this class
|
static Enumeration<URL> |
loadResourcesAsURL(String uri)
Attempts to load the given resources from the given package name using the thread context
class loader or the class loader used to load this class
|
static Enumeration<URL> |
loadResourcesAsURL(String uri,
ClassLoader loader)
Attempts to load the given resources from the given package name using the thread context
class loader or the class loader used to load this class
|
static Class<?> |
loadSimpleType(String name)
Load a simple type
|
static String |
lookupConstantFieldValue(Class<?> clazz,
String name)
Lookup the constant field on the given class with the given name
|
static boolean |
matches(List<?> list)
Returns the predicate matching boolean on a
List result set where
if the first element is a boolean its value is used otherwise this method
returns true if the collection is not empty |
static String |
name(Class<?> type)
Returns the type name of the given type or null if the type variable is
null
|
static <T> T |
notNull(T value,
String name)
Asserts whether the value is not null
|
static <T> T |
notNull(T value,
String name,
Object on)
Asserts whether the value is not null
|
static <T> T |
supplyIfEmpty(T value,
Supplier<T> supplier)
Tests whether the value is null, an empty string, an empty collection or a map
|
static Boolean |
toBoolean(Object value) |
static Object |
type(Object bean)
Returns the type of the given object or null if the value is null
|
static RuntimeException |
wrapRuntimeCamelException(Throwable e)
Deprecated.
Use
org.apache.camel.RuntimeCamelException#wrapRuntimeCamelException instead |
public static boolean equal(Object a, Object b)
public static boolean equalIgnoreCase(Object a, Object b)
public static boolean equal(Object a, Object b, boolean ignoreCase)
public static boolean equalByteArray(byte[] a, byte[] b)
public static boolean isEqualToAny(Object object, Object... values)
public static <T> T notNull(T value, String name)
value - the value to testname - the key that resolved the valuevalue as isIllegalArgumentException - is thrown if assertion failspublic static <T> T notNull(T value, String name, Object on)
value - the value to teston - additional description to indicate where this problem occurred (appended as toString())name - the key that resolved the valuevalue as isIllegalArgumentException - is thrown if assertion failspublic static boolean isEmpty(Object value)
value - the value, if its a String it will be tested for text length as wellpublic static boolean isNotEmpty(Object value)
value - the value, if its a String it will be tested for text length as wellpublic static Optional<Object> firstNotNull(Object... values)
values - the valuespublic static <T> T supplyIfEmpty(T value, Supplier<T> supplier)
value - the value, if its a String it will be tested for text length as wellsupplier - the supplier, the supplier to be used to get a value if value is nullpublic static <T> void ifNotEmpty(T value, Consumer<T> consumer)
value - the value, if its a String it will be tested for text length as wellconsumer - the consumer, the operation to be executed against value if not emptypublic static boolean matches(List<?> list)
List result set where
if the first element is a boolean its value is used otherwise this method
returns true if the collection is not emptypublic static String getSystemProperty(String name, String defaultValue)
name - the name of the system property requireddefaultValue - the default value to use if the property is not
available or a security exception prevents accesspublic static boolean getSystemProperty(String name, Boolean defaultValue)
name - the name of the system property requireddefaultValue - the default value to use if the property is not
available or a security exception prevents accesspublic static String name(Class<?> type)
public static String classCanonicalName(Object value)
public static Class<?> loadClass(String name)
name - the name of the class to loadpublic static Class<?> loadClass(String name, ClassLoader loader)
name - the name of the class to loadloader - the class loader to use after the thread context class loaderpublic static Class<?> loadClass(String name, ClassLoader loader, boolean needToWarn)
name - the name of the class to loadloader - the class loader to use after the thread context class loaderneedToWarn - when true logs a warning when a class with the given name could not be loadedpublic static Class<?> loadSimpleType(String name)
name - the name of the class to loadpublic static InputStream loadResourceAsStream(String name)
name - the name of the resource to loadpublic static InputStream loadResourceAsStream(String name, ClassLoader loader)
name - the name of the resource to loadloader - optional classloader to attempt firstpublic static URL loadResourceAsURL(String name)
name - the name of the resource to loadpublic static URL loadResourceAsURL(String name, ClassLoader loader)
name - the name of the resource to loadloader - optional classloader to attempt firstpublic static Enumeration<URL> loadResourcesAsURL(String uri)
uri - the name of the package to load its resourcespublic static Enumeration<URL> loadResourcesAsURL(String uri, ClassLoader loader)
uri - the name of the package to load its resourcesloader - optional classloader to attempt firstpublic static boolean isOverridingMethod(Method source, Method target)
source - the source methodtarget - the target methodpublic static boolean isOverridingMethod(Method source, Method target, boolean exact)
source - the source methodtarget - the target methodexact - true if the override must be exact same types, false if the types should be assignablepublic static boolean isOverridingMethod(Class<?> inheritingClass, Method source, Method target, boolean exact)
inheritingClass - the class inheriting the target method overriding
the source methodsource - the source methodtarget - the target methodexact - true if the override must be exact same types,
false if the types should be assignablepublic static List<Method> findMethodsWithAnnotation(Class<?> type, Class<? extends Annotation> annotationType)
type - the type to reflect onannotationType - the annotation typepublic static List<Method> findMethodsWithAnnotation(Class<?> type, Class<? extends Annotation> annotationType, boolean checkMetaAnnotations)
type - the type to reflect onannotationType - the annotation typecheckMetaAnnotations - check for meta annotationspublic static boolean hasAnnotation(AnnotatedElement elem, Class<? extends Annotation> annotationType, boolean checkMetaAnnotations)
elem - the Class or Method to reflect onannotationType - the annotation typecheckMetaAnnotations - check for meta annotationspublic static String asString(Object[] objects)
objects - an array of objects or nullpublic static boolean isAssignableFrom(Class<?> a, Class<?> b)
Class.isAssignableFrom(Class) method but which also includes
coercion between primitive types to deal with Java 5 primitive type
wrappingpublic static boolean isPrimitiveArrayType(Class<?> clazz)
clazz type is a Java primitive array type.clazz - the Java type to be checkedtrue if the given type is a Java primitive array typepublic static int arrayLength(Object[] pojo)
public static Class<?> convertPrimitiveTypeToWrapperType(Class<?> type)
Integerpublic static String getDefaultCharacterSet()
public static String getPropertyName(Method method)
public static boolean hasAnnotation(Annotation[] annotations, Class<?> type)
public static <A extends Annotation> A getAnnotation(Object instance, Class<A> type)
instance - the instancetype - the annotationpublic static <T> T cast(Class<T> toType, Object value)
public static boolean hasDefaultPublicNoArgConstructor(Class<?> type)
public static Object type(Object bean)
public static boolean evaluateValuePredicate(Object value)
public static Iterable<Throwable> createExceptionIterable(Throwable exception)
exception - the exceptionIterablepublic static Iterator<Throwable> createExceptionIterator(Throwable exception)
exception - the exceptionIteratorpublic static <T> T getException(Class<T> type, Throwable exception)
type - the exception type wanted to retrieveexception - the caused exceptionpublic static String getIdentityHashCode(Object object)
public static String lookupConstantFieldValue(Class<?> clazz, String name)
clazz - the classname - the name of the field to lookuppublic static boolean isNaN(Object value)
value - the valueFloat.NaN or Double.NaN.@Deprecated public static RuntimeException wrapRuntimeCamelException(Throwable e)
org.apache.camel.RuntimeCamelException#wrapRuntimeCamelException insteadRuntimeException if its not
already such an exception.e - the caused exceptionApache Camel