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  
18  package org.apache.commons.jexl2.internal;
19  import java.util.List;
20  import java.lang.reflect.Array;
21  /**
22   * Specialized executor to get a property from a List or array.
23   * @since 2.0
24   */
25  public final class ListGetExecutor extends AbstractExecutor.Get {
26      /** The java.lang.reflect.Array.get method used as an active marker in ListGet. */
27      private static final java.lang.reflect.Method ARRAY_GET =
28              initMarker(Array.class, "get", Object.class, Integer.TYPE);
29      /** The java.util.obj.get method used as an active marker in ListGet. */
30      private static final java.lang.reflect.Method LIST_GET =
31              initMarker(List.class, "get", Integer.TYPE);
32      /** The property. */
33      private final Integer property;
34  
35      /**
36       * Creates an instance checking for the List interface or Array capability.
37       * @param is the introspector
38       * @param clazz the class to introspect
39       * @param key the key to use in obj.get(key)
40       */
41      public ListGetExecutor(Introspector is, Class<?> clazz, Integer key) {
42          super(clazz, discover(clazz));
43          property = key;
44      }
45  
46      /** {@inheritDoc} */
47      @Override
48      public Object getTargetProperty() {
49          return property;
50      }
51      
52      /**
53       * Get the property from the obj or array.
54       * @param obj the List/array.
55       * @return obj.get(key)
56       */
57      @Override
58      public Object execute(final Object obj) {
59          if (method == ARRAY_GET) {
60              return java.lang.reflect.Array.get(obj, property.intValue());
61          } else {
62              return ((List<?>) obj).get(property.intValue());
63          }
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public Object tryExecute(final Object obj, Object key) {
69          if (obj != null && method != null
70              && objectClass.equals(obj.getClass())
71              && key instanceof Integer) {
72              if (method == ARRAY_GET) {
73                  return java.lang.reflect.Array.get(obj, ((Integer) key).intValue());
74              } else {
75                  return ((List<?>) obj).get(((Integer) key).intValue());
76              }
77          }
78          return TRY_FAILED;
79      }
80  
81  
82      /**
83       * Finds the method to perform the get on a obj of array.
84       * @param clazz the class to introspect
85       * @return a marker method, obj.get or array.get
86       */
87      static java.lang.reflect.Method discover(Class<?> clazz) {
88          //return discoverList(false, clazz, property);
89          if (clazz.isArray()) {
90              return ARRAY_GET;
91          }
92          if (List.class.isAssignableFrom(clazz)) {
93              return LIST_GET;
94          }
95          return null;
96      }
97  }