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