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