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