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    *      http://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.util.List;
20  import java.lang.reflect.Array;
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      /** The property. */
34      private final Integer property;
35  
36      /**
37       * Attempts to discover a ListGetExecutor.
38       *
39       * @param is the introspector
40       * @param clazz the class to find the get method from
41       * @param index the index to use as an argument to the get method
42       * @return the executor if found, null otherwise
43       */
44      public static ListGetExecutor discover(final Introspector is, final Class<?> clazz, final Integer index) {
45          if (index != null) {
46              if (clazz.isArray()) {
47                  return new ListGetExecutor(clazz, ARRAY_GET, index);
48              }
49              // we still need to ensure permissions grant access to get(...)
50              if (List.class.isAssignableFrom(clazz) && is.getMethod(clazz, "get", index) != null) {
51                  return new ListGetExecutor(clazz, LIST_GET, index);
52              }
53          }
54          return null;
55      }
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  }