    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
    018package org.apache.commons.jexl3.internal.introspection;
    019import java.util.Map;
    020
    021/**
    022 * Specialized executor to set a property in a Map.
    023 * @since 2.0
    024 */
    025public final class MapSetExecutor extends AbstractExecutor.Set {
    026    /** The java.util.map.put method used as an active marker in MapSet. */
    027    private static final java.lang.reflect.Method MAP_SET = initMarker(Map.class, "put", Object.class, Object.class);
    028    /** The property. */
    029    private final Object property;
    030    /** The property value class. */
    031    private final Class<?> valueClass;
    032
    033    /**
    034     * Attempts to discover a MapSetExecutor.
    035     *
    036     * @param is the introspector
    037     * @param clazz the class to find the set method from
    038     * @param identifier the key to use as an argument to the get method
    039     * @param value the value to use as argument in map.put(key,value)
    040     * @return the executor if found, null otherwise
    041     */
    042    public static MapSetExecutor discover(final Introspector is,
    043                                          final Class<?> clazz,
    044                                          final Object identifier,
    045                                          final Object value) {
    046        if (Map.class.isAssignableFrom(clazz)) {
    047            return new MapSetExecutor(clazz, MAP_SET, identifier, value);
    048        }
    049        return null;
    050    }
    051
    052    /**
    053     * Creates an instance.
    054     * @param clazz the class the set method applies to
    055     * @param method the method called through this executor
    056     * @param key the key to use as 1st argument to the set method
    057     * @param value the value to use as 2nd argument to the set method
    058     */
    059    private MapSetExecutor(final Class<?> clazz, final java.lang.reflect.Method method, final Object key, final Object value) {
    060        super(clazz, method);
    061        property = key;
    062        valueClass = classOf(value);
    063    }
    064
    065    @Override
    066    public Object getTargetProperty() {
    067        return property;
    068    }
    069
    070    @Override
    071    public Object invoke(final Object obj, final Object value) {
    072        @SuppressWarnings("unchecked") // ctor only allows Map instances - see discover() method
    073        final Map<Object,Object> map = ((Map<Object, Object>) obj);
    074        map.put(property, value);
    075        return value;
    076    }
    077
    078    @Override
    079    public Object tryInvoke(final Object obj, final Object key, final Object value) {
    080        if (obj != null
    081            && method != null
    082            && objectClass.equals(obj.getClass())
    083            && ((property == null && key == null)
    084                || (property != null && key != null && property.getClass().equals(key.getClass())))
    085            && valueClass.equals(classOf(value))) {
    086            @SuppressWarnings("unchecked") // ctor only allows Map instances - see discover() method
    087            final Map<Object,Object> map = ((Map<Object, Object>) obj);
    088            map.put(key, value);
    089            return value;
    090        }
    091        return TRY_FAILED;
    092    }
    093}