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     * returns the return type of the method invoked.
033     *
034     * @return return type
035     */
036    Class<?> getReturnType();
037
038    /**
039     * Invocation method, called when the method invocation should be performed
040     * and a value returned.
041
042     * @param obj the object
043     * @param params method parameters.
044     * @return the result
045     * @throws Exception on any error.
046     */
047    Object invoke(Object obj, Object... params) throws Exception;
048
049    /**
050     * Specifies if this JexlMethod is cacheable and able to be reused for this
051     * class of object it was returned for.
052     *
053     * @return true if can be reused for this class, false if not
054     */
055    boolean isCacheable();
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     * Attempts to reuse this JexlMethod, checking that it is compatible with
068     * the actual set of arguments.
069     * Related to isCacheable since this method is often used with cached JexlMethod instances.
070     *
071     * @param name the method name
072     * @param obj the object to invoke the method upon
073     * @param params the method arguments
074     * @return the result of the method invocation that should be checked by tryFailed to determine if it succeeded
075     * or failed.
076     * @throws JexlException.TryFailed if the underlying method was invoked but threw an exception
077     * ({@link java.lang.reflect.InvocationTargetException})
078     */
079    Object tryInvoke(String name, Object obj, Object... params) throws JexlException.TryFailed;
080}