public class ConstantLookup extends Object implements Lookup
Looks up constant fields in classes.
Variable names passed in must be of the form mypackage.MyClass.FIELD
. The lookup()
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.
Constructor and Description |
---|
ConstantLookup() |
Modifier and Type | Method and Description |
---|---|
static void |
clear()
Clears the shared cache with the so far resolved constants.
|
protected Class<?> |
fetchClass(String className)
Loads the class with the specified name.
|
Object |
lookup(String var)
Looks up a variable.
|
protected Object |
resolveField(String className,
String fieldName)
Determines the value of the specified constant member field of a class.
|
public ConstantLookup()
public Object lookup(String var)
resolveField()
method and pass in the name of the class and the field.public static void clear()
protected Object resolveField(String className, String fieldName) throws Exception
fetchClass()
to obtain the java.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 accessible.className
- the name of the classfieldName
- the name of the member field of that class to readException
- if an error occursprotected Class<?> fetchClass(String className) throws ClassNotFoundException
getClass()
method of Commons Lang's
ClassUtils
.className
- the name of the class to be loadedClassNotFoundException
- if the class cannot be loadedCopyright © 2001–2022 The Apache Software Foundation. All rights reserved.