Package org.apache.camel.util
Class ObjectHelper
- java.lang.Object
-
- org.apache.camel.util.ObjectHelper
-
public final class ObjectHelper extends Object
A number of useful helper methods for working with Objects
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static void
addListByIndex(List<Object> list, int idx, Object value)
Adds the value to the list at the given indexstatic int
arrayLength(Object[] pojo)
Used by camel-beanstatic List<Object>
asList(Object[] objects)
Turns the input array to a list of objects.static String
asString(Object[] objects)
Turns the given object arrays into a meaningful stringstatic <T> T
cast(Class<T> toType, Object value)
Converts the given value to the required type or throw a meaningful exceptionstatic String
classCanonicalName(Object value)
Returns the canonical type name of the given valuestatic String
className(Object value)
Returns the type name of the given valuestatic Class<?>
convertPrimitiveTypeToWrapperType(Class<?> type)
Converts primitive types such as int to its wrapper type likeInteger
static Class<?>
convertWrapperTypeToPrimitiveType(Class<?> type)
Converts wrapper type likeInteger
to its primitive type, i.e.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 nullsstatic boolean
equal(Object a, Object b, boolean ignoreCase)
A helper method for comparing objects for equality while handling nullsstatic boolean
equalByteArray(byte[] a, byte[] b)
A helper method for comparing byte arrays for equality while handling nullsstatic boolean
equalIgnoreCase(Object a, Object b)
A helper method for comparing objects for equality while handling case insensitivitystatic 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 nullstatic List<Method>
findMethodsWithAnnotation(Class<?> type, Class<? extends Annotation> annotationType)
Returns a list of methods which are annotated with the given annotationstatic List<Method>
findMethodsWithAnnotation(Class<?> type, Class<? extends Annotation> annotationType, boolean checkMetaAnnotations)
Returns a list of methods which are annotated with the given annotationstatic Optional<Object>
firstNotNull(Object... values)
Returns the first non null object null.static <A extends Annotation>
AgetAnnotation(Object instance, Class<A> type)
Gets the annotation from the given instance.static String
getDefaultCharacterSet()
Helper method to return the default character set namestatic <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 setterstatic boolean
getSystemProperty(String name, Boolean defaultValue)
A helper method to access a boolean system property, catching any security exceptionsstatic String
getSystemProperty(String name, String defaultValue)
A helper method to access a system property, catching any security exceptionsstatic boolean
hasAnnotation(Annotation[] annotations, Class<?> type)
Returns true if the given collection of annotations matches the given typestatic boolean
hasAnnotation(AnnotatedElement elem, Class<? extends Annotation> annotationType, boolean checkMetaAnnotations)
Checks if a Class or Method are annotated with the given annotationstatic boolean
hasDefaultNoArgConstructor(Class<?> type)
Does the given class have a default no-arg constructor (public or inherited).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 mapstatic boolean
isAssignableFrom(Class<?> a, Class<?> b)
Returns true if a class is assignable from another class like theClass.isAssignableFrom(Class)
method but which also includes coercion between primitive types to deal with Java 5 primitive type wrappingstatic boolean
isEmpty(String value)
Tests whether the value is null or an empty string or an empty collection/map.static boolean
isEmpty(Collection<?> value)
Tests whether the value is null or an an empty collectionstatic boolean
isEmpty(Map<?,?> value)
Tests whether the value is null or an an empty mapstatic <T> boolean
isEmpty(T value)
Tests whether the value is null, an empty string or an empty collection/map.static boolean
isEqualToAny(Object object, Object... values)
Returns true if the given object is equal to any of the expected valuestatic boolean
isNaN(Object value)
Is the given value a numeric NaN typestatic boolean
isNotEmpty(String value)
Tests whether the value is not null or an empty stringstatic boolean
isNotEmpty(Collection<?> value)
Tests whether the value is null or an an empty collectionstatic boolean
isNotEmpty(Map<?,?> value)
Tests whether the value is null or an an empty mapstatic <T> boolean
isNotEmpty(T 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 givenclazz
type is a Java primitive array type.static boolean
isSubclass(Class<?> clazz, Class<?> subClass)
Checks if the given class has a subclass (extends or implements)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 classstatic Class<?>
loadClass(String name, ClassLoader loader)
Attempts to load the given class name using the thread context class loader or the given class loaderstatic 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 loaderstatic 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 classstatic 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 classstatic 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 classstatic 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 classstatic 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 classstatic 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 classstatic Class<?>
loadSimpleType(String name)
Load a simple typestatic String
lookupConstantFieldValue(Class<?> clazz, String name)
Lookup the constant field on the given class with the given namestatic boolean
matches(List<?> list)
Returns the predicate matching boolean on aList
result set where if the first element is a boolean its value is used otherwise this method returns true if the collection is not emptystatic String
name(Class<?> type)
Returns the type name of the given type or null if the type variable is nullstatic <T> T
notNull(T value, String name)
Asserts whether the value is not nullstatic <T> T
notNull(T value, String name, Object on)
Asserts whether the value is not nullstatic <T> T
supplyIfEmpty(T value, Supplier<T> supplier)
Tests whether the value is null, an empty string, an empty collection or a mapstatic Boolean
toBoolean(Object value)
static Object
type(Object bean)
Returns the type of the given object or null if the value is nullstatic RuntimeException
wrapRuntimeCamelException(Throwable e)
Deprecated.Useorg.apache.camel.RuntimeCamelException#wrapRuntimeCamelException
instead
-
-
-
Method Detail
-
equal
public static boolean equal(Object a, Object b)
A helper method for comparing objects for equality while handling nulls
-
equalIgnoreCase
public static boolean equalIgnoreCase(Object a, Object b)
A helper method for comparing objects for equality while handling case insensitivity
-
equal
public static boolean equal(Object a, Object b, boolean ignoreCase)
A helper method for comparing objects for equality while handling nulls
-
equalByteArray
public static boolean equalByteArray(byte[] a, byte[] b)
A helper method for comparing byte arrays for equality while handling nulls
-
isEqualToAny
public static boolean isEqualToAny(Object object, Object... values)
Returns true if the given object is equal to any of the expected value
-
notNull
public static <T> T notNull(T value, String name)
Asserts whether the value is not null- Parameters:
value
- the value to testname
- the key that resolved the value- Returns:
- the passed
value
as is - Throws:
IllegalArgumentException
- is thrown if assertion fails
-
notNull
public static <T> T notNull(T value, String name, Object on)
Asserts whether the value is not null- Parameters:
value
- the value to teston
- additional description to indicate where this problem occurred (appended as toString())name
- the key that resolved the value- Returns:
- the passed
value
as is - Throws:
IllegalArgumentException
- is thrown if assertion fails
-
isEmpty
public static boolean isEmpty(String value)
Tests whether the value is null or an empty string or an empty collection/map.- Parameters:
value
- the value, if its a String it will be tested for text length as well- Returns:
- true if empty
-
isEmpty
public static boolean isEmpty(Collection<?> value)
Tests whether the value is null or an an empty collection- Parameters:
value
- the value to test- Returns:
- true if empty
-
isEmpty
public static boolean isEmpty(Map<?,?> value)
Tests whether the value is null or an an empty map- Parameters:
value
- the value to test- Returns:
- true if empty
-
isEmpty
public static <T> boolean isEmpty(T value)
Tests whether the value is null, an empty string or an empty collection/map.- Parameters:
value
- the value, if its a String it will be tested for text length as well- Returns:
- true if empty
-
isNotEmpty
public static <T> boolean isNotEmpty(T value)
Tests whether the value is not null, an empty string or an empty collection/map.- Parameters:
value
- the value, if its a String it will be tested for text length as well- Returns:
- true if not empty
-
isNotEmpty
public static boolean isNotEmpty(String value)
Tests whether the value is not null or an empty string- Parameters:
value
- the value, if its a String it will be tested for text length as well- Returns:
- true if not empty
-
isNotEmpty
public static boolean isNotEmpty(Collection<?> value)
Tests whether the value is null or an an empty collection- Parameters:
value
- the value to test- Returns:
- true if empty
-
isNotEmpty
public static boolean isNotEmpty(Map<?,?> value)
Tests whether the value is null or an an empty map- Parameters:
value
- the value to test- Returns:
- true if empty
-
firstNotNull
public static Optional<Object> firstNotNull(Object... values)
Returns the first non null object null.- Parameters:
values
- the values- Returns:
- an Optional
-
supplyIfEmpty
public static <T> T supplyIfEmpty(T value, Supplier<T> supplier)
Tests whether the value is null, an empty string, an empty collection or a map- Parameters:
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 null
-
ifNotEmpty
public 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- Parameters:
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 empty
-
matches
public static boolean matches(List<?> list)
Returns the predicate matching boolean on aList
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- Returns:
- true if the first element is a boolean and its value is true or if the list is non empty
-
getSystemProperty
public static String getSystemProperty(String name, String defaultValue)
A helper method to access a system property, catching any security exceptions- Parameters:
name
- the name of the system property requireddefaultValue
- the default value to use if the property is not available or a security exception prevents access- Returns:
- the system property value or the default value if the property is not available or security does not allow its access
-
getSystemProperty
public static boolean getSystemProperty(String name, Boolean defaultValue)
A helper method to access a boolean system property, catching any security exceptions- Parameters:
name
- the name of the system property requireddefaultValue
- the default value to use if the property is not available or a security exception prevents access- Returns:
- the boolean representation of the system property value or the default value if the property is not available or security does not allow its access
-
name
public static String name(Class<?> type)
Returns the type name of the given type or null if the type variable is null
-
classCanonicalName
public static String classCanonicalName(Object value)
Returns the canonical type name of the given value
-
loadClass
public 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- Parameters:
name
- the name of the class to load- Returns:
- the class or null if it could not be loaded
-
loadClass
public 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- Parameters:
name
- the name of the class to loadloader
- the class loader to use after the thread context class loader- Returns:
- the class or null if it could not be loaded
-
loadClass
public 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- Parameters:
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 loaded- Returns:
- the class or null if it could not be loaded
-
loadSimpleType
public static Class<?> loadSimpleType(String name)
Load a simple type- Parameters:
name
- the name of the class to load- Returns:
- the class or null if it could not be loaded
-
loadResourceAsStream
public 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- Parameters:
name
- the name of the resource to load- Returns:
- the stream or null if it could not be loaded
-
loadResourceAsStream
public 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- Parameters:
name
- the name of the resource to loadloader
- optional classloader to attempt first- Returns:
- the stream or null if it could not be loaded
-
loadResourceAsURL
public 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- Parameters:
name
- the name of the resource to load- Returns:
- the stream or null if it could not be loaded
-
loadResourceAsURL
public 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- Parameters:
name
- the name of the resource to loadloader
- optional classloader to attempt first- Returns:
- the stream or null if it could not be loaded
-
loadResourcesAsURL
public 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- Parameters:
uri
- the name of the package to load its resources- Returns:
- the URLs for the resources or null if it could not be loaded
-
loadResourcesAsURL
public 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- Parameters:
uri
- the name of the package to load its resourcesloader
- optional classloader to attempt first- Returns:
- the URLs for the resources or null if it could not be loaded
-
isOverridingMethod
public static boolean isOverridingMethod(Method source, Method target)
Tests whether the target method overrides the source method. Tests whether they have the same name, return type, and parameter list.- Parameters:
source
- the source methodtarget
- the target method- Returns:
- true if it override, false otherwise
-
isOverridingMethod
public static boolean isOverridingMethod(Method source, Method target, boolean exact)
Tests whether the target method overrides the source method. Tests whether they have the same name, return type, and parameter list.- Parameters:
source
- the source methodtarget
- the target methodexact
- true if the override must be exact same types, false if the types should be assignable- Returns:
- true if it override, false otherwise
-
isOverridingMethod
public static boolean isOverridingMethod(Class<?> inheritingClass, Method source, Method target, boolean exact)
Tests whether the target method overrides the source method from the inheriting class. Tests whether they have the same name, return type, and parameter list.- Parameters:
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 assignable- Returns:
- true if it override, false otherwise
-
findMethodsWithAnnotation
public static List<Method> findMethodsWithAnnotation(Class<?> type, Class<? extends Annotation> annotationType)
Returns a list of methods which are annotated with the given annotation- Parameters:
type
- the type to reflect onannotationType
- the annotation type- Returns:
- a list of the methods found
-
findMethodsWithAnnotation
public static List<Method> findMethodsWithAnnotation(Class<?> type, Class<? extends Annotation> annotationType, boolean checkMetaAnnotations)
Returns a list of methods which are annotated with the given annotation- Parameters:
type
- the type to reflect onannotationType
- the annotation typecheckMetaAnnotations
- check for meta annotations- Returns:
- a list of the methods found
-
hasAnnotation
public static boolean hasAnnotation(AnnotatedElement elem, Class<? extends Annotation> annotationType, boolean checkMetaAnnotations)
Checks if a Class or Method are annotated with the given annotation- Parameters:
elem
- the Class or Method to reflect onannotationType
- the annotation typecheckMetaAnnotations
- check for meta annotations- Returns:
- true if annotations is present
-
asString
public static String asString(Object[] objects)
Turns the given object arrays into a meaningful string- Parameters:
objects
- an array of objects or null- Returns:
- a meaningful string
-
isAssignableFrom
public static boolean isAssignableFrom(Class<?> a, Class<?> b)
Returns true if a class is assignable from another class like theClass.isAssignableFrom(Class)
method but which also includes coercion between primitive types to deal with Java 5 primitive type wrapping
-
isPrimitiveArrayType
public static boolean isPrimitiveArrayType(Class<?> clazz)
Returns if the givenclazz
type is a Java primitive array type.- Parameters:
clazz
- the Java type to be checked- Returns:
true
if the given type is a Java primitive array type
-
isSubclass
public static boolean isSubclass(Class<?> clazz, Class<?> subClass)
Checks if the given class has a subclass (extends or implements)- Parameters:
clazz
- the classsubClass
- the subclass (class or interface)
-
arrayLength
public static int arrayLength(Object[] pojo)
Used by camel-bean
-
convertPrimitiveTypeToWrapperType
public static Class<?> convertPrimitiveTypeToWrapperType(Class<?> type)
Converts primitive types such as int to its wrapper type likeInteger
-
convertWrapperTypeToPrimitiveType
public static Class<?> convertWrapperTypeToPrimitiveType(Class<?> type)
Converts wrapper type likeInteger
to its primitive type, i.e. int.
-
getDefaultCharacterSet
public static String getDefaultCharacterSet()
Helper method to return the default character set name
-
getPropertyName
public static String getPropertyName(Method method)
Returns the Java Bean property name of the given method, if it is a setter
-
hasAnnotation
public static boolean hasAnnotation(Annotation[] annotations, Class<?> type)
Returns true if the given collection of annotations matches the given type
-
getAnnotation
public static <A extends Annotation> A getAnnotation(Object instance, Class<A> type)
Gets the annotation from the given instance.- Parameters:
instance
- the instancetype
- the annotation- Returns:
- the annotation, or null if the instance does not have the given annotation
-
cast
public static <T> T cast(Class<T> toType, Object value)
Converts the given value to the required type or throw a meaningful exception
-
hasDefaultPublicNoArgConstructor
public static boolean hasDefaultPublicNoArgConstructor(Class<?> type)
Does the given class have a default public no-arg constructor.
-
hasDefaultNoArgConstructor
public static boolean hasDefaultNoArgConstructor(Class<?> type)
Does the given class have a default no-arg constructor (public or inherited).
-
type
public static Object type(Object bean)
Returns the type of the given object or null if the value is null
-
evaluateValuePredicate
public 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
-
createExceptionIterable
public 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).- Parameters:
exception
- the exception- Returns:
- the Iterable
- See Also:
Iterable
-
createExceptionIterator
public 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).- Parameters:
exception
- the exception- Returns:
- the Iterator
- See Also:
Iterator
-
getException
public static <T> T getException(Class<T> type, Throwable exception)
Retrieves the given exception type from the exception. Is used to get the caused exception that typically have been wrapped in some sort of Camel wrapper exception The strategy is to look in the exception hierarchy to find the first given cause that matches the type. Will start from the bottom (the real cause) and walk upwards.- Parameters:
type
- the exception type wanted to retrieveexception
- the caused exception- Returns:
- the exception found (or null if not found in the exception hierarchy)
-
getIdentityHashCode
public static String getIdentityHashCode(Object object)
-
lookupConstantFieldValue
public static String lookupConstantFieldValue(Class<?> clazz, String name)
Lookup the constant field on the given class with the given name- Parameters:
clazz
- the classname
- the name of the field to lookup- Returns:
- the value of the constant field, or null if not found
-
isNaN
public static boolean isNaN(Object value)
Is the given value a numeric NaN type- Parameters:
value
- the value- Returns:
- true if its a
Float.NaN
orDouble.NaN
.
-
wrapRuntimeCamelException
@Deprecated public static RuntimeException wrapRuntimeCamelException(Throwable e)
Deprecated.Useorg.apache.camel.RuntimeCamelException#wrapRuntimeCamelException
insteadWraps the caused exception in aRuntimeException
if its not already such an exception.- Parameters:
e
- the caused exception- Returns:
- the wrapper exception
-
asList
public static List<Object> asList(Object[] objects)
Turns the input array to a list of objects.- Parameters:
objects
- an array of objects or null- Returns:
- an object list
-
addListByIndex
public static void addListByIndex(List<Object> list, int idx, Object value)
Adds the value to the list at the given index
-
-