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