Class ConstantLookup
- java.lang.Object
-
- org.apache.commons.configuration2.interpol.ConstantLookup
-
- All Implemented Interfaces:
Lookup
public class ConstantLookup extends java.lang.Object implements Lookup
Looks up constant fields in classes.
Variable names passed in must be of the form
mypackage.MyClass.FIELD
. Thelookup()
method will split the passed in string at the last dot, separating the fully qualified class name and the name of the constant (i.e. static final) member field. Then the class is loaded and the field's value is obtained using reflection.Once retrieved values are cached for fast access. This class is thread-safe. It can be used as a standard (i.e. global) lookup object and serve multiple clients concurrently.
- Since:
- 1.4
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Map<java.lang.String,java.lang.Object>
CACHE
Cache of field values.private static char
FIELD_SEPRATOR
Constant for the field separator.private org.apache.commons.logging.Log
log
The logger.
-
Constructor Summary
Constructors Constructor Description ConstantLookup()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
clear()
Clears the shared cache with the so far resolved constants.protected java.lang.Class<?>
fetchClass(java.lang.String className)
Loads the class with the specified name.java.lang.Object
lookup(java.lang.String var)
Looks up a variable.protected java.lang.Object
resolveField(java.lang.String className, java.lang.String fieldName)
Determines the value of the specified constant member field of a class.
-
-
-
Field Detail
-
FIELD_SEPRATOR
private static final char FIELD_SEPRATOR
Constant for the field separator.- See Also:
- Constant Field Values
-
CACHE
private static final java.util.Map<java.lang.String,java.lang.Object> CACHE
Cache of field values.
-
log
private final org.apache.commons.logging.Log log
The logger.
-
-
Method Detail
-
clear
public static void clear()
Clears the shared cache with the so far resolved constants.
-
fetchClass
protected java.lang.Class<?> fetchClass(java.lang.String className) throws java.lang.ClassNotFoundException
Loads the class with the specified name. If an application has special needs regarding the class loaders to be used, it can hook in here. This implementation delegates to thegetClass()
method of Commons Lang'sClassUtils
.- Parameters:
className
- the name of the class to be loaded- Returns:
- the corresponding class object
- Throws:
java.lang.ClassNotFoundException
- if the class cannot be loaded
-
lookup
public java.lang.Object lookup(java.lang.String var)
Looks up a variable. The passed in variable name is interpreted as the name of a static final member field of a class. If the value has already been obtained, it can be retrieved from an internal cache. Otherwise this method will invoke theresolveField()
method and pass in the name of the class and the field.
-
resolveField
protected java.lang.Object resolveField(java.lang.String className, java.lang.String fieldName) throws java.lang.Exception
Determines the value of the specified constant member field of a class. This implementation will callfetchClass()
to obtain theClass
object for the target class. Then it will use reflection to obtain the field's value. For this to work the field must be accessible.- Parameters:
className
- the name of the classfieldName
- the name of the member field of that class to read- Returns:
- the field's value
- Throws:
java.lang.Exception
- if an error occurs
-
-