View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jexl3.internal.introspection;
19  import java.lang.reflect.InvocationTargetException;
20  import org.apache.commons.jexl3.JexlException;
21  /**
22   * Specialized executor to get a boolean property from an object.
23   * @since 2.0
24   */
25  public final class BooleanGetExecutor extends AbstractExecutor.Get {
26      /** The property. */
27      private final String property;
28  
29      /**
30       * Discovers a BooleanGetExecutor.
31       * <p>The method to be found should be named "is{P,p}property and return a boolean.</p>
32       *
33       * @param is the introspector
34       * @param clazz the class to find the get method from
35       * @param property the property name
36       * @return the executor if found, null otherwise
37       */
38      public static BooleanGetExecutor discover(final Introspector is, final Class<?> clazz, final String property) {
39          if (property != null && !property.isEmpty()) {
40              final java.lang.reflect.Method m = PropertyGetExecutor.discoverGet(is, "is", clazz, property);
41              if (m != null && (m.getReturnType() == Boolean.TYPE || m.getReturnType() == Boolean.class)) {
42                  return new BooleanGetExecutor(clazz, m, property);
43              }
44          }
45          return null;
46      }
47  
48      /**
49       * Creates an instance by attempting discovery of the get method.
50       * @param clazz the class to introspect
51       * @param method the method held by this executor
52       * @param key the property to get
53       */
54      private BooleanGetExecutor(final Class<?> clazz, final java.lang.reflect.Method method, final String key) {
55          super(clazz, method);
56          property = key;
57      }
58  
59      @Override
60      public Object getTargetProperty() {
61          return property;
62      }
63  
64      @Override
65      public Object invoke(final Object obj) throws IllegalAccessException, InvocationTargetException {
66          return method == null ? null : method.invoke(obj, (Object[]) null);
67      }
68  
69      @Override
70      public Object tryInvoke(final Object obj, final Object key) {
71          if (obj != null && method !=  null
72              // ensure method name matches the property name
73              && property.equals(key)
74              && objectClass.equals(obj.getClass())) {
75              try {
76                  return method.invoke(obj, (Object[]) null);
77              } catch (final IllegalAccessException xill) {
78                  return TRY_FAILED;// fail
79              } catch (final InvocationTargetException xinvoke) {
80                  throw JexlException.tryFailed(xinvoke); // throw
81              }
82          }
83          return TRY_FAILED;
84      }
85  }