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  
20  import org.apache.commons.jexl3.JexlException;
21  
22  /**
23   * Interface for getting values that appear to be properties.
24   * Ex.
25   * <code>
26   * ${foo.bar}
27   * </code>
28   *
29   * @since 1.0
30   */
31  public interface JexlPropertyGet {
32  
33      /**
34       * Method used to get the property value of an object.
35       *
36       * @param obj the object to get the property value from.
37       * @return the property value.
38       * @throws Exception on any error.
39       */
40      Object invoke(Object obj) throws Exception;
41  
42      /**
43       * Specifies if this JexlPropertyGet is cacheable and able to be reused for
44       * this class of object it was returned for.
45       *
46       * @return true if can be reused for this class, false if not
47       */
48      boolean isCacheable();
49  
50      /**
51       * Checks whether this JexlPropertyGet returns a constant.
52       *
53       * @return true if invoking this getter will always return the same value, false otherwise
54       */
55      default boolean isConstant() {
56          return false;
57      }
58  
59      /**
60       * Checks whether a tryInvoke failed or not.
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 JexlPropertyGet, checking that it is compatible with
69       * the actual set of arguments.
70       *
71       * @param obj the object to invoke the property get upon
72       * @param key the property key to get
73       * @return the result of the method invocation that should be checked by tryFailed to determine if it succeeded
74       * or failed.
75       * @throws JexlException.TryFailed if the underlying method was invoked but threw an exception
76       * ({@link java.lang.reflect.InvocationTargetException})
77       */
78      Object tryInvoke(Object obj, Object key) throws JexlException.TryFailed;
79  }