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 set a property of an object.
25   * <p>Duck as in duck-typing for an interface like:
26   * <code>
27   * interface Setable {
28   *      Object set(Object property, Object value);
29   * }
30   * </code>
31   * or
32   * <code>
33   * interface Putable {
34   *      Object put(Object property, Object value);
35   * }
36   * </code>
37   * </p>
38   * @since 2.0
39   */
40  public final class DuckSetExecutor extends AbstractExecutor.Set {
41      /**
42       * Discovers a DuckSetExecutor.
43       *
44       * @param is the introspector
45       * @param clazz the class to find the set method from
46       * @param key the key to use as 1st argument to the set method
47       * @param value the value to use as 2nd argument to the set method
48       * @return the executor if found, null otherwise
49       */
50      public static DuckSetExecutor discover(final Introspector is, final Class<?> clazz, final Object key, final Object value) {
51          java.lang.reflect.Method method = is.getMethod(clazz, "set", key, value);
52          if (method == null) {
53              method = is.getMethod(clazz, "put", makeArgs(key, value));
54          }
55          return method == null ? null : new DuckSetExecutor(clazz, method, key, value);
56      }
57      /** The property, may be null. */
58      private final Object property;
59  
60      /** The property value class. */
61      private final Class<?> valueClass;
62  
63      /**
64       * Creates an instance.
65       * @param clazz the class the set method applies to
66       * @param method the method called through this executor
67       * @param key the key to use as 1st argument to the set method
68       * @param value the value to use as 2nd argument to the set method
69       */
70      private DuckSetExecutor(final Class<?> clazz, final java.lang.reflect.Method method, final Object key, final Object value) {
71          super(clazz, method);
72          property = key;
73          valueClass = classOf(value);
74      }
75  
76      @Override
77      public Object getTargetProperty() {
78          return property;
79      }
80  
81      @Override
82      public Object invoke(final Object obj, final Object value) throws IllegalAccessException, InvocationTargetException {
83          final Object[] pargs = {property, value};
84          if (method != null) {
85                  method.invoke(obj, pargs);
86              }
87          return value;
88      }
89  
90      @Override
91      public Object tryInvoke(final Object obj, final Object key, final Object value) {
92          if (obj != null
93              && objectClass.equals(obj.getClass())
94              && method !=  null
95              && (property != null && property.equals(key)
96                  || property == null && key == null)
97              && valueClass.equals(classOf(value))) {
98              try {
99                  final Object[] args = {property, value};
100                 method.invoke(obj, args);
101                 return value;
102             } catch (IllegalAccessException | IllegalArgumentException xill) {
103                 return TRY_FAILED; // fail
104             } catch (final InvocationTargetException xinvoke) {
105                 throw JexlException.tryFailed(xinvoke); // throw
106             }
107         }
108         return TRY_FAILED;
109     }
110 }