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.component.bean; 018 019import java.lang.annotation.Annotation; 020import java.lang.reflect.Method; 021import java.lang.reflect.Modifier; 022import java.lang.reflect.Proxy; 023import java.util.ArrayList; 024import java.util.Arrays; 025import java.util.Collection; 026import java.util.Collections; 027import java.util.Comparator; 028import java.util.HashMap; 029import java.util.Iterator; 030import java.util.List; 031import java.util.Map; 032 033import org.apache.camel.AttachmentObjects; 034import org.apache.camel.Attachments; 035import org.apache.camel.Body; 036import org.apache.camel.CamelContext; 037import org.apache.camel.Exchange; 038import org.apache.camel.ExchangeException; 039import org.apache.camel.ExchangeProperties; 040import org.apache.camel.ExchangeProperty; 041import org.apache.camel.Expression; 042import org.apache.camel.Handler; 043import org.apache.camel.Header; 044import org.apache.camel.Headers; 045import org.apache.camel.Message; 046import org.apache.camel.OutHeaders; 047import org.apache.camel.Properties; 048import org.apache.camel.Property; 049import org.apache.camel.PropertyInject; 050import org.apache.camel.builder.ExpressionBuilder; 051import org.apache.camel.language.LanguageAnnotation; 052import org.apache.camel.spi.Registry; 053import org.apache.camel.util.CastUtils; 054import org.apache.camel.util.IntrospectionSupport; 055import org.apache.camel.util.ObjectHelper; 056import org.apache.camel.util.StringHelper; 057import org.apache.camel.util.StringQuoteHelper; 058import org.slf4j.Logger; 059import org.slf4j.LoggerFactory; 060 061/** 062 * Represents the metadata about a bean type created via a combination of 063 * introspection and annotations together with some useful sensible defaults 064 */ 065public class BeanInfo { 066 private static final Logger LOG = LoggerFactory.getLogger(BeanInfo.class); 067 private static final String CGLIB_CLASS_SEPARATOR = "$$"; 068 private static final List<Method> EXCLUDED_METHODS = new ArrayList<>(); 069 private final CamelContext camelContext; 070 private final BeanComponent component; 071 private final Class<?> type; 072 private final ParameterMappingStrategy strategy; 073 private final MethodInfo defaultMethod; 074 // shared state with details of operations introspected from the bean, created during the constructor 075 private Map<String, List<MethodInfo>> operations = new HashMap<>(); 076 private List<MethodInfo> operationsWithBody = new ArrayList<>(); 077 private List<MethodInfo> operationsWithNoBody = new ArrayList<>(); 078 private List<MethodInfo> operationsWithCustomAnnotation = new ArrayList<>(); 079 private List<MethodInfo> operationsWithHandlerAnnotation = new ArrayList<>(); 080 private Map<Method, MethodInfo> methodMap = new HashMap<>(); 081 private boolean publicConstructors; 082 083 static { 084 // exclude all java.lang.Object methods as we dont want to invoke them 085 EXCLUDED_METHODS.addAll(Arrays.asList(Object.class.getMethods())); 086 // exclude all java.lang.reflect.Proxy methods as we dont want to invoke them 087 EXCLUDED_METHODS.addAll(Arrays.asList(Proxy.class.getMethods())); 088 try { 089 // but keep toString as this method is okay 090 EXCLUDED_METHODS.remove(Object.class.getMethod("toString")); 091 EXCLUDED_METHODS.remove(Proxy.class.getMethod("toString")); 092 } catch (Throwable e) { 093 // ignore 094 } 095 } 096 097 public BeanInfo(CamelContext camelContext, Class<?> type) { 098 this(camelContext, type, createParameterMappingStrategy(camelContext)); 099 } 100 101 public BeanInfo(CamelContext camelContext, Method explicitMethod) { 102 this(camelContext, explicitMethod.getDeclaringClass(), explicitMethod, createParameterMappingStrategy(camelContext)); 103 } 104 105 public BeanInfo(CamelContext camelContext, Class<?> type, ParameterMappingStrategy strategy) { 106 this(camelContext, type, null, strategy); 107 } 108 109 public BeanInfo(CamelContext camelContext, Class<?> type, Method explicitMethod, ParameterMappingStrategy strategy) { 110 this.camelContext = camelContext; 111 this.type = type; 112 this.strategy = strategy; 113 this.component = camelContext.getComponent("bean", BeanComponent.class); 114 115 final BeanInfoCacheKey key = new BeanInfoCacheKey(type, explicitMethod); 116 117 // lookup if we have a bean info cache 118 BeanInfo beanInfo = component.getBeanInfoFromCache(key); 119 if (beanInfo != null) { 120 // copy the values from the cache we need 121 defaultMethod = beanInfo.defaultMethod; 122 operations = beanInfo.operations; 123 operationsWithBody = beanInfo.operationsWithBody; 124 operationsWithNoBody = beanInfo.operationsWithNoBody; 125 operationsWithCustomAnnotation = beanInfo.operationsWithCustomAnnotation; 126 operationsWithHandlerAnnotation = beanInfo.operationsWithHandlerAnnotation; 127 methodMap = beanInfo.methodMap; 128 publicConstructors = beanInfo.publicConstructors; 129 return; 130 } 131 132 if (explicitMethod != null) { 133 // must be a valid method 134 if (!isValidMethod(type, explicitMethod)) { 135 throw new IllegalArgumentException("The method " + explicitMethod + " is not valid (for example the method must be public)"); 136 } 137 introspect(getType(), explicitMethod); 138 } else { 139 introspect(getType()); 140 } 141 142 // if there are only 1 method with 1 operation then select it as a default/fallback method 143 MethodInfo method = null; 144 if (operations.size() == 1) { 145 List<MethodInfo> methods = operations.values().iterator().next(); 146 if (methods.size() == 1) { 147 method = methods.get(0); 148 } 149 } 150 defaultMethod = method; 151 152 // mark the operations lists as unmodifiable, as they should not change during runtime 153 // to keep this code thread safe 154 operations = Collections.unmodifiableMap(operations); 155 operationsWithBody = Collections.unmodifiableList(operationsWithBody); 156 operationsWithNoBody = Collections.unmodifiableList(operationsWithNoBody); 157 operationsWithCustomAnnotation = Collections.unmodifiableList(operationsWithCustomAnnotation); 158 operationsWithHandlerAnnotation = Collections.unmodifiableList(operationsWithHandlerAnnotation); 159 methodMap = Collections.unmodifiableMap(methodMap); 160 161 // add new bean info to cache 162 component.addBeanInfoToCache(key, this); 163 } 164 165 public Class<?> getType() { 166 return type; 167 } 168 169 public CamelContext getCamelContext() { 170 return camelContext; 171 } 172 173 public static ParameterMappingStrategy createParameterMappingStrategy(CamelContext camelContext) { 174 // lookup in registry first if there is a user define strategy 175 Registry registry = camelContext.getRegistry(); 176 ParameterMappingStrategy answer = registry.lookupByNameAndType(BeanConstants.BEAN_PARAMETER_MAPPING_STRATEGY, ParameterMappingStrategy.class); 177 if (answer == null) { 178 // no then use the default one 179 answer = new DefaultParameterMappingStrategy(); 180 } 181 182 return answer; 183 } 184 185 public MethodInvocation createInvocation(Object pojo, Exchange exchange) 186 throws AmbiguousMethodCallException, MethodNotFoundException { 187 return createInvocation(pojo, exchange, null); 188 } 189 190 private MethodInvocation createInvocation(Object pojo, Exchange exchange, Method explicitMethod) 191 throws AmbiguousMethodCallException, MethodNotFoundException { 192 MethodInfo methodInfo = null; 193 194 // find the explicit method to invoke 195 if (explicitMethod != null) { 196 for (List<MethodInfo> infos : operations.values()) { 197 for (MethodInfo info : infos) { 198 if (explicitMethod.equals(info.getMethod())) { 199 return info.createMethodInvocation(pojo, info.hasParameters(), exchange); 200 } 201 } 202 } 203 throw new MethodNotFoundException(exchange, pojo, explicitMethod.getName()); 204 } 205 206 String methodName = exchange.getIn().getHeader(Exchange.BEAN_METHOD_NAME, String.class); 207 if (methodName != null) { 208 209 // do not use qualifier for name 210 String name = methodName; 211 if (methodName.contains("(")) { 212 name = StringHelper.before(methodName, "("); 213 // the must be a ending parenthesis 214 if (!methodName.endsWith(")")) { 215 throw new IllegalArgumentException("Method should end with parenthesis, was " + methodName); 216 } 217 // and there must be an even number of parenthesis in the syntax 218 // (we can use betweenOuterPair as it return null if the syntax is invalid) 219 if (StringHelper.betweenOuterPair(methodName, '(', ')') == null) { 220 throw new IllegalArgumentException("Method should have even pair of parenthesis, was " + methodName); 221 } 222 } 223 boolean emptyParameters = methodName.endsWith("()"); 224 225 // special for getClass, as we want the user to be able to invoke this method 226 // for example to log the class type or the likes 227 if ("class".equals(name) || "getClass".equals(name)) { 228 try { 229 Method method = pojo.getClass().getMethod("getClass"); 230 methodInfo = new MethodInfo(exchange.getContext(), pojo.getClass(), method, Collections.<ParameterInfo>emptyList(), Collections.<ParameterInfo>emptyList(), false, false); 231 } catch (NoSuchMethodException e) { 232 throw new MethodNotFoundException(exchange, pojo, "getClass"); 233 } 234 // special for length on an array type 235 } else if ("length".equals(name) && pojo.getClass().isArray()) { 236 try { 237 // need to use arrayLength method from ObjectHelper as Camel's bean OGNL support is method invocation based 238 // and not for accessing fields. And hence we need to create a MethodInfo instance with a method to call 239 // and therefore use arrayLength from ObjectHelper to return the array length field. 240 Method method = ObjectHelper.class.getMethod("arrayLength", Object[].class); 241 ParameterInfo pi = new ParameterInfo(0, Object[].class, null, ExpressionBuilder.mandatoryBodyExpression(Object[].class, true)); 242 List<ParameterInfo> lpi = new ArrayList<>(1); 243 lpi.add(pi); 244 methodInfo = new MethodInfo(exchange.getContext(), pojo.getClass(), method, lpi, lpi, false, false); 245 // Need to update the message body to be pojo for the invocation 246 exchange.getIn().setBody(pojo); 247 } catch (NoSuchMethodException e) { 248 throw new MethodNotFoundException(exchange, pojo, "getClass"); 249 } 250 } else { 251 List<MethodInfo> methods = getOperations(name); 252 if (methods != null && methods.size() == 1) { 253 // only one method then choose it 254 methodInfo = methods.get(0); 255 256 // validate that if we want an explicit no-arg method, then that's what we get 257 if (emptyParameters && methodInfo.hasParameters()) { 258 throw new MethodNotFoundException(exchange, pojo, methodName, "(with no parameters)"); 259 } 260 } else if (methods != null) { 261 // there are more methods with that name so we cannot decide which to use 262 263 // but first let's try to choose a method and see if that complies with the name 264 // must use the method name which may have qualifiers 265 methodInfo = chooseMethod(pojo, exchange, methodName); 266 267 // validate that if we want an explicit no-arg method, then that's what we get 268 if (emptyParameters) { 269 if (methodInfo == null || methodInfo.hasParameters()) { 270 // we could not find a no-arg method with that name 271 throw new MethodNotFoundException(exchange, pojo, methodName, "(with no parameters)"); 272 } 273 } 274 275 if (methodInfo == null || (name != null && !name.equals(methodInfo.getMethod().getName()))) { 276 throw new AmbiguousMethodCallException(exchange, methods); 277 } 278 } else { 279 // a specific method was given to invoke but not found 280 throw new MethodNotFoundException(exchange, pojo, methodName); 281 } 282 } 283 } 284 285 if (methodInfo == null && methodMap.size() >= 2) { 286 // only try to choose if there is at least 2 methods 287 methodInfo = chooseMethod(pojo, exchange, null); 288 } 289 if (methodInfo == null) { 290 methodInfo = defaultMethod; 291 } 292 if (methodInfo != null) { 293 LOG.trace("Chosen method to invoke: {} on bean: {}", methodInfo, pojo); 294 return methodInfo.createMethodInvocation(pojo, methodInfo.hasParameters(), exchange); 295 } 296 297 LOG.debug("Cannot find suitable method to invoke on bean: {}", pojo); 298 return null; 299 } 300 301 /** 302 * Introspects the given class 303 * 304 * @param clazz the class 305 */ 306 private void introspect(Class<?> clazz) { 307 308 // does the class have any public constructors? 309 publicConstructors = clazz.getConstructors().length > 0; 310 311 MethodsFilter methods = new MethodsFilter(getType()); 312 introspect(clazz, methods); 313 314 // now introspect the methods and filter non valid methods 315 for (Method method : methods.asReadOnlyList()) { 316 boolean valid = isValidMethod(clazz, method); 317 LOG.trace("Method: {} is valid: {}", method, valid); 318 if (valid) { 319 introspect(clazz, method); 320 } 321 } 322 } 323 324 private void introspect(Class<?> clazz, MethodsFilter filteredMethods) { 325 // get the target clazz as it could potentially have been enhanced by 326 // CGLIB etc. 327 clazz = getTargetClass(clazz); 328 ObjectHelper.notNull(clazz, "clazz", this); 329 330 LOG.trace("Introspecting class: {}", clazz); 331 332 for (Method m : Arrays.asList(clazz.getDeclaredMethods())) { 333 filteredMethods.filterMethod(m); 334 } 335 336 Class<?> superClass = clazz.getSuperclass(); 337 if (superClass != null && !superClass.equals(Object.class)) { 338 introspect(superClass, filteredMethods); 339 } 340 for (Class<?> superInterface : clazz.getInterfaces()) { 341 introspect(superInterface, filteredMethods); 342 } 343 } 344 345 /** 346 * Introspects the given method 347 * 348 * @param clazz the class 349 * @param method the method 350 * @return the method info, is newer <tt>null</tt> 351 */ 352 private MethodInfo introspect(Class<?> clazz, Method method) { 353 LOG.trace("Introspecting class: {}, method: {}", clazz, method); 354 String opName = method.getName(); 355 356 MethodInfo methodInfo = createMethodInfo(clazz, method); 357 358 // Foster the use of a potentially already registered most specific override 359 MethodInfo existingMethodInfo = findMostSpecificOverride(methodInfo); 360 if (existingMethodInfo != null) { 361 LOG.trace("This method is already overridden in a subclass, so the method from the sub class is preferred: {}", existingMethodInfo); 362 return existingMethodInfo; 363 } 364 365 LOG.trace("Adding operation: {} for method: {}", opName, methodInfo); 366 367 List<MethodInfo> existing = getOperations(opName); 368 if (existing != null) { 369 // we have an overloaded method so add the method info to the same key 370 existing.add(methodInfo); 371 } else { 372 // its a new method we have not seen before so wrap it in a list and add it 373 List<MethodInfo> methods = new ArrayList<>(); 374 methods.add(methodInfo); 375 operations.put(opName, methods); 376 } 377 378 if (methodInfo.hasCustomAnnotation()) { 379 operationsWithCustomAnnotation.add(methodInfo); 380 } else if (methodInfo.hasBodyParameter()) { 381 operationsWithBody.add(methodInfo); 382 } else { 383 operationsWithNoBody.add(methodInfo); 384 } 385 386 if (methodInfo.hasHandlerAnnotation()) { 387 operationsWithHandlerAnnotation.add(methodInfo); 388 } 389 390 // must add to method map last otherwise we break stuff 391 methodMap.put(method, methodInfo); 392 393 return methodInfo; 394 } 395 396 /** 397 * Returns the {@link MethodInfo} for the given method if it exists or null 398 * if there is no metadata available for the given method 399 */ 400 public MethodInfo getMethodInfo(Method method) { 401 MethodInfo answer = methodMap.get(method); 402 if (answer == null) { 403 // maybe the method overrides, and the method map keeps info of the source override we can use 404 for (Map.Entry<Method, MethodInfo> methodEntry : methodMap.entrySet()) { 405 Method source = methodEntry.getKey(); 406 if (ObjectHelper.isOverridingMethod(getType(), source, method, false)) { 407 answer = methodEntry.getValue(); 408 break; 409 } 410 } 411 } 412 413 if (answer == null) { 414 // maybe the method is defined on a base class? 415 if (type != Object.class) { 416 Class<?> superclass = type.getSuperclass(); 417 if (superclass != null && superclass != Object.class) { 418 BeanInfo superBeanInfo = new BeanInfo(camelContext, superclass, strategy); 419 return superBeanInfo.getMethodInfo(method); 420 } 421 } 422 } 423 return answer; 424 } 425 426 protected MethodInfo createMethodInfo(Class<?> clazz, Method method) { 427 Class<?>[] parameterTypes = method.getParameterTypes(); 428 List<Annotation>[] parametersAnnotations = collectParameterAnnotations(clazz, method); 429 430 List<ParameterInfo> parameters = new ArrayList<>(); 431 List<ParameterInfo> bodyParameters = new ArrayList<>(); 432 433 boolean hasCustomAnnotation = false; 434 boolean hasHandlerAnnotation = ObjectHelper.hasAnnotation(method.getAnnotations(), Handler.class); 435 436 int size = parameterTypes.length; 437 if (LOG.isTraceEnabled()) { 438 LOG.trace("Creating MethodInfo for class: {} method: {} having {} parameters", clazz, method, size); 439 } 440 441 for (int i = 0; i < size; i++) { 442 Class<?> parameterType = parameterTypes[i]; 443 Annotation[] parameterAnnotations = parametersAnnotations[i].toArray(new Annotation[parametersAnnotations[i].size()]); 444 Expression expression = createParameterUnmarshalExpression(clazz, method, parameterType, parameterAnnotations); 445 hasCustomAnnotation |= expression != null; 446 447 ParameterInfo parameterInfo = new ParameterInfo(i, parameterType, parameterAnnotations, expression); 448 LOG.trace("Parameter #{}: {}", i, parameterInfo); 449 parameters.add(parameterInfo); 450 if (expression == null) { 451 boolean bodyAnnotation = ObjectHelper.hasAnnotation(parameterAnnotations, Body.class); 452 LOG.trace("Parameter #{} has @Body annotation", i); 453 hasCustomAnnotation |= bodyAnnotation; 454 if (bodyParameters.isEmpty()) { 455 // okay we have not yet set the body parameter and we have found 456 // the candidate now to use as body parameter 457 if (Exchange.class.isAssignableFrom(parameterType)) { 458 // use exchange 459 expression = ExpressionBuilder.exchangeExpression(); 460 } else { 461 // assume it's the body and it must be mandatory convertible to the parameter type 462 // but we allow null bodies in case the message really contains a null body 463 expression = ExpressionBuilder.mandatoryBodyExpression(parameterType, true); 464 } 465 LOG.trace("Parameter #{} is the body parameter using expression {}", i, expression); 466 parameterInfo.setExpression(expression); 467 bodyParameters.add(parameterInfo); 468 } else { 469 // will ignore the expression for parameter evaluation 470 } 471 } 472 LOG.trace("Parameter #{} has parameter info: ", i, parameterInfo); 473 } 474 475 // now let's add the method to the repository 476 return new MethodInfo(camelContext, clazz, method, parameters, bodyParameters, hasCustomAnnotation, hasHandlerAnnotation); 477 } 478 479 @SuppressWarnings("unchecked") 480 protected List<Annotation>[] collectParameterAnnotations(Class<?> c, Method m) { 481 List<Annotation>[] annotations = new List[m.getParameterCount()]; 482 for (int i = 0; i < annotations.length; i++) { 483 annotations[i] = new ArrayList<>(); 484 } 485 collectParameterAnnotations(c, m, annotations); 486 return annotations; 487 } 488 489 protected void collectParameterAnnotations(Class<?> c, Method m, List<Annotation>[] a) { 490 try { 491 Annotation[][] pa = c.getDeclaredMethod(m.getName(), m.getParameterTypes()).getParameterAnnotations(); 492 for (int i = 0; i < pa.length; i++) { 493 a[i].addAll(Arrays.asList(pa[i])); 494 } 495 } catch (NoSuchMethodException e) { 496 // no method with signature of m declared on c 497 } 498 for (Class<?> i : c.getInterfaces()) { 499 collectParameterAnnotations(i, m, a); 500 } 501 if (!c.isInterface() && c.getSuperclass() != null) { 502 collectParameterAnnotations(c.getSuperclass(), m, a); 503 } 504 } 505 506 /** 507 * Choose one of the available methods to invoke if we can match 508 * the message body to the body parameter 509 * 510 * @param pojo the bean to invoke a method on 511 * @param exchange the message exchange 512 * @param name an optional name of the method that must match, use <tt>null</tt> to indicate all methods 513 * @return the method to invoke or null if no definitive method could be matched 514 * @throws AmbiguousMethodCallException is thrown if cannot choose method due to ambiguity 515 */ 516 protected MethodInfo chooseMethod(Object pojo, Exchange exchange, String name) throws AmbiguousMethodCallException { 517 // @Handler should be select first 518 // then any single method that has a custom @annotation 519 // or any single method that has a match parameter type that matches the Exchange payload 520 // and last then try to select the best among the rest 521 522 // must use defensive copy, to avoid altering the shared lists 523 // and we want to remove unwanted operations from these local lists 524 List<MethodInfo> localOperationsWithBody = null; 525 if (!operationsWithBody.isEmpty()) { 526 localOperationsWithBody = new ArrayList<>(operationsWithBody); 527 } 528 List<MethodInfo> localOperationsWithNoBody = null; 529 if (!operationsWithNoBody.isEmpty()) { 530 localOperationsWithNoBody = new ArrayList<>(operationsWithNoBody); 531 } 532 List<MethodInfo> localOperationsWithCustomAnnotation = null; 533 if (!operationsWithCustomAnnotation.isEmpty()) { 534 localOperationsWithCustomAnnotation = new ArrayList<>(operationsWithCustomAnnotation); 535 } 536 List<MethodInfo> localOperationsWithHandlerAnnotation = null; 537 if (!operationsWithHandlerAnnotation.isEmpty()) { 538 localOperationsWithHandlerAnnotation = new ArrayList<>(operationsWithHandlerAnnotation); 539 } 540 541 // remove all abstract methods 542 if (localOperationsWithBody != null) { 543 removeAllAbstractMethods(localOperationsWithBody); 544 } 545 if (localOperationsWithNoBody != null) { 546 removeAllAbstractMethods(localOperationsWithNoBody); 547 } 548 if (localOperationsWithCustomAnnotation != null) { 549 removeAllAbstractMethods(localOperationsWithCustomAnnotation); 550 } 551 if (localOperationsWithHandlerAnnotation != null) { 552 removeAllAbstractMethods(localOperationsWithHandlerAnnotation); 553 } 554 555 if (name != null) { 556 // filter all lists to only include methods with this name 557 if (localOperationsWithHandlerAnnotation != null) { 558 removeNonMatchingMethods(localOperationsWithHandlerAnnotation, name); 559 } 560 if (localOperationsWithCustomAnnotation != null) { 561 removeNonMatchingMethods(localOperationsWithCustomAnnotation, name); 562 } 563 if (localOperationsWithBody != null) { 564 removeNonMatchingMethods(localOperationsWithBody, name); 565 } 566 if (localOperationsWithNoBody != null) { 567 removeNonMatchingMethods(localOperationsWithNoBody, name); 568 } 569 } else { 570 // remove all getter/setter as we do not want to consider these methods 571 if (localOperationsWithHandlerAnnotation != null) { 572 removeAllSetterOrGetterMethods(localOperationsWithHandlerAnnotation); 573 } 574 if (localOperationsWithCustomAnnotation != null) { 575 removeAllSetterOrGetterMethods(localOperationsWithCustomAnnotation); 576 } 577 if (localOperationsWithBody != null) { 578 removeAllSetterOrGetterMethods(localOperationsWithBody); 579 } 580 if (localOperationsWithNoBody != null) { 581 removeAllSetterOrGetterMethods(localOperationsWithNoBody); 582 } 583 } 584 585 if (localOperationsWithHandlerAnnotation != null && localOperationsWithHandlerAnnotation.size() > 1) { 586 // if we have more than 1 @Handler then its ambiguous 587 throw new AmbiguousMethodCallException(exchange, localOperationsWithHandlerAnnotation); 588 } 589 590 if (localOperationsWithHandlerAnnotation != null && localOperationsWithHandlerAnnotation.size() == 1) { 591 // methods with handler should be preferred 592 return localOperationsWithHandlerAnnotation.get(0); 593 } else if (localOperationsWithCustomAnnotation != null && localOperationsWithCustomAnnotation.size() == 1) { 594 // if there is one method with an annotation then use that one 595 return localOperationsWithCustomAnnotation.get(0); 596 } 597 598 // named method and with no parameters 599 boolean noParameters = name != null && name.endsWith("()"); 600 if (noParameters && localOperationsWithNoBody != null && localOperationsWithNoBody.size() == 1) { 601 // if there was a method name configured and it has no parameters, then use the method with no body (eg no parameters) 602 return localOperationsWithNoBody.get(0); 603 } else if (!noParameters && (localOperationsWithBody != null && localOperationsWithBody.size() == 1 && localOperationsWithCustomAnnotation == null)) { 604 // if there is one method with body then use that one 605 return localOperationsWithBody.get(0); 606 } 607 608 if (localOperationsWithBody != null || localOperationsWithCustomAnnotation != null) { 609 Collection<MethodInfo> possibleOperations = new ArrayList<>(); 610 if (localOperationsWithBody != null) { 611 possibleOperations.addAll(localOperationsWithBody); 612 } 613 if (localOperationsWithCustomAnnotation != null) { 614 possibleOperations.addAll(localOperationsWithCustomAnnotation); 615 } 616 617 if (!possibleOperations.isEmpty()) { 618 MethodInfo answer = null; 619 620 if (name != null) { 621 // do we have hardcoded parameters values provided from the method name then use that for matching 622 String parameters = StringHelper.between(name, "(", ")"); 623 if (parameters != null) { 624 // special as we have hardcoded parameters, so we need to choose method that matches those parameters the best 625 LOG.trace("Choosing best matching method matching parameters: {}", parameters); 626 answer = chooseMethodWithMatchingParameters(exchange, parameters, possibleOperations); 627 } 628 } 629 if (answer == null) { 630 // multiple possible operations so find the best suited if possible 631 answer = chooseMethodWithMatchingBody(exchange, possibleOperations, localOperationsWithCustomAnnotation); 632 } 633 if (answer == null && possibleOperations.size() > 1) { 634 answer = getSingleCovariantMethod(possibleOperations); 635 } 636 637 if (answer == null) { 638 throw new AmbiguousMethodCallException(exchange, possibleOperations); 639 } else { 640 return answer; 641 } 642 } 643 } 644 645 // not possible to determine 646 return null; 647 } 648 649 private MethodInfo chooseMethodWithMatchingParameters(Exchange exchange, String parameters, Collection<MethodInfo> operationList) 650 throws AmbiguousMethodCallException { 651 // we have hardcoded parameters so need to match that with the given operations 652 Iterator<?> it = ObjectHelper.createIterator(parameters); 653 int count = 0; 654 while (it.hasNext()) { 655 it.next(); 656 count++; 657 } 658 659 List<MethodInfo> operations = new ArrayList<>(); 660 for (MethodInfo info : operationList) { 661 if (info.getParameters().size() == count) { 662 operations.add(info); 663 } 664 } 665 666 if (operations.isEmpty()) { 667 return null; 668 } else if (operations.size() == 1) { 669 return operations.get(0); 670 } 671 672 // okay we still got multiple operations, so need to match the best one 673 List<MethodInfo> candidates = new ArrayList<>(); 674 MethodInfo fallbackCandidate = null; 675 for (MethodInfo info : operations) { 676 it = ObjectHelper.createIterator(parameters, ",", false); 677 int index = 0; 678 boolean matches = true; 679 while (it.hasNext()) { 680 String parameter = (String) it.next(); 681 if (parameter != null) { 682 // must trim 683 parameter = parameter.trim(); 684 } 685 686 Class<?> parameterType = BeanHelper.getValidParameterType(parameter); 687 Class<?> expectedType = info.getParameters().get(index).getType(); 688 689 if (parameterType != null && expectedType != null) { 690 691 // if its a simple language then we need to evaluate the expression 692 // so we have the result and can find out what type the parameter actually is 693 if (StringHelper.hasStartToken(parameter, "simple")) { 694 LOG.trace("Evaluating simple expression for parameter #{}: {} to determine the class type of the parameter", index, parameter); 695 Object out = getCamelContext().resolveLanguage("simple").createExpression(parameter).evaluate(exchange, Object.class); 696 if (out != null) { 697 parameterType = out.getClass(); 698 } 699 } 700 701 // skip java.lang.Object type, when we have multiple possible methods we want to avoid it if possible 702 if (Object.class.equals(expectedType)) { 703 fallbackCandidate = info; 704 matches = false; 705 break; 706 } 707 708 boolean matchingTypes = isParameterMatchingType(parameterType, expectedType); 709 if (!matchingTypes) { 710 matches = false; 711 break; 712 } 713 } 714 715 index++; 716 } 717 718 if (matches) { 719 candidates.add(info); 720 } 721 } 722 723 if (candidates.size() > 1) { 724 MethodInfo answer = getSingleCovariantMethod(candidates); 725 if (answer != null) { 726 return answer; 727 } 728 } 729 return candidates.size() == 1 ? candidates.get(0) : fallbackCandidate; 730 } 731 732 private boolean isParameterMatchingType(Class<?> parameterType, Class<?> expectedType) { 733 if (Number.class.equals(parameterType)) { 734 // number should match long/int/etc. 735 if (Integer.class.isAssignableFrom(expectedType) || Long.class.isAssignableFrom(expectedType) 736 || int.class.isAssignableFrom(expectedType) || long.class.isAssignableFrom(expectedType)) { 737 return true; 738 } 739 } 740 return parameterType.isAssignableFrom(expectedType); 741 } 742 743 private MethodInfo getSingleCovariantMethod(Collection<MethodInfo> candidates) { 744 // if all the candidates are actually covariant, it doesn't matter which one we call 745 MethodInfo firstCandidate = candidates.iterator().next(); 746 for (MethodInfo candidate : candidates) { 747 if (!firstCandidate.isCovariantWith(candidate)) { 748 return null; 749 } 750 } 751 return firstCandidate; 752 } 753 754 private MethodInfo chooseMethodWithMatchingBody(Exchange exchange, Collection<MethodInfo> operationList, 755 List<MethodInfo> operationsWithCustomAnnotation) 756 throws AmbiguousMethodCallException { 757 // see if we can find a method whose body param type matches the message body 758 Message in = exchange.getIn(); 759 Object body = in.getBody(); 760 if (body != null) { 761 Class<?> bodyType = body.getClass(); 762 if (LOG.isTraceEnabled()) { 763 LOG.trace("Matching for method with a single parameter that matches type: {}", bodyType.getCanonicalName()); 764 } 765 766 List<MethodInfo> possibles = new ArrayList<>(); 767 List<MethodInfo> possiblesWithException = null; 768 for (MethodInfo methodInfo : operationList) { 769 // test for MEP pattern matching 770 boolean out = exchange.getPattern().isOutCapable(); 771 if (out && methodInfo.isReturnTypeVoid()) { 772 // skip this method as the MEP is Out so the method must return something 773 continue; 774 } 775 776 // try to match the arguments 777 if (methodInfo.bodyParameterMatches(bodyType)) { 778 LOG.trace("Found a possible method: {}", methodInfo); 779 if (methodInfo.hasExceptionParameter()) { 780 // methods with accepts exceptions 781 if (possiblesWithException == null) { 782 possiblesWithException = new ArrayList<>(); 783 } 784 possiblesWithException.add(methodInfo); 785 } else { 786 // regular methods with no exceptions 787 possibles.add(methodInfo); 788 } 789 } 790 } 791 792 // find best suited method to use 793 return chooseBestPossibleMethodInfo(exchange, operationList, body, possibles, possiblesWithException, operationsWithCustomAnnotation); 794 } 795 796 // no match so return null 797 return null; 798 } 799 800 private MethodInfo chooseBestPossibleMethodInfo(Exchange exchange, Collection<MethodInfo> operationList, Object body, 801 List<MethodInfo> possibles, List<MethodInfo> possiblesWithException, 802 List<MethodInfo> possibleWithCustomAnnotation) 803 throws AmbiguousMethodCallException { 804 805 Exception exception = ExpressionBuilder.exchangeExceptionExpression().evaluate(exchange, Exception.class); 806 if (exception != null && possiblesWithException != null && possiblesWithException.size() == 1) { 807 LOG.trace("Exchange has exception set so we prefer method that also has exception as parameter"); 808 // prefer the method that accepts exception in case we have an exception also 809 return possiblesWithException.get(0); 810 } else if (possibles.size() == 1) { 811 return possibles.get(0); 812 } else if (possibles.isEmpty()) { 813 LOG.trace("No possible methods so now trying to convert body to parameter types"); 814 815 // let's try converting 816 Object newBody = null; 817 MethodInfo matched = null; 818 int matchCounter = 0; 819 for (MethodInfo methodInfo : operationList) { 820 if (methodInfo.getBodyParameterType() != null) { 821 if (methodInfo.getBodyParameterType().isInstance(body)) { 822 return methodInfo; 823 } 824 825 // we should only try to convert, as we are looking for best match 826 Object value = exchange.getContext().getTypeConverter().tryConvertTo(methodInfo.getBodyParameterType(), exchange, body); 827 if (value != null) { 828 if (LOG.isTraceEnabled()) { 829 LOG.trace("Converted body from: {} to: {}", 830 body.getClass().getCanonicalName(), methodInfo.getBodyParameterType().getCanonicalName()); 831 } 832 matchCounter++; 833 newBody = value; 834 matched = methodInfo; 835 } 836 } 837 } 838 if (matchCounter > 1) { 839 throw new AmbiguousMethodCallException(exchange, Arrays.asList(matched, matched)); 840 } 841 if (matched != null) { 842 LOG.trace("Setting converted body: {}", body); 843 Message in = exchange.getIn(); 844 in.setBody(newBody); 845 return matched; 846 } 847 } else { 848 // if we only have a single method with custom annotations, let's use that one 849 if (possibleWithCustomAnnotation != null && possibleWithCustomAnnotation.size() == 1) { 850 MethodInfo answer = possibleWithCustomAnnotation.get(0); 851 LOG.trace("There are only one method with annotations so we choose it: {}", answer); 852 return answer; 853 } 854 // try to choose among multiple methods with annotations 855 MethodInfo chosen = chooseMethodWithCustomAnnotations(possibles); 856 if (chosen != null) { 857 return chosen; 858 } 859 // just make sure the methods aren't all actually the same 860 chosen = getSingleCovariantMethod(possibles); 861 if (chosen != null) { 862 return chosen; 863 } 864 throw new AmbiguousMethodCallException(exchange, possibles); 865 } 866 867 // cannot find a good method to use 868 return null; 869 } 870 871 /** 872 * Validates whether the given method is a valid candidate for Camel Bean Binding. 873 * 874 * @param clazz the class 875 * @param method the method 876 * @return true if valid, false to skip the method 877 */ 878 protected boolean isValidMethod(Class<?> clazz, Method method) { 879 // must not be in the excluded list 880 for (Method excluded : EXCLUDED_METHODS) { 881 if (ObjectHelper.isOverridingMethod(excluded, method)) { 882 // the method is overriding an excluded method so its not valid 883 return false; 884 } 885 } 886 887 // must be a public method 888 if (!Modifier.isPublic(method.getModifiers())) { 889 return false; 890 } 891 892 // return type must not be an Exchange and it should not be a bridge method 893 if ((method.getReturnType() != null && Exchange.class.isAssignableFrom(method.getReturnType())) || method.isBridge()) { 894 return false; 895 } 896 897 return true; 898 } 899 900 /** 901 * Gets the most specific override of a given method, if any. Indeed, 902 * overrides may have already been found while inspecting sub classes. Or 903 * the given method could override an interface extra method. 904 * 905 * @param proposedMethodInfo the method for which a more specific override is 906 * searched 907 * @return The already registered most specific override if any, otherwise 908 * <code>null</code> 909 */ 910 private MethodInfo findMostSpecificOverride(MethodInfo proposedMethodInfo) { 911 for (MethodInfo alreadyRegisteredMethodInfo : methodMap.values()) { 912 Method alreadyRegisteredMethod = alreadyRegisteredMethodInfo.getMethod(); 913 Method proposedMethod = proposedMethodInfo.getMethod(); 914 915 if (ObjectHelper.isOverridingMethod(getType(), proposedMethod, alreadyRegisteredMethod, false)) { 916 return alreadyRegisteredMethodInfo; 917 } else if (ObjectHelper.isOverridingMethod(getType(), alreadyRegisteredMethod, proposedMethod, false)) { 918 return proposedMethodInfo; 919 } 920 } 921 922 return null; 923 } 924 925 private MethodInfo chooseMethodWithCustomAnnotations(Collection<MethodInfo> possibles) { 926 // if we have only one method with custom annotations let's choose that 927 MethodInfo chosen = null; 928 for (MethodInfo possible : possibles) { 929 if (possible.hasCustomAnnotation()) { 930 if (chosen != null) { 931 chosen = null; 932 break; 933 } else { 934 chosen = possible; 935 } 936 } 937 } 938 return chosen; 939 } 940 941 /** 942 * Creates an expression for the given parameter type if the parameter can 943 * be mapped automatically or null if the parameter cannot be mapped due to 944 * insufficient annotations or not fitting with the default type 945 * conventions. 946 */ 947 private Expression createParameterUnmarshalExpression(Class<?> clazz, Method method, 948 Class<?> parameterType, Annotation[] parameterAnnotation) { 949 950 // look for a parameter annotation that converts into an expression 951 for (Annotation annotation : parameterAnnotation) { 952 Expression answer = createParameterUnmarshalExpressionForAnnotation(clazz, method, parameterType, annotation); 953 if (answer != null) { 954 return answer; 955 } 956 } 957 // no annotations then try the default parameter mappings 958 return strategy.getDefaultParameterTypeExpression(parameterType); 959 } 960 961 private Expression createParameterUnmarshalExpressionForAnnotation(Class<?> clazz, Method method, 962 Class<?> parameterType, Annotation annotation) { 963 if (annotation instanceof AttachmentObjects) { 964 return ExpressionBuilder.attachmentObjectsExpression(); 965 } else if (annotation instanceof Attachments) { 966 return ExpressionBuilder.attachmentsExpression(); 967 } else if (annotation instanceof Property) { 968 Property propertyAnnotation = (Property)annotation; 969 return ExpressionBuilder.exchangePropertyExpression(propertyAnnotation.value()); 970 } else if (annotation instanceof ExchangeProperty) { 971 ExchangeProperty propertyAnnotation = (ExchangeProperty)annotation; 972 return ExpressionBuilder.exchangePropertyExpression(propertyAnnotation.value()); 973 } else if (annotation instanceof Properties) { 974 return ExpressionBuilder.exchangePropertiesExpression(); 975 } else if (annotation instanceof ExchangeProperties) { 976 return ExpressionBuilder.exchangePropertiesExpression(); 977 } else if (annotation instanceof Header) { 978 Header headerAnnotation = (Header)annotation; 979 return ExpressionBuilder.headerExpression(headerAnnotation.value()); 980 } else if (annotation instanceof Headers) { 981 return ExpressionBuilder.headersExpression(); 982 } else if (annotation instanceof OutHeaders) { 983 return ExpressionBuilder.outHeadersExpression(); 984 } else if (annotation instanceof ExchangeException) { 985 return ExpressionBuilder.exchangeExceptionExpression(CastUtils.cast(parameterType, Exception.class)); 986 } else if (annotation instanceof PropertyInject) { 987 PropertyInject propertyAnnotation = (PropertyInject) annotation; 988 Expression inject = ExpressionBuilder.propertiesComponentExpression(propertyAnnotation.value(), null, propertyAnnotation.defaultValue()); 989 return ExpressionBuilder.convertToExpression(inject, parameterType); 990 } else { 991 LanguageAnnotation languageAnnotation = annotation.annotationType().getAnnotation(LanguageAnnotation.class); 992 if (languageAnnotation != null) { 993 Class<?> type = languageAnnotation.factory(); 994 Object object = camelContext.getInjector().newInstance(type); 995 if (object instanceof AnnotationExpressionFactory) { 996 AnnotationExpressionFactory expressionFactory = (AnnotationExpressionFactory) object; 997 return expressionFactory.createExpression(camelContext, annotation, languageAnnotation, parameterType); 998 } else { 999 LOG.warn("Ignoring bad annotation: " + languageAnnotation + "on method: " + method 1000 + " which declares a factory: " + type.getName() 1001 + " which does not implement " + AnnotationExpressionFactory.class.getName()); 1002 } 1003 } 1004 } 1005 1006 return null; 1007 } 1008 1009 private static void removeAllSetterOrGetterMethods(List<MethodInfo> methods) { 1010 Iterator<MethodInfo> it = methods.iterator(); 1011 while (it.hasNext()) { 1012 MethodInfo info = it.next(); 1013 if (IntrospectionSupport.isGetter(info.getMethod())) { 1014 // skip getters 1015 it.remove(); 1016 } else if (IntrospectionSupport.isSetter(info.getMethod())) { 1017 // skip setters 1018 it.remove(); 1019 } 1020 } 1021 } 1022 1023 private void removeNonMatchingMethods(List<MethodInfo> methods, String name) { 1024 // method does not match so remove it 1025 methods.removeIf(info -> !matchMethod(info.getMethod(), name)); 1026 } 1027 1028 private void removeAllAbstractMethods(List<MethodInfo> methods) { 1029 Iterator<MethodInfo> it = methods.iterator(); 1030 while (it.hasNext()) { 1031 MethodInfo info = it.next(); 1032 // if the class is an interface then keep the method 1033 boolean isFromInterface = Modifier.isInterface(info.getMethod().getDeclaringClass().getModifiers()); 1034 if (!isFromInterface && Modifier.isAbstract(info.getMethod().getModifiers())) { 1035 // we cannot invoke an abstract method 1036 it.remove(); 1037 } 1038 } 1039 } 1040 1041 private boolean matchMethod(Method method, String methodName) { 1042 if (methodName == null) { 1043 return true; 1044 } 1045 1046 if (methodName.contains("(") && !methodName.endsWith(")")) { 1047 throw new IllegalArgumentException("Name must have both starting and ending parenthesis, was: " + methodName); 1048 } 1049 1050 // do not use qualifier for name matching 1051 String name = methodName; 1052 if (name.contains("(")) { 1053 name = StringHelper.before(name, "("); 1054 } 1055 1056 // must match name 1057 if (name != null && !name.equals(method.getName())) { 1058 return false; 1059 } 1060 1061 // is it a method with no parameters 1062 boolean noParameters = methodName.endsWith("()"); 1063 if (noParameters) { 1064 return method.getParameterCount() == 0; 1065 } 1066 1067 // match qualifier types which is used to select among overloaded methods 1068 String types = StringHelper.between(methodName, "(", ")"); 1069 if (ObjectHelper.isNotEmpty(types)) { 1070 // we must qualify based on types to match method 1071 String[] parameters = StringQuoteHelper.splitSafeQuote(types, ','); 1072 Class<?>[] parameterTypes = null; 1073 Iterator<?> it = ObjectHelper.createIterator(parameters); 1074 for (int i = 0; i < method.getParameterCount(); i++) { 1075 if (it.hasNext()) { 1076 if (parameterTypes == null) { 1077 parameterTypes = method.getParameterTypes(); 1078 } 1079 Class<?> parameterType = parameterTypes[i]; 1080 1081 String qualifyType = (String) it.next(); 1082 if (ObjectHelper.isEmpty(qualifyType)) { 1083 continue; 1084 } 1085 // trim the type 1086 qualifyType = qualifyType.trim(); 1087 1088 if ("*".equals(qualifyType)) { 1089 // * is a wildcard so we accept and match that parameter type 1090 continue; 1091 } 1092 1093 if (BeanHelper.isValidParameterValue(qualifyType)) { 1094 // its a parameter value, so continue to next parameter 1095 // as we should only check for FQN/type parameters 1096 continue; 1097 } 1098 1099 // if qualify type indeed is a class, then it must be assignable with the parameter type 1100 Boolean assignable = BeanHelper.isAssignableToExpectedType(getCamelContext().getClassResolver(), qualifyType, parameterType); 1101 // the method will return null if the qualifyType is not a class 1102 if (assignable != null && !assignable) { 1103 return false; 1104 } 1105 1106 } else { 1107 // there method has more parameters than was specified in the method name qualifiers 1108 return false; 1109 } 1110 } 1111 1112 // if the method has no more types then we can only regard it as matched 1113 // if there are no more qualifiers 1114 if (it.hasNext()) { 1115 return false; 1116 } 1117 } 1118 1119 // the method matched 1120 return true; 1121 } 1122 1123 private static Class<?> getTargetClass(Class<?> clazz) { 1124 if (clazz != null && clazz.getName().contains(CGLIB_CLASS_SEPARATOR)) { 1125 Class<?> superClass = clazz.getSuperclass(); 1126 if (superClass != null && !Object.class.equals(superClass)) { 1127 return superClass; 1128 } 1129 } 1130 return clazz; 1131 } 1132 1133 /** 1134 * Do we have a method with the given name. 1135 * <p/> 1136 * Shorthand method names for getters is supported, so you can pass in eg 'name' and Camel 1137 * will can find the real 'getName' method instead. 1138 * 1139 * @param methodName the method name 1140 * @return <tt>true</tt> if we have such a method. 1141 */ 1142 public boolean hasMethod(String methodName) { 1143 return getOperations(methodName) != null; 1144 } 1145 1146 /** 1147 * Do we have a static method with the given name. 1148 * <p/> 1149 * Shorthand method names for getters is supported, so you can pass in eg 'name' and Camel 1150 * will can find the real 'getName' method instead. 1151 * 1152 * @param methodName the method name 1153 * @return <tt>true</tt> if we have such a static method. 1154 */ 1155 public boolean hasStaticMethod(String methodName) { 1156 List<MethodInfo> methods = getOperations(methodName); 1157 if (methods == null || methods.isEmpty()) { 1158 return false; 1159 } 1160 for (MethodInfo method : methods) { 1161 if (method.isStaticMethod()) { 1162 return true; 1163 } 1164 } 1165 return false; 1166 } 1167 1168 /** 1169 * Returns whether the bean class has any public constructors. 1170 */ 1171 public boolean hasPublicConstructors() { 1172 return publicConstructors; 1173 } 1174 1175 /** 1176 * Gets the list of methods sorted by A..Z method name. 1177 * 1178 * @return the methods. 1179 */ 1180 public List<MethodInfo> getMethods() { 1181 if (operations.isEmpty()) { 1182 return Collections.emptyList(); 1183 } 1184 1185 List<MethodInfo> methods = new ArrayList<>(); 1186 for (Collection<MethodInfo> col : operations.values()) { 1187 methods.addAll(col); 1188 } 1189 1190 if (methods.size() > 1) { 1191 // sort the methods by name A..Z 1192 methods.sort(Comparator.comparing(o -> o.getMethod().getName())); 1193 } 1194 return methods; 1195 } 1196 1197 /** 1198 * Does any of the methods have a Canel @Handler annotation. 1199 */ 1200 public boolean hasAnyMethodHandlerAnnotation() { 1201 return !operationsWithHandlerAnnotation.isEmpty(); 1202 } 1203 1204 /** 1205 * Get the operation(s) with the given name. We can have multiple when methods is overloaded. 1206 * <p/> 1207 * Shorthand method names for getters is supported, so you can pass in eg 'name' and Camel 1208 * will can find the real 'getName' method instead. 1209 * 1210 * @param methodName the method name 1211 * @return the found method, or <tt>null</tt> if not found 1212 */ 1213 private List<MethodInfo> getOperations(String methodName) { 1214 // do not use qualifier for name 1215 if (methodName.contains("(")) { 1216 methodName = StringHelper.before(methodName, "("); 1217 } 1218 1219 List<MethodInfo> answer = operations.get(methodName); 1220 if (answer != null) { 1221 return answer; 1222 } 1223 1224 // now try all getters to see if any of those matched the methodName 1225 for (Method method : methodMap.keySet()) { 1226 if (IntrospectionSupport.isGetter(method)) { 1227 String shorthandMethodName = IntrospectionSupport.getGetterShorthandName(method); 1228 // if the two names matches then see if we can find it using that name 1229 if (methodName != null && methodName.equals(shorthandMethodName)) { 1230 return operations.get(method.getName()); 1231 } 1232 } 1233 } 1234 1235 return null; 1236 } 1237 1238}