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