FieldMethodizer (Velocity 1.4 API)
Keywords: org.apache.velocity.app.FieldMethodizer class

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.apache.velocity.app
Class FieldMethodizer

    java.lang.Object
      org.apache.velocity.app.FieldMethodizer

public class FieldMethodizer
extends java.lang.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

>       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.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 $
Author:
  Geir Magnusson Jr.

Field Summary
`private java.util.HashMap` | `classHash` Hold the class objects by field name
`private java.util.HashMap` | `fieldHash` Hold the field objects by field name

Constructor Summary
`FieldMethodizer()` Allow object to be initialized without any data.
`FieldMethodizer(java.lang.Object o)` Constructor that takes as it's arg a living object to methodize.
`FieldMethodizer(java.lang.String s)` Constructor that takes as it's arg the name of the class to methodize.

Method Summary
`void` | `addObject(java.lang.Object o)` Add an Object to methodize
`void` | `addObject(java.lang.String s)` Add the Name of the class to methodize
`java.lang.Object` | `get(java.lang.String fieldName)` Accessor method to get the fields by name.
`private void` | `inspect(java.lang.Class clas)` Method that retrieves all public static fields in the class we are methodizing.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### fieldHash

    private java.util.HashMap fieldHash

  Hold the field objects by field name

### classHash

    private java.util.HashMap classHash

  Hold the class objects by field name

Constructor Detail

### FieldMethodizer

    public FieldMethodizer()

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

### FieldMethodizer

    public FieldMethodizer(java.lang.String s)

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

Parameters:
  `s` - Name of class to methodize.

### FieldMethodizer

    public FieldMethodizer(java.lang.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.

Method Detail

### addObject

    public void addObject(java.lang.String s)
                   throws java.lang.Exception

  Add the Name of the class to methodize

  Throws:
    `java.lang.Exception`

### addObject

    public void addObject(java.lang.Object o)
                   throws java.lang.Exception

  Add an Object to methodize

  Throws:
    `java.lang.Exception`

### get

    public java.lang.Object get(java.lang.String fieldName)

  Accessor method to get the fields by name.

  Parameters:
    `fieldName` - Name of static field to retrieve
  Returns:
    The value of the given field.

### inspect

    private void inspect(java.lang.Class clas)

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

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2002 Apache Software Foundation. All Rights Reserved.