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.internal.introspection;
19  import java.util.Map;
20  
21  /**
22   * Specialized executor to set a property in a Map.
23   *
24   * @since 2.0
25   */
26  public final class MapSetExecutor extends AbstractExecutor.Set {
27  
28      /** The java.util.map.put method used as an active marker in MapSet. */
29      private static final java.lang.reflect.Method MAP_SET = initMarker(Map.class, "put", Object.class, Object.class);
30  
31      /**
32       * Attempts to discover a MapSetExecutor.
33       *
34       * @param is the introspector
35       * @param clazz the class to find the set method from
36       * @param identifier the key to use as an argument to the get method
37       * @param value the value to use as argument in map.put(key, value)
38       * @return the executor if found, null otherwise
39       */
40      public static MapSetExecutor discover(final Introspector is,
41                                            final Class<?> clazz,
42                                            final Object identifier,
43                                            final Object value) {
44          // we still need to ensure permissions grant access to put(...)
45          if (Map.class.isAssignableFrom(clazz) && is.getMethod(clazz, "put", identifier, value) != null) {
46              return new MapSetExecutor(clazz, MAP_SET, identifier, value);
47          }
48          return null;
49      }
50  
51      /** The property. */
52      private final Object property;
53  
54      /** The property value class. */
55      private final Class<?> valueClass;
56  
57      /**
58       * Creates an instance.
59       *
60       * @param clazz the class the set method applies to
61       * @param method the method called through this executor
62       * @param key the key to use as 1st argument to the set method
63       * @param value the value to use as 2nd argument to the set method
64       */
65      private MapSetExecutor(final Class<?> clazz, final java.lang.reflect.Method method, final Object key, final Object value) {
66          super(clazz, method);
67          property = key;
68          valueClass = classOf(value);
69      }
70  
71      @Override
72      public Object getTargetProperty() {
73          return property;
74      }
75  
76      @Override
77      public Object invoke(final Object obj, final Object value) {
78          @SuppressWarnings("unchecked") // ctor only allows Map instances - see discover() method
79          final Map<Object,Object> map = (Map<Object, Object>) obj;
80          map.put(property, value);
81          return value;
82      }
83  
84      @Override
85      public Object tryInvoke(final Object obj, final Object key, final Object value) {
86          if (obj != null
87              && method != null
88              && objectClass.equals(obj.getClass())
89              && (property == null && key == null
90                  || property != null && key != null && property.getClass().equals(key.getClass()))
91              && valueClass.equals(classOf(value))) {
92              @SuppressWarnings("unchecked") // ctor only allows Map instances - see discover() method
93              final Map<Object,Object> map = (Map<Object, Object>) obj;
94              map.put(key, value);
95              return value;
96          }
97          return TRY_FAILED;
98      }
99  }