View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jexl3.introspection;
19  import org.apache.commons.jexl3.JexlException;
20  
21  /**
22   * Interface used for regular method invocation.
23   * Ex.
24   * <code>
25   * ${foo.bar()}
26   * </code>
27   *
28   * @since 1.0
29   */
30  public interface JexlMethod {
31  
32      /**
33       * returns the return type of the method invoked.
34       *
35       * @return return type
36       */
37      Class<?> getReturnType();
38  
39      /**
40       * Invocation method, called when the method invocation should be performed
41       * and a value returned.
42  
43       * @param obj the object
44       * @param params method parameters.
45       * @return the result
46       * @throws Exception on any error.
47       */
48      Object invoke(Object obj, Object... params) throws Exception;
49  
50      /**
51       * Specifies if this JexlMethod is cacheable and able to be reused for this
52       * class of object it was returned for.
53       *
54       * @return true if can be reused for this class, false if not
55       */
56      boolean isCacheable();
57  
58      /**
59       * Checks whether a tryInvoke return value indicates a failure or not.
60       * <p>Usage is : <code>Object r = tryInvoke(...); if (tryFailed(r) {...} else {...}</code>
61       *
62       * @param rval the value returned by tryInvoke
63       * @return true if tryInvoke failed, false otherwise
64       */
65      boolean tryFailed(Object rval);
66  
67      /**
68       * Attempts to reuse this JexlMethod, checking that it is compatible with
69       * the actual set of arguments.
70       * Related to isCacheable since this method is often used with cached JexlMethod instances.
71       *
72       * @param name the method name
73       * @param obj the object to invoke the method upon
74       * @param params the method arguments
75       * @return the result of the method invocation that should be checked by tryFailed to determine if it succeeded
76       * or failed.
77       * @throws JexlException.TryFailed if the underlying method was invoked but threw an exception
78       * ({@link java.lang.reflect.InvocationTargetException})
79       */
80      Object tryInvoke(String name, Object obj, Object... params) throws JexlException.TryFailed;
81  }