@DefaultKey(value="field") @ValidScope(value="application") public class FieldTool extends SafeConfig
This is a simple tools class to allow easy access to static fields in a class, such as string constants from within a template. Velocity will not introspect for class fields (and won't in the future :), but writing setter/getter methods to do this is a pain, so use this if you really have to access fields.
Example uses in a template:
## here we access a constant in a class include in the configuration $field.COUNTER_NAME ## here we dynamically lookup a class' fields to find another constant $field.in("org.com.SomeClass").ANOTHER_CONSTANT ## here we pass an object instance in (an Integer in this case) and ## retrieve a static constant from that instance's class $field.in(0).MIN_VALUE ## by default, once we've searched a class' fields, those fields stay ## available in the tool (change this by storeDynamicLookups="false") ## so here we get another constant from the Integer class $field.MAX_VALUE
Example tools.xml config:
<tools> <toolbox scope="application"> <tool class="org.apache.velocity.tools.generic.FieldTool" include="org.apache.velocity.runtime.RuntimeConstants,com.org.MyConstants"/> </toolbox> </tools>
Right now, this tool only gives access to public static
fields.
It seems that anything else is too dangerous. This is for convenient access
to 'constants'. If you have fields that aren't static
,
handle them by explicitly placing them into the context or writing a getter
method.
Modifier and Type | Class and Description |
---|---|
static class |
FieldTool.FieldToolSub
Holds a
Map of results for a particular class. |
class |
FieldTool.MutableField
|
Modifier and Type | Field and Description |
---|---|
static String |
INCLUDE_KEY
The key used for specifying which classes should be inspected
for public static methods to be made available.
|
protected HashMap |
storage |
static String |
STORE_DYNAMIC_KEY
The key used for specifying whether or not the tool should store
fields in classes dynamically looked up from within a template.
|
protected boolean |
storeDynamicLookups |
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY
Constructor and Description |
---|
FieldTool() |
Modifier and Type | Method and Description |
---|---|
protected void |
configure(ValueParser values)
Does the actual configuration.
|
Object |
get(String name)
Returns the value for the specified field name as found
in the stored
Map of field names to values (or placeholders). |
FieldTool.FieldToolSub |
in(Class clazz)
Returns a
FieldTool.FieldToolSub holding a Map
of all the public static field names to values (or a placeholder
if the value is not final) for the specified Class . |
FieldTool.FieldToolSub |
in(Object instance)
Returns a
FieldTool.FieldToolSub holding a Map
of all the public static field names to values (or a placeholder
if the value is not final) for the Class of the
specified Object. |
FieldTool.FieldToolSub |
in(String classname)
Returns a
FieldTool.FieldToolSub holding a Map
of all the public static field names to values (or a placeholder
if the value is not final) for the specified class(name). |
protected Map<String,Object> |
inspect(Class clazz)
Looks for all public, static fields in the specified class and
stores their value (if final) or else a
FieldTool.MutableField for
in a Map under the fields' names. |
protected Object |
retrieve(Field field,
Class clazz)
|
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
public static final String INCLUDE_KEY
public static final String STORE_DYNAMIC_KEY
protected HashMap storage
protected boolean storeDynamicLookups
protected void configure(ValueParser values)
SafeConfig
configure
in class SafeConfig
values
- configuration valuespublic Object get(String name)
Map
of field names to values (or placeholders).
Returns null
if there is no matching field.name
- field namepublic FieldTool.FieldToolSub in(String classname)
FieldTool.FieldToolSub
holding a Map
of all the public static field names to values (or a placeholder
if the value is not final) for the specified class(name). If the
Class
with the specified name cannot be loaded, this will
return null
, rather than throw an exception.classname
- target class nameFieldTool.FieldToolSub
objectin(Class clazz)
public FieldTool.FieldToolSub in(Object instance)
FieldTool.FieldToolSub
holding a Map
of all the public static field names to values (or a placeholder
if the value is not final) for the Class
of the
specified Object.instance
- target instanceFieldTool.FieldToolSub
objectin(Class clazz)
public FieldTool.FieldToolSub in(Class clazz)
FieldTool.FieldToolSub
holding a Map
of all the public static field names to values (or a placeholder
if the value is not final) for the specified Class
.clazz
- target classFieldTool.FieldToolSub
objectprotected Map<String,Object> inspect(Class clazz)
FieldTool.MutableField
for
in a Map
under the fields' names. This will never return
null, only an empty Map if there are no public static fields.clazz
- target classCopyright © 2002–2018 The Apache Software Foundation. All rights reserved.