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  package org.apache.commons.jexl3.internal.introspection;
18  
19  import java.lang.reflect.InvocationTargetException;
20  
21  import org.apache.commons.jexl3.JexlException;
22  
23  /**
24   * Specialized executor to get a property from an object.
25   * <p>Duck as in duck-typing for an interface like:
26   * <code>
27   * interface Get {
28   *      Object get(Object key);
29   * }
30   * </code>
31   * </p>
32   *
33   * @since 2.0
34   */
35  public final class DuckGetExecutor extends AbstractExecutor.Get {
36  
37      /**
38       * Attempts to discover a DuckGetExecutor.
39       *
40       * @param is the introspector
41       * @param clazz the class to find the get method from
42       * @param identifier the key to use as an argument to the get method
43       * @return the executor if found, null otherwise
44       */
45      public static DuckGetExecutor discover(final Introspector is, final Class<?> clazz, final Object identifier) {
46          final java.lang.reflect.Method method = is.getMethod(clazz, "get", identifier);
47          return method == null ? null : new DuckGetExecutor(clazz, method, identifier);
48      }
49  
50      /** The property, may be null. */
51      private final Object property;
52  
53      /**
54       * Creates an instance.
55       *
56       * @param clazz he class the get method applies to
57       * @param method the method held by this executor
58       * @param identifier the property to get
59       */
60      private DuckGetExecutor(final Class<?> clazz, final java.lang.reflect.Method method, final Object identifier) {
61          super(clazz, method);
62          property = identifier;
63      }
64  
65      @Override
66      public Object getTargetProperty() {
67          return property;
68      }
69  
70      @Override
71      public Object invoke(final Object obj) throws IllegalAccessException, InvocationTargetException {
72          final Object[] args = {property};
73          return method == null ? null : method.invoke(obj, args);
74      }
75  
76      @Override
77      public Object tryInvoke(final Object obj, final Object key) {
78          if (obj != null
79                  && objectClass.equals(obj.getClass())
80                  // ensure method name matches the property name
81                  && method != null
82                  && (property == null && key == null
83                  || property != null && property.equals(key))) {
84              try {
85                  final Object[] args = {property};
86                  return method.invoke(obj, args);
87              } catch (IllegalAccessException | IllegalArgumentException xill) {
88                  return TRY_FAILED; // fail
89              } catch (final InvocationTargetException xinvoke) {
90                  throw JexlException.tryFailed(xinvoke); // throw
91              }
92          }
93          return TRY_FAILED;
94      }
95  }