    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 * Specialized executor to get a boolean property from an object.
    023 * @since 2.0
    024 */
    025public final class BooleanGetExecutor extends AbstractExecutor.Get {
    026    /** The property. */
    027    private final String property;
    028
    029    /**
    030     * Discovers a BooleanGetExecutor.
    031     * <p>The method to be found should be named "is{P,p}property and return a boolean.</p>
    032     *
    033     * @param is the introspector
    034     * @param clazz the class to find the get method from
    035     * @param property the property name
    036     * @return the executor if found, null otherwise
    037     */
    038    public static BooleanGetExecutor discover(final Introspector is, final Class<?> clazz, final String property) {
    039        if (property != null && !property.isEmpty()) {
    040            final java.lang.reflect.Method m = PropertyGetExecutor.discoverGet(is, "is", clazz, property);
    041            if (m != null && (m.getReturnType() == Boolean.TYPE || m.getReturnType() == Boolean.class)) {
    042                return new BooleanGetExecutor(clazz, m, property);
    043            }
    044        }
    045        return null;
    046    }
    047
    048    /**
    049     * Creates an instance by attempting discovery of the get method.
    050     * @param clazz the class to introspect
    051     * @param method the method held by this executor
    052     * @param key the property to get
    053     */
    054    private BooleanGetExecutor(final Class<?> clazz, final java.lang.reflect.Method method, final String 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) throws IllegalAccessException, InvocationTargetException {
    066        return method == null ? null : method.invoke(obj, (Object[]) null);
    067    }
    068
    069    @Override
    070    public Object tryInvoke(final Object obj, final Object key) {
    071        if (obj != null && method !=  null
    072            // ensure method name matches the property name
    073            && property.equals(key)
    074            && objectClass.equals(obj.getClass())) {
    075            try {
    076                return method.invoke(obj, (Object[]) null);
    077            } catch (final IllegalAccessException xill) {
    078                return TRY_FAILED;// fail
    079            } catch (final InvocationTargetException xinvoke) {
    080                throw JexlException.tryFailed(xinvoke); // throw
    081            }
    082        }
    083        return TRY_FAILED;
    084    }
    085}