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