Coverage Report - org.apache.camel.component.bean.MethodInfo
 
Classes in this File Line Coverage Branch Coverage Complexity
MethodInfo
38% 
50% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.component.bean;
 18  
 
 19  
 import java.lang.reflect.AccessibleObject;
 20  
 import java.lang.reflect.InvocationTargetException;
 21  
 import java.lang.reflect.Method;
 22  
 import java.util.Arrays;
 23  
 import java.util.List;
 24  
 
 25  
 import org.apache.camel.Exchange;
 26  
 import org.apache.camel.Expression;
 27  
 import org.apache.camel.util.ObjectHelper;
 28  
 
 29  
 /**
 30  
  * @version $Revision: $
 31  
  */
 32  0
 public class MethodInfo {
 33  
     private Class type;
 34  
     private Method method;
 35  
     private final List<ParameterInfo> parameters;
 36  
     private final List<ParameterInfo> bodyParameters;
 37  
     private Expression parametersExpression;
 38  
 
 39  12
     public MethodInfo(Class type, Method method, List<ParameterInfo> parameters, List<ParameterInfo> bodyParameters) {
 40  12
         this.type = type;
 41  12
         this.method = method;
 42  12
         this.parameters = parameters;
 43  12
         this.bodyParameters = bodyParameters;
 44  12
         this.parametersExpression = createParametersExpression();
 45  12
     }
 46  
 
 47  
     public String toString() {
 48  0
         return method.toString();
 49  
     }
 50  
 
 51  
     public MethodInvocation createMethodInvocation(final Object pojo, final Exchange messageExchange) {
 52  0
         final Object[] arguments = (Object[]) parametersExpression.evaluate(messageExchange);
 53  0
         return new MethodInvocation() {
 54  
             public Method getMethod() {
 55  0
                 return method;
 56  
             }
 57  
 
 58  
             public Object[] getArguments() {
 59  0
                 return arguments;
 60  
             }
 61  
 
 62  
             public Object proceed() throws Throwable {
 63  0
                 return invoke(method, pojo, arguments, messageExchange);
 64  
             }
 65  
 
 66  
             public Object getThis() {
 67  0
                 return pojo;
 68  
             }
 69  
 
 70  0
             public AccessibleObject getStaticPart() {
 71  0
                 return method;
 72  
             }
 73  
         };
 74  
     }
 75  
 
 76  
     public Class getType() {
 77  0
         return type;
 78  
     }
 79  
 
 80  
     public Method getMethod() {
 81  0
         return method;
 82  
     }
 83  
 
 84  
     public Expression getParametersExpression() {
 85  0
         return parametersExpression;
 86  
     }
 87  
 
 88  
     public List<ParameterInfo> getBodyParameters() {
 89  0
         return bodyParameters;
 90  
     }
 91  
 
 92  
     public Class getBodyParameterType() {
 93  0
         ParameterInfo parameterInfo = bodyParameters.get(0);
 94  0
         return parameterInfo.getType();
 95  
     }
 96  
 
 97  
 
 98  
     public boolean bodyParameterMatches(Class bodyType) {
 99  0
         Class actualType = getBodyParameterType();
 100  0
         return actualType != null && ObjectHelper.isAssignableFrom(bodyType, actualType);
 101  
     }
 102  
 
 103  
     public List<ParameterInfo> getParameters() {
 104  0
         return parameters;
 105  
     }
 106  
 
 107  
     public boolean hasBodyParameter() {
 108  12
         return !bodyParameters.isEmpty();
 109  
     }
 110  
 
 111  
     protected Object invoke(Method mth, Object pojo, Object[] arguments, Exchange exchange) throws IllegalAccessException, InvocationTargetException {
 112  0
         return mth.invoke(pojo, arguments);
 113  
     }
 114  
 
 115  
     protected Expression createParametersExpression() {
 116  12
         final int size = parameters.size();
 117  12
         final Expression[] expressions = new Expression[size];
 118  15
         for (int i = 0; i < size; i++) {
 119  3
             Expression parameterExpression = parameters.get(i).getExpression();
 120  3
             expressions[i] = parameterExpression;
 121  
         }
 122  12
         return new Expression<Exchange>() {
 123  
             public Object evaluate(Exchange exchange) {
 124  0
                 Object[] answer = new Object[size];
 125  0
                 for (int i = 0; i < size; i++) {
 126  0
                     answer[i] = expressions[i].evaluate(exchange);
 127  
                 }
 128  0
                 return answer;
 129  
             }
 130  
 
 131  
             @Override
 132  12
             public String toString() {
 133  0
                 return "ParametersExpression: " + Arrays.asList(expressions);
 134  
             }
 135  
         };
 136  
     }
 137  
 }