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  
20  import static java.lang.reflect.Modifier.isFinal;
21  import static java.lang.reflect.Modifier.isStatic;
22  
23  import java.lang.reflect.Field;
24  
25  import org.apache.commons.jexl3.introspection.JexlPropertyGet;
26  
27  /**
28   * A JexlPropertyGet for public fields.
29   */
30  public final class FieldGetExecutor implements JexlPropertyGet {
31      /**
32       * Attempts to discover a FieldGetExecutor.
33       *
34       * @param is the introspector
35       * @param clazz the class to find the get method from
36       * @param identifier the key to use as an argument to the get method
37       * @return the executor if found, null otherwise
38       */
39      public static JexlPropertyGet discover(final Introspector is, final Class<?> clazz, final String identifier) {
40          if (identifier != null) {
41              final Field field = is.getField(clazz, identifier);
42              if (field != null) {
43                  return new FieldGetExecutor(field);
44              }
45          }
46          return null;
47      }
48  
49      /**
50       * The public field.
51       */
52      private final Field field;
53      /**
54       * Creates a new instance of FieldPropertyGet.
55       * @param theField the class public field
56       */
57      private FieldGetExecutor(final Field theField) {
58          field = theField;
59      }
60  
61      @Override
62      public Object invoke(final Object obj) throws Exception {
63          return field.get(obj);
64      }
65  
66      @Override
67      public boolean isCacheable() {
68          return true;
69      }
70  
71      @Override
72      public boolean isConstant() {
73          if (field.isEnumConstant()) {
74              return true;
75          }
76          final int modifiers = field.getModifiers();
77          // public static final fields are (considered) constants
78          return isFinal(modifiers) && isStatic(modifiers);
79      }
80  
81      @Override
82      public boolean tryFailed(final Object rval) {
83          return rval == Uberspect.TRY_FAILED;
84      }
85  
86      @Override
87      public Object tryInvoke(final Object obj, final Object key) {
88          if (obj.getClass().equals(field.getDeclaringClass()) && key.equals(field.getName())) {
89              try {
90                  return field.get(obj);
91              } catch (final IllegalAccessException xill) {
92                  return Uberspect.TRY_FAILED;
93              }
94          }
95          return Uberspect.TRY_FAILED;
96      }
97  
98  }