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