    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
    018    package org.apache.commons.jexl2.internal;
    019    import java.lang.reflect.InvocationTargetException;
    020    /**
    021     * Specialized executor to get a boolean property from an object.
    022     * @since 2.0
    023     */
    024    public final class BooleanGetExecutor extends AbstractExecutor.Get {
    025        /** The property. */
    026        private final String property;
    027        /**
    028         * Creates an instance by attempting discovery of the get method.
    029         * @param is the introspector
    030         * @param clazz the class to introspect
    031         * @param key the property to get
    032         */
    033        public BooleanGetExecutor(Introspector is, Class<?> clazz, String key) {
    034            super(clazz, discover(is, clazz, key));
    035            property = key;
    036        }
    037
    038        /** {@inheritDoc} */
    039        @Override
    040        public Object getTargetProperty() {
    041            return property;
    042        }
    043
    044        /** {@inheritDoc} */
    045        @Override
    046        public Object execute(Object obj)
    047            throws IllegalAccessException, InvocationTargetException {
    048            return method == null ? null : method.invoke(obj, (Object[]) null);
    049        }
    050
    051        /** {@inheritDoc} */
    052        @Override
    053        public Object tryExecute(Object obj, Object key) {
    054            if (obj != null && method !=  null
    055                // ensure method name matches the property name
    056                && property.equals(key)
    057                && objectClass.equals(obj.getClass())) {
    058                try {
    059                    return method.invoke(obj, (Object[]) null);
    060                } catch (InvocationTargetException xinvoke) {
    061                    return TRY_FAILED; // fail
    062                } catch (IllegalAccessException xill) {
    063                    return TRY_FAILED;// fail
    064                }
    065            }
    066            return TRY_FAILED;
    067        }
    068
    069        /**
    070         * Discovers the method for a {@link BooleanGet}.
    071         * <p>The method to be found should be named "is{P,p}property and return a boolean.</p>
    072         *@param is the introspector
    073         *@param clazz the class to find the get method from
    074         *@param property the the property name
    075         *@return the method if found, null otherwise
    076         */
    077        static java.lang.reflect.Method discover(Introspector is, final Class<?> clazz, String property) {
    078            java.lang.reflect.Method m = PropertyGetExecutor.discoverGet(is, "is", clazz, property);
    079            return (m != null && m.getReturnType() == Boolean.TYPE) ? m : null;
    080        }
    081
    082    }