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