1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jexl3.internal.introspection;
18
19 import java.util.List;
20 import java.lang.reflect.Array;
21
22
23
24
25
26 public final class ListSetExecutor extends AbstractExecutor.Set {
27
28 private static final java.lang.reflect.Method ARRAY_SET =
29 initMarker(Array.class, "set", Object.class, Integer.TYPE, Object.class);
30
31 private static final java.lang.reflect.Method LIST_SET =
32 initMarker(List.class, "set", Integer.TYPE, Object.class);
33
34 private final Integer property;
35
36
37
38
39
40
41
42
43
44
45 public static ListSetExecutor discover(final Introspector is,
46 final Class<?> clazz,
47 final Object identifier,
48 final Object value) {
49 final Integer index = castInteger(identifier);
50 if (index != null) {
51 if (clazz.isArray()) {
52
53
54
55
56
57 return new ListSetExecutor(clazz, ARRAY_SET, index);
58
59 }
60
61 if (List.class.isAssignableFrom(clazz)
62 && is.getMethod(clazz, "set", index, value) != null) {
63 return new ListSetExecutor(clazz, LIST_SET, index);
64 }
65 }
66 return null;
67 }
68
69
70
71
72
73
74
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")
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")
108 final List<Object> list = (List<Object>) obj;
109 list.set(index, value);
110 }
111 return value;
112 }
113 return TRY_FAILED;
114 }
115 }