Class ConstantValueTag

  • All Implemented Interfaces:
    org.apache.commons.jelly.Tag

    public class ConstantValueTag
    extends org.apache.commons.jelly.TagSupport

    A specialized tag handler implementation for resolving values of constant fields.

    Tags implemented by this tag handler class can be used to obtain the values of constant fields, i.e. members of a class declared as public static final. This can be useful for instance, if the values of such constants are to be passed to constructors, method invocations, or be set as properties.

    This tag provides the same functionality as the <getStatic> tag from the Jelly core tag library. However, there are the following differences:

    • This tag supports specifying the class loader for loading the target class. It provides the typical facilities for specifying the class as supported by other tags of the dependency injection framework.
    • If the parent tag of this tag implements the ValueSupport interface, the resolved constant value is also passed to this tag. This makes it very easy to integrate the functionality provided by this implementation with other tags of the dependency injection framework.

    The following table lists all attributes supported by this tag handler implementation:

    Attribute Description Optional
    var Here the name of a variable can be specified under which the resolved constant value is stored in the Jelly context. This variable can later be accessed by other tags, so the constant value can be reused. Yes
    targetClass Defines the class from which the constant field is to be resolved. Either the class or the class name must be specified. Yes
    targetClassName Defines the name of the class from which the constant field is to be resolved. Either the class or the class name must be specified. Yes
    targetClassLoader With this attribute the symbolic name of the class loader can be specified that should be used for resolving the class name. A class loader with this name is retrieved from the current ClassLoaderProvider to load the class. This attribute is only evaluated if the targetClassName attribute is set. Yes
    field This attribute determines the field to be read from the target class. Here the name of an accessible static field must be provided. No

    If neither the var attribute is set nor the tag is nested inside a ValueSupport tag, an exception is thrown. The following example shows how this tag can be used to pass the value of the Integer.MAX_VALUE constant to a set property invocation:

     <di:setProperty property="number">
       <di:const targetClassName="java.lang.Integer" field="MAX_VALUE"/>
     </di:setProperty>
     

    Version:
    $Id: ConstantValueTag.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Field Summary

      • Fields inherited from class org.apache.commons.jelly.TagSupport

        body, context, hasTrimmed, parent, shouldTrim
    • Constructor Summary

      Constructors 
      Constructor Description
      ConstantValueTag()
      Creates a new instance of ConstantValueTag.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void doTag​(org.apache.commons.jelly.XMLOutput output)
      Executes this tag.
      String getField()
      Returns the name of the constant field that is to be retrieved by this tag.
      String getVar()
      Returns the name of the variable under which the resolved value is to be stored.
      protected Object resolveConstantValue​(Class<?> targetClass)
      Obtains the value of the constant field from the target class.
      void setField​(String field)
      Set method of the field attribute.
      void setTargetClass​(Class<?> cls)
      Set method of the targetClass attribute.
      void setTargetClassLoader​(String loader)
      Set method of the targetClassLoader attribute.
      void setTargetClassName​(String clsName)
      Set method of the targetClassName attribute.
      void setVar​(String var)
      Set method of the var attribute.
      • Methods inherited from class org.apache.commons.jelly.TagSupport

        findAncestorWithClass, findAncestorWithClass, findAncestorWithClass, findAncestorWithClass, findAncestorWithClass, findAncestorWithClass, getBody, getBodyText, getBodyText, getContext, getParent, invokeBody, isEscapeText, isTrim, setBody, setContext, setEscapeText, setParent, setTrim, trimBody
    • Constructor Detail

      • ConstantValueTag

        public ConstantValueTag()
        Creates a new instance of ConstantValueTag.
    • Method Detail

      • getVar

        public String getVar()
        Returns the name of the variable under which the resolved value is to be stored.
        Returns:
        the variable name
      • setVar

        public void setVar​(String var)
        Set method of the var attribute.
        Parameters:
        var - the attribute's value
      • getField

        public String getField()
        Returns the name of the constant field that is to be retrieved by this tag.
        Returns:
        the name of the field
      • setField

        public void setField​(String field)
        Set method of the field attribute.
        Parameters:
        field - the attribute's value
      • setTargetClass

        public void setTargetClass​(Class<?> cls)
        Set method of the targetClass attribute.
        Parameters:
        cls - the attribute's value
      • setTargetClassName

        public void setTargetClassName​(String clsName)
        Set method of the targetClassName attribute.
        Parameters:
        clsName - the attribute's value
      • setTargetClassLoader

        public void setTargetClassLoader​(String loader)
        Set method of the targetClassLoader attribute.
        Parameters:
        loader - the attribute's value
      • doTag

        public void doTag​(org.apache.commons.jelly.XMLOutput output)
                   throws org.apache.commons.jelly.JellyTagException
        Executes this tag. Delegates to the helper methods to check the parameters, fetch the constant value and passing it to the parent tag or storing it in a variable.
        Parameters:
        output - the output object
        Throws:
        org.apache.commons.jelly.JellyTagException - if an error occurs
      • resolveConstantValue

        protected Object resolveConstantValue​(Class<?> targetClass)
                                       throws org.apache.commons.jelly.JellyTagException
        Obtains the value of the constant field from the target class. This method is called by doTag(XMLOutput). When it is called it has already been checked whether all mandatory attributes are present. This implementation reads the value of the constant field using reflection.
        Parameters:
        targetClass - the target class
        Returns:
        the value of the constant field
        Throws:
        org.apache.commons.jelly.JellyTagException - if an error occurs