org.apache.commons.configuration2.interpol

## Class ConstantLookup

- java.lang.Object
- - org.apache.commons.configuration2.interpol.ConstantLookup

- All Implemented Interfaces:
    Lookup

      public class ConstantLookup
      extends Object
      implements Lookup

  A specialized lookup implementation that allows access to constant fields of classes.

  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 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.

  Since:
    1.4
  Version:
    $Id: ConstantLookup.java 1624601 2014-09-12 18:04:36Z oheger $
  Author:
    Commons Configuration team

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `ConstantLookup()`

  - ### Method Summary

    Methods
    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)` Tries to resolve the specified variable.
    `protected Object` | `resolveField(String className, String fieldName)` Determines the value of the specified constant member field of a class.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### ConstantLookup

          public ConstantLookup()

  - ### Method Detail

    - #### lookup

          public Object lookup(String var)

      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.
      Specified by:
        `lookup` in interface `Lookup`
      Parameters:
        `var` - the name of the variable to be resolved
      Returns:
        the value of this variable or null if it cannot be resolved

    - #### clear

          public static void clear()

      Clears the shared cache with the so far resolved constants.

    - #### resolveField

          protected Object resolveField(String className,
                            String fieldName)
                                 throws Exception

      Determines the value of the specified constant member field of a class. This implementation will call `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 accessable.
      Parameters:
        `className` - the name of the class
        `fieldName` - the name of the member field of that class to read
      Returns:
        the field's value
      Throws:
        `Exception` - if an error occurs

    - #### fetchClass

          protected Class<?> fetchClass(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` - the name of the class to be loaded
      Returns:
        the corresponding class object
      Throws:
        `ClassNotFoundException` - if the class cannot be loaded

Copyright © 2001–2014 The Apache Software Foundation. All rights reserved.