001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.jexl2.internal;
019 import java.util.List;
020 import java.lang.reflect.Array;
021 /**
022 * Specialized executor to set a property in a List or array.
023 * @since 2.0
024 */
025 public final class ListSetExecutor extends AbstractExecutor.Set {
026 /** The java.lang.reflect.Array.get method used as an active marker in ListGet. */
027 private static final java.lang.reflect.Method ARRAY_SET =
028 initMarker(Array.class, "set", Object.class, Integer.TYPE, Object.class);
029 /** The java.util.obj.set method used as an active marker in ListSet. */
030 private static final java.lang.reflect.Method LIST_SET =
031 initMarker(List.class, "set", Integer.TYPE, Object.class);
032 /** The property. */
033 private final Integer property;
034
035 /**
036 * Creates an instance checking for the List interface or Array capability.
037 * @param is the introspector
038 * @param clazz the class that might implement the map interface
039 * @param key the key to use in obj.set(key,value)
040 * @param value the value to use in obj.set(key,value)
041 */
042 public ListSetExecutor(Introspector is, Class<?> clazz, Integer key, Object value) {
043 super(clazz, discover(clazz));
044 property = key;
045 }
046
047 /** {@inheritDoc} */
048 @Override
049 public Object getTargetProperty() {
050 return property;
051 }
052
053 /** {@inheritDoc} */
054 @Override
055 public Object execute(final Object obj, Object value) {
056 if (method == ARRAY_SET) {
057 java.lang.reflect.Array.set(obj, property.intValue(), value);
058 } else {
059 @SuppressWarnings("unchecked") // LSE should only be created for array or list types
060 final List<Object> list = (List<Object>) obj;
061 list.set(property.intValue(), value);
062 }
063 return value;
064 }
065
066 /** {@inheritDoc} */
067 @Override
068 public Object tryExecute(final Object obj, Object key, Object value) {
069 if (obj != null && method != null
070 && objectClass.equals(obj.getClass())
071 && key instanceof Integer) {
072 if (method == ARRAY_SET) {
073 Array.set(obj, ((Integer) key).intValue(), value);
074 } else {
075 @SuppressWarnings("unchecked") // LSE should only be created for array or list types
076 final List<Object> list = (List<Object>) obj;
077 list.set(((Integer) key).intValue(), value);
078 }
079 return value;
080 }
081 return TRY_FAILED;
082 }
083
084
085 /**
086 * Finds the method to perform 'set' on a obj of array.
087 * @param clazz the class to introspect
088 * @return a marker method, obj.set or array.set
089 */
090 static java.lang.reflect.Method discover(Class<?> clazz) {
091 if (clazz.isArray()) {
092 // we could verify if the call can be performed but it does not change
093 // the fact we would fail...
094 // Class<?> formal = clazz.getComponentType();
095 // Class<?> actual = value == null? Object.class : value.getClass();
096 // if (IntrospectionUtils.isMethodInvocationConvertible(formal, actual, false)) {
097 return ARRAY_SET;
098 // }
099 }
100 if (List.class.isAssignableFrom(clazz)) {
101 return LIST_SET;
102 }
103 return null;
104 }
105 }