    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.lang.reflect.InvocationTargetException;
    020import org.apache.commons.jexl3.JexlException;
    021
    022/**
    023 * Specialized executor to get a property from an object.
    024 * @since 2.0
    025 */
    026public final class PropertyGetExecutor extends AbstractExecutor.Get {
    027    /** A static signature for method(). */
    028    private static final Object[] EMPTY_PARAMS = {};
    029    /** The property. */
    030    private final String property;
    031
    032    /**
    033     * Discovers a PropertyGetExecutor.
    034     * <p>The method to be found should be named "get{P,p}property.</p>
    035     *
    036     * @param is the introspector
    037     * @param clazz the class to find the get method from
    038     * @param property the property name to find
    039     * @return the executor if found, null otherwise
    040     */
    041    public static PropertyGetExecutor discover(final Introspector is, final Class<?> clazz, final String property) {
    042        final java.lang.reflect.Method method = discoverGet(is, "get", clazz, property);
    043        return method == null? null : new PropertyGetExecutor(clazz, method, property);
    044    }
    045
    046    /**
    047     * Creates an instance.
    048     * @param clazz he class the get method applies to
    049     * @param method the method held by this executor
    050     * @param identifier the property to get
    051     */
    052    private PropertyGetExecutor(final Class<?> clazz, final java.lang.reflect.Method method, final String identifier) {
    053        super(clazz, method);
    054        property = identifier;
    055    }
    056
    057    @Override
    058    public Object getTargetProperty() {
    059        return property;
    060    }
    061
    062    @Override
    063    public Object invoke(final Object o) throws IllegalAccessException, InvocationTargetException {
    064        return method == null ? null : method.invoke(o, (Object[]) null);
    065    }
    066
    067    @Override
    068    public Object tryInvoke(final Object o, final Object identifier) {
    069        if (o != null && method != null
    070            && property.equals(castString(identifier))
    071            && objectClass.equals(o.getClass())) {
    072            try {
    073                return method.invoke(o, (Object[]) null);
    074            } catch (IllegalAccessException | IllegalArgumentException xill) {
    075                return TRY_FAILED;// fail
    076            } catch (final InvocationTargetException xinvoke) {
    077                throw JexlException.tryFailed(xinvoke); // throw
    078            }
    079        }
    080        return TRY_FAILED;
    081    }
    082
    083    /**
    084     * Base method for boolean and object property get.
    085     * @param is the introspector
    086     * @param which "is" or "get" for boolean or object
    087     * @param clazz The class being examined.
    088     * @param property The property being addressed.
    089     * @return The {get,is}{p,P}roperty method if one exists, null otherwise.
    090     */
    091    static java.lang.reflect.Method discoverGet(final Introspector is,
    092                                                final String which,
    093                                                final Class<?> clazz,
    094                                                final String property) {
    095        if (property == null || property.isEmpty()) {
    096            return null;
    097        }
    098        //  this is gross and linear, but it keeps it straightforward.
    099        java.lang.reflect.Method method;
    100        final int start = which.length(); // "get" or "is" so 3 or 2 for char case switch
    101        // start with get<Property>
    102        final StringBuilder sb = new StringBuilder(which);
    103        sb.append(property);
    104        // uppercase nth char
    105        final char c = sb.charAt(start);
    106        sb.setCharAt(start, Character.toUpperCase(c));
    107        method = is.getMethod(clazz, sb.toString(), EMPTY_PARAMS);
    108        //lowercase nth char
    109        if (method == null) {
    110            sb.setCharAt(start, Character.toLowerCase(c));
    111            method = is.getMethod(clazz, sb.toString(), EMPTY_PARAMS);
    112        }
    113        return method;
    114    }
    115}
    116