Class ConstantStringLookup
- java.lang.Object
-
- org.apache.commons.text.lookup.AbstractStringLookup
-
- org.apache.commons.text.lookup.ConstantStringLookup
-
- All Implemented Interfaces:
StringLookup
class ConstantStringLookup extends AbstractStringLookup
Looks up the value of a fully-qualified static final value.
Sometimes it is necessary in a configuration file to refer to a constant defined in a class. This can be done with this lookup implementation. Variable names must be in the format
apackage.AClass.AFIELD
. Thelookup(String)
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.
Using a
StringLookup
from theStringLookupFactory
:StringLookupFactory.INSTANCE.constantStringLookup().lookup("java.awt.event.KeyEvent.VK_ESCAPE");
Using a
StringSubstitutor
:StringSubstitutor.createInterpolator().replace("... ${const:java.awt.event.KeyEvent.VK_ESCAPE} ..."));
The above examples convert
java.awt.event.KeyEvent.VK_ESCAPE
to"27"
.This class was adapted from Apache Commons Configuration.
- Since:
- 1.5
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.concurrent.ConcurrentHashMap<java.lang.String,java.lang.String>
CONSTANT_CACHE
An internally used cache for already retrieved values.private static char
FIELD_SEPARATOR
Constant for the field separator.(package private) static ConstantStringLookup
INSTANCE
Defines the singleton for this class.-
Fields inherited from class org.apache.commons.text.lookup.AbstractStringLookup
SPLIT_CH, SPLIT_STR
-
-
Constructor Summary
Constructors Constructor Description ConstantStringLookup()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) 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.String
lookup(java.lang.String key)
Tries to resolve the specified 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.-
Methods inherited from class org.apache.commons.text.lookup.AbstractStringLookup
substringAfter, substringAfter, substringAfterLast, toLookupKey, toLookupKey
-
-
-
-
Field Detail
-
CONSTANT_CACHE
private static final java.util.concurrent.ConcurrentHashMap<java.lang.String,java.lang.String> CONSTANT_CACHE
An internally used cache for already retrieved values.
-
FIELD_SEPARATOR
private static final char FIELD_SEPARATOR
Constant for the field separator.- See Also:
- Constant Field Values
-
INSTANCE
static final ConstantStringLookup INSTANCE
Defines the singleton for this class.
-
-
Method Detail
-
clear
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.String lookup(java.lang.String key)
Tries to resolve the specified 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.- Parameters:
key
- the name of the variable to be resolved- Returns:
- The value of this variable or null if it cannot be resolved
-
resolveField
protected java.lang.Object resolveField(java.lang.String className, java.lang.String fieldName) throws java.lang.ReflectiveOperationException
Determines the value of the specified constant member field of a class. This implementation will callfetchClass()
to obtain thejava.lang.Class
object for the target class. Then it will use reflection to obtain the field's value. For this to work the field must be accessable.- 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.ReflectiveOperationException
- if an error occurs
-
-