Top Description Fields Constructors Methods
org.apache.commons.text.lookup

pack-priv Class ConstantStringLookup

extends AbstractStringLookup
Class Inheritance
Imports
java.util.Objects, java.util.concurrent.ConcurrentHashMap, org.apache.commons.lang3.ClassUtils, org.apache.commons.text.StringSubstitutor

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. The lookup(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 the StringLookupFactory:

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.

Public access is through StringLookupFactory.

Since
1.5
See Also
StringLookupFactory

Field Summary

Modifier and TypeField and Description
private static final ConcurrentHashMap<String, String>
CONSTANT_CACHE

An internally used cache for already retrieved values.

private static final char
FIELD_SEPARATOR

Constant for the field separator.

pack-priv static final ConstantStringLookup
INSTANCE

Defines the singleton for this class.

Inherited from org.apache.commons.text.lookup.AbstractStringLookup:
SPLIT_CHSPLIT_STR

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv static void
clear()

Clears the shared cache with the so far resolved constants.

protected Class<?>

Returns:

The corresponding class object.
fetchClass
(final String
the name of the class to be loaded.
className
)

Loads the class with the specified name.

public synchronized String

Returns:

The value of this variable or null if it cannot be resolved.
lookup
(final String
the name of the variable to be resolved.
key
)

Implements org.apache.commons.text.lookup.StringLookup.lookup.

Tries to resolve the specified variable.

protected Object

Returns:

The field's value.
resolveField
(final String
the name of the class.
className
,
final String
the name of the member field of that class to read.
fieldName
)

Determines the value of the specified constant member field of a class.

Inherited from org.apache.commons.text.lookup.AbstractStringLookup:
substringAftersubstringAftersubstringAfterLasttoLookupKeytoLookupKey

Field Detail

CONSTANT_CACHEback to summary
private static final ConcurrentHashMap<String, String> CONSTANT_CACHE

An internally used cache for already retrieved values.

FIELD_SEPARATORback to summary
private static final char FIELD_SEPARATOR

Constant for the field separator.

INSTANCEback to summary
pack-priv static final ConstantStringLookup INSTANCE

Defines the singleton for this class.

Constructor Detail

ConstantStringLookupback to summary
pack-priv ConstantStringLookup()

Method Detail

clearback to summary
pack-priv static void clear()

Clears the shared cache with the so far resolved constants.

fetchClassback to summary
protected Class<?> fetchClass(final String className) throws 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 the getClass() method of Commons Lang's ClassUtils.

Parameters
className:String

the name of the class to be loaded.

Returns:Class<?>

The corresponding class object.

Exceptions
ClassNotFoundException:
if the class cannot be loaded.
lookupback to summary
public synchronized String lookup(final String key)

Implements org.apache.commons.text.lookup.StringLookup.lookup.

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 the resolveField() method and pass in the name of the class and the field.

Parameters
key:String

the name of the variable to be resolved.

Returns:String

The value of this variable or null if it cannot be resolved.

Annotations
@Override
resolveFieldback to summary
protected Object resolveField(final String className, final String fieldName) throws ReflectiveOperationException

Determines the value of the specified constant member field of a class. This implementation will call fetchClass() to obtain the 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 accessible.

Parameters
className:String

the name of the class.

fieldName:String

the name of the member field of that class to read.

Returns:Object

The field's value.

Exceptions
ReflectiveOperationException:
if an error occurs.