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.jexl2.internal;
19 import java.util.Map;
20 import java.lang.reflect.InvocationTargetException;
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
31 /**
32 * Creates an instance checking for the Map interface.
33 *@param is the introspector
34 *@param clazz the class that might implement the map interface
35 *@param key the key to use as argument in map.put(key,value)
36 *@param value the value to use as argument in map.put(key,value)
37 */
38 public MapSetExecutor(Introspector is, Class<?> clazz, Object key, Object value) {
39 super(clazz, discover(clazz));
40 property = key;
41 }
42
43 /** {@inheritDoc} */
44 @Override
45 public Object getTargetProperty() {
46 return property;
47 }
48
49 /** {@inheritDoc} */
50 @Override
51 public Object execute(final Object obj, Object value)
52 throws IllegalAccessException, InvocationTargetException {
53 @SuppressWarnings("unchecked") // ctor only allows Map instances - see discover() method
54 final Map<Object,Object> map = ((Map<Object, Object>) obj);
55 map.put(property, value);
56 return value;
57 }
58
59 /** {@inheritDoc} */
60 @Override
61 public Object tryExecute(final Object obj, Object key, Object value) {
62 if (obj != null && method != null
63 && objectClass.equals(obj.getClass())
64 && (key == null || property.getClass().equals(key.getClass()))) {
65 @SuppressWarnings("unchecked") // ctor only allows Map instances - see discover() method
66 final Map<Object,Object> map = ((Map<Object, Object>) obj);
67 map.put(key, value);
68 return value;
69 }
70 return TRY_FAILED;
71 }
72
73 /**
74 * Finds the method to perform 'set' on a map.
75 * @param clazz the class to introspect
76 * @return a marker method, map.get
77 */
78 static java.lang.reflect.Method discover(Class<?> clazz) {
79 return (Map.class.isAssignableFrom(clazz))? MAP_SET : null;
80 }
81 }