View Javadoc
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  
18  package org.apache.commons.jxpath.functions;
19  
20  import java.lang.reflect.InvocationTargetException;
21  import java.lang.reflect.Method;
22  import java.lang.reflect.Modifier;
23  
24  import org.apache.commons.jxpath.ExpressionContext;
25  import org.apache.commons.jxpath.Function;
26  import org.apache.commons.jxpath.JXPathInvalidAccessException;
27  import org.apache.commons.jxpath.util.TypeUtils;
28  import org.apache.commons.jxpath.util.ValueUtils;
29  
30  /**
31   * An XPath extension function implemented as an individual Java method.
32   */
33  public class MethodFunction implements Function {
34  
35      private static final Object[] EMPTY_ARRAY = {};
36      private final Method method;
37  
38      /**
39       * Constructs a new MethodFunction.
40       *
41       * @param method implementing Method
42       */
43      public MethodFunction(final Method method) {
44          this.method = ValueUtils.getAccessibleMethod(method);
45      }
46  
47      @Override
48      public Object invoke(final ExpressionContext context, Object[] parameters) {
49          try {
50              Object target;
51              Object[] args;
52              if (Modifier.isStatic(method.getModifiers())) {
53                  target = null;
54                  if (parameters == null) {
55                      parameters = EMPTY_ARRAY;
56                  }
57                  int pi = 0;
58                  final Class[] types = method.getParameterTypes();
59                  if (types.length >= 1 && ExpressionContext.class.isAssignableFrom(types[0])) {
60                      pi = 1;
61                  }
62                  args = new Object[parameters.length + pi];
63                  if (pi == 1) {
64                      args[0] = context;
65                  }
66                  for (int i = 0; i < parameters.length; i++) {
67                      args[i + pi] = TypeUtils.convert(parameters[i], types[i + pi]);
68                  }
69              } else {
70                  int pi = 0;
71                  final Class[] types = method.getParameterTypes();
72                  if (types.length >= 1 && ExpressionContext.class.isAssignableFrom(types[0])) {
73                      pi = 1;
74                  }
75                  target = TypeUtils.convert(parameters[0], method.getDeclaringClass());
76                  args = new Object[parameters.length - 1 + pi];
77                  if (pi == 1) {
78                      args[0] = context;
79                  }
80                  for (int i = 1; i < parameters.length; i++) {
81                      args[pi + i - 1] = TypeUtils.convert(parameters[i], types[i + pi - 1]);
82                  }
83              }
84              return method.invoke(target, args);
85          } catch (Throwable ex) {
86              if (ex instanceof InvocationTargetException) {
87                  ex = ((InvocationTargetException) ex).getTargetException();
88              }
89              throw new JXPathInvalidAccessException("Cannot invoke " + method, ex);
90          }
91      }
92  
93      @Override
94      public String toString() {
95          return method.toString();
96      }
97  }