1 package org.apache.velocity.runtime.parser.node;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.lang.reflect.InvocationTargetException;
23
24 import org.apache.velocity.app.event.EventHandlerUtil;
25 import org.apache.velocity.context.InternalContextAdapter;
26 import org.apache.velocity.exception.MethodInvocationException;
27 import org.apache.velocity.exception.TemplateInitException;
28 import org.apache.velocity.runtime.parser.Parser;
29 import org.apache.velocity.runtime.parser.ParserVisitor;
30 import org.apache.velocity.util.introspection.Info;
31 import org.apache.velocity.util.introspection.IntrospectionCacheData;
32 import org.apache.velocity.util.introspection.VelPropertyGet;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class ASTIdentifier extends SimpleNode
51 {
52 private String identifier = "";
53
54
55
56
57 protected Info uberInfo;
58
59
60
61
62 public ASTIdentifier(int id)
63 {
64 super(id);
65 }
66
67
68
69
70
71 public ASTIdentifier(Parser p, int id)
72 {
73 super(p, id);
74 }
75
76
77
78
79
80 public Object jjtAccept(ParserVisitor visitor, Object data)
81 {
82 return visitor.visit(this, data);
83 }
84
85
86
87
88
89
90
91
92
93 public Object init(InternalContextAdapter context, Object data)
94 throws TemplateInitException
95 {
96 super.init(context, data);
97
98 identifier = getFirstToken().image;
99
100 uberInfo = new Info(context.getCurrentTemplateName(),
101 getLine(), getColumn());
102
103 return data;
104 }
105
106
107
108
109 public Object execute(Object o, InternalContextAdapter context)
110 throws MethodInvocationException
111 {
112
113 VelPropertyGet vg = null;
114
115 try
116 {
117
118
119
120
121 IntrospectionCacheData icd = context.icacheGet(this);
122
123
124
125
126
127
128
129
130 if ( icd != null && (o != null) && (icd.contextData == o.getClass()) )
131 {
132 vg = (VelPropertyGet) icd.thingy;
133 }
134 else
135 {
136
137
138
139
140
141 vg = rsvc.getUberspect().getPropertyGet(o,identifier, uberInfo);
142
143 if (vg != null && vg.isCacheable() && (o != null))
144 {
145 icd = new IntrospectionCacheData();
146 icd.contextData = o.getClass();
147 icd.thingy = vg;
148 context.icachePut(this,icd);
149 }
150 }
151 }
152
153
154
155
156 catch( RuntimeException e )
157 {
158 throw e;
159 }
160 catch(Exception e)
161 {
162 log.error("ASTIdentifier.execute() : identifier = "+identifier, e);
163 }
164
165
166
167
168
169 if (vg == null)
170 {
171 return null;
172 }
173
174
175
176
177
178 try
179 {
180 return vg.invoke(o);
181 }
182 catch(InvocationTargetException ite)
183 {
184
185
186
187
188
189 Throwable t = ite.getTargetException();
190 if (t instanceof Exception)
191 {
192 try
193 {
194 return EventHandlerUtil.methodException(rsvc, context, o.getClass(), vg.getMethodName(),
195 (Exception) t);
196 }
197
198
199
200
201
202
203 catch( Exception e )
204 {
205 throw new MethodInvocationException(
206 "Invocation of method '" + vg.getMethodName() + "'"
207 + " in " + o.getClass()
208 + " threw exception "
209 + ite.getTargetException().toString(),
210 ite.getTargetException(), vg.getMethodName(), context.getCurrentTemplateName(), this.getLine(), this.getColumn());
211 }
212 }
213 else
214 {
215
216
217
218
219 throw new MethodInvocationException(
220 "Invocation of method '" + vg.getMethodName() + "'"
221 + " in " + o.getClass()
222 + " threw exception "
223 + ite.getTargetException().toString(),
224 ite.getTargetException(), vg.getMethodName(), context.getCurrentTemplateName(), this.getLine(), this.getColumn());
225
226
227 }
228 }
229 catch(IllegalArgumentException iae)
230 {
231 return null;
232 }
233
234
235
236 catch( RuntimeException e )
237 {
238 throw e;
239 }
240 catch(Exception e)
241 {
242 log.error("ASTIdentifier() : exception invoking method "
243 + "for identifier '" + identifier + "' in "
244 + o.getClass() + " : " + e);
245 }
246
247 return null;
248 }
249 }