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