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  package org.apache.commons.jexl.util;
18  
19  import java.lang.reflect.InvocationTargetException;
20  
21  import org.apache.commons.jexl.util.introspection.Introspector;
22  import org.apache.commons.logging.Log;
23  
24  /**
25   * Returned the value of object property when executed.
26   * @since 1.0
27   */
28  public class PropertyExecutor extends AbstractExecutor {
29  
30      /** index of the first character of the property. */
31      private static final int PROPERTY_START_INDEX = 3;
32      /** The JEXL introspector used. */
33      protected Introspector introspector = null;
34  
35      /** The method used. */
36      protected String methodUsed = null;
37  
38      /**
39       * Constructor.
40       *
41       * @param r The log for this property executor instance.
42       * @param ispctr The JEXL introspector.
43       * @param clazz The class being examined.
44       * @param property The property being addressed.
45       */
46      public PropertyExecutor(Log r, Introspector ispctr,
47              Class clazz, String property) {
48          rlog = r;
49          introspector = ispctr;
50  
51          discover(clazz, property);
52      }
53  
54      /**
55       * Locate the getter method for this property.
56       *
57       * @param clazz The class being analyzed.
58       * @param property Name of the property.
59       */
60      protected void discover(Class clazz, String property) {
61          /*
62           *  this is gross and linear, but it keeps it straightforward.
63           */
64  
65          try {
66              char c;
67              StringBuffer sb;
68  
69              Object[] params = {};
70  
71              /*
72               *  start with get<property>
73               *  this leaves the property name
74               *  as is...
75               */
76              sb = new StringBuffer("get");
77              sb.append(property);
78  
79              methodUsed = sb.toString();
80  
81              method = introspector.getMethod(clazz, methodUsed, params);
82  
83              if (method != null) {
84                  return;
85              }
86  
87              /*
88               *  now the convenience, flip the 1st character
89               */
90  
91              c = sb.charAt(PROPERTY_START_INDEX);
92  
93              if (Character.isLowerCase(c)) {
94                  sb.setCharAt(PROPERTY_START_INDEX, Character.toUpperCase(c));
95              } else {
96                  sb.setCharAt(PROPERTY_START_INDEX, Character.toLowerCase(c));
97              }
98  
99              methodUsed = sb.toString();
100             method = introspector.getMethod(clazz, methodUsed, params);
101 
102             if (method != null) {
103                 return;
104             }
105         /**
106          * pass through application level runtime exceptions
107          */
108         } catch( RuntimeException e ) {
109             throw e;
110         } catch(Exception e) {
111             rlog.error("PROGRAMMER ERROR : PropertyExector() : " + e);
112         }
113     }
114 
115 
116     /**
117      * {@inheritDoc}
118      */
119     public Object execute(Object o)
120     throws IllegalAccessException,  InvocationTargetException {
121         if (method == null) {
122             return null;
123         }
124 
125         return method.invoke(o, null);
126     }
127 }
128