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.beans.PropertyEditor; 020import java.beans.PropertyEditorManager; 021import java.lang.reflect.InvocationTargetException; 022import java.lang.reflect.Method; 023import java.lang.reflect.Proxy; 024import java.net.URI; 025import java.net.URISyntaxException; 026import java.util.ArrayList; 027import java.util.Arrays; 028import java.util.Collection; 029import java.util.HashSet; 030import java.util.Iterator; 031import java.util.LinkedHashMap; 032import java.util.LinkedHashSet; 033import java.util.LinkedList; 034import java.util.List; 035import java.util.Locale; 036import java.util.Map; 037import java.util.Set; 038import java.util.regex.Pattern; 039 040import org.apache.camel.CamelContext; 041import org.apache.camel.Component; 042import org.apache.camel.NoTypeConversionAvailableException; 043import org.apache.camel.TypeConverter; 044import org.apache.camel.component.properties.PropertiesComponent; 045import org.slf4j.Logger; 046import org.slf4j.LoggerFactory; 047 048import static org.apache.camel.util.ObjectHelper.isAssignableFrom; 049 050/** 051 * Helper for introspections of beans. 052 * <p/> 053 * <b>Important: </b> Its recommended to call the {@link #stop()} method when 054 * {@link org.apache.camel.CamelContext} is being stopped. This allows to clear the introspection cache. 055 * <p/> 056 * This implementation will skip methods from <tt>java.lang.Object</tt> and <tt>java.lang.reflect.Proxy</tt>. 057 * <p/> 058 * This implementation will use a cache when the {@link #getProperties(Object, java.util.Map, String)} 059 * method is being used. Also the {@link #cacheClass(Class)} method gives access to the introspect cache. 060 */ 061public final class IntrospectionSupport { 062 063 private static final Logger LOG = LoggerFactory.getLogger(IntrospectionSupport.class); 064 private static final Pattern GETTER_PATTERN = Pattern.compile("(get|is)[A-Z].*"); 065 private static final Pattern SETTER_PATTERN = Pattern.compile("set[A-Z].*"); 066 private static final List<Method> EXCLUDED_METHODS = new ArrayList<Method>(); 067 // use a cache to speedup introspecting for known classes during startup 068 // use a weak cache as we dont want the cache to keep around as it reference classes 069 // which could prevent classloader to unload classes if being referenced from this cache 070 private static final LRUCache<Class<?>, ClassInfo> CACHE = new LRUWeakCache<Class<?>, ClassInfo>(1000); 071 private static final Object LOCK = new Object(); 072 073 static { 074 // exclude all java.lang.Object methods as we dont want to invoke them 075 EXCLUDED_METHODS.addAll(Arrays.asList(Object.class.getMethods())); 076 // exclude all java.lang.reflect.Proxy methods as we dont want to invoke them 077 EXCLUDED_METHODS.addAll(Arrays.asList(Proxy.class.getMethods())); 078 } 079 080 private static final Set<Class<?>> PRIMITIVE_CLASSES = new HashSet<Class<?>>(); 081 082 static { 083 PRIMITIVE_CLASSES.add(String.class); 084 PRIMITIVE_CLASSES.add(Character.class); 085 PRIMITIVE_CLASSES.add(Boolean.class); 086 PRIMITIVE_CLASSES.add(Byte.class); 087 PRIMITIVE_CLASSES.add(Short.class); 088 PRIMITIVE_CLASSES.add(Integer.class); 089 PRIMITIVE_CLASSES.add(Long.class); 090 PRIMITIVE_CLASSES.add(Float.class); 091 PRIMITIVE_CLASSES.add(Double.class); 092 PRIMITIVE_CLASSES.add(char.class); 093 PRIMITIVE_CLASSES.add(boolean.class); 094 PRIMITIVE_CLASSES.add(byte.class); 095 PRIMITIVE_CLASSES.add(short.class); 096 PRIMITIVE_CLASSES.add(int.class); 097 PRIMITIVE_CLASSES.add(long.class); 098 PRIMITIVE_CLASSES.add(float.class); 099 PRIMITIVE_CLASSES.add(double.class); 100 } 101 102 /** 103 * Structure of an introspected class. 104 */ 105 public static final class ClassInfo { 106 public Class<?> clazz; 107 public MethodInfo[] methods; 108 } 109 110 /** 111 * Structure of an introspected method. 112 */ 113 public static final class MethodInfo { 114 public Method method; 115 public Boolean isGetter; 116 public Boolean isSetter; 117 public String getterOrSetterShorthandName; 118 public Boolean hasGetterAndSetter; 119 } 120 121 /** 122 * Utility classes should not have a public constructor. 123 */ 124 private IntrospectionSupport() { 125 } 126 127 /** 128 * {@link org.apache.camel.CamelContext} should call this stop method when its stopping. 129 * <p/> 130 * This implementation will clear its introspection cache. 131 */ 132 public static void stop() { 133 if (LOG.isDebugEnabled()) { 134 LOG.debug("Clearing cache[size={}, hits={}, misses={}, evicted={}]", new Object[]{CACHE.size(), CACHE.getHits(), CACHE.getMisses(), CACHE.getEvicted()}); 135 } 136 CACHE.clear(); 137 138 // flush java beans introspector as it may be in use by the PropertyEditor 139 java.beans.Introspector.flushCaches(); 140 } 141 142 public static boolean isGetter(Method method) { 143 String name = method.getName(); 144 Class<?> type = method.getReturnType(); 145 Class<?> params[] = method.getParameterTypes(); 146 147 if (!GETTER_PATTERN.matcher(name).matches()) { 148 return false; 149 } 150 151 // special for isXXX boolean 152 if (name.startsWith("is")) { 153 return params.length == 0 && type.getSimpleName().equalsIgnoreCase("boolean"); 154 } 155 156 return params.length == 0 && !type.equals(Void.TYPE); 157 } 158 159 public static String getGetterShorthandName(Method method) { 160 if (!isGetter(method)) { 161 return method.getName(); 162 } 163 164 String name = method.getName(); 165 if (name.startsWith("get")) { 166 name = name.substring(3); 167 name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1); 168 } else if (name.startsWith("is")) { 169 name = name.substring(2); 170 name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1); 171 } 172 173 return name; 174 } 175 176 public static String getSetterShorthandName(Method method) { 177 if (!isSetter(method)) { 178 return method.getName(); 179 } 180 181 String name = method.getName(); 182 if (name.startsWith("set")) { 183 name = name.substring(3); 184 name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1); 185 } 186 187 return name; 188 } 189 190 public static boolean isSetter(Method method, boolean allowBuilderPattern) { 191 String name = method.getName(); 192 Class<?> type = method.getReturnType(); 193 Class<?> params[] = method.getParameterTypes(); 194 195 if (!SETTER_PATTERN.matcher(name).matches()) { 196 return false; 197 } 198 199 return params.length == 1 && (type.equals(Void.TYPE) || (allowBuilderPattern && method.getDeclaringClass().isAssignableFrom(type))); 200 } 201 202 public static boolean isSetter(Method method) { 203 return isSetter(method, false); 204 } 205 206 207 /** 208 * Will inspect the target for properties. 209 * <p/> 210 * Notice a property must have both a getter/setter method to be included. 211 * Notice all <tt>null</tt> values will be included. 212 * 213 * @param target the target bean 214 * @param properties the map to fill in found properties 215 * @param optionPrefix an optional prefix to append the property key 216 * @return <tt>true</tt> if any properties was found, <tt>false</tt> otherwise. 217 */ 218 public static boolean getProperties(Object target, Map<String, Object> properties, String optionPrefix) { 219 return getProperties(target, properties, optionPrefix, true); 220 } 221 222 /** 223 * Will inspect the target for properties. 224 * <p/> 225 * Notice a property must have both a getter/setter method to be included. 226 * 227 * @param target the target bean 228 * @param properties the map to fill in found properties 229 * @param optionPrefix an optional prefix to append the property key 230 * @param includeNull whether to include <tt>null</tt> values 231 * @return <tt>true</tt> if any properties was found, <tt>false</tt> otherwise. 232 */ 233 public static boolean getProperties(Object target, Map<String, Object> properties, String optionPrefix, boolean includeNull) { 234 ObjectHelper.notNull(target, "target"); 235 ObjectHelper.notNull(properties, "properties"); 236 boolean rc = false; 237 if (optionPrefix == null) { 238 optionPrefix = ""; 239 } 240 241 ClassInfo cache = cacheClass(target.getClass()); 242 243 for (MethodInfo info : cache.methods) { 244 Method method = info.method; 245 // we can only get properties if we have both a getter and a setter 246 if (info.isGetter && info.hasGetterAndSetter) { 247 String name = info.getterOrSetterShorthandName; 248 try { 249 // we may want to set options on classes that has package view visibility, so override the accessible 250 method.setAccessible(true); 251 Object value = method.invoke(target); 252 if (value != null || includeNull) { 253 properties.put(optionPrefix + name, value); 254 rc = true; 255 } 256 } catch (Exception e) { 257 if (LOG.isTraceEnabled()) { 258 LOG.trace("Error invoking getter method " + method + ". This exception is ignored.", e); 259 } 260 } 261 } 262 } 263 264 return rc; 265 } 266 267 /** 268 * Introspects the given class. 269 * 270 * @param clazz the class 271 * @return the introspection result as a {@link ClassInfo} structure. 272 */ 273 public static ClassInfo cacheClass(Class<?> clazz) { 274 ClassInfo cache = CACHE.get(clazz); 275 if (cache == null) { 276 cache = doIntrospectClass(clazz); 277 CACHE.put(clazz, cache); 278 } 279 return cache; 280 } 281 282 private static ClassInfo doIntrospectClass(Class<?> clazz) { 283 ClassInfo answer = new ClassInfo(); 284 answer.clazz = clazz; 285 286 // loop each method on the class and gather details about the method 287 // especially about getter/setters 288 List<MethodInfo> found = new ArrayList<MethodInfo>(); 289 Method[] methods = clazz.getMethods(); 290 for (Method method : methods) { 291 if (EXCLUDED_METHODS.contains(method)) { 292 continue; 293 } 294 295 MethodInfo cache = new MethodInfo(); 296 cache.method = method; 297 if (isGetter(method)) { 298 cache.isGetter = true; 299 cache.isSetter = false; 300 cache.getterOrSetterShorthandName = getGetterShorthandName(method); 301 } else if (isSetter(method)) { 302 cache.isGetter = false; 303 cache.isSetter = true; 304 cache.getterOrSetterShorthandName = getSetterShorthandName(method); 305 } else { 306 cache.isGetter = false; 307 cache.isSetter = false; 308 cache.hasGetterAndSetter = false; 309 } 310 found.add(cache); 311 } 312 313 // for all getter/setter, find out if there is a corresponding getter/setter, 314 // so we have a read/write bean property. 315 for (MethodInfo info : found) { 316 info.hasGetterAndSetter = false; 317 if (info.isGetter) { 318 // loop and find the matching setter 319 for (MethodInfo info2 : found) { 320 if (info2.isSetter && info.getterOrSetterShorthandName.equals(info2.getterOrSetterShorthandName)) { 321 info.hasGetterAndSetter = true; 322 break; 323 } 324 } 325 } else if (info.isSetter) { 326 // loop and find the matching getter 327 for (MethodInfo info2 : found) { 328 if (info2.isGetter && info.getterOrSetterShorthandName.equals(info2.getterOrSetterShorthandName)) { 329 info.hasGetterAndSetter = true; 330 break; 331 } 332 } 333 } 334 } 335 336 answer.methods = found.toArray(new MethodInfo[found.size()]); 337 return answer; 338 } 339 340 public static boolean hasProperties(Map<String, Object> properties, String optionPrefix) { 341 ObjectHelper.notNull(properties, "properties"); 342 343 if (ObjectHelper.isNotEmpty(optionPrefix)) { 344 for (Object o : properties.keySet()) { 345 String name = (String) o; 346 if (name.startsWith(optionPrefix)) { 347 return true; 348 } 349 } 350 // no parameters with this prefix 351 return false; 352 } else { 353 return !properties.isEmpty(); 354 } 355 } 356 357 public static Object getProperty(Object target, String property) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { 358 ObjectHelper.notNull(target, "target"); 359 ObjectHelper.notNull(property, "property"); 360 361 property = property.substring(0, 1).toUpperCase(Locale.ENGLISH) + property.substring(1); 362 363 Class<?> clazz = target.getClass(); 364 Method method = getPropertyGetter(clazz, property); 365 return method.invoke(target); 366 } 367 368 public static Object getOrElseProperty(Object target, String property, Object defaultValue) { 369 try { 370 return getProperty(target, property); 371 } catch (Exception e) { 372 return defaultValue; 373 } 374 } 375 376 public static Method getPropertyGetter(Class<?> type, String propertyName) throws NoSuchMethodException { 377 if (isPropertyIsGetter(type, propertyName)) { 378 return type.getMethod("is" + ObjectHelper.capitalize(propertyName)); 379 } else { 380 return type.getMethod("get" + ObjectHelper.capitalize(propertyName)); 381 } 382 } 383 384 public static Method getPropertySetter(Class<?> type, String propertyName) throws NoSuchMethodException { 385 String name = "set" + ObjectHelper.capitalize(propertyName); 386 for (Method method : type.getMethods()) { 387 if (isSetter(method) && method.getName().equals(name)) { 388 return method; 389 } 390 } 391 throw new NoSuchMethodException(type.getCanonicalName() + "." + name); 392 } 393 394 public static boolean isPropertyIsGetter(Class<?> type, String propertyName) { 395 try { 396 Method method = type.getMethod("is" + ObjectHelper.capitalize(propertyName)); 397 if (method != null) { 398 return method.getReturnType().isAssignableFrom(boolean.class) || method.getReturnType().isAssignableFrom(Boolean.class); 399 } 400 } catch (NoSuchMethodException e) { 401 // ignore 402 } 403 return false; 404 } 405 406 public static boolean setProperties(Object target, Map<String, Object> properties, String optionPrefix, boolean allowBuilderPattern) throws Exception { 407 ObjectHelper.notNull(target, "target"); 408 ObjectHelper.notNull(properties, "properties"); 409 boolean rc = false; 410 411 for (Iterator<Map.Entry<String, Object>> it = properties.entrySet().iterator(); it.hasNext();) { 412 Map.Entry<String, Object> entry = it.next(); 413 String name = entry.getKey().toString(); 414 if (name.startsWith(optionPrefix)) { 415 Object value = properties.get(name); 416 name = name.substring(optionPrefix.length()); 417 if (setProperty(target, name, value, allowBuilderPattern)) { 418 it.remove(); 419 rc = true; 420 } 421 } 422 } 423 424 return rc; 425 } 426 427 public static boolean setProperties(Object target, Map<String, Object> properties, String optionPrefix) throws Exception { 428 ObjectHelper.notEmpty(optionPrefix, "optionPrefix"); 429 return setProperties(target, properties, optionPrefix, false); 430 } 431 432 public static Map<String, Object> extractProperties(Map<String, Object> properties, String optionPrefix) { 433 ObjectHelper.notNull(properties, "properties"); 434 435 Map<String, Object> rc = new LinkedHashMap<String, Object>(properties.size()); 436 437 for (Iterator<Map.Entry<String, Object>> it = properties.entrySet().iterator(); it.hasNext();) { 438 Map.Entry<String, Object> entry = it.next(); 439 String name = entry.getKey(); 440 if (name.startsWith(optionPrefix)) { 441 Object value = properties.get(name); 442 name = name.substring(optionPrefix.length()); 443 rc.put(name, value); 444 it.remove(); 445 } 446 } 447 448 return rc; 449 } 450 451 public static Map<String, String> extractStringProperties(Map<String, Object> properties) { 452 ObjectHelper.notNull(properties, "properties"); 453 454 Map<String, String> rc = new LinkedHashMap<String, String>(properties.size()); 455 456 for (Iterator<Map.Entry<String, Object>> it = properties.entrySet().iterator(); it.hasNext();) { 457 Map.Entry<String, Object> entry = it.next(); 458 String name = entry.getKey(); 459 String value = entry.getValue().toString(); 460 rc.put(name, value); 461 } 462 463 return rc; 464 } 465 466 public static boolean setProperties(CamelContext context, TypeConverter typeConverter, Object target, Map<String, Object> properties) throws Exception { 467 ObjectHelper.notNull(target, "target"); 468 ObjectHelper.notNull(properties, "properties"); 469 boolean rc = false; 470 471 for (Iterator<Map.Entry<String, Object>> iter = properties.entrySet().iterator(); iter.hasNext();) { 472 Map.Entry<String, Object> entry = iter.next(); 473 if (setProperty(context, typeConverter, target, entry.getKey(), entry.getValue())) { 474 iter.remove(); 475 rc = true; 476 } 477 } 478 479 return rc; 480 } 481 482 public static boolean setProperties(TypeConverter typeConverter, Object target, Map<String, Object> properties) throws Exception { 483 return setProperties(null, typeConverter, target, properties); 484 } 485 486 public static boolean setProperties(Object target, Map<String, Object> properties) throws Exception { 487 return setProperties(null, target, properties); 488 } 489 490 /** 491 * This method supports two modes to set a property: 492 * 493 * 1. Setting a property that has already been resolved, this is the case when {@code context} and {@code refName} are 494 * NULL and {@code value} is non-NULL. 495 * 496 * 2. Setting a property that has not yet been resolved, the property will be resolved based on the suitable methods 497 * found matching the property name on the {@code target} bean. For this mode to be triggered the parameters 498 * {@code context} and {@code refName} must NOT be NULL, and {@code value} MUST be NULL. 499 * 500 */ 501 public static boolean setProperty(CamelContext context, TypeConverter typeConverter, Object target, String name, Object value, String refName, boolean allowBuilderPattern) throws Exception { 502 Class<?> clazz = target.getClass(); 503 Collection<Method> setters; 504 505 // we need to lookup the value from the registry 506 if (context != null && refName != null && value == null) { 507 setters = findSetterMethodsOrderedByParameterType(clazz, name, allowBuilderPattern); 508 } else { 509 // find candidates of setter methods as there can be overloaded setters 510 setters = findSetterMethods(clazz, name, value, allowBuilderPattern); 511 } 512 if (setters.isEmpty()) { 513 return false; 514 } 515 516 // loop and execute the best setter method 517 Exception typeConversionFailed = null; 518 for (Method setter : setters) { 519 Class<?> parameterType = setter.getParameterTypes()[0]; 520 Object ref = value; 521 // try and lookup the reference based on the method 522 if (context != null && refName != null && ref == null) { 523 String s = StringHelper.replaceAll(refName, "#", ""); 524 ref = CamelContextHelper.lookup(context, s); 525 if (ref == null) { 526 // try the next method if nothing was found 527 continue; 528 } else { 529 // setter method has not the correct type 530 // (must use ObjectHelper.isAssignableFrom which takes primitive types into account) 531 boolean assignable = isAssignableFrom(parameterType, ref.getClass()); 532 if (!assignable) { 533 continue; 534 } 535 } 536 } 537 538 try { 539 try { 540 // If the type is null or it matches the needed type, just use the value directly 541 if (value == null || isAssignableFrom(parameterType, ref.getClass())) { 542 // we may want to set options on classes that has package view visibility, so override the accessible 543 setter.setAccessible(true); 544 setter.invoke(target, ref); 545 if (LOG.isDebugEnabled()) { 546 LOG.debug("Configured property: {} on bean: {} with value: {}", new Object[]{name, target, ref}); 547 } 548 return true; 549 } else { 550 // We need to convert it 551 Object convertedValue = convert(typeConverter, parameterType, ref); 552 // we may want to set options on classes that has package view visibility, so override the accessible 553 setter.setAccessible(true); 554 setter.invoke(target, convertedValue); 555 if (LOG.isDebugEnabled()) { 556 LOG.debug("Configured property: {} on bean: {} with value: {}", new Object[]{name, target, ref}); 557 } 558 return true; 559 } 560 } catch (InvocationTargetException e) { 561 // lets unwrap the exception 562 Throwable throwable = e.getCause(); 563 if (throwable instanceof Exception) { 564 Exception exception = (Exception)throwable; 565 throw exception; 566 } else { 567 Error error = (Error)throwable; 568 throw error; 569 } 570 } 571 // ignore exceptions as there could be another setter method where we could type convert successfully 572 } catch (SecurityException e) { 573 typeConversionFailed = e; 574 } catch (NoTypeConversionAvailableException e) { 575 typeConversionFailed = e; 576 } catch (IllegalArgumentException e) { 577 typeConversionFailed = e; 578 } 579 if (LOG.isTraceEnabled()) { 580 LOG.trace("Setter \"{}\" with parameter type \"{}\" could not be used for type conversions of {}", 581 new Object[]{setter, parameterType, ref}); 582 } 583 } 584 585 if (typeConversionFailed != null && !isPropertyPlaceholder(context, value)) { 586 // we did not find a setter method to use, and if we did try to use a type converter then throw 587 // this kind of exception as the caused by will hint this error 588 throw new IllegalArgumentException("Could not find a suitable setter for property: " + name 589 + " as there isn't a setter method with same type: " + (value != null ? value.getClass().getCanonicalName() : "[null]") 590 + " nor type conversion possible: " + typeConversionFailed.getMessage()); 591 } else { 592 return false; 593 } 594 } 595 596 private static boolean isPropertyPlaceholder(CamelContext context, Object value) { 597 if (context != null) { 598 Component component = context.hasComponent("properties"); 599 if (component != null) { 600 PropertiesComponent pc; 601 try { 602 pc = context.getTypeConverter().mandatoryConvertTo(PropertiesComponent.class, component); 603 } catch (Exception e) { 604 return false; 605 } 606 if (value.toString().contains(pc.getPrefixToken()) && value.toString().contains(pc.getSuffixToken())) { 607 return true; 608 } 609 } 610 } 611 return false; 612 } 613 614 public static boolean setProperty(CamelContext context, TypeConverter typeConverter, Object target, String name, Object value) throws Exception { 615 // allow build pattern as a setter as well 616 return setProperty(context, typeConverter, target, name, value, null, true); 617 } 618 619 public static boolean setProperty(TypeConverter typeConverter, Object target, String name, Object value) throws Exception { 620 // allow build pattern as a setter as well 621 return setProperty(null, typeConverter, target, name, value, null, true); 622 } 623 624 public static boolean setProperty(Object target, String name, Object value, boolean allowBuilderPattern) throws Exception { 625 return setProperty(null, null, target, name, value, null, allowBuilderPattern); 626 } 627 628 public static boolean setProperty(Object target, String name, Object value) throws Exception { 629 // allow build pattern as a setter as well 630 return setProperty(target, name, value, true); 631 } 632 633 private static Object convert(TypeConverter typeConverter, Class<?> type, Object value) 634 throws URISyntaxException, NoTypeConversionAvailableException { 635 if (typeConverter != null) { 636 return typeConverter.mandatoryConvertTo(type, value); 637 } 638 if (type == URI.class) { 639 return new URI(value.toString()); 640 } 641 PropertyEditor editor = PropertyEditorManager.findEditor(type); 642 if (editor != null) { 643 // property editor is not thread safe, so we need to lock 644 Object answer; 645 synchronized (LOCK) { 646 editor.setAsText(value.toString()); 647 answer = editor.getValue(); 648 } 649 return answer; 650 } 651 return null; 652 } 653 654 public static Set<Method> findSetterMethods(Class<?> clazz, String name, boolean allowBuilderPattern) { 655 Set<Method> candidates = new LinkedHashSet<Method>(); 656 657 // Build the method name. 658 name = "set" + ObjectHelper.capitalize(name); 659 while (clazz != Object.class) { 660 // Since Object.class.isInstance all the objects, 661 // here we just make sure it will be add to the bottom of the set. 662 Method objectSetMethod = null; 663 Method[] methods = clazz.getMethods(); 664 for (Method method : methods) { 665 if (method.getName().equals(name) && isSetter(method, allowBuilderPattern)) { 666 Class<?> params[] = method.getParameterTypes(); 667 if (params[0].equals(Object.class)) { 668 objectSetMethod = method; 669 } else { 670 candidates.add(method); 671 } 672 } 673 } 674 if (objectSetMethod != null) { 675 candidates.add(objectSetMethod); 676 } 677 clazz = clazz.getSuperclass(); 678 } 679 return candidates; 680 } 681 682 private static Set<Method> findSetterMethods(Class<?> clazz, String name, Object value, boolean allowBuilderPattern) { 683 Set<Method> candidates = findSetterMethods(clazz, name, allowBuilderPattern); 684 685 if (candidates.isEmpty()) { 686 return candidates; 687 } else if (candidates.size() == 1) { 688 // only one 689 return candidates; 690 } else { 691 // find the best match if possible 692 LOG.trace("Found {} suitable setter methods for setting {}", candidates.size(), name); 693 // prefer to use the one with the same instance if any exists 694 for (Method method : candidates) { 695 if (method.getParameterTypes()[0].isInstance(value)) { 696 LOG.trace("Method {} is the best candidate as it has parameter with same instance type", method); 697 // retain only this method in the answer 698 candidates.clear(); 699 candidates.add(method); 700 return candidates; 701 } 702 } 703 // fallback to return what we have found as candidates so far 704 return candidates; 705 } 706 } 707 708 protected static List<Method> findSetterMethodsOrderedByParameterType(Class<?> target, String propertyName, boolean allowBuilderPattern) { 709 List<Method> answer = new LinkedList<Method>(); 710 List<Method> primitives = new LinkedList<Method>(); 711 Set<Method> setters = findSetterMethods(target, propertyName, allowBuilderPattern); 712 for (Method setter : setters) { 713 Class<?> parameterType = setter.getParameterTypes()[0]; 714 if (PRIMITIVE_CLASSES.contains(parameterType)) { 715 primitives.add(setter); 716 } else { 717 answer.add(setter); 718 } 719 } 720 // primitives get added last 721 answer.addAll(primitives); 722 return answer; 723 } 724 725}