View Javadoc

1   package org.apache.velocity.runtime.parser.node;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "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.lang.reflect.InvocationTargetException;
23  
24  import org.apache.commons.lang.ArrayUtils;
25  import org.apache.commons.lang.StringUtils;
26  import org.apache.velocity.app.event.EventHandlerUtil;
27  import org.apache.velocity.context.InternalContextAdapter;
28  import org.apache.velocity.exception.MethodInvocationException;
29  import org.apache.velocity.exception.TemplateInitException;
30  import org.apache.velocity.exception.VelocityException;
31  import org.apache.velocity.runtime.RuntimeConstants;
32  import org.apache.velocity.runtime.directive.StopCommand;
33  import org.apache.velocity.runtime.parser.Parser;
34  import org.apache.velocity.util.ClassUtils;
35  import org.apache.velocity.util.introspection.Info;
36  import org.apache.velocity.util.introspection.VelMethod;
37  
38  /**
39   *  ASTMethod.java
40   *
41   *  Method support for references :  $foo.method()
42   *
43   *  NOTE :
44   *
45   *  introspection is now done at render time.
46   *
47   *  Please look at the Parser.jjt file which is
48   *  what controls the generation of this class.
49   *
50   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
51   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
52   * @version $Id: ASTMethod.java 898032 2010-01-11 19:51:03Z nbubna $
53   */
54  public class ASTMethod extends SimpleNode
55  {
56      private String methodName = "";
57      private int paramCount = 0;
58  
59      protected Info uberInfo;
60  
61      /**
62       * Indicates if we are running in strict reference mode.
63       */
64      protected boolean strictRef = false;
65  
66      /**
67       * @param id
68       */
69      public ASTMethod(int id)
70      {
71          super(id);
72      }
73  
74      /**
75       * @param p
76       * @param id
77       */
78      public ASTMethod(Parser p, int id)
79      {
80          super(p, id);
81      }
82  
83      /**
84       * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.node.ParserVisitor, java.lang.Object)
85       */
86      public Object jjtAccept(ParserVisitor visitor, Object data)
87      {
88          return visitor.visit(this, data);
89      }
90  
91      /**
92       *  simple init - init our subtree and get what we can from
93       *  the AST
94       * @param context
95       * @param data
96       * @return The init result
97       * @throws TemplateInitException
98       */
99      public Object init(  InternalContextAdapter context, Object data)
100         throws TemplateInitException
101     {
102         super.init(  context, data );
103 
104         /*
105          * make an uberinfo - saves new's later on
106          */
107 
108         uberInfo = new Info(getTemplateName(),
109                 getLine(),getColumn());
110         /*
111          *  this is about all we can do
112          */
113 
114         methodName = getFirstToken().image;
115         paramCount = jjtGetNumChildren() - 1;
116 
117         strictRef = rsvc.getBoolean(RuntimeConstants.RUNTIME_REFERENCES_STRICT, false);
118         
119         return data;
120     }
121 
122     /**
123      *  invokes the method.  Returns null if a problem, the
124      *  actual return if the method returns something, or
125      *  an empty string "" if the method returns void
126      * @param o
127      * @param context
128      * @return Result or null.
129      * @throws MethodInvocationException
130      */
131     public Object execute(Object o, InternalContextAdapter context)
132         throws MethodInvocationException
133     {
134         /*
135          *  new strategy (strategery!) for introspection. Since we want
136          *  to be thread- as well as context-safe, we *must* do it now,
137          *  at execution time.  There can be no in-node caching,
138          *  but if we are careful, we can do it in the context.
139          */
140         Object [] params = new Object[paramCount];
141 
142           /*
143            * sadly, we do need recalc the values of the args, as this can
144            * change from visit to visit
145            */
146         final Class[] paramClasses =       
147             paramCount > 0 ? new Class[paramCount] : ArrayUtils.EMPTY_CLASS_ARRAY;
148 
149         for (int j = 0; j < paramCount; j++)
150         {
151             params[j] = jjtGetChild(j + 1).value(context);
152             if (params[j] != null)
153             {
154                 paramClasses[j] = params[j].getClass();
155             }
156         }
157             
158         VelMethod method = ClassUtils.getMethod(methodName, params, paramClasses, 
159             o, context, this, strictRef);
160         if (method == null) return null;
161 
162         try
163         {
164             /*
165              *  get the returned object.  It may be null, and that is
166              *  valid for something declared with a void return type.
167              *  Since the caller is expecting something to be returned,
168              *  as long as things are peachy, we can return an empty
169              *  String so ASTReference() correctly figures out that
170              *  all is well.
171              */
172 
173             Object obj = method.invoke(o, params);
174 
175             if (obj == null)
176             {
177                 if( method.getReturnType() == Void.TYPE)
178                 {
179                     return "";
180                 }
181             }
182 
183             return obj;
184         }
185         catch( InvocationTargetException ite )
186         {
187             return handleInvocationException(o, context, ite.getTargetException());
188         }
189         
190         /** Can also be thrown by method invocation **/
191         catch( IllegalArgumentException t )
192         {
193             return handleInvocationException(o, context, t);
194         }
195         
196         /**
197          * pass through application level runtime exceptions
198          */
199         catch( RuntimeException e )
200         {
201             throw e;
202         }
203         catch( Exception e )
204         {
205             String msg = "ASTMethod.execute() : exception invoking method '"
206                          + methodName + "' in " + o.getClass();
207             log.error(msg, e);
208             throw new VelocityException(msg, e);
209         }
210     }
211 
212     private Object handleInvocationException(Object o, InternalContextAdapter context, Throwable t)
213     {
214         /*
215          * We let StopCommands go up to the directive they are for/from
216          */
217         if (t instanceof StopCommand)
218         {
219             throw (StopCommand)t;
220         }
221 
222         /*
223          *  In the event that the invocation of the method
224          *  itself throws an exception, we want to catch that
225          *  wrap it, and throw.  We don't log here as we want to figure
226          *  out which reference threw the exception, so do that
227          *  above
228          */
229         else if (t instanceof Exception)
230         {
231             try
232             {
233                 return EventHandlerUtil.methodException( rsvc, context, o.getClass(), methodName, (Exception) t );
234             }
235 
236             /**
237              * If the event handler throws an exception, then wrap it
238              * in a MethodInvocationException.  Don't pass through RuntimeExceptions like other
239              * similar catchall code blocks.
240              */
241             catch( Exception e )
242             {
243                 throw new MethodInvocationException(
244                     "Invocation of method '"
245                     + methodName + "' in  " + o.getClass()
246                     + " threw exception "
247                     + e.toString(),
248                     e, methodName, getTemplateName(), this.getLine(), this.getColumn());
249             }
250         }
251 
252         /*
253          *  let non-Exception Throwables go...
254          */
255         else
256         {
257             /*
258              * no event cartridge to override. Just throw
259              */
260 
261             throw new MethodInvocationException(
262             "Invocation of method '"
263             + methodName + "' in  " + o.getClass()
264             + " threw exception "
265             + t.toString(),
266             t, methodName, getTemplateName(), this.getLine(), this.getColumn());
267         }
268     }
269 
270     /**
271      * Internal class used as key for method cache.  Combines
272      * ASTMethod fields with array of parameter classes.  Has
273      * public access (and complete constructor) for unit test 
274      * purposes.
275      * @since 1.5
276      */
277     public static class MethodCacheKey
278     {
279         private final String methodName;  
280         private final Class[] params;
281 
282         public MethodCacheKey(String methodName, Class[] params)
283         {
284             /** 
285              * Should never be initialized with nulls, but to be safe we refuse 
286              * to accept them.
287              */
288             this.methodName = (methodName != null) ? methodName : StringUtils.EMPTY;
289             this.params = (params != null) ? params : ArrayUtils.EMPTY_CLASS_ARRAY;
290         }
291 
292         /**
293          * @see java.lang.Object#equals(java.lang.Object)
294          */
295         public boolean equals(Object o)
296         {
297             /** 
298              * note we skip the null test for methodName and params
299              * due to the earlier test in the constructor
300              */            
301             if (o instanceof MethodCacheKey) 
302             {
303                 final MethodCacheKey other = (MethodCacheKey) o;                
304                 if (params.length == other.params.length && 
305                         methodName.equals(other.methodName)) 
306                 {              
307                     for (int i = 0; i < params.length; ++i) 
308                     {
309                         if (params[i] == null)
310                         {
311                             if (params[i] != other.params[i])
312                             {
313                                 return false;
314                             }
315                         }
316                         else if (!params[i].equals(other.params[i]))
317                         {
318                             return false;
319                         }
320                     }
321                     return true;
322                 }
323             }
324             return false;
325         }
326         
327 
328         /**
329          * @see java.lang.Object#hashCode()
330          */
331         public int hashCode()
332         {
333             int result = 17;
334 
335             /** 
336              * note we skip the null test for methodName and params
337              * due to the earlier test in the constructor
338              */            
339             for (int i = 0; i < params.length; ++i)
340             {
341                 final Class param = params[i];
342                 if (param != null)
343                 {
344                     result = result * 37 + param.hashCode();
345                 }
346             }
347             
348             result = result * 37 + methodName.hashCode();
349 
350             return result;
351         } 
352     }
353 
354     /**
355      * @return Returns the methodName.
356      * @since 1.5
357      */
358     public String getMethodName()
359     {
360         return methodName;
361     }
362     
363     
364 }