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: * GetExecutor that is smart about Maps. If it detects one, it does not
28: * use Reflection but a cast to access the getter.
29: *
30: * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a>
31: * @version $Id: MapGetExecutor.java 463298 2006-10-12 16:10:32Z henning $
32: */
33: public class MapGetExecutor extends AbstractExecutor {
34: private final String property;
35:
36: public MapGetExecutor(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("get",
50: new Class[] { Object.class }));
51: }
52: }
53: /**
54: * pass through application level runtime exceptions
55: */
56: catch (RuntimeException e) {
57: throw e;
58: } catch (Exception e) {
59: log.error("While looking for get('" + property
60: + "') method:", e);
61: }
62: break;
63: }
64: }
65: }
66:
67: public Object execute(final Object o) {
68: return ((Map) o).get(property);
69: }
70: }
|