Top Description Inners Fields Constructors Methods
org.springframework.core

public Class Constants

extends Object
Class Inheritance
Annotations
@Deprecated
since:6.1
Imports
java.lang.reflect.Field, java.util.HashMap, .HashSet, .Locale, .Map, .Set, org.springframework.lang.Nullable, org.springframework.util.Assert, .ReflectionUtils

Deprecated

since 6.1.

since 6.1 with no replacement; use an enum, map, or similar custom solution instead

This class can be used to parse other classes containing constant definitions in public static final members. The asXXXX methods of this class allow these constant values to be accessed via their string names.

Consider class Foo containing public static final int CONSTANT1 = 66; An instance of this class wrapping Foo.class will return the constant value of 66 from its asNumber method given the argument "CONSTANT1".

This class is ideal for use in PropertyEditors, enabling them to recognize the same names as the constants themselves, and freeing them from maintaining their own mapping.

Authors
Rod Johnson, Juergen Hoeller
Since
16.03.2003

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
Constants.ConstantException

Exception thrown when the Constants class is asked for an invalid constant name.

Field Summary

Modifier and TypeField and Description
private final String
className

The name of the introspected class.

private final Map<String, Object>
fieldCache

Map from String field name to object value.

Constructor Summary

AccessConstructor and Description
public
Constants(Class<?>
the class to analyze
clazz
)

Create a new Constants converter class wrapping the given class.

Method Summary

Modifier and TypeMethod and Description
public Number

Returns:

the Number value
asNumber
(String
the name of the field (never null)
code
)

Return a constant value cast to a Number.

public Object

Returns:

the Object value
asObject
(String
the name of the field (never null)
code
)

Parse the given String (upper or lower case accepted) and return the appropriate value if it's the name of a constant field in the class that we're analyzing.

public String

Returns:

the String value Works even if it's not a string (invokes toString()).
asString
(String
the name of the field (never null)
code
)

Return a constant value as a String.

public final String
getClassName()

Return the name of the analyzed class.

protected final Map<String, Object>
getFieldCache()

Exposes the field cache to subclasses: a Map from String field name to object value.

public Set<String>

Returns:

the set of constant names
getNames
(String
prefix of the constant names to search (may be null)
namePrefix
)

Return all names of the given group of constants.

public Set<String>

Returns:

the set of values
getNamesForProperty
(String
the name of the bean property
propertyName
)

Return all names of the group of constants for the given bean property name.

public Set<String>

Returns:

the set of constant names
getNamesForSuffix
(String
suffix of the constant names to search (may be null)
nameSuffix
)

Return all names of the given group of constants.

public final int
getSize()

Return the number of constants exposed.

public Set<Object>

Returns:

the set of values
getValues
(String
prefix of the constant names to search (may be null)
namePrefix
)

Return all values of the given group of constants.

public Set<Object>

Returns:

the set of values
getValuesForProperty
(String
the name of the bean property
propertyName
)

Return all values of the group of constants for the given bean property name.

public Set<Object>

Returns:

the set of values
getValuesForSuffix
(String
suffix of the constant names to search (may be null)
nameSuffix
)

Return all values of the given group of constants.

public String

Returns:

the corresponding constant name prefix
propertyToConstantNamePrefix
(String
the name of the bean property
propertyName
)

Convert the given bean property name to a constant name prefix.

public String

Returns:

the name of the constant field
toCode
(Object
constant value to look up
value
,
String
prefix of the constant names to search (may be null)
namePrefix
)

Look up the given value within the given group of constants.

public String

Returns:

the name of the constant field
toCodeForProperty
(Object
constant value to look up
value
,
String
the name of the bean property
propertyName
)

Look up the given value within the group of constants for the given bean property name.

public String

Returns:

the name of the constant field
toCodeForSuffix
(Object
constant value to look up
value
,
String
suffix of the constant names to search (may be null)
nameSuffix
)

Look up the given value within the given group of constants.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

classNameback to summary
private final String className

The name of the introspected class.

fieldCacheback to summary
private final Map<String, Object> fieldCache

Map from String field name to object value.

Constructor Detail

Constantsback to summary
public Constants(Class<?> clazz)

Create a new Constants converter class wrapping the given class.

All public static final variables will be exposed, whatever their type.

Parameters
clazz:Class<?>

the class to analyze

Exceptions
IllegalArgumentException:
if the supplied clazz is null

Method Detail

asNumberback to summary
public Number asNumber(String code) throws ConstantException

Return a constant value cast to a Number.

Parameters
code:String

the name of the field (never null)

Returns:Number

the Number value

Exceptions
ConstantException:
if the field name wasn't found or if the type wasn't compatible with Number
See Also
asObject
asObjectback to summary
public Object asObject(String code) throws ConstantException

Parse the given String (upper or lower case accepted) and return the appropriate value if it's the name of a constant field in the class that we're analyzing.

Parameters
code:String

the name of the field (never null)

Returns:Object

the Object value

Exceptions
ConstantException:
if there's no such field
asStringback to summary
public String asString(String code) throws ConstantException

Return a constant value as a String.

Parameters
code:String

the name of the field (never null)

Returns:String

the String value Works even if it's not a string (invokes toString()).

Exceptions
ConstantException:
if the field name wasn't found
See Also
asObject
getClassNameback to summary
public final String getClassName()

Return the name of the analyzed class.

getFieldCacheback to summary
protected final Map<String, Object> getFieldCache()

Exposes the field cache to subclasses: a Map from String field name to object value.

getNamesback to summary
public Set<String> getNames(String namePrefix)

Return all names of the given group of constants.

Note that this method assumes that constants are named in accordance with the standard Java convention for constant values (i.e. all uppercase). The supplied namePrefix will be uppercased (in a locale-insensitive fashion) prior to the main logic of this method kicking in.

Parameters
namePrefix:String

prefix of the constant names to search (may be null)

Returns:Set<String>

the set of constant names

getNamesForPropertyback to summary
public Set<String> getNamesForProperty(String propertyName)

Return all names of the group of constants for the given bean property name.

Parameters
propertyName:String

the name of the bean property

Returns:Set<String>

the set of values

See Also
propertyToConstantNamePrefix
getNamesForSuffixback to summary
public Set<String> getNamesForSuffix(String nameSuffix)

Return all names of the given group of constants.

Note that this method assumes that constants are named in accordance with the standard Java convention for constant values (i.e. all uppercase). The supplied nameSuffix will be uppercased (in a locale-insensitive fashion) prior to the main logic of this method kicking in.

Parameters
nameSuffix:String

suffix of the constant names to search (may be null)

Returns:Set<String>

the set of constant names

getSizeback to summary
public final int getSize()

Return the number of constants exposed.

getValuesback to summary
public Set<Object> getValues(String namePrefix)

Return all values of the given group of constants.

Note that this method assumes that constants are named in accordance with the standard Java convention for constant values (i.e. all uppercase). The supplied namePrefix will be uppercased (in a locale-insensitive fashion) prior to the main logic of this method kicking in.

Parameters
namePrefix:String

prefix of the constant names to search (may be null)

Returns:Set<Object>

the set of values

getValuesForPropertyback to summary
public Set<Object> getValuesForProperty(String propertyName)

Return all values of the group of constants for the given bean property name.

Parameters
propertyName:String

the name of the bean property

Returns:Set<Object>

the set of values

See Also
propertyToConstantNamePrefix
getValuesForSuffixback to summary
public Set<Object> getValuesForSuffix(String nameSuffix)

Return all values of the given group of constants.

Note that this method assumes that constants are named in accordance with the standard Java convention for constant values (i.e. all uppercase). The supplied nameSuffix will be uppercased (in a locale-insensitive fashion) prior to the main logic of this method kicking in.

Parameters
nameSuffix:String

suffix of the constant names to search (may be null)

Returns:Set<Object>

the set of values

propertyToConstantNamePrefixback to summary
public String propertyToConstantNamePrefix(String propertyName)

Convert the given bean property name to a constant name prefix.

Uses a common naming idiom: turning all lower case characters to upper case, and prepending upper case characters with an underscore.

Example: "imageSize" → "IMAGE_SIZE"
Example: "imagesize" → "IMAGESIZE".
Example: "ImageSize" → "_IMAGE_SIZE".
Example: "IMAGESIZE" → "_I_M_A_G_E_S_I_Z_E"

Parameters
propertyName:String

the name of the bean property

Returns:String

the corresponding constant name prefix

See Also
getValuesForProperty, toCodeForProperty
toCodeback to summary
public String toCode(Object value, String namePrefix) throws ConstantException

Look up the given value within the given group of constants.

Will return the first match.

Parameters
value:Object

constant value to look up

namePrefix:String

prefix of the constant names to search (may be null)

Returns:String

the name of the constant field

Exceptions
ConstantException:
if the value wasn't found
toCodeForPropertyback to summary
public String toCodeForProperty(Object value, String propertyName) throws ConstantException

Look up the given value within the group of constants for the given bean property name. Will return the first match.

Parameters
value:Object

constant value to look up

propertyName:String

the name of the bean property

Returns:String

the name of the constant field

Exceptions
ConstantException:
if the value wasn't found
See Also
propertyToConstantNamePrefix
toCodeForSuffixback to summary
public String toCodeForSuffix(Object value, String nameSuffix) throws ConstantException

Look up the given value within the given group of constants.

Will return the first match.

Parameters
value:Object

constant value to look up

nameSuffix:String

suffix of the constant names to search (may be null)

Returns:String

the name of the constant field

Exceptions
ConstantException:
if the value wasn't found
org.springframework.core back to summary

public Class Constants.ConstantException

extends IllegalArgumentException
Class Inheritance
Annotations
@SuppressWarnings:serial

Exception thrown when the Constants class is asked for an invalid constant name.

Constructor Summary

AccessConstructor and Description
public
ConstantException(String
name of the class containing the constant definitions
className
,
String
invalid constant name
field
,
String
description of the problem
message
)

Thrown when an invalid constant name is requested.

public
ConstantException(String
name of the class containing the constant definitions
className
,
String
prefix of the searched constant names
namePrefix
,
Object
the looked up constant value
value
)

Thrown when an invalid constant value is looked up.

Constructor Detail

ConstantExceptionback to summary
public ConstantException(String className, String field, String message)

Thrown when an invalid constant name is requested.

Parameters
className:String

name of the class containing the constant definitions

field:String

invalid constant name

message:String

description of the problem

ConstantExceptionback to summary
public ConstantException(String className, String namePrefix, Object value)

Thrown when an invalid constant value is looked up.

Parameters
className:String

name of the class containing the constant definitions

namePrefix:String

prefix of the searched constant names

value:Object

the looked up constant value