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.reflect.AccessibleObject;
020    import java.lang.reflect.InvocationTargetException;
021    import java.lang.reflect.Method;
022    import java.util.Arrays;
023    import java.util.List;
024    
025    import org.apache.camel.Exchange;
026    import org.apache.camel.Expression;
027    import org.apache.camel.util.ObjectHelper;
028    
029    /**
030     * @version $Revision: $
031     */
032    public class MethodInfo {
033        private Class type;
034        private Method method;
035        private final List<ParameterInfo> parameters;
036        private final List<ParameterInfo> bodyParameters;
037        private Expression parametersExpression;
038    
039        public MethodInfo(Class type, Method method, List<ParameterInfo> parameters, List<ParameterInfo> bodyParameters) {
040            this.type = type;
041            this.method = method;
042            this.parameters = parameters;
043            this.bodyParameters = bodyParameters;
044            this.parametersExpression = createParametersExpression();
045        }
046    
047        public String toString() {
048            return method.toString();
049        }
050    
051        public MethodInvocation createMethodInvocation(final Object pojo, final Exchange messageExchange) {
052            final Object[] arguments = (Object[]) parametersExpression.evaluate(messageExchange);
053            return new MethodInvocation() {
054                public Method getMethod() {
055                    return method;
056                }
057    
058                public Object[] getArguments() {
059                    return arguments;
060                }
061    
062                public Object proceed() throws Throwable {
063                    return invoke(method, pojo, arguments, messageExchange);
064                }
065    
066                public Object getThis() {
067                    return pojo;
068                }
069    
070                public AccessibleObject getStaticPart() {
071                    return method;
072                }
073            };
074        }
075    
076        public Class getType() {
077            return type;
078        }
079    
080        public Method getMethod() {
081            return method;
082        }
083    
084        public Expression getParametersExpression() {
085            return parametersExpression;
086        }
087    
088        public List<ParameterInfo> getBodyParameters() {
089            return bodyParameters;
090        }
091    
092        public Class getBodyParameterType() {
093            ParameterInfo parameterInfo = bodyParameters.get(0);
094            return parameterInfo.getType();
095        }
096    
097    
098        public boolean bodyParameterMatches(Class bodyType) {
099            Class actualType = getBodyParameterType();
100            return actualType != null && ObjectHelper.isAssignableFrom(bodyType, actualType);
101        }
102    
103        public List<ParameterInfo> getParameters() {
104            return parameters;
105        }
106    
107        public boolean hasBodyParameter() {
108            return !bodyParameters.isEmpty();
109        }
110    
111        protected Object invoke(Method mth, Object pojo, Object[] arguments, Exchange exchange) throws IllegalAccessException, InvocationTargetException {
112            return mth.invoke(pojo, arguments);
113        }
114    
115        protected Expression createParametersExpression() {
116            final int size = parameters.size();
117            final Expression[] expressions = new Expression[size];
118            for (int i = 0; i < size; i++) {
119                Expression parameterExpression = parameters.get(i).getExpression();
120                expressions[i] = parameterExpression;
121            }
122            return new Expression<Exchange>() {
123                public Object evaluate(Exchange exchange) {
124                    Object[] answer = new Object[size];
125                    for (int i = 0; i < size; i++) {
126                        answer[i] = expressions[i].evaluate(exchange);
127                    }
128                    return answer;
129                }
130    
131                @Override
132                public String toString() {
133                    return "ParametersExpression: " + Arrays.asList(expressions);
134                }
135            };
136        }
137    }