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