You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Tim Pizey <ti...@paneris.org> on 2008/04/02 01:06:06 UTC

invalidMethod

Hi, 

I am trying to get Velocity to work as the template engine for 
http://melati.org/ which uses WebMacro by default.

I have run up against problems as it seems that 
InvalidReferenceEventHandler.invalidMethod is 
invoked when a method returns null. 

I have ended up working around it with the most horrific hack 
(which ignores method signatures) : 

  public Object invalidMethod(Context context, String reference, 
                                         Object object, String method, 
                                          Info info) {
    if(object != null) { 
      Method[] methods = object.getClass().getMethods();
      for (int i = 0; i < methods.length; i++)
        if (methods[i].getName().equals(method)) 
          return null;
    }
    String message = "Invalid method ";
    message += (method == null) ? "(null)" : method;
    message += " called ";
    message += (reference == null) ? "(null reference)" : reference;
    message += (object == null) ? " (null object)" : " on object " + object;
    message += " at " + info;
    throw new TemplateEngineException(message);
  }

The javadoc states: 
     * Called when object is null or the given method does not exist.
     * invalidMethod() will be called in sequence for each link in 
     * the chain until the first non-null value is returned. 

but in fact it is called when object is not null, when the method is valid 
but the return value of the method call is null.


It maybe that I am not using idiomatic VTL

#if( !$field.getRaw() )
 #if( $c )
  // ($field.getName() = null)
 #end
#end

what is the normal way to tackle this?

thanks in advance
TimP


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org