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.lang.reflect.Array;
20 import java.util.List;
21
22
23
24
25
26 public final class ListGetExecutor extends AbstractExecutor.Get {
27
28 private static final java.lang.reflect.Method ARRAY_GET =
29 initMarker(Array.class, "get", Object.class, Integer.TYPE);
30
31 private static final java.lang.reflect.Method LIST_GET =
32 initMarker(List.class, "get", Integer.TYPE);
33
34
35
36
37
38
39
40
41 public static ListGetExecutor discover(final Introspector is, final Class<?> clazz, final Integer index) {
42 if (index != null) {
43 if (clazz.isArray()) {
44 return new ListGetExecutor(clazz, ARRAY_GET, index);
45 }
46
47 if (List.class.isAssignableFrom(clazz) && is.getMethod(clazz, "get", index) != null) {
48 return new ListGetExecutor(clazz, LIST_GET, index);
49 }
50 }
51 return null;
52 }
53
54
55 private final Integer property;
56
57
58
59
60
61
62
63 private ListGetExecutor(final Class<?> clazz, final java.lang.reflect.Method method, final Integer index) {
64 super(clazz, method);
65 property = index;
66 }
67
68 @Override
69 public Object getTargetProperty() {
70 return property;
71 }
72
73 @Override
74 public Object invoke(final Object obj) {
75 if (method == ARRAY_GET) {
76 return Array.get(obj, property);
77 }
78 return ((List<?>) obj).get(property);
79 }
80
81 @Override
82 public Object tryInvoke(final Object obj, final Object identifier) {
83 final Integer index = castInteger(identifier);
84 if (obj != null && method != null
85 && objectClass.equals(obj.getClass())
86 && index != null) {
87 if (method == ARRAY_GET) {
88 return Array.get(obj, index);
89 }
90 return ((List<?>) obj).get(index);
91 }
92 return TRY_FAILED;
93 }
94 }