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