geirm       00/11/22 04:12:00

  Modified:    src/java/org/apache/velocity/runtime/parser/node
                        ASTMethod.java
  Log:
  Small fix to execute().  If the invoked method returns null, then check to see if 
the method was declared return type void.  If so, then return an empty String, so the 
caller (presumed to be ASTDirective) understands everything is ok, and doesn't output 
any nonsense.
  
  Revision  Changes    Path
  1.5       +99 -6     
jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTMethod.java
  
  Index: ASTMethod.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTMethod.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ASTMethod.java    2000/11/01 18:19:28     1.4
  +++ ASTMethod.java    2000/11/22 12:11:59     1.5
  @@ -1,5 +1,72 @@
   /* Generated By:JJTree: Do not edit this line. ASTMethod.java */
   
  +/*
  + * The Apache Software License, Version 1.1
  + *
  + * Copyright (c) 2000 The Apache Software Foundation.  All rights
  + * reserved.
  + *
  + * Redistribution and use in source and binary forms, with or without
  + * modification, are permitted provided that the following conditions
  + * are met:
  + *
  + * 1. Redistributions of source code must retain the above copyright
  + *    notice, this list of conditions and the following disclaimer.
  + *
  + * 2. Redistributions in binary form must reproduce the above copyright
  + *    notice, this list of conditions and the following disclaimer in
  + *    the documentation and/or other materials provided with the
  + *    distribution.
  + *
  + * 3. The end-user documentation included with the redistribution, if
  + *    any, must include the following acknowlegement:
  + *       "This product includes software developed by the
  + *        Apache Software Foundation (http://www.apache.org/)."
  + *    Alternately, this acknowlegement may appear in the software itself,
  + *    if and wherever such third-party acknowlegements normally appear.
  + *
  + * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  + *    Foundation" must not be used to endorse or promote products derived
  + *    from this software without prior written permission. For written
  + *    permission, please contact [EMAIL PROTECTED]
  + *
  + * 5. Products derived from this software may not be called "Apache"
  + *    nor may "Apache" appear in their names without prior written
  + *    permission of the Apache Group.
  + *
  + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  + * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  + * SUCH DAMAGE.
  + * ====================================================================
  + *
  + * This software consists of voluntary contributions made by many
  + * individuals on behalf of the Apache Software Foundation.  For more
  + * information on the Apache Software Foundation, please see
  + * <http://www.apache.org/>.
  + */
  +
  +/**
  + *  ASTMethod.java
  + *
  + *  Method support for references :  $foo.method()
  + *
  + *  Please look at the Parser.jjt file which is
  + *  what controls the generation of this class.
  + *
  + * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
  + * @version $Id: ASTMethod.java,v 1.5 2000/11/22 12:11:59 geirm Exp $ 
  + */
  +
   package org.apache.velocity.runtime.parser.node;
   
   import java.lang.reflect.Method;
  @@ -39,9 +106,11 @@
           paramCount = jjtGetNumChildren() - 1;
           params = new Object[paramCount];
           
  -        // Now the parameters have to be processed, there
  -        // may be references contained within that need
  -        // to be introspected.
  +        /*
  +         *  Now the parameters have to be processed, there
  +         *  may be references contained within that need
  +         *  to be introspected.
  +         */
           
           for (int i = 0; i < paramCount; i++)
               jjtGetChild(i + 1).init(context, null);
  @@ -57,17 +126,41 @@
           return method.getReturnType();
       }
       
  +    /**
  +     *  invokes the method.  Returns null if a problem, the
  +     *  actual return if the method returns something, or 
  +     *  an empty string "" if the method returns void
  +     */
       public Object execute(Object o, Context context)
       {
  -        // I need to pass in the arguments to the
  -        // method. 
  +        /*
  +         *  I need to pass in the arguments to the
  +         *  method. 
  +         */
   
           for (int j = 0; j < paramCount; j++)
               params[j] = jjtGetChild(j + 1).value(context);
           
           try
           {
  -            return method.invoke(o, params);
  +            /*
  +             *  get the returned object.  It may be null, and that is
  +             *  valid for something declared with a void return type.
  +             *  Since the caller is expecting something to be returned,
  +             *  as long as things are peachy, we can return an empty 
  +             *  String so ASTReference() correctly figures out that
  +             *  all is well.
  +             */
  +
  +            Object obj = method.invoke(o, params);
  +            
  +            if (obj == null)
  +            {
  +                if (method.getReturnType().toString().equals("void"))
  +                    return new String("");
  +            }
  +            
  +            return obj;
           }
           catch (Exception e)
           {
  
  
  

Reply via email to