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