01: package org.apache.velocity.runtime.parser.node;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
20: */
21:
22: import java.util.Map;
23:
24: import org.apache.velocity.runtime.log.Log;
25:
26: /**
27: * SetExecutor that is smart about Maps. If it detects one, it does not
28: * use Reflection but a cast to access the setter.
29: *
30: * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a>
31: * @version $Id: MapSetExecutor.java 463298 2006-10-12 16:10:32Z henning $
32: */
33: public class MapSetExecutor extends SetExecutor {
34: private final String property;
35:
36: public MapSetExecutor(final Log log, final Class clazz,
37: final String property) {
38: this .log = log;
39: this .property = property;
40: discover(clazz);
41: }
42:
43: protected void discover(final Class clazz) {
44: Class[] interfaces = clazz.getInterfaces();
45: for (int i = 0; i < interfaces.length; i++) {
46: if (interfaces[i].equals(Map.class)) {
47: try {
48: if (property != null) {
49: setMethod(Map.class.getMethod("put",
50: new Class[] { Object.class,
51: Object.class }));
52: }
53: }
54: /**
55: * pass through application level runtime exceptions
56: */
57: catch (RuntimeException e) {
58: throw e;
59: } catch (Exception e) {
60: log.error("While looking for get('" + property
61: + "') method:", e);
62: }
63: break;
64: }
65: }
66: }
67:
68: public Object execute(final Object o, final Object arg) {
69: return ((Map) o).put(property, arg);
70: }
71: }
|