View Javadoc
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   *
25   * @since 2.0
26   */
27  public final class ListGetExecutor extends AbstractExecutor.Get {
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_GET =
31              initMarker(Array.class, "get", Object.class, Integer.TYPE);
32  
33      /** The java.util.obj.get method used as an active marker in ListGet. */
34      private static final java.lang.reflect.Method LIST_GET =
35              initMarker(List.class, "get", Integer.TYPE);
36  
37      /**
38       * Attempts to discover a ListGetExecutor.
39       *
40       * @param is the introspector
41       * @param clazz the class to find the get method from
42       * @param index the index to use as an argument to the get method
43       * @return the executor if found, null otherwise
44       */
45      public static ListGetExecutor discover(final Introspector is, final Class<?> clazz, final Integer index) {
46          if (index != null) {
47              if (clazz.isArray()) {
48                  return new ListGetExecutor(clazz, ARRAY_GET, index);
49              }
50              // we still need to ensure permissions grant access to get(...)
51              if (List.class.isAssignableFrom(clazz) && is.getMethod(clazz, "get", index) != null) {
52                  return new ListGetExecutor(clazz, LIST_GET, index);
53              }
54          }
55          return null;
56      }
57  
58      /** The property. */
59      private final Integer property;
60  
61      /**
62       * Creates an instance.
63       *
64       * @param clazz he class the get method applies to
65       * @param method the method held by this executor
66       * @param index the index to use as an argument to the get method
67       */
68      private ListGetExecutor(final Class<?> clazz, final java.lang.reflect.Method method, final Integer index) {
69          super(clazz, method);
70          property = index;
71      }
72  
73      @Override
74      public Object getTargetProperty() {
75          return property;
76      }
77  
78      @Override
79      public Object invoke(final Object obj) {
80          if (method == ARRAY_GET) {
81              return Array.get(obj, property);
82          }
83          return ((List<?>) obj).get(property);
84      }
85  
86      @Override
87      public Object tryInvoke(final Object obj, final Object identifier) {
88          final Integer index = castInteger(identifier);
89          if (obj != null && method != null
90              && objectClass.equals(obj.getClass())
91              && index != null) {
92              if (method == ARRAY_GET) {
93                  return Array.get(obj, index);
94              }
95              return ((List<?>) obj).get(index);
96          }
97          return TRY_FAILED;
98      }
99  }