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