001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.util;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.lang.annotation.Annotation;
022import java.lang.reflect.AnnotatedElement;
023import java.lang.reflect.Constructor;
024import java.lang.reflect.Field;
025import java.lang.reflect.Method;
026import java.net.URL;
027import java.nio.charset.Charset;
028import java.util.ArrayList;
029import java.util.Arrays;
030import java.util.Collection;
031import java.util.Collections;
032import java.util.Enumeration;
033import java.util.Iterator;
034import java.util.List;
035import java.util.Locale;
036import java.util.Map;
037import java.util.Objects;
038import java.util.Optional;
039import java.util.function.Consumer;
040import java.util.function.Supplier;
041
042import org.w3c.dom.Node;
043import org.w3c.dom.NodeList;
044
045import org.slf4j.Logger;
046import org.slf4j.LoggerFactory;
047
048/**
049 * A number of useful helper methods for working with Objects
050 */
051public final class ObjectHelper {
052
053    private static final Logger LOG = LoggerFactory.getLogger(ObjectHelper.class);
054
055    private static final Float FLOAT_NAN = Float.NaN;
056    private static final Double DOUBLE_NAN = Double.NaN;
057
058    /**
059     * Utility classes should not have a public constructor.
060     */
061    private ObjectHelper() {
062    }
063
064    /**
065     * A helper method for comparing objects for equality while handling nulls
066     */
067    public static boolean equal(Object a, Object b) {
068        return equal(a, b, false);
069    }
070
071    /**
072     * A helper method for comparing objects for equality while handling case insensitivity
073     */
074    public static boolean equalIgnoreCase(Object a, Object b) {
075        return equal(a, b, true);
076    }
077
078    /**
079     * A helper method for comparing objects for equality while handling nulls
080     */
081    public static boolean equal(final Object a, final Object b, final boolean ignoreCase) {
082        if (a == b) {
083            return true;
084        }
085
086        if (a == null || b == null) {
087            return false;
088        }
089
090        if (ignoreCase) {
091            if (a instanceof String && b instanceof String) {
092                return ((String) a).equalsIgnoreCase((String) b);
093            }
094        }
095
096        if (a.getClass().isArray() && b.getClass().isArray()) {
097            // uses array based equals
098            return Objects.deepEquals(a, b);
099        } else {
100            // use regular equals
101            return a.equals(b);
102        }
103    }
104
105    /**
106     * A helper method for comparing byte arrays for equality while handling
107     * nulls
108     */
109    public static boolean equalByteArray(byte[] a, byte[] b) {
110        return Arrays.equals(a, b);
111    }
112
113    /**
114     * Returns true if the given object is equal to any of the expected value
115     */
116    public static boolean isEqualToAny(Object object, Object... values) {
117        for (Object value : values) {
118            if (equal(object, value)) {
119                return true;
120            }
121        }
122        return false;
123    }
124
125    public static Boolean toBoolean(Object value) {
126        if (value instanceof Boolean) {
127            return (Boolean) value;
128        }
129        if (value instanceof String) {
130            // we only want to accept true or false as accepted values
131            String str = (String) value;
132            if ("true".equalsIgnoreCase(str) || "false".equalsIgnoreCase(str)) {
133                return Boolean.valueOf(str);
134            }
135        }
136        if (value instanceof Integer) {
137            return (Integer) value > 0 ? Boolean.TRUE : Boolean.FALSE;
138        }
139        return null;
140    }
141
142    /**
143     * Asserts whether the value is <b>not</b> <tt>null</tt>
144     *
145     * @param value  the value to test
146     * @param name   the key that resolved the value
147     * @return the passed {@code value} as is
148     * @throws IllegalArgumentException is thrown if assertion fails
149     */
150    public static <T> T notNull(T value, String name) {
151        if (value == null) {
152            throw new IllegalArgumentException(name + " must be specified");
153        }
154
155        return value;
156    }
157
158    /**
159     * Asserts whether the value is <b>not</b> <tt>null</tt>
160     *
161     * @param value  the value to test
162     * @param on     additional description to indicate where this problem occurred (appended as toString())
163     * @param name   the key that resolved the value
164     * @return the passed {@code value} as is
165     * @throws IllegalArgumentException is thrown if assertion fails
166     */
167    public static <T> T notNull(T value, String name, Object on) {
168        if (on == null) {
169            notNull(value, name);
170        } else if (value == null) {
171            throw new IllegalArgumentException(name + " must be specified on: " + on);
172        }
173
174        return value;
175    }
176
177    /**
178     * Tests whether the value is <tt>null</tt> or an empty string.
179     *
180     * @param value  the value, if its a String it will be tested for text length as well
181     * @return true if empty
182     */
183    public static boolean isEmpty(Object value) {
184        return !isNotEmpty(value);
185    }
186
187    /**
188     * Tests whether the value is <b>not</b> <tt>null</tt>, an empty string or an empty collection/map.
189     *
190     * @param value  the value, if its a String it will be tested for text length as well
191     * @return true if <b>not</b> empty
192     */
193    public static boolean isNotEmpty(Object value) {
194        if (value == null) {
195            return false;
196        } else if (value instanceof String) {
197            String text = (String) value;
198            return text.trim().length() > 0;
199        } else if (value instanceof Collection) {
200            return !((Collection<?>) value).isEmpty();
201        } else if (value instanceof Map) {
202            return !((Map<?, ?>) value).isEmpty();
203        } else {
204            return true;
205        }
206    }
207
208
209    /**
210     * Returns the first non null object <tt>null</tt>.
211     *
212     * @param values the values
213     * @return an Optional
214     */
215    public static Optional<Object> firstNotNull(Object... values) {
216        for (Object value : values) {
217            if (value != null) {
218                return Optional.of(value);
219            }
220        }
221
222        return Optional.empty();
223    }
224
225    /**
226     * Tests whether the value is  <tt>null</tt>, an empty string, an empty collection or a map
227     *
228     * @param value  the value, if its a String it will be tested for text length as well
229     * @param supplier  the supplier, the supplier to be used to get a value if value is null
230     */
231    public static <T> T supplyIfEmpty(T value, Supplier<T> supplier) {
232        org.apache.camel.util.ObjectHelper.notNull(supplier, "Supplier");
233        if (isNotEmpty(value)) {
234            return value;
235        }
236
237        return supplier.get();
238    }
239
240    /**
241     * Tests whether the value is <b>not</b> <tt>null</tt>, an empty string, an empty collection or a map
242     *
243     * @param value  the value, if its a String it will be tested for text length as well
244     * @param consumer  the consumer, the operation to be executed against value if not empty
245     */
246    public static <T> void ifNotEmpty(T value, Consumer<T> consumer) {
247        if (isNotEmpty(value)) {
248            consumer.accept(value);
249        }
250    }
251
252    /**
253     * Returns the predicate matching boolean on a {@link List} result set where
254     * if the first element is a boolean its value is used otherwise this method
255     * returns true if the collection is not empty
256     *
257     * @return <tt>true</tt> if the first element is a boolean and its value
258     *         is true or if the list is non empty
259     */
260    public static boolean matches(List<?> list) {
261        if (!list.isEmpty()) {
262            Object value = list.get(0);
263            if (value instanceof Boolean) {
264                return (Boolean) value;
265            } else {
266                // lets assume non-empty results are true
267                return true;
268            }
269        }
270        return false;
271    }
272
273    /**
274     * A helper method to access a system property, catching any security exceptions
275     *
276     * @param name         the name of the system property required
277     * @param defaultValue the default value to use if the property is not
278     *                     available or a security exception prevents access
279     * @return the system property value or the default value if the property is
280     *         not available or security does not allow its access
281     */
282    public static String getSystemProperty(String name, String defaultValue) {
283        try {
284            return System.getProperty(name, defaultValue);
285        } catch (Exception e) {
286            if (LOG.isDebugEnabled()) {
287                LOG.debug("Caught security exception accessing system property: " + name + ". Will use default value: " + defaultValue, e);
288            }
289            return defaultValue;
290        }
291    }
292
293    /**
294     * A helper method to access a boolean system property, catching any
295     * security exceptions
296     *
297     * @param name         the name of the system property required
298     * @param defaultValue the default value to use if the property is not
299     *                     available or a security exception prevents access
300     * @return the boolean representation of the system property value or the
301     *         default value if the property is not available or security does
302     *         not allow its access
303     */
304    public static boolean getSystemProperty(String name, Boolean defaultValue) {
305        String result = getSystemProperty(name, defaultValue.toString());
306        return Boolean.parseBoolean(result);
307    }
308
309    /**
310     * Returns the type name of the given type or null if the type variable is
311     * null
312     */
313    public static String name(Class<?> type) {
314        return type != null ? type.getName() : null;
315    }
316
317    /**
318     * Returns the type name of the given value
319     */
320    public static String className(Object value) {
321        return name(value != null ? value.getClass() : null);
322    }
323
324    /**
325     * Returns the canonical type name of the given value
326     */
327    public static String classCanonicalName(Object value) {
328        if (value != null) {
329            return value.getClass().getCanonicalName();
330        } else {
331            return null;
332        }
333    }
334
335    /**
336     * Attempts to load the given class name using the thread context class
337     * loader or the class loader used to load this class
338     *
339     * @param name the name of the class to load
340     * @return the class or <tt>null</tt> if it could not be loaded
341     */
342    public static Class<?> loadClass(String name) {
343        return loadClass(name, ObjectHelper.class.getClassLoader());
344    }
345
346    /**
347     * Attempts to load the given class name using the thread context class
348     * loader or the given class loader
349     *
350     * @param name the name of the class to load
351     * @param loader the class loader to use after the thread context class loader
352     * @return the class or <tt>null</tt> if it could not be loaded
353     */
354    public static Class<?> loadClass(String name, ClassLoader loader) {
355        return loadClass(name, loader, false);
356    }
357
358    /**
359     * Attempts to load the given class name using the thread context class
360     * loader or the given class loader
361     *
362     * @param name the name of the class to load
363     * @param loader the class loader to use after the thread context class loader
364     * @param needToWarn when <tt>true</tt> logs a warning when a class with the given name could not be loaded
365     * @return the class or <tt>null</tt> if it could not be loaded
366     */
367    public static Class<?> loadClass(String name, ClassLoader loader, boolean needToWarn) {
368        // must clean the name so its pure java name, eg removing \n or whatever people can do in the Spring XML
369        name = StringHelper.normalizeClassName(name);
370        if (org.apache.camel.util.ObjectHelper.isEmpty(name)) {
371            return null;
372        }
373
374        // Try simple type first
375        Class<?> clazz = loadSimpleType(name);
376        if (clazz == null) {
377            // try context class loader
378            clazz = doLoadClass(name, Thread.currentThread().getContextClassLoader());
379        }
380        if (clazz == null) {
381            // then the provided loader
382            clazz = doLoadClass(name, loader);
383        }
384        if (clazz == null) {
385            // and fallback to the loader the loaded the ObjectHelper class
386            clazz = doLoadClass(name, ObjectHelper.class.getClassLoader());
387        }
388
389        if (clazz == null) {
390            if (needToWarn) {
391                LOG.warn("Cannot find class: {}", name);
392            } else {
393                LOG.debug("Cannot find class: {}", name);
394            }
395        }
396
397        return clazz;
398    }
399
400
401    /**
402     * Load a simple type
403     *
404     * @param name the name of the class to load
405     * @return the class or <tt>null</tt> if it could not be loaded
406     */
407    //CHECKSTYLE:OFF
408    public static Class<?> loadSimpleType(String name) {
409        // special for byte[] or Object[] as its common to use
410        if ("java.lang.byte[]".equals(name) || "byte[]".equals(name)) {
411            return byte[].class;
412        } else if ("java.lang.Byte[]".equals(name) || "Byte[]".equals(name)) {
413            return Byte[].class;
414        } else if ("java.lang.Object[]".equals(name) || "Object[]".equals(name)) {
415            return Object[].class;
416        } else if ("java.lang.String[]".equals(name) || "String[]".equals(name)) {
417            return String[].class;
418            // and these is common as well
419        } else if ("java.lang.String".equals(name) || "String".equals(name)) {
420            return String.class;
421        } else if ("java.lang.Boolean".equals(name) || "Boolean".equals(name)) {
422            return Boolean.class;
423        } else if ("boolean".equals(name)) {
424            return boolean.class;
425        } else if ("java.lang.Integer".equals(name) || "Integer".equals(name)) {
426            return Integer.class;
427        } else if ("int".equals(name)) {
428            return int.class;
429        } else if ("java.lang.Long".equals(name) || "Long".equals(name)) {
430            return Long.class;
431        } else if ("long".equals(name)) {
432            return long.class;
433        } else if ("java.lang.Short".equals(name) || "Short".equals(name)) {
434            return Short.class;
435        } else if ("short".equals(name)) {
436            return short.class;
437        } else if ("java.lang.Byte".equals(name) || "Byte".equals(name)) {
438            return Byte.class;
439        } else if ("byte".equals(name)) {
440            return byte.class;
441        } else if ("java.lang.Float".equals(name) || "Float".equals(name)) {
442            return Float.class;
443        } else if ("float".equals(name)) {
444            return float.class;
445        } else if ("java.lang.Double".equals(name) || "Double".equals(name)) {
446            return Double.class;
447        } else if ("double".equals(name)) {
448            return double.class;
449        } else if ("java.lang.Character".equals(name) || "Character".equals(name)) {
450            return Character.class;
451        } else if ("char".equals(name)) {
452            return char.class;
453        }
454        return null;
455    }
456    //CHECKSTYLE:ON
457
458    /**
459     * Loads the given class with the provided classloader (may be null).
460     * Will ignore any class not found and return null.
461     *
462     * @param name    the name of the class to load
463     * @param loader  a provided loader (may be null)
464     * @return the class, or null if it could not be loaded
465     */
466    private static Class<?> doLoadClass(String name, ClassLoader loader) {
467        StringHelper.notEmpty(name, "name");
468        if (loader == null) {
469            return null;
470        }
471
472        try {
473            LOG.trace("Loading class: {} using classloader: {}", name, loader);
474            return loader.loadClass(name);
475        } catch (ClassNotFoundException e) {
476            if (LOG.isTraceEnabled()) {
477                LOG.trace("Cannot load class: " + name + " using classloader: " + loader, e);
478            }
479        }
480
481        return null;
482    }
483
484    /**
485     * Attempts to load the given resource as a stream using the thread context
486     * class loader or the class loader used to load this class
487     *
488     * @param name the name of the resource to load
489     * @return the stream or null if it could not be loaded
490     */
491    public static InputStream loadResourceAsStream(String name) {
492        return loadResourceAsStream(name, null);
493    }
494
495    /**
496     * Attempts to load the given resource as a stream using 
497     * first the given class loader, then the thread context
498     * class loader and finally the class loader used to load this class
499     *
500     * @param name the name of the resource to load
501     * @param loader optional classloader to attempt first
502     * @return the stream or null if it could not be loaded
503     */
504    public static InputStream loadResourceAsStream(String name, ClassLoader loader) {
505        try {
506            URL res = loadResourceAsURL(name, loader);
507            return res != null ? res.openStream() : null;
508        } catch (IOException e) {
509            return null;
510        }
511    }
512
513    /**
514     * Attempts to load the given resource as a stream using the thread context
515     * class loader or the class loader used to load this class
516     *
517     * @param name the name of the resource to load
518     * @return the stream or null if it could not be loaded
519     */
520    public static URL loadResourceAsURL(String name) {
521        return loadResourceAsURL(name, null);
522    }
523
524    /**
525     * Attempts to load the given resource as a stream using the thread context
526     * class loader or the class loader used to load this class
527     *
528     * @param name the name of the resource to load
529     * @param loader optional classloader to attempt first
530     * @return the stream or null if it could not be loaded
531     */
532    public static URL loadResourceAsURL(String name, ClassLoader loader) {
533
534        URL url = null;
535        String resolvedName = resolveUriPath(name);
536
537        // #1 First, try the given class loader
538
539        if (loader != null) {
540            url = loader.getResource(resolvedName);
541            if (url != null) {
542                return url;
543            }
544        }
545
546        // #2 Next, is the TCCL
547
548        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
549        if (tccl != null) {
550
551            url = tccl.getResource(resolvedName);
552            if (url != null) {
553                return url;
554            }
555
556            // #3 The TCCL may be able to see camel-core, but not META-INF resources
557
558            try {
559
560                Class<?> clazz = tccl.loadClass("org.apache.camel.impl.DefaultCamelContext");
561                url = clazz.getClassLoader().getResource(resolvedName);
562                if (url != null) {
563                    return url;
564                }
565
566            } catch (ClassNotFoundException e) {
567                // ignore
568            }
569        }
570
571        // #4 Last, for the unlikely case that stuff can be loaded from camel-util
572
573        url = ObjectHelper.class.getClassLoader().getResource(resolvedName);
574        if (url != null) {
575            return url;
576        }
577
578        url = ObjectHelper.class.getResource(resolvedName);
579        return url;
580    }
581
582    /**
583     * Attempts to load the given resources from the given package name using the thread context
584     * class loader or the class loader used to load this class
585     *
586     * @param uri the name of the package to load its resources
587     * @return the URLs for the resources or null if it could not be loaded
588     */
589    public static Enumeration<URL> loadResourcesAsURL(String uri) {
590        return loadResourcesAsURL(uri, null);
591    }
592
593    /**
594     * Attempts to load the given resources from the given package name using the thread context
595     * class loader or the class loader used to load this class
596     *
597     * @param uri the name of the package to load its resources
598     * @param loader optional classloader to attempt first
599     * @return the URLs for the resources or null if it could not be loaded
600     */
601    public static Enumeration<URL> loadResourcesAsURL(String uri, ClassLoader loader) {
602
603        Enumeration<URL> res = null;
604
605        // #1 First, try the given class loader
606
607        if (loader != null) {
608            try {
609                res = loader.getResources(uri);
610                if (res != null) {
611                    return res;
612                }
613            } catch (IOException e) {
614                // ignore
615            }
616        }
617
618        // #2 Next, is the TCCL
619
620        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
621        if (tccl != null) {
622
623            try {
624                res = tccl.getResources(uri);
625                if (res != null) {
626                    return res;
627                }
628            } catch (IOException e1) {
629                // ignore
630            }
631
632            // #3 The TCCL may be able to see camel-core, but not META-INF resources
633
634            try {
635
636                Class<?> clazz = tccl.loadClass("org.apache.camel.impl.DefaultCamelContext");
637                res = clazz.getClassLoader().getResources(uri);
638                if (res != null) {
639                    return res;
640                }
641
642            } catch (ClassNotFoundException | IOException e) {
643                // ignore
644            }
645        }
646
647        // #4 Last, for the unlikely case that stuff can be loaded from camel-util
648
649        try {
650            res = ObjectHelper.class.getClassLoader().getResources(uri);
651        } catch (IOException e) {
652            // ignore
653        }
654
655        return res;
656    }
657
658    /**
659     * Helper operation used to remove relative path notation from 
660     * resources.  Most critical for resources on the Classpath
661     * as resource loaders will not resolve the relative paths correctly.
662     *
663     * @param name the name of the resource to load
664     * @return the modified or unmodified string if there were no changes
665     */
666    private static String resolveUriPath(String name) {
667        // compact the path and use / as separator as that's used for loading resources on the classpath
668        return FileUtil.compactPath(name, '/');
669    }
670
671    /**
672     * Tests whether the target method overrides the source method.
673     * <p/>
674     * Tests whether they have the same name, return type, and parameter list.
675     *
676     * @param source  the source method
677     * @param target  the target method
678     * @return <tt>true</tt> if it override, <tt>false</tt> otherwise
679     */
680    public static boolean isOverridingMethod(Method source, Method target) {
681        return isOverridingMethod(source, target, true);
682    }
683
684    /**
685     * Tests whether the target method overrides the source method.
686     * <p/>
687     * Tests whether they have the same name, return type, and parameter list.
688     *
689     * @param source  the source method
690     * @param target  the target method
691     * @param exact   <tt>true</tt> if the override must be exact same types, <tt>false</tt> if the types should be assignable
692     * @return <tt>true</tt> if it override, <tt>false</tt> otherwise
693     */
694    public static boolean isOverridingMethod(Method source, Method target, boolean exact) {
695        return isOverridingMethod(target.getDeclaringClass(), source, target, exact);
696    }
697
698    /**
699     * Tests whether the target method overrides the source method from the
700     * inheriting class.
701     * <p/>
702     * Tests whether they have the same name, return type, and parameter list.
703     *
704     * @param inheritingClass the class inheriting the target method overriding
705     *            the source method
706     * @param source the source method
707     * @param target the target method
708     * @param exact <tt>true</tt> if the override must be exact same types,
709     *            <tt>false</tt> if the types should be assignable
710     * @return <tt>true</tt> if it override, <tt>false</tt> otherwise
711     */
712    public static boolean isOverridingMethod(Class<?> inheritingClass, Method source, Method target, boolean exact) {
713
714        if (source.equals(target)) {
715            return true;
716        } else if (target.getDeclaringClass().isAssignableFrom(source.getDeclaringClass())) {
717            return false;
718        } else if (!source.getDeclaringClass().isAssignableFrom(inheritingClass) || !target.getDeclaringClass().isAssignableFrom(inheritingClass)) {
719            return false;
720        }
721
722        if (!source.getName().equals(target.getName())) {
723            return false;
724        }
725
726        if (exact) {
727            if (!source.getReturnType().equals(target.getReturnType())) {
728                return false;
729            }
730        } else {
731            if (!source.getReturnType().isAssignableFrom(target.getReturnType())) {
732                boolean b1 = source.isBridge();
733                boolean b2 = target.isBridge();
734                // must not be bridge methods
735                if (!b1 && !b2) {
736                    return false;
737                }
738            }
739        }
740
741        // must have same number of parameter types
742        if (source.getParameterCount() != target.getParameterCount()) {
743            return false;
744        }
745
746        Class<?>[] sourceTypes = source.getParameterTypes();
747        Class<?>[] targetTypes = target.getParameterTypes();
748        // test if parameter types is the same as well
749        for (int i = 0; i < source.getParameterCount(); i++) {
750            if (exact) {
751                if (!(sourceTypes[i].equals(targetTypes[i]))) {
752                    return false;
753                }
754            } else {
755                if (!(sourceTypes[i].isAssignableFrom(targetTypes[i]))) {
756                    boolean b1 = source.isBridge();
757                    boolean b2 = target.isBridge();
758                    // must not be bridge methods
759                    if (!b1 && !b2) {
760                        return false;
761                    }
762                }
763            }
764        }
765
766        // the have same name, return type and parameter list, so its overriding
767        return true;
768    }
769
770    /**
771     * Returns a list of methods which are annotated with the given annotation
772     *
773     * @param type the type to reflect on
774     * @param annotationType the annotation type
775     * @return a list of the methods found
776     */
777    public static List<Method> findMethodsWithAnnotation(Class<?> type,
778                                                         Class<? extends Annotation> annotationType) {
779        return findMethodsWithAnnotation(type, annotationType, false);
780    }
781
782    /**
783     * Returns a list of methods which are annotated with the given annotation
784     *
785     * @param type the type to reflect on
786     * @param annotationType the annotation type
787     * @param checkMetaAnnotations check for meta annotations
788     * @return a list of the methods found
789     */
790    public static List<Method> findMethodsWithAnnotation(Class<?> type,
791                                                         Class<? extends Annotation> annotationType,
792                                                         boolean checkMetaAnnotations) {
793        List<Method> answer = new ArrayList<>();
794        do {
795            Method[] methods = type.getDeclaredMethods();
796            for (Method method : methods) {
797                if (hasAnnotation(method, annotationType, checkMetaAnnotations)) {
798                    answer.add(method);
799                }
800            }
801            type = type.getSuperclass();
802        } while (type != null);
803        return answer;
804    }
805
806    /**
807     * Checks if a Class or Method are annotated with the given annotation
808     *
809     * @param elem the Class or Method to reflect on
810     * @param annotationType the annotation type
811     * @param checkMetaAnnotations check for meta annotations
812     * @return true if annotations is present
813     */
814    public static boolean hasAnnotation(AnnotatedElement elem, Class<? extends Annotation> annotationType,
815                                        boolean checkMetaAnnotations) {
816        if (elem.isAnnotationPresent(annotationType)) {
817            return true;
818        }
819        if (checkMetaAnnotations) {
820            for (Annotation a : elem.getAnnotations()) {
821                for (Annotation meta : a.annotationType().getAnnotations()) {
822                    if (meta.annotationType().getName().equals(annotationType.getName())) {
823                        return true;
824                    }
825                }
826            }
827        }
828        return false;
829    }
830
831    /**
832     * Turns the given object arrays into a meaningful string
833     *
834     * @param objects an array of objects or null
835     * @return a meaningful string
836     */
837    public static String asString(Object[] objects) {
838        if (objects == null) {
839            return "null";
840        } else {
841            StringBuilder buffer = new StringBuilder("{");
842            int counter = 0;
843            for (Object object : objects) {
844                if (counter++ > 0) {
845                    buffer.append(", ");
846                }
847                String text = (object == null) ? "null" : object.toString();
848                buffer.append(text);
849            }
850            buffer.append("}");
851            return buffer.toString();
852        }
853    }
854
855    /**
856     * Returns true if a class is assignable from another class like the
857     * {@link Class#isAssignableFrom(Class)} method but which also includes
858     * coercion between primitive types to deal with Java 5 primitive type
859     * wrapping
860     */
861    public static boolean isAssignableFrom(Class<?> a, Class<?> b) {
862        a = convertPrimitiveTypeToWrapperType(a);
863        b = convertPrimitiveTypeToWrapperType(b);
864        return a.isAssignableFrom(b);
865    }
866
867    /**
868     * Returns if the given {@code clazz} type is a Java primitive array type.
869     *
870     * @param clazz the Java type to be checked
871     * @return {@code true} if the given type is a Java primitive array type
872     */
873    public static boolean isPrimitiveArrayType(Class<?> clazz) {
874        if (clazz != null && clazz.isArray()) {
875            return clazz.getComponentType().isPrimitive();
876        }
877        return false;
878    }
879
880    public static int arrayLength(Object[] pojo) {
881        return pojo.length;
882    }
883
884    /**
885     * Converts primitive types such as int to its wrapper type like
886     * {@link Integer}
887     */
888    public static Class<?> convertPrimitiveTypeToWrapperType(Class<?> type) {
889        Class<?> rc = type;
890        if (type.isPrimitive()) {
891            if (type == int.class) {
892                rc = Integer.class;
893            } else if (type == long.class) {
894                rc = Long.class;
895            } else if (type == double.class) {
896                rc = Double.class;
897            } else if (type == float.class) {
898                rc = Float.class;
899            } else if (type == short.class) {
900                rc = Short.class;
901            } else if (type == byte.class) {
902                rc = Byte.class;
903            } else if (type == boolean.class) {
904                rc = Boolean.class;
905            } else if (type == char.class) {
906                rc = Character.class;
907            }
908        }
909        return rc;
910    }
911
912    /**
913     * Helper method to return the default character set name
914     */
915    public static String getDefaultCharacterSet() {
916        return Charset.defaultCharset().name();
917    }
918
919    /**
920     * Returns the Java Bean property name of the given method, if it is a
921     * setter
922     */
923    public static String getPropertyName(Method method) {
924        String propertyName = method.getName();
925        if (propertyName.startsWith("set") && method.getParameterCount() == 1) {
926            propertyName = propertyName.substring(3, 4).toLowerCase(Locale.ENGLISH) + propertyName.substring(4);
927        }
928        return propertyName;
929    }
930
931    /**
932     * Returns true if the given collection of annotations matches the given type
933     */
934    public static boolean hasAnnotation(Annotation[] annotations, Class<?> type) {
935        for (Annotation annotation : annotations) {
936            if (type.isInstance(annotation)) {
937                return true;
938            }
939        }
940        return false;
941    }
942
943    /**
944     * Gets the annotation from the given instance.
945     *
946     * @param instance the instance
947     * @param type  the annotation
948     * @return the annotation, or <tt>null</tt> if the instance does not have the given annotation
949     */
950    public static <A extends java.lang.annotation.Annotation> A getAnnotation(Object instance, Class<A> type) {
951        return instance.getClass().getAnnotation(type);
952    }
953
954
955    /**
956     * Converts the given value to the required type or throw a meaningful exception
957     */
958    @SuppressWarnings("unchecked")
959    public static <T> T cast(Class<T> toType, Object value) {
960        if (toType == boolean.class) {
961            return (T) cast(Boolean.class, value);
962        } else if (toType.isPrimitive()) {
963            Class<?> newType = convertPrimitiveTypeToWrapperType(toType);
964            if (newType != toType) {
965                return (T) cast(newType, value);
966            }
967        }
968        try {
969            return toType.cast(value);
970        } catch (ClassCastException e) {
971            throw new IllegalArgumentException("Failed to convert: "
972                + value + " to type: " + toType.getName() + " due to: " + e, e);
973        }
974    }
975
976    /**
977     * Does the given class have a default public no-arg constructor.
978     */
979    public static boolean hasDefaultPublicNoArgConstructor(Class<?> type) {
980        // getConstructors() returns only public constructors
981        for (Constructor<?> ctr : type.getConstructors()) {
982            if (ctr.getParameterCount() == 0) {
983                return true;
984            }
985        }
986        return false;
987    }
988
989    /**
990     * Returns the type of the given object or null if the value is null
991     */
992    public static Object type(Object bean) {
993        return bean != null ? bean.getClass() : null;
994    }
995
996    /**
997     * Evaluate the value as a predicate which attempts to convert the value to
998     * a boolean otherwise true is returned if the value is not null
999     */
1000    public static boolean evaluateValuePredicate(Object value) {
1001        if (value instanceof Boolean) {
1002            return (Boolean) value;
1003        } else if (value instanceof String) {
1004            if ("true".equalsIgnoreCase((String) value)) {
1005                return true;
1006            } else if ("false".equalsIgnoreCase((String) value)) {
1007                return false;
1008            }
1009        } else if (value instanceof NodeList) {
1010            // is it an empty dom with empty attributes
1011            if (value instanceof Node && ((Node) value).hasAttributes()) {
1012                return true;
1013            }
1014            NodeList list = (NodeList) value;
1015            return list.getLength() > 0;
1016        } else if (value instanceof Collection) {
1017            // is it an empty collection
1018            Collection<?> col = (Collection<?>) value;
1019            return col.size() > 0;
1020        }
1021        return value != null;
1022    }
1023
1024    /**
1025     * Creates an Iterable to walk the exception from the bottom up
1026     * (the last caused by going upwards to the root exception).
1027     *
1028     * @see java.lang.Iterable
1029     * @param exception  the exception
1030     * @return the Iterable
1031     */
1032    public static Iterable<Throwable> createExceptionIterable(Throwable exception) {
1033        List<Throwable> throwables = new ArrayList<>();
1034
1035        Throwable current = exception;
1036        // spool to the bottom of the caused by tree
1037        while (current != null) {
1038            throwables.add(current);
1039            current = current.getCause();
1040        }
1041        Collections.reverse(throwables);
1042
1043        return throwables;
1044    }
1045
1046    /**
1047     * Creates an Iterator to walk the exception from the bottom up
1048     * (the last caused by going upwards to the root exception).
1049     *
1050     * @see Iterator
1051     * @param exception  the exception
1052     * @return the Iterator
1053     */
1054    public static Iterator<Throwable> createExceptionIterator(Throwable exception) {
1055        return createExceptionIterable(exception).iterator();
1056    }
1057
1058    /**
1059     * Retrieves the given exception type from the exception.
1060     * <p/>
1061     * Is used to get the caused exception that typically have been wrapped in some sort
1062     * of Camel wrapper exception
1063     * <p/>
1064     * The strategy is to look in the exception hierarchy to find the first given cause that matches the type.
1065     * Will start from the bottom (the real cause) and walk upwards.
1066     *
1067     * @param type the exception type wanted to retrieve
1068     * @param exception the caused exception
1069     * @return the exception found (or <tt>null</tt> if not found in the exception hierarchy)
1070     */
1071    public static <T> T getException(Class<T> type, Throwable exception) {
1072        if (exception == null) {
1073            return null;
1074        }
1075
1076        //check the suppressed exception first
1077        for (Throwable throwable : exception.getSuppressed()) {
1078            if (type.isInstance(throwable)) {
1079                return type.cast(throwable);
1080            }
1081        }
1082
1083        // walk the hierarchy and look for it
1084        for (final Throwable throwable : createExceptionIterable(exception)) {
1085            if (type.isInstance(throwable)) {
1086                return type.cast(throwable);
1087            }
1088        }
1089
1090        // not found
1091        return null;
1092    }
1093
1094    public static String getIdentityHashCode(Object object) {
1095        return "0x" + Integer.toHexString(System.identityHashCode(object));
1096    }
1097
1098    /**
1099     * Lookup the constant field on the given class with the given name
1100     *
1101     * @param clazz  the class
1102     * @param name   the name of the field to lookup
1103     * @return the value of the constant field, or <tt>null</tt> if not found
1104     */
1105    public static String lookupConstantFieldValue(Class<?> clazz, String name) {
1106        if (clazz == null) {
1107            return null;
1108        }
1109
1110        // remove leading dots
1111        if (name.startsWith(",")) {
1112            name = name.substring(1);
1113        }
1114
1115        for (Field field : clazz.getFields()) {
1116            if (field.getName().equals(name)) {
1117                try {
1118                    Object v = field.get(null);
1119                    return v.toString();
1120                } catch (IllegalAccessException e) {
1121                    // ignore
1122                    return null;
1123                }
1124            }
1125        }
1126
1127        return null;
1128    }
1129
1130    /**
1131     * Is the given value a numeric NaN type
1132     *
1133     * @param value the value
1134     * @return <tt>true</tt> if its a {@link Float#NaN} or {@link Double#NaN}.
1135     */
1136    public static boolean isNaN(Object value) {
1137        return (value instanceof Number)
1138            && (FLOAT_NAN.equals(value) || DOUBLE_NAN.equals(value));
1139    }
1140
1141    /**
1142     * Wraps the caused exception in a {@link RuntimeException} if its not
1143     * already such an exception.
1144     *
1145     * @param e the caused exception
1146     * @return the wrapper exception
1147     * @deprecated Use {@link org.apache.camel.RuntimeCamelException#wrapRuntimeCamelException} instead
1148     */
1149    @Deprecated
1150    public static RuntimeException wrapRuntimeCamelException(Throwable e) {
1151        try {
1152            Class<? extends RuntimeException> clazz = (Class) Class.forName("org.apache.camel.RuntimeException");
1153            if (clazz.isInstance(e)) {
1154                // don't double wrap
1155                return clazz.cast(e);
1156            } else {
1157                return clazz.getConstructor(Throwable.class).newInstance(e);
1158            }
1159        } catch (Throwable t) {
1160            // ignore
1161        }
1162        if (e instanceof RuntimeException) {
1163            // don't double wrap
1164            return (RuntimeException) e;
1165        } else {
1166            return new RuntimeException(e);
1167        }
1168    }
1169
1170    /**
1171     * Turns the input array to a list of objects.
1172     * 
1173     * @param args an array of objects or null
1174     * @return an object list
1175     */
1176    public static List<Object> asList(Object[] objects) {
1177        return objects != null ? Arrays.asList(objects) : Collections.emptyList();
1178    }
1179    
1180}