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