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 */
017package org.apache.commons.jxpath.functions;
018
019import java.lang.reflect.InvocationTargetException;
020import java.lang.reflect.Method;
021import java.lang.reflect.Modifier;
022
023import org.apache.commons.jxpath.ExpressionContext;
024import org.apache.commons.jxpath.Function;
025import org.apache.commons.jxpath.JXPathInvalidAccessException;
026import org.apache.commons.jxpath.util.TypeUtils;
027import org.apache.commons.jxpath.util.ValueUtils;
028
029/**
030 * An XPath extension function implemented as an individual Java method.
031 *
032 * @author Dmitri Plotnikov
033 * @version $Revision: 652845 $ $Date: 2008-05-02 19:46:46 +0200 (Fr, 02 Mai 2008) $
034 */
035public class MethodFunction implements Function {
036
037    private Method method;
038    private static final Object[] EMPTY_ARRAY = new Object[0];
039
040    /**
041     * Create a new MethodFunction.
042     * @param method implementing Method
043     */
044    public MethodFunction(Method method) {
045        this.method = ValueUtils.getAccessibleMethod(method);
046    }
047
048    public Object invoke(ExpressionContext context, Object[] parameters) {
049        try {
050            Object target;
051            Object[] args;
052            if (Modifier.isStatic(method.getModifiers())) {
053                target = null;
054                if (parameters == null) {
055                    parameters = EMPTY_ARRAY;
056                }
057                int pi = 0;
058                Class[] types = method.getParameterTypes();
059                if (types.length >= 1
060                    && ExpressionContext.class.isAssignableFrom(types[0])) {
061                    pi = 1;
062                }
063                args = new Object[parameters.length + pi];
064                if (pi == 1) {
065                    args[0] = context;
066                }
067                for (int i = 0; i < parameters.length; i++) {
068                    args[i + pi] =
069                        TypeUtils.convert(parameters[i], types[i + pi]);
070                }
071            }
072            else {
073                int pi = 0;
074                Class[] types = method.getParameterTypes();
075                if (types.length >= 1
076                    && ExpressionContext.class.isAssignableFrom(types[0])) {
077                    pi = 1;
078                }
079                target =
080                    TypeUtils.convert(
081                        parameters[0],
082                        method.getDeclaringClass());
083                args = new Object[parameters.length - 1 + pi];
084                if (pi == 1) {
085                    args[0] = context;
086                }
087                for (int i = 1; i < parameters.length; i++) {
088                    args[pi + i - 1] =
089                        TypeUtils.convert(parameters[i], types[i + pi - 1]);
090                }
091            }
092
093            return method.invoke(target, args);
094        }
095        catch (Throwable ex) {
096            if (ex instanceof InvocationTargetException) {
097                ex = ((InvocationTargetException) ex).getTargetException();
098            }
099            throw new JXPathInvalidAccessException("Cannot invoke " + method,
100                    ex);
101        }
102    }
103
104    public String toString() {
105        return method.toString();
106    }
107}