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