org.apache.velocity.app
public class FieldMethodizer extends Object
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
and that is the same thing we are trying to allow here.MyClass.STRING_CONSTANT
So to use in your Java code, do something like this :
and then in your template, you can access any of your static fields in this way :context.put("runtime", new FieldMethodizer( "org.apache.velocity.runtime.Runtime" ));
$runtime.RUNTIME_LOG_WARN_STACKTRACE
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.
Version: $Id: FieldMethodizer.java,v 1.3.14.1 2004/03/03 23:22:53 geirm Exp $
Field Summary | |
---|---|
HashMap | classHash Hold the class objects by field name |
HashMap | fieldHash Hold the field objects by field name |
Constructor Summary | |
---|---|
FieldMethodizer()
Allow object to be initialized without any data. | |
FieldMethodizer(String s)
Constructor that takes as it's arg the name of the class
to methodize.
| |
FieldMethodizer(Object o)
Constructor that takes as it's arg a living
object to methodize. |
Method Summary | |
---|---|
void | addObject(String s)
Add the Name of the class to methodize |
void | addObject(Object o)
Add an Object to methodize |
Object | get(String fieldName)
Accessor method to get the fields by name.
|
void | inspect(Class clas)
Method that retrieves all public static fields
in the class we are methodizing. |
Parameters: s Name of class to methodize.
Parameters: s Name of class to methodize.
Parameters: fieldName Name of static field to retrieve
Returns: The value of the given field.