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