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 */ 017 package org.apache.camel.component.bean; 018 019 import java.lang.annotation.Annotation; 020 import java.lang.reflect.Method; 021 import java.lang.reflect.Modifier; 022 import java.lang.reflect.Proxy; 023 import java.util.ArrayList; 024 import java.util.Arrays; 025 import java.util.Collection; 026 import java.util.Collections; 027 import java.util.Comparator; 028 import java.util.HashMap; 029 import java.util.Iterator; 030 import java.util.List; 031 import java.util.Map; 032 033 import org.apache.camel.Attachments; 034 import org.apache.camel.Body; 035 import org.apache.camel.CamelContext; 036 import org.apache.camel.Exchange; 037 import org.apache.camel.ExchangeException; 038 import org.apache.camel.Expression; 039 import org.apache.camel.Handler; 040 import org.apache.camel.Header; 041 import org.apache.camel.Headers; 042 import org.apache.camel.Message; 043 import org.apache.camel.OutHeaders; 044 import org.apache.camel.Properties; 045 import org.apache.camel.Property; 046 import org.apache.camel.builder.ExpressionBuilder; 047 import org.apache.camel.language.LanguageAnnotation; 048 import org.apache.camel.spi.Registry; 049 import org.apache.camel.util.CastUtils; 050 import org.apache.camel.util.IntrospectionSupport; 051 import org.apache.camel.util.ObjectHelper; 052 import org.apache.camel.util.StringQuoteHelper; 053 import org.slf4j.Logger; 054 import org.slf4j.LoggerFactory; 055 056 /** 057 * Represents the metadata about a bean type created via a combination of 058 * introspection and annotations together with some useful sensible defaults 059 * 060 * @version 061 */ 062 public class BeanInfo { 063 private static final transient Logger LOG = LoggerFactory.getLogger(BeanInfo.class); 064 private static final String CGLIB_CLASS_SEPARATOR = "$$"; 065 private static final List<Method> EXCLUDED_METHODS = new ArrayList<Method>(); 066 private final CamelContext camelContext; 067 private final Class<?> type; 068 private final ParameterMappingStrategy strategy; 069 private final MethodInfo defaultMethod; 070 // shared state with details of operations introspected from the bean, created during the constructor 071 private Map<String, List<MethodInfo>> operations = new HashMap<String, List<MethodInfo>>(); 072 private List<MethodInfo> operationsWithBody = new ArrayList<MethodInfo>(); 073 private List<MethodInfo> operationsWithNoBody = new ArrayList<MethodInfo>(); 074 private List<MethodInfo> operationsWithCustomAnnotation = new ArrayList<MethodInfo>(); 075 private List<MethodInfo> operationsWithHandlerAnnotation = new ArrayList<MethodInfo>(); 076 private Map<Method, MethodInfo> methodMap = new HashMap<Method, MethodInfo>(); 077 078 static { 079 // exclude all java.lang.Object methods as we dont want to invoke them 080 EXCLUDED_METHODS.addAll(Arrays.asList(Object.class.getMethods())); 081 // exclude all java.lang.reflect.Proxy methods as we dont want to invoke them 082 EXCLUDED_METHODS.addAll(Arrays.asList(Proxy.class.getMethods())); 083 try { 084 // but keep toString as this method is okay 085 EXCLUDED_METHODS.remove(Object.class.getMethod("toString")); 086 EXCLUDED_METHODS.remove(Proxy.class.getMethod("toString")); 087 } catch (Throwable e) { 088 // ignore 089 } 090 } 091 092 public BeanInfo(CamelContext camelContext, Class<?> type) { 093 this(camelContext, type, createParameterMappingStrategy(camelContext)); 094 } 095 096 public BeanInfo(CamelContext camelContext, Method explicitMethod) { 097 this(camelContext, explicitMethod.getDeclaringClass(), explicitMethod, createParameterMappingStrategy(camelContext)); 098 } 099 100 public BeanInfo(CamelContext camelContext, Class<?> type, ParameterMappingStrategy strategy) { 101 this(camelContext, type, null, strategy); 102 } 103 104 public BeanInfo(CamelContext camelContext, Class<?> type, Method explicitMethod, ParameterMappingStrategy strategy) { 105 this.camelContext = camelContext; 106 this.type = type; 107 this.strategy = strategy; 108 109 if (explicitMethod != null) { 110 // must be a valid method 111 if (!isValidMethod(type, explicitMethod)) { 112 throw new IllegalArgumentException("The method " + explicitMethod + " is not valid (for example the method must be public)"); 113 } 114 introspect(getType(), explicitMethod); 115 } else { 116 introspect(getType()); 117 } 118 119 // if there are only 1 method with 1 operation then select it as a default/fallback method 120 MethodInfo method = null; 121 if (operations.size() == 1) { 122 List<MethodInfo> methods = operations.values().iterator().next(); 123 if (methods.size() == 1) { 124 method = methods.get(0); 125 } 126 } 127 defaultMethod = method; 128 129 // mark the operations lists as unmodifiable, as they should not change during runtime 130 // to keep this code thread safe 131 operations = Collections.unmodifiableMap(operations); 132 operationsWithBody = Collections.unmodifiableList(operationsWithBody); 133 operationsWithNoBody = Collections.unmodifiableList(operationsWithNoBody); 134 operationsWithCustomAnnotation = Collections.unmodifiableList(operationsWithCustomAnnotation); 135 operationsWithHandlerAnnotation = Collections.unmodifiableList(operationsWithHandlerAnnotation); 136 methodMap = Collections.unmodifiableMap(methodMap); 137 } 138 139 public Class<?> getType() { 140 return type; 141 } 142 143 public CamelContext getCamelContext() { 144 return camelContext; 145 } 146 147 public static ParameterMappingStrategy createParameterMappingStrategy(CamelContext camelContext) { 148 // lookup in registry first if there is a user define strategy 149 Registry registry = camelContext.getRegistry(); 150 ParameterMappingStrategy answer = registry.lookup(BeanConstants.BEAN_PARAMETER_MAPPING_STRATEGY, ParameterMappingStrategy.class); 151 if (answer == null) { 152 // no then use the default one 153 answer = new DefaultParameterMappingStrategy(); 154 } 155 156 return answer; 157 } 158 159 public MethodInvocation createInvocation(Object pojo, Exchange exchange) 160 throws AmbiguousMethodCallException, MethodNotFoundException { 161 return createInvocation(pojo, exchange, null); 162 } 163 164 private MethodInvocation createInvocation(Object pojo, Exchange exchange, Method explicitMethod) 165 throws AmbiguousMethodCallException, MethodNotFoundException { 166 MethodInfo methodInfo = null; 167 168 // find the explicit method to invoke 169 if (explicitMethod != null) { 170 Iterator<List<MethodInfo>> it = operations.values().iterator(); 171 while (it.hasNext()) { 172 List<MethodInfo> infos = it.next(); 173 for (MethodInfo info : infos) { 174 if (explicitMethod.equals(info.getMethod())) { 175 return info.createMethodInvocation(pojo, exchange); 176 } 177 } 178 } 179 throw new MethodNotFoundException(exchange, pojo, explicitMethod.getName()); 180 } 181 182 String methodName = exchange.getIn().getHeader(Exchange.BEAN_METHOD_NAME, String.class); 183 if (methodName != null) { 184 185 // do not use qualifier for name 186 String name = methodName; 187 if (methodName.contains("(")) { 188 name = ObjectHelper.before(methodName, "("); 189 } 190 191 // special for getClass, as we want the user to be able to invoke this method 192 // for example to log the class type or the likes 193 if ("class".equals(name) || "getClass".equals(name)) { 194 try { 195 Method method = pojo.getClass().getMethod("getClass"); 196 methodInfo = new MethodInfo(exchange.getContext(), pojo.getClass(), method, Collections.<ParameterInfo>emptyList(), Collections.<ParameterInfo>emptyList(), false, false); 197 } catch (NoSuchMethodException e) { 198 throw new MethodNotFoundException(exchange, pojo, "getClass"); 199 } 200 } else { 201 List<MethodInfo> methods = getOperations(name); 202 if (methods != null && methods.size() == 1) { 203 // only one method then choose it 204 methodInfo = methods.get(0); 205 } else if (methods != null) { 206 // there are more methods with that name so we cannot decide which to use 207 208 // but first let's try to choose a method and see if that complies with the name 209 // must use the method name which may have qualifiers 210 methodInfo = chooseMethod(pojo, exchange, methodName); 211 212 if (methodInfo == null || !name.equals(methodInfo.getMethod().getName())) { 213 throw new AmbiguousMethodCallException(exchange, methods); 214 } 215 } else { 216 // a specific method was given to invoke but not found 217 throw new MethodNotFoundException(exchange, pojo, methodName); 218 } 219 } 220 } 221 222 if (methodInfo == null) { 223 // no name or type 224 methodInfo = chooseMethod(pojo, exchange, null); 225 } 226 if (methodInfo == null) { 227 methodInfo = defaultMethod; 228 } 229 if (methodInfo != null) { 230 LOG.trace("Chosen method to invoke: {} on bean: {}", methodInfo, pojo); 231 return methodInfo.createMethodInvocation(pojo, exchange); 232 } 233 234 LOG.debug("Cannot find suitable method to invoke on bean: {}", pojo); 235 return null; 236 } 237 238 /** 239 * Introspects the given class 240 * 241 * @param clazz the class 242 */ 243 private void introspect(Class<?> clazz) { 244 // get the target clazz as it could potentially have been enhanced by CGLIB etc. 245 clazz = getTargetClass(clazz); 246 ObjectHelper.notNull(clazz, "clazz", this); 247 248 LOG.trace("Introspecting class: {}", clazz); 249 250 // if the class is not public then fallback and use interface methods if possible 251 // this allow Camel to invoke private beans which implements interfaces 252 List<Method> methods = Arrays.asList(clazz.getDeclaredMethods()); 253 if (!Modifier.isPublic(clazz.getModifiers())) { 254 LOG.trace("Preferring interface methods as class: {} is not public accessible", clazz); 255 List<Method> interfaceMethods = getInterfaceMethods(clazz); 256 257 // still keep non-accessible class methods to provide more specific Exception if method is non-accessible 258 interfaceMethods.addAll(methods); 259 methods = interfaceMethods; 260 } 261 262 for (Method method : methods) { 263 boolean valid = isValidMethod(clazz, method); 264 LOG.trace("Method: {} is valid: {}", method, valid); 265 if (valid) { 266 introspect(clazz, method); 267 } 268 } 269 270 Class<?> superclass = clazz.getSuperclass(); 271 if (superclass != null && !superclass.equals(Object.class)) { 272 introspect(superclass); 273 } 274 } 275 276 /** 277 * Introspects the given method 278 * 279 * @param clazz the class 280 * @param method the method 281 * @return the method info, is newer <tt>null</tt> 282 */ 283 private MethodInfo introspect(Class<?> clazz, Method method) { 284 LOG.trace("Introspecting class: {}, method: {}", clazz, method); 285 String opName = method.getName(); 286 287 MethodInfo methodInfo = createMethodInfo(clazz, method); 288 289 // methods already registered should be preferred to use instead of super classes of existing methods 290 // we want to us the method from the sub class over super classes, so if we have already registered 291 // the method then use it (we are traversing upwards: sub (child) -> super (farther) ) 292 MethodInfo existingMethodInfo = overridesExistingMethod(methodInfo); 293 if (existingMethodInfo != null) { 294 LOG.trace("This method is already overridden in a subclass, so the method from the sub class is preferred: {}", existingMethodInfo); 295 return existingMethodInfo; 296 } 297 298 LOG.trace("Adding operation: {} for method: {}", opName, methodInfo); 299 300 if (hasMethod(opName)) { 301 // we have an overloaded method so add the method info to the same key 302 List<MethodInfo> existing = getOperations(opName); 303 existing.add(methodInfo); 304 } else { 305 // its a new method we have not seen before so wrap it in a list and add it 306 List<MethodInfo> methods = new ArrayList<MethodInfo>(); 307 methods.add(methodInfo); 308 operations.put(opName, methods); 309 } 310 311 if (methodInfo.hasCustomAnnotation()) { 312 operationsWithCustomAnnotation.add(methodInfo); 313 } else if (methodInfo.hasBodyParameter()) { 314 operationsWithBody.add(methodInfo); 315 } else { 316 operationsWithNoBody.add(methodInfo); 317 } 318 319 if (methodInfo.hasHandlerAnnotation()) { 320 operationsWithHandlerAnnotation.add(methodInfo); 321 } 322 323 // must add to method map last otherwise we break stuff 324 methodMap.put(method, methodInfo); 325 326 return methodInfo; 327 } 328 329 330 /** 331 * Returns the {@link MethodInfo} for the given method if it exists or null 332 * if there is no metadata available for the given method 333 */ 334 public MethodInfo getMethodInfo(Method method) { 335 MethodInfo answer = methodMap.get(method); 336 if (answer == null) { 337 // maybe the method is defined on a base class? 338 if (type != Object.class) { 339 Class<?> superclass = type.getSuperclass(); 340 if (superclass != null && superclass != Object.class) { 341 BeanInfo superBeanInfo = new BeanInfo(camelContext, superclass, strategy); 342 return superBeanInfo.getMethodInfo(method); 343 } 344 } 345 } 346 return answer; 347 } 348 349 protected MethodInfo createMethodInfo(Class<?> clazz, Method method) { 350 Class<?>[] parameterTypes = method.getParameterTypes(); 351 List<Annotation>[] parametersAnnotations = collectParameterAnnotations(clazz, method); 352 353 List<ParameterInfo> parameters = new ArrayList<ParameterInfo>(); 354 List<ParameterInfo> bodyParameters = new ArrayList<ParameterInfo>(); 355 356 boolean hasCustomAnnotation = false; 357 boolean hasHandlerAnnotation = ObjectHelper.hasAnnotation(method.getAnnotations(), Handler.class); 358 359 int size = parameterTypes.length; 360 if (LOG.isTraceEnabled()) { 361 LOG.trace("Creating MethodInfo for class: {} method: {} having {} parameters", new Object[]{clazz, method, size}); 362 } 363 364 for (int i = 0; i < size; i++) { 365 Class<?> parameterType = parameterTypes[i]; 366 Annotation[] parameterAnnotations = parametersAnnotations[i].toArray(new Annotation[parametersAnnotations[i].size()]); 367 Expression expression = createParameterUnmarshalExpression(clazz, method, parameterType, parameterAnnotations); 368 hasCustomAnnotation |= expression != null; 369 370 ParameterInfo parameterInfo = new ParameterInfo(i, parameterType, parameterAnnotations, expression); 371 LOG.trace("Parameter #{}: {}", i, parameterInfo); 372 parameters.add(parameterInfo); 373 if (expression == null) { 374 boolean bodyAnnotation = ObjectHelper.hasAnnotation(parameterAnnotations, Body.class); 375 LOG.trace("Parameter #{} has @Body annotation", i); 376 hasCustomAnnotation |= bodyAnnotation; 377 if (bodyParameters.isEmpty()) { 378 // okay we have not yet set the body parameter and we have found 379 // the candidate now to use as body parameter 380 if (Exchange.class.isAssignableFrom(parameterType)) { 381 // use exchange 382 expression = ExpressionBuilder.exchangeExpression(); 383 } else { 384 // assume it's the body and it must be mandatory convertible to the parameter type 385 // but we allow null bodies in case the message really contains a null body 386 expression = ExpressionBuilder.mandatoryBodyExpression(parameterType, true); 387 } 388 LOG.trace("Parameter #{} is the body parameter using expression {}", i, expression); 389 parameterInfo.setExpression(expression); 390 bodyParameters.add(parameterInfo); 391 } else { 392 // will ignore the expression for parameter evaluation 393 } 394 } 395 LOG.trace("Parameter #{} has parameter info: ", i, parameterInfo); 396 } 397 398 // now let's add the method to the repository 399 return new MethodInfo(camelContext, clazz, method, parameters, bodyParameters, hasCustomAnnotation, hasHandlerAnnotation); 400 } 401 402 protected List<Annotation>[] collectParameterAnnotations(Class<?> c, Method m) { 403 @SuppressWarnings("unchecked") 404 List<Annotation>[] annotations = new List[m.getParameterTypes().length]; 405 for (int i = 0; i < annotations.length; i++) { 406 annotations[i] = new ArrayList<Annotation>(); 407 } 408 collectParameterAnnotations(c, m, annotations); 409 return annotations; 410 } 411 412 protected void collectParameterAnnotations(Class<?> c, Method m, List<Annotation>[] a) { 413 try { 414 Annotation[][] pa = c.getDeclaredMethod(m.getName(), m.getParameterTypes()).getParameterAnnotations(); 415 for (int i = 0; i < pa.length; i++) { 416 a[i].addAll(Arrays.asList(pa[i])); 417 } 418 } catch (NoSuchMethodException e) { 419 // no method with signature of m declared on c 420 } 421 for (Class<?> i : c.getInterfaces()) { 422 collectParameterAnnotations(i, m, a); 423 } 424 if (!c.isInterface() && c.getSuperclass() != null) { 425 collectParameterAnnotations(c.getSuperclass(), m, a); 426 } 427 } 428 429 /** 430 * Choose one of the available methods to invoke if we can match 431 * the message body to the body parameter 432 * 433 * @param pojo the bean to invoke a method on 434 * @param exchange the message exchange 435 * @param name an optional name of the method that must match, use <tt>null</tt> to indicate all methods 436 * @return the method to invoke or null if no definitive method could be matched 437 * @throws AmbiguousMethodCallException is thrown if cannot choose method due to ambiguity 438 */ 439 protected MethodInfo chooseMethod(Object pojo, Exchange exchange, String name) throws AmbiguousMethodCallException { 440 // @Handler should be select first 441 // then any single method that has a custom @annotation 442 // or any single method that has a match parameter type that matches the Exchange payload 443 // and last then try to select the best among the rest 444 445 // must use defensive copy, to avoid altering the shared lists 446 // and we want to remove unwanted operations from these local lists 447 final List<MethodInfo> localOperationsWithBody = new ArrayList<MethodInfo>(operationsWithBody); 448 final List<MethodInfo> localOperationsWithNoBody = new ArrayList<MethodInfo>(operationsWithNoBody); 449 final List<MethodInfo> localOperationsWithCustomAnnotation = new ArrayList<MethodInfo>(operationsWithCustomAnnotation); 450 final List<MethodInfo> localOperationsWithHandlerAnnotation = new ArrayList<MethodInfo>(operationsWithHandlerAnnotation); 451 452 if (name != null) { 453 // filter all lists to only include methods with this name 454 removeNonMatchingMethods(localOperationsWithHandlerAnnotation, name); 455 removeNonMatchingMethods(localOperationsWithCustomAnnotation, name); 456 removeNonMatchingMethods(localOperationsWithBody, name); 457 removeNonMatchingMethods(localOperationsWithNoBody, name); 458 } else { 459 // remove all getter/setter as we do not want to consider these methods 460 removeAllSetterOrGetterMethods(localOperationsWithHandlerAnnotation); 461 removeAllSetterOrGetterMethods(localOperationsWithCustomAnnotation); 462 removeAllSetterOrGetterMethods(localOperationsWithBody); 463 removeAllSetterOrGetterMethods(localOperationsWithNoBody); 464 } 465 466 if (localOperationsWithHandlerAnnotation.size() > 1) { 467 // if we have more than 1 @Handler then its ambiguous 468 throw new AmbiguousMethodCallException(exchange, localOperationsWithHandlerAnnotation); 469 } 470 471 if (localOperationsWithHandlerAnnotation.size() == 1) { 472 // methods with handler should be preferred 473 return localOperationsWithHandlerAnnotation.get(0); 474 } else if (localOperationsWithCustomAnnotation.size() == 1) { 475 // if there is one method with an annotation then use that one 476 return localOperationsWithCustomAnnotation.get(0); 477 } 478 479 // named method and with no parameters 480 boolean noParameters = name != null && name.endsWith("()"); 481 if (noParameters && localOperationsWithNoBody.size() == 1) { 482 // if there was a method name configured and it has no parameters, then use the method with no body (eg no parameters) 483 return localOperationsWithNoBody.get(0); 484 } else if (localOperationsWithBody.size() == 1) { 485 // if there is one method with body then use that one 486 return localOperationsWithBody.get(0); 487 } 488 489 Collection<MethodInfo> possibleOperations = new ArrayList<MethodInfo>(); 490 possibleOperations.addAll(localOperationsWithBody); 491 possibleOperations.addAll(localOperationsWithCustomAnnotation); 492 493 if (!possibleOperations.isEmpty()) { 494 // multiple possible operations so find the best suited if possible 495 MethodInfo answer = chooseMethodWithMatchingBody(exchange, possibleOperations, localOperationsWithCustomAnnotation); 496 if (answer == null) { 497 throw new AmbiguousMethodCallException(exchange, possibleOperations); 498 } else { 499 return answer; 500 } 501 } 502 503 // not possible to determine 504 return null; 505 } 506 507 private MethodInfo chooseMethodWithMatchingBody(Exchange exchange, Collection<MethodInfo> operationList, 508 List<MethodInfo> operationsWithCustomAnnotation) 509 throws AmbiguousMethodCallException { 510 // see if we can find a method whose body param type matches the message body 511 Message in = exchange.getIn(); 512 Object body = in.getBody(); 513 if (body != null) { 514 Class<?> bodyType = body.getClass(); 515 if (LOG.isTraceEnabled()) { 516 LOG.trace("Matching for method with a single parameter that matches type: {}", bodyType.getCanonicalName()); 517 } 518 519 List<MethodInfo> possibles = new ArrayList<MethodInfo>(); 520 List<MethodInfo> possiblesWithException = new ArrayList<MethodInfo>(); 521 for (MethodInfo methodInfo : operationList) { 522 // test for MEP pattern matching 523 boolean out = exchange.getPattern().isOutCapable(); 524 if (out && methodInfo.isReturnTypeVoid()) { 525 // skip this method as the MEP is Out so the method must return something 526 continue; 527 } 528 529 // try to match the arguments 530 if (methodInfo.bodyParameterMatches(bodyType)) { 531 LOG.trace("Found a possible method: {}", methodInfo); 532 if (methodInfo.hasExceptionParameter()) { 533 // methods with accepts exceptions 534 possiblesWithException.add(methodInfo); 535 } else { 536 // regular methods with no exceptions 537 possibles.add(methodInfo); 538 } 539 } 540 } 541 542 // find best suited method to use 543 return chooseBestPossibleMethodInfo(exchange, operationList, body, possibles, possiblesWithException, operationsWithCustomAnnotation); 544 } 545 546 // no match so return null 547 return null; 548 } 549 550 private MethodInfo chooseBestPossibleMethodInfo(Exchange exchange, Collection<MethodInfo> operationList, Object body, 551 List<MethodInfo> possibles, List<MethodInfo> possiblesWithException, 552 List<MethodInfo> possibleWithCustomAnnotation) 553 throws AmbiguousMethodCallException { 554 555 Exception exception = ExpressionBuilder.exchangeExceptionExpression().evaluate(exchange, Exception.class); 556 if (exception != null && possiblesWithException.size() == 1) { 557 LOG.trace("Exchange has exception set so we prefer method that also has exception as parameter"); 558 // prefer the method that accepts exception in case we have an exception also 559 return possiblesWithException.get(0); 560 } else if (possibles.size() == 1) { 561 return possibles.get(0); 562 } else if (possibles.isEmpty()) { 563 LOG.trace("No possible methods so now trying to convert body to parameter types"); 564 565 // let's try converting 566 Object newBody = null; 567 MethodInfo matched = null; 568 int matchCounter = 0; 569 for (MethodInfo methodInfo : operationList) { 570 if (methodInfo.getBodyParameterType().isInstance(body)) { 571 return methodInfo; 572 } 573 574 // we should only try to convert, as we are looking for best match 575 Object value = exchange.getContext().getTypeConverter().tryConvertTo(methodInfo.getBodyParameterType(), exchange, body); 576 if (value != null) { 577 if (LOG.isTraceEnabled()) { 578 LOG.trace("Converted body from: {} to: {}", 579 body.getClass().getCanonicalName(), methodInfo.getBodyParameterType().getCanonicalName()); 580 } 581 matchCounter++; 582 newBody = value; 583 matched = methodInfo; 584 } 585 } 586 if (matchCounter > 1) { 587 throw new AmbiguousMethodCallException(exchange, Arrays.asList(matched, matched)); 588 } 589 if (matched != null) { 590 LOG.trace("Setting converted body: {}", body); 591 Message in = exchange.getIn(); 592 in.setBody(newBody); 593 return matched; 594 } 595 } else { 596 // if we only have a single method with custom annotations, let's use that one 597 if (possibleWithCustomAnnotation.size() == 1) { 598 MethodInfo answer = possibleWithCustomAnnotation.get(0); 599 LOG.trace("There are only one method with annotations so we choose it: {}", answer); 600 return answer; 601 } 602 // phew try to choose among multiple methods with annotations 603 return chooseMethodWithCustomAnnotations(exchange, possibles); 604 } 605 606 // cannot find a good method to use 607 return null; 608 } 609 610 /** 611 * Validates whether the given method is a valid candidate for Camel Bean Binding. 612 * 613 * @param clazz the class 614 * @param method the method 615 * @return true if valid, false to skip the method 616 */ 617 protected boolean isValidMethod(Class<?> clazz, Method method) { 618 // must not be in the excluded list 619 for (Method excluded : EXCLUDED_METHODS) { 620 if (ObjectHelper.isOverridingMethod(excluded, method)) { 621 // the method is overriding an excluded method so its not valid 622 return false; 623 } 624 } 625 626 // must be a public method 627 if (!Modifier.isPublic(method.getModifiers())) { 628 return false; 629 } 630 631 // return type must not be an Exchange and it should not be a bridge method 632 if ((method.getReturnType() != null && Exchange.class.isAssignableFrom(method.getReturnType())) || method.isBridge()) { 633 return false; 634 } 635 636 return true; 637 } 638 639 /** 640 * Does the given method info override an existing method registered before (from a subclass) 641 * 642 * @param methodInfo the method to test 643 * @return the already registered method to use, null if not overriding any 644 */ 645 private MethodInfo overridesExistingMethod(MethodInfo methodInfo) { 646 for (MethodInfo info : methodMap.values()) { 647 Method source = info.getMethod(); 648 Method target = methodInfo.getMethod(); 649 650 boolean override = ObjectHelper.isOverridingMethod(source, target); 651 if (override) { 652 // same name, same parameters, then its overrides an existing class 653 return info; 654 } 655 } 656 657 return null; 658 } 659 660 private MethodInfo chooseMethodWithCustomAnnotations(Exchange exchange, Collection<MethodInfo> possibles) 661 throws AmbiguousMethodCallException { 662 // if we have only one method with custom annotations let's choose that 663 MethodInfo chosen = null; 664 for (MethodInfo possible : possibles) { 665 if (possible.hasCustomAnnotation()) { 666 if (chosen != null) { 667 chosen = null; 668 break; 669 } else { 670 chosen = possible; 671 } 672 } 673 } 674 if (chosen != null) { 675 return chosen; 676 } 677 throw new AmbiguousMethodCallException(exchange, possibles); 678 } 679 680 /** 681 * Creates an expression for the given parameter type if the parameter can 682 * be mapped automatically or null if the parameter cannot be mapped due to 683 * insufficient annotations or not fitting with the default type 684 * conventions. 685 */ 686 private Expression createParameterUnmarshalExpression(Class<?> clazz, Method method, 687 Class<?> parameterType, Annotation[] parameterAnnotation) { 688 689 // look for a parameter annotation that converts into an expression 690 for (Annotation annotation : parameterAnnotation) { 691 Expression answer = createParameterUnmarshalExpressionForAnnotation(clazz, method, parameterType, annotation); 692 if (answer != null) { 693 return answer; 694 } 695 } 696 // no annotations then try the default parameter mappings 697 return strategy.getDefaultParameterTypeExpression(parameterType); 698 } 699 700 private Expression createParameterUnmarshalExpressionForAnnotation(Class<?> clazz, Method method, 701 Class<?> parameterType, Annotation annotation) { 702 if (annotation instanceof Attachments) { 703 return ExpressionBuilder.attachmentsExpression(); 704 } else if (annotation instanceof Property) { 705 Property propertyAnnotation = (Property)annotation; 706 return ExpressionBuilder.propertyExpression(propertyAnnotation.value()); 707 } else if (annotation instanceof Properties) { 708 return ExpressionBuilder.propertiesExpression(); 709 } else if (annotation instanceof Header) { 710 Header headerAnnotation = (Header)annotation; 711 return ExpressionBuilder.headerExpression(headerAnnotation.value()); 712 } else if (annotation instanceof Headers) { 713 return ExpressionBuilder.headersExpression(); 714 } else if (annotation instanceof OutHeaders) { 715 return ExpressionBuilder.outHeadersExpression(); 716 } else if (annotation instanceof ExchangeException) { 717 return ExpressionBuilder.exchangeExceptionExpression(CastUtils.cast(parameterType, Exception.class)); 718 } else { 719 LanguageAnnotation languageAnnotation = annotation.annotationType().getAnnotation(LanguageAnnotation.class); 720 if (languageAnnotation != null) { 721 Class<?> type = languageAnnotation.factory(); 722 Object object = camelContext.getInjector().newInstance(type); 723 if (object instanceof AnnotationExpressionFactory) { 724 AnnotationExpressionFactory expressionFactory = (AnnotationExpressionFactory) object; 725 return expressionFactory.createExpression(camelContext, annotation, languageAnnotation, parameterType); 726 } else { 727 LOG.warn("Ignoring bad annotation: " + languageAnnotation + "on method: " + method 728 + " which declares a factory: " + type.getName() 729 + " which does not implement " + AnnotationExpressionFactory.class.getName()); 730 } 731 } 732 } 733 734 return null; 735 } 736 737 private static List<Method> getInterfaceMethods(Class<?> clazz) { 738 final List<Method> answer = new ArrayList<Method>(); 739 for (Class<?> interfaceClazz : clazz.getInterfaces()) { 740 for (Method interfaceMethod : interfaceClazz.getDeclaredMethods()) { 741 answer.add(interfaceMethod); 742 } 743 } 744 745 return answer; 746 } 747 748 private static void removeAllSetterOrGetterMethods(List<MethodInfo> methods) { 749 Iterator<MethodInfo> it = methods.iterator(); 750 while (it.hasNext()) { 751 MethodInfo info = it.next(); 752 if (IntrospectionSupport.isGetter(info.getMethod())) { 753 // skip getters 754 it.remove(); 755 } else if (IntrospectionSupport.isSetter(info.getMethod())) { 756 // skip setters 757 it.remove(); 758 } 759 } 760 } 761 762 private void removeNonMatchingMethods(List<MethodInfo> methods, String name) { 763 Iterator<MethodInfo> it = methods.iterator(); 764 while (it.hasNext()) { 765 MethodInfo info = it.next(); 766 if (!matchMethod(info.getMethod(), name)) { 767 // method does not match so remove it 768 it.remove(); 769 } 770 } 771 } 772 773 private boolean matchMethod(Method method, String methodName) { 774 if (methodName == null) { 775 return true; 776 } 777 778 if (methodName.contains("(") && !methodName.endsWith(")")) { 779 throw new IllegalArgumentException("Name must have both starting and ending parenthesis, was: " + methodName); 780 } 781 782 // do not use qualifier for name matching 783 String name = methodName; 784 if (name.contains("(")) { 785 name = ObjectHelper.before(name, "("); 786 } 787 788 // must match name 789 if (!name.equals(method.getName())) { 790 return false; 791 } 792 793 // match qualifier types which is used to select among overloaded methods 794 String types = ObjectHelper.between(methodName, "(", ")"); 795 if (types != null) { 796 // we must qualify based on types to match method 797 String[] parameters = StringQuoteHelper.splitSafeQuote(types, ','); 798 Iterator<?> it = ObjectHelper.createIterator(parameters); 799 for (int i = 0; i < method.getParameterTypes().length; i++) { 800 if (it.hasNext()) { 801 Class<?> parameterType = method.getParameterTypes()[i]; 802 803 String qualifyType = (String) it.next(); 804 if (ObjectHelper.isEmpty(qualifyType)) { 805 continue; 806 } 807 // trim the type 808 qualifyType = qualifyType.trim(); 809 810 if ("*".equals(qualifyType)) { 811 // * is a wildcard so we accept and match that parameter type 812 continue; 813 } 814 815 if (BeanHelper.isValidParameterValue(qualifyType)) { 816 // its a parameter value, so continue to next parameter 817 // as we should only check for FQN/type parameters 818 continue; 819 } 820 821 // if qualify type indeed is a class, then it must be assignable with the parameter type 822 Boolean assignable = BeanHelper.isAssignableToExpectedType(getCamelContext().getClassResolver(), qualifyType, parameterType); 823 // the method will return null if the qualifyType is not a class 824 if (assignable != null && !assignable) { 825 return false; 826 } 827 828 } else { 829 // there method has more parameters than was specified in the method name qualifiers 830 return false; 831 } 832 } 833 834 // if the method has no more types then we can only regard it as matched 835 // if there are no more qualifiers 836 if (it.hasNext()) { 837 return false; 838 } 839 } 840 841 // the method matched 842 return true; 843 } 844 845 private static Class<?> getTargetClass(Class<?> clazz) { 846 if (clazz != null && clazz.getName().contains(CGLIB_CLASS_SEPARATOR)) { 847 Class<?> superClass = clazz.getSuperclass(); 848 if (superClass != null && !Object.class.equals(superClass)) { 849 return superClass; 850 } 851 } 852 return clazz; 853 } 854 855 /** 856 * Do we have a method with the given name. 857 * <p/> 858 * Shorthand method names for getters is supported, so you can pass in eg 'name' and Camel 859 * will can find the real 'getName' method instead. 860 * 861 * @param methodName the method name 862 * @return <tt>true</tt> if we have such a method. 863 */ 864 public boolean hasMethod(String methodName) { 865 return getOperations(methodName) != null; 866 } 867 868 /** 869 * Do we have a static method with the given name. 870 * <p/> 871 * Shorthand method names for getters is supported, so you can pass in eg 'name' and Camel 872 * will can find the real 'getName' method instead. 873 * 874 * @param methodName the method name 875 * @return <tt>true</tt> if we have such a static method. 876 */ 877 public boolean hasStaticMethod(String methodName) { 878 List<MethodInfo> methods = getOperations(methodName); 879 if (methods == null || methods.isEmpty()) { 880 return false; 881 } 882 for (MethodInfo method : methods) { 883 if (method.isStaticMethod()) { 884 return true; 885 } 886 } 887 return false; 888 } 889 890 /** 891 * Gets the list of methods sorted by A..Z method name. 892 * 893 * @return the methods. 894 */ 895 public List<MethodInfo> getMethods() { 896 if (operations.isEmpty()) { 897 return Collections.emptyList(); 898 } 899 900 List<MethodInfo> methods = new ArrayList<MethodInfo>(); 901 for (Collection<MethodInfo> col : operations.values()) { 902 methods.addAll(col); 903 } 904 905 // sort the methods by name A..Z 906 Collections.sort(methods, new Comparator<MethodInfo>() { 907 public int compare(MethodInfo o1, MethodInfo o2) { 908 return o1.getMethod().getName().compareTo(o2.getMethod().getName()); 909 } 910 }); 911 return methods; 912 } 913 914 /** 915 * Get the operation(s) with the given name. We can have multiple when methods is overloaded. 916 * <p/> 917 * Shorthand method names for getters is supported, so you can pass in eg 'name' and Camel 918 * will can find the real 'getName' method instead. 919 * 920 * @param methodName the method name 921 * @return the found method, or <tt>null</tt> if not found 922 */ 923 private List<MethodInfo> getOperations(String methodName) { 924 // do not use qualifier for name 925 if (methodName.contains("(")) { 926 methodName = ObjectHelper.before(methodName, "("); 927 } 928 929 List<MethodInfo> answer = operations.get(methodName); 930 if (answer != null) { 931 return answer; 932 } 933 934 // now try all getters to see if any of those matched the methodName 935 for (Method method : methodMap.keySet()) { 936 if (IntrospectionSupport.isGetter(method)) { 937 String shorthandMethodName = IntrospectionSupport.getGetterShorthandName(method); 938 // if the two names matches then see if we can find it using that name 939 if (methodName.equals(shorthandMethodName)) { 940 return operations.get(method.getName()); 941 } 942 } 943 } 944 945 return null; 946 } 947 948 }