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