KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jexl > util > PropertyExecutor


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 JavaDoc;
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 JavaDoc 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 JavaDoc clazz, String JavaDoc 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 JavaDoc clazz, String JavaDoc property) {
60         /*
61          * this is gross and linear, but it keeps it straightforward.
62          */

63
64         try {
65             char c;
66             StringBuffer JavaDoc sb;
67
68             Object JavaDoc[] params = {};
69
70             /*
71              * start with get<property>
72              * this leaves the property name
73              * as is...
74              */

75             sb = new StringBuffer JavaDoc("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              * now the convenience, flip the 1st character
88              */

89          
90             sb = new StringBuffer JavaDoc("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 JavaDoc e) {
109             rlog.error("PROGRAMMER ERROR : PropertyExector() : " + e);
110         }
111     }
112
113
114     /**
115      * {@inheritDoc}
116      */

117     public Object JavaDoc execute(Object JavaDoc o)
118     throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
119         if (method == null) {
120             return null;
121         }
122
123         return method.invoke(o, null);
124     }
125 }
126
127
Popular Tags