C# Class NVelocity.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( "NVelocity.Runtime.Runtime" )); 
and then in your template, you can access any of your static fields in this way :
 $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.

显示文件 Open project: rasmus-toftdahl-olesen/NVelocity

Public Methods

Method Description
AddObject ( Object o ) : void

Add an Object to methodize

AddObject ( String s ) : void

Add the Name of the class to methodize

FieldMethodizer ( ) : System

Allow object to be initialized without any data. You would use addObject() to add data later.

FieldMethodizer ( Object o ) : System

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.

FieldMethodizer ( String s ) : System

Constructor that takes as it's arg the name of the class to methodize.

Get ( String fieldName ) : Object

Accessor method to get the fields by name.

Private Methods

Method Description
Inspect ( Type type ) : void

Method that retrieves all public static fields in the class we are methodizing.

Method Details

AddObject() public method

Add an Object to methodize
public AddObject ( Object o ) : void
o Object
return void

AddObject() public method

Add the Name of the class to methodize
public AddObject ( String s ) : void
s String
return void

FieldMethodizer() public method

Allow object to be initialized without any data. You would use addObject() to add data later.
public FieldMethodizer ( ) : System
return System

FieldMethodizer() public method

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.
public FieldMethodizer ( Object o ) : System
o Object object to methodize.
return System

FieldMethodizer() public method

Constructor that takes as it's arg the name of the class to methodize.
public FieldMethodizer ( String s ) : System
s String Name of class to methodize.
return System

Get() public method

Accessor method to get the fields by name.
public Get ( String fieldName ) : Object
fieldName String Name of static field to retrieve
return Object