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