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