    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
    018package org.apache.commons.jexl3.introspection;
    019import org.apache.commons.jexl3.JexlException;
    020
    021/**
    022 * Interface used for regular method invocation.
    023 * Ex.
    024 * <code>
    025 * ${foo.bar()}
    026 * </code>
    027 *
    028 * @since 1.0
    029 */
    030public interface JexlMethod {
    031    /**
    032     * Invocation method, called when the method invocation should be performed
    033     * and a value returned.
    034
    035     * @param obj the object
    036     * @param params method parameters.
    037     * @return the result
    038     * @throws Exception on any error.
    039     */
    040    Object invoke(Object obj, Object... params) throws Exception;
    041
    042    /**
    043     * Attempts to reuse this JexlMethod, checking that it is compatible with
    044     * the actual set of arguments.
    045     * Related to isCacheable since this method is often used with cached JexlMethod instances.
    046     *
    047     * @param name the method name
    048     * @param obj the object to invoke the method upon
    049     * @param params the method arguments
    050     * @return the result of the method invocation that should be checked by tryFailed to determine if it succeeded
    051     * or failed.
    052     * @throws JexlException.TryFailed if the underlying method was invoked but threw an exception
    053     * ({@link java.lang.reflect.InvocationTargetException})
    054     */
    055    Object tryInvoke(String name, Object obj, Object... params) throws JexlException.TryFailed;
    056
    057    /**
    058     * Checks whether a tryInvoke return value indicates a failure or not.
    059     * <p>Usage is : <code>Object r = tryInvoke(...); if (tryFailed(r) {...} else {...}</code>
    060     *
    061     * @param rval the value returned by tryInvoke
    062     * @return true if tryInvoke failed, false otherwise
    063     */
    064    boolean tryFailed(Object rval);
    065
    066    /**
    067     * Specifies if this JexlMethod is cacheable and able to be reused for this
    068     * class of object it was returned for.
    069     *
    070     * @return true if can be reused for this class, false if not
    071     */
    072    boolean isCacheable();
    073
    074    /**
    075     * returns the return type of the method invoked.
    076     *
    077     * @return return type
    078     */
    079    Class<?> getReturnType();
    080}