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.Array;
20 import java.util.List;
21
22 /**
23 * Specialized executor to set a property in a List or array.
24 * @since 2.0
25 */
26 public final class ListSetExecutor extends AbstractExecutor.Set {
27 /** The java.lang.reflect.Array.get method used as an active marker in ListGet. */
28 private static final java.lang.reflect.Method ARRAY_SET =
29 initMarker(Array.class, "set", Object.class, Integer.TYPE, Object.class);
30 /** The java.util.obj.set method used as an active marker in ListSet. */
31 private static final java.lang.reflect.Method LIST_SET =
32 initMarker(List.class, "set", Integer.TYPE, Object.class);
33 /**
34 * Attempts to discover a ListSetExecutor.
35 *
36 * @param is the introspector
37 * @param clazz the class to find the get 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 list.put(key,value)
40 * @return the executor if found, null otherwise
41 */
42 public static ListSetExecutor discover(final Introspector is,
43 final Class<?> clazz,
44 final Object identifier,
45 final Object value) {
46 final Integer index = castInteger(identifier);
47 if (index != null) {
48 if (clazz.isArray()) {
49 // we could verify if the call can be performed but it does not change
50 // the fact we would fail...
51 // Class<?> formal = clazz.getComponentType();
52 // Class<?> actual = value == null ? Object.class : value.getClass();
53 // if (IntrospectionUtils.isMethodInvocationConvertible(formal, actual, false)) {
54 return new ListSetExecutor(clazz, ARRAY_SET, index);
55 // }
56 }
57 // we still need to ensure permissions grant access to set(...)
58 if (List.class.isAssignableFrom(clazz)
59 && is.getMethod(clazz, "set", index, value) != null) {
60 return new ListSetExecutor(clazz, LIST_SET, index);
61 }
62 }
63 return null;
64 }
65
66 /** The property. */
67 private final Integer property;
68
69 /**
70 * Creates an instance.
71 *
72 * @param clazz the class the set method applies to
73 * @param method the method called through this executor
74 * @param key the key to use as 1st argument to the set method
75 */
76 private ListSetExecutor(final Class<?> clazz, final java.lang.reflect.Method method, final Integer key) {
77 super(clazz, method);
78 property = key;
79 }
80
81 @Override
82 public Object getTargetProperty() {
83 return property;
84 }
85
86 @Override
87 public Object invoke(final Object obj, final Object value) {
88 if (method == ARRAY_SET) {
89 Array.set(obj, property, value);
90 } else {
91 @SuppressWarnings("unchecked") // LSE should only be created for array or list types
92 final List<Object> list = (List<Object>) obj;
93 list.set(property, value);
94 }
95 return value;
96 }
97
98 @Override
99 public Object tryInvoke(final Object obj, final Object key, final Object value) {
100 final Integer index = castInteger(key);
101 if (obj != null && method != null
102 && objectClass.equals(obj.getClass())
103 && index != null) {
104 if (method == ARRAY_SET) {
105 Array.set(obj, index, value);
106 } else {
107 @SuppressWarnings("unchecked") // LSE should only be created for array or list types
108 final List<Object> list = (List<Object>) obj;
109 list.set(index, value);
110 }
111 return value;
112 }
113 return TRY_FAILED;
114 }
115 }