1 | package org.apache.velocity.runtime.parser.node; |
2 | /* |
3 | * Copyright 2000-2001,2004 The Apache Software Foundation. |
4 | * |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | * you may not use this file except in compliance with the License. |
7 | * You may obtain a copy of the License at |
8 | * |
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | * |
11 | * Unless required by applicable law or agreed to in writing, software |
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | * See the License for the specific language governing permissions and |
15 | * limitations under the License. |
16 | */ |
17 | |
18 | import org.apache.velocity.context.InternalContextAdapter; |
19 | import org.apache.velocity.util.introspection.Introspector; |
20 | |
21 | import java.lang.reflect.InvocationTargetException; |
22 | import org.apache.velocity.exception.MethodInvocationException; |
23 | |
24 | import org.apache.velocity.runtime.RuntimeLogger; |
25 | |
26 | |
27 | /** |
28 | * Executor that simply tries to execute a get(key) |
29 | * operation. This will try to find a get(key) method |
30 | * for any type of object, not just objects that |
31 | * implement the Map interface as was previously |
32 | * the case. |
33 | * |
34 | * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> |
35 | * @version $Id: GetExecutor.java,v 1.8.4.1 2004/03/03 23:22:59 geirm Exp $ |
36 | */ |
37 | public class GetExecutor extends AbstractExecutor |
38 | { |
39 | /** |
40 | * Container to hold the 'key' part of |
41 | * get(key). |
42 | */ |
43 | private Object[] args = new Object[1]; |
44 | |
45 | /** |
46 | * Default constructor. |
47 | */ |
48 | public GetExecutor(RuntimeLogger r, Introspector ispect, Class c, String key) |
49 | throws Exception |
50 | { |
51 | rlog = r; |
52 | args[0] = key; |
53 | method = ispect.getMethod(c, "get", args); |
54 | } |
55 | |
56 | /** |
57 | * Execute method against context. |
58 | */ |
59 | public Object execute(Object o) |
60 | throws IllegalAccessException, InvocationTargetException |
61 | { |
62 | if (method == null) |
63 | return null; |
64 | |
65 | return method.invoke(o, args); |
66 | } |
67 | |
68 | /** |
69 | * Execute method against context. |
70 | */ |
71 | public Object OLDexecute(Object o, InternalContextAdapter context) |
72 | throws IllegalAccessException, MethodInvocationException |
73 | { |
74 | if (method == null) |
75 | return null; |
76 | |
77 | try |
78 | { |
79 | return method.invoke(o, args); |
80 | } |
81 | catch(InvocationTargetException ite) |
82 | { |
83 | /* |
84 | * the method we invoked threw an exception. |
85 | * package and pass it up |
86 | */ |
87 | |
88 | throw new MethodInvocationException( |
89 | "Invocation of method 'get(\"" + args[0] + "\")'" |
90 | + " in " + o.getClass() |
91 | + " threw exception " |
92 | + ite.getTargetException().getClass(), |
93 | ite.getTargetException(), "get"); |
94 | } |
95 | catch(IllegalArgumentException iae) |
96 | { |
97 | return null; |
98 | } |
99 | } |
100 | } |
101 | |
102 | |
103 | |
104 | |
105 | |
106 | |
107 | |
108 | |
109 | |