View Javadoc

1   /*
2    * Copyright 2000-2002,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   */
26  public class PropertyExecutor extends AbstractExecutor
27  {
28      protected Introspector introspector = null;
29  
30      protected String methodUsed = null;
31  
32      public PropertyExecutor(Log r, Introspector ispctr,
33                              Class clazz, String property)
34      {
35          rlog = r;
36          introspector = ispctr;
37  
38          discover(clazz, property);
39      }
40  
41      protected void discover(Class clazz, String property)
42      {
43          /*
44           *  this is gross and linear, but it keeps it straightforward.
45           */
46  
47          try
48          {
49              char c;
50              StringBuffer sb;
51  
52              Object[] params = {  };
53  
54              /*
55               *  start with get<property>
56               *  this leaves the property name 
57               *  as is...
58               */
59              sb = new StringBuffer("get");
60              sb.append(property);
61  
62              methodUsed = sb.toString();
63  
64              method = introspector.getMethod(clazz, methodUsed, params);
65               
66              if (method != null)
67                  return;
68          
69              /*
70               *  now the convenience, flip the 1st character
71               */
72           
73              sb = new StringBuffer("get");
74              sb.append(property);
75  
76              c = sb.charAt(3);
77  
78              if (Character.isLowerCase(c))
79              {
80                  sb.setCharAt(3, Character.toUpperCase(c));
81              }
82              else
83              {
84                  sb.setCharAt(3, Character.toLowerCase(c));
85              }
86  
87              methodUsed = sb.toString();
88              method = introspector.getMethod(clazz, methodUsed, params);
89  
90              if (method != null)
91                  return; 
92              
93          }
94          catch(Exception e)
95          {
96              rlog.error("PROGRAMMER ERROR : PropertyExector() : " + e );
97          }
98      }
99  
100 
101     /***
102      * Execute method against context.
103      */
104     public Object execute(Object o)
105         throws IllegalAccessException,  InvocationTargetException
106     {
107         if (method == null)
108             return null;
109 
110         return method.invoke(o, null);
111     }
112 }
113 
114