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