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 get a property from a List or array.
24 * @since 2.0
25 */
26 public final class ListGetExecutor extends AbstractExecutor.Get {
27 /** The java.lang.reflect.Array.get method used as an active marker in ListGet. */
28 private static final java.lang.reflect.Method ARRAY_GET =
29 initMarker(Array.class, "get", Object.class, Integer.TYPE);
30 /** The java.util.obj.get method used as an active marker in ListGet. */
31 private static final java.lang.reflect.Method LIST_GET =
32 initMarker(List.class, "get", Integer.TYPE);
33 /**
34 * Attempts to discover a ListGetExecutor.
35 *
36 * @param is the introspector
37 * @param clazz the class to find the get method from
38 * @param index the index to use as an argument to the get method
39 * @return the executor if found, null otherwise
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 // we still need to ensure permissions grant access to get(...)
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 /** The property. */
55 private final Integer property;
56
57 /**
58 * Creates an instance.
59 * @param clazz he class the get method applies to
60 * @param method the method held by this executor
61 * @param index the index to use as an argument to the get method
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 }