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 set a property in a List or array.
24   * @since 2.0
25   */
26  public final class ListSetExecutor extends AbstractExecutor.Set {
27      /** The java.lang.reflect.Array.get method used as an active marker in ListGet. */
28      private static final java.lang.reflect.Method ARRAY_SET =
29              initMarker(Array.class, "set", Object.class, Integer.TYPE, Object.class);
30      /** The java.util.obj.set method used as an active marker in ListSet. */
31      private static final java.lang.reflect.Method LIST_SET =
32              initMarker(List.class, "set", Integer.TYPE, Object.class);
33      /** The property. */
34      private final Integer property;
35  
36      /**
37       * Attempts to discover a ListSetExecutor.
38       *
39       * @param is the introspector
40       * @param clazz the class to find the get method from
41       * @param identifier the key to use as an argument to the get method
42       * @param value the value to use as argument in list.put(key,value)
43       * @return the executor if found, null otherwise
44       */
45      public static ListSetExecutor discover(final Introspector is,
46                                             final Class<?> clazz,
47                                             final Object identifier,
48                                             final Object value) {
49          final Integer index = castInteger(identifier);
50          if (index != null) {
51              if (clazz.isArray()) {
52                  // we could verify if the call can be performed but it does not change
53                  // the fact we would fail...
54                  // Class<?> formal = clazz.getComponentType();
55                  // Class<?> actual = value == null? Object.class : value.getClass();
56                  // if (IntrospectionUtils.isMethodInvocationConvertible(formal, actual, false)) {
57                  return new ListSetExecutor(clazz, ARRAY_SET, index);
58                  // }
59              }
60              // we still need to ensure permissions grant access to set(...)
61              if (List.class.isAssignableFrom(clazz)
62                  && is.getMethod(clazz, "set", index, value) != null) {
63                  return new ListSetExecutor(clazz, LIST_SET, index);
64              }
65          }
66          return null;
67      }
68  
69      /**
70       * Creates an instance.
71       *
72       * @param clazz the class the set method applies to
73       * @param method the method called through this executor
74       * @param key the key to use as 1st argument to the set method
75       */
76      private ListSetExecutor(final Class<?> clazz, final java.lang.reflect.Method method, final Integer key) {
77          super(clazz, method);
78          property = key;
79      }
80  
81      @Override
82      public Object getTargetProperty() {
83          return property;
84      }
85  
86      @Override
87      public Object invoke(final Object obj, final Object value) {
88          if (method == ARRAY_SET) {
89              Array.set(obj, property, value);
90          } else {
91              @SuppressWarnings("unchecked") // LSE should only be created for array or list types
92              final List<Object> list = (List<Object>) obj;
93              list.set(property, value);
94          }
95          return value;
96      }
97  
98      @Override
99      public Object tryInvoke(final Object obj, final Object key, final Object value) {
100         final Integer index = castInteger(key);
101         if (obj != null && method != null
102                 && objectClass.equals(obj.getClass())
103                 && index != null) {
104             if (method == ARRAY_SET) {
105                 Array.set(obj, index, value);
106             } else {
107                 @SuppressWarnings("unchecked")  // LSE should only be created for array or list types
108                 final List<Object> list = (List<Object>) obj;
109                 list.set(index, value);
110             }
111             return value;
112         }
113         return TRY_FAILED;
114     }
115 }