001: /*
002: * Copyright 2000-2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jexl.util;
017:
018: import java.lang.reflect.InvocationTargetException;
019:
020: import org.apache.commons.jexl.util.introspection.Introspector;
021: import org.apache.commons.logging.Log;
022:
023: /**
024: * Returned the value of object property when executed.
025: * @since 1.0
026: */
027: public class PropertyExecutor extends AbstractExecutor {
028:
029: /** index of the first character of the property. */
030: private static final int PROPERTY_START_INDEX = 3;
031: /** The JEXL introspector used. */
032: protected Introspector introspector = null;
033:
034: /** The method used. */
035: protected String methodUsed = null;
036:
037: /**
038: * Constructor.
039: *
040: * @param r The log for this property executor instance.
041: * @param ispctr The JEXL introspector.
042: * @param clazz The class being examined.
043: * @param property The property being addressed.
044: */
045: public PropertyExecutor(Log r, Introspector ispctr, Class clazz,
046: String property) {
047: rlog = r;
048: introspector = ispctr;
049:
050: discover(clazz, property);
051: }
052:
053: /**
054: * Locate the getter method for this property.
055: *
056: * @param clazz The class being analyzed.
057: * @param property Name of the property.
058: */
059: protected void discover(Class clazz, String property) {
060: /*
061: * this is gross and linear, but it keeps it straightforward.
062: */
063:
064: try {
065: char c;
066: StringBuffer sb;
067:
068: Object[] params = {};
069:
070: /*
071: * start with get<property>
072: * this leaves the property name
073: * as is...
074: */
075: sb = new StringBuffer("get");
076: sb.append(property);
077:
078: methodUsed = sb.toString();
079:
080: method = introspector.getMethod(clazz, methodUsed, params);
081:
082: if (method != null) {
083: return;
084: }
085:
086: /*
087: * now the convenience, flip the 1st character
088: */
089:
090: sb = new StringBuffer("get");
091: sb.append(property);
092:
093: c = sb.charAt(PROPERTY_START_INDEX);
094:
095: if (Character.isLowerCase(c)) {
096: sb.setCharAt(PROPERTY_START_INDEX, Character
097: .toUpperCase(c));
098: } else {
099: sb.setCharAt(PROPERTY_START_INDEX, Character
100: .toLowerCase(c));
101: }
102:
103: methodUsed = sb.toString();
104: method = introspector.getMethod(clazz, methodUsed, params);
105:
106: if (method != null) {
107: return;
108: }
109:
110: } catch (Exception e) {
111: rlog.error("PROGRAMMER ERROR : PropertyExector() : " + e);
112: }
113: }
114:
115: /**
116: * {@inheritDoc}
117: */
118: public Object execute(Object o) throws IllegalAccessException,
119: InvocationTargetException {
120: if (method == null) {
121: return null;
122: }
123:
124: return method.invoke(o, null);
125: }
126: }
|