Save This Page
Home » velocity-1.5 » org.apache » velocity » app » [javadoc | source]
org.apache.velocity.app
public class: FieldMethodizer [javadoc | source]
java.lang.Object
   org.apache.velocity.app.FieldMethodizer

This is a small utility class allow easy access to static fields in a class, such as string constants. Velocity will not introspect for class fields (and won't in the future :), but writing setter/getter methods to do this really is a pain, so use this if you really have to access fields.

The idea it so enable access to the fields just like you would in Java. For example, in Java, you would access a static field like

MyClass.STRING_CONSTANT
and that is the same thing we are trying to allow here.

So to use in your Java code, do something like this :

context.put("runtime", new FieldMethodizer( "org.apache.velocity.runtime.Runtime" ));
and then in your template, you can access any of your static fields in this way :
$runtime.COUNTER_NAME

Right now, this class only methodizes public static fields. It seems that anything else is too dangerous. This class is for convenience accessing 'constants'. If you have fields that aren't static it may be better to handle them by explicitly placing them into the context.

Constructor:
 public FieldMethodizer() 
 public FieldMethodizer(String s) 
    Constructor that takes as it's arg the name of the class to methodize.
    Parameters:
    s - Name of class to methodize.
 public FieldMethodizer(Object o) 
    Constructor that takes as it's arg a living object to methodize. Note that it will still only methodized the public static fields of the class.
    Parameters:
    o - Name of class to methodize.
Method from org.apache.velocity.app.FieldMethodizer Summary:
addObject,   addObject,   get
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.apache.velocity.app.FieldMethodizer Detail:
 public  void addObject(String s) throws Exception 
    Add the Name of the class to methodize
 public  void addObject(Object o) throws Exception 
    Add an Object to methodize
 public Object get(String fieldName) 
    Accessor method to get the fields by name.