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    *      https://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  
21  import org.apache.commons.jexl3.JexlException;
22  
23  /**
24   * Specialized executor to get a boolean property from an object.
25   *
26   * @since 2.0
27   */
28  public final class BooleanGetExecutor extends AbstractExecutor.Get {
29  
30      /**
31       * Discovers a BooleanGetExecutor.
32       * <p>The method to be found should be named "is{P,p}property and return a boolean.</p>
33       *
34       * @param is the introspector
35       * @param clazz the class to find the get method from
36       * @param property the property name
37       * @return the executor if found, null otherwise
38       */
39      public static BooleanGetExecutor discover(final Introspector is, final Class<?> clazz, final String property) {
40          if (property != null && !property.isEmpty()) {
41              final java.lang.reflect.Method m = PropertyGetExecutor.discoverGet(is, "is", clazz, property);
42              if (m != null && (m.getReturnType() == Boolean.TYPE || m.getReturnType() == Boolean.class)) {
43                  return new BooleanGetExecutor(clazz, m, property);
44              }
45          }
46          return null;
47      }
48  
49      /** The property. */
50      private final String property;
51  
52      /**
53       * Creates an instance by attempting discovery of the get method.
54       *
55       * @param clazz the class to introspect
56       * @param method the method held by this executor
57       * @param key the property to get
58       */
59      private BooleanGetExecutor(final Class<?> clazz, final java.lang.reflect.Method method, final String key) {
60          super(clazz, method);
61          property = key;
62      }
63  
64      @Override
65      public Object getTargetProperty() {
66          return property;
67      }
68  
69      @Override
70      public Object invoke(final Object obj) throws IllegalAccessException, InvocationTargetException {
71          return method == null ? null : method.invoke(obj, (Object[]) null);
72      }
73  
74      @Override
75      public Object tryInvoke(final Object obj, final Object key) {
76          if (obj != null && method !=  null
77              // ensure method name matches the property name
78              && property.equals(key)
79              && objectClass.equals(obj.getClass())) {
80              try {
81                  return method.invoke(obj, (Object[]) null);
82              } catch (final IllegalAccessException xill) {
83                  return TRY_FAILED; // fail
84              } catch (final InvocationTargetException xinvoke) {
85                  throw JexlException.tryFailed(xinvoke); // throw
86              }
87          }
88          return TRY_FAILED;
89      }
90  }