001: package org.apache.velocity.runtime.parser.node;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.lang.reflect.InvocationTargetException;
023:
024: import org.apache.commons.lang.StringUtils;
025: import org.apache.velocity.runtime.log.Log;
026: import org.apache.velocity.util.introspection.Introspector;
027:
028: /**
029: * Executor for looking up property names in the passed in class
030: * This will try to find a set<foo>(key, value) method
031: *
032: * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a>
033: * @version $Id: SetPropertyExecutor.java 463298 2006-10-12 16:10:32Z henning $
034: */
035: public class SetPropertyExecutor extends SetExecutor {
036: private final Introspector introspector;
037:
038: /**
039: * @param log
040: * @param introspector
041: * @param clazz
042: * @param property
043: * @param arg
044: */
045: public SetPropertyExecutor(final Log log,
046: final Introspector introspector, final Class clazz,
047: final String property, final Object arg) {
048: this .log = log;
049: this .introspector = introspector;
050:
051: // Don't allow passing in the empty string or null because
052: // it will either fail with a StringIndexOutOfBounds error
053: // or the introspector will get confused.
054: if (StringUtils.isNotEmpty(property)) {
055: discover(clazz, property, arg);
056: }
057: }
058:
059: /**
060: * @return The current introspector.
061: */
062: protected Introspector getIntrospector() {
063: return this .introspector;
064: }
065:
066: /**
067: * @param clazz
068: * @param property
069: * @param arg
070: */
071: protected void discover(final Class clazz, final String property,
072: final Object arg) {
073: Object[] params = new Object[] { arg };
074:
075: try {
076: StringBuffer sb = new StringBuffer("set");
077: sb.append(property);
078:
079: setMethod(introspector.getMethod(clazz, sb.toString(),
080: params));
081:
082: if (!isAlive()) {
083: /*
084: * now the convenience, flip the 1st character
085: */
086:
087: char c = sb.charAt(3);
088:
089: if (Character.isLowerCase(c)) {
090: sb.setCharAt(3, Character.toUpperCase(c));
091: } else {
092: sb.setCharAt(3, Character.toLowerCase(c));
093: }
094:
095: setMethod(introspector.getMethod(clazz, sb.toString(),
096: params));
097: }
098: }
099: /**
100: * pass through application level runtime exceptions
101: */
102: catch (RuntimeException e) {
103: throw e;
104: } catch (Exception e) {
105: log.error("While looking for property setter for '"
106: + property + "':", e);
107: }
108: }
109:
110: /**
111: * Execute method against context.
112: * @param o
113: * @param value
114: * @return The value of the invocation.
115: * @throws IllegalAccessException
116: * @throws InvocationTargetException
117: */
118: public Object execute(final Object o, final Object value)
119: throws IllegalAccessException, InvocationTargetException {
120: Object[] params = new Object[] { value };
121: return isAlive() ? getMethod().invoke(o, params) : null;
122: }
123: }
|