001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jxpath.functions;
017:
018: import java.lang.reflect.InvocationTargetException;
019: import java.lang.reflect.Method;
020: import java.lang.reflect.Modifier;
021:
022: import org.apache.commons.jxpath.ExpressionContext;
023: import org.apache.commons.jxpath.Function;
024: import org.apache.commons.jxpath.JXPathException;
025: import org.apache.commons.jxpath.util.TypeUtils;
026: import org.apache.commons.jxpath.util.ValueUtils;
027:
028: /**
029: * An XPath extension function implemented as an individual Java method.
030: *
031: * @author Dmitri Plotnikov
032: * @version $Revision: 1.11 $ $Date: 2004/02/29 14:17:44 $
033: */
034: public class MethodFunction implements Function {
035:
036: private Method method;
037: private static final Object EMPTY_ARRAY[] = new Object[0];
038:
039: public MethodFunction(Method method) {
040: this .method = ValueUtils.getAccessibleMethod(method);
041: }
042:
043: public Object invoke(ExpressionContext context, Object[] parameters) {
044: try {
045: Object target;
046: Object[] args;
047: if (Modifier.isStatic(method.getModifiers())) {
048: target = null;
049: if (parameters == null) {
050: parameters = EMPTY_ARRAY;
051: }
052: int pi = 0;
053: Class types[] = method.getParameterTypes();
054: if (types.length >= 1
055: && ExpressionContext.class
056: .isAssignableFrom(types[0])) {
057: pi = 1;
058: }
059: args = new Object[parameters.length + pi];
060: if (pi == 1) {
061: args[0] = context;
062: }
063: for (int i = 0; i < parameters.length; i++) {
064: args[i + pi] = TypeUtils.convert(parameters[i],
065: types[i + pi]);
066: }
067: } else {
068: int pi = 0;
069: Class types[] = method.getParameterTypes();
070: if (types.length >= 1
071: && ExpressionContext.class
072: .isAssignableFrom(types[0])) {
073: pi = 1;
074: }
075: target = TypeUtils.convert(parameters[0], method
076: .getDeclaringClass());
077: args = new Object[parameters.length - 1 + pi];
078: if (pi == 1) {
079: args[0] = context;
080: }
081: for (int i = 1; i < parameters.length; i++) {
082: args[pi + i - 1] = TypeUtils.convert(parameters[i],
083: types[i + pi - 1]);
084: }
085: }
086:
087: return method.invoke(target, args);
088: } catch (Throwable ex) {
089: if (ex instanceof InvocationTargetException) {
090: ex = ((InvocationTargetException) ex)
091: .getTargetException();
092: }
093: throw new JXPathException("Cannot invoke " + method, ex);
094: }
095: }
096:
097: public String toString() {
098: return method.toString();
099: }
100: }
|