类 Constants
asXXXX methods of this class
allow these constant values to be accessed via their string names.
Consider class Foo containing public final static 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.
- 作者:
- Rod Johnson, Juergen Hoeller
-
嵌套类概要
嵌套类修饰符和类型类说明static classException thrown when theConstantsclass is asked for an invalid constant name. -
字段概要
字段 -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明Return a constant value cast to a Number.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 analysing.Return a constant value as a String.final StringReturn the name of the analyzed class.Exposes the field cache to subclasses: a Map from String field name to object value.Return all names of the given group of constants.getNamesForProperty(String propertyName) Return all names of the group of constants for the given bean property name.getNamesForSuffix(String nameSuffix) Return all names of the given group of constants.private static StringgetPrefixToUse(String namePrefix) final intgetSize()Return the number of constants exposed.Return all values of the given group of constants.getValuesForProperty(String propertyName) Return all values of the group of constants for the given bean property name.getValuesForSuffix(String nameSuffix) Return all values of the given group of constants.propertyToConstantNamePrefix(String propertyName) Convert the given bean property name to a constant name prefix.Look up the given value within the given group of constants.toCodeForProperty(Object value, String propertyName) Look up the given value within the group of constants for the given bean property name.toCodeForSuffix(Object value, String nameSuffix) Look up the given value within the given group of constants.
-
字段详细资料
-
className
The name of the introspected class -
fieldCache
Map from String field name to object value
-
-
构造器详细资料
-
Constants
Create a new Constants converter class wrapping the given class.All public static final variables will be exposed, whatever their type.
- 参数:
clazz- the class to analyze- 抛出:
IllegalArgumentException- if the suppliedclazzisnull
-
-
方法详细资料
-
getClassName
Return the name of the analyzed class. -
getSize
public final int getSize()Return the number of constants exposed. -
getFieldCache
Exposes the field cache to subclasses: a Map from String field name to object value. -
asNumber
Return a constant value cast to a Number.- 参数:
code- the name of the field (nevernull)- 返回:
- the Number value
- 抛出:
Constants.ConstantException- if the field name wasn't found or if the type wasn't compatible with Number- 另请参阅:
-
asString
Return a constant value as a String.- 参数:
code- the name of the field (nevernull)- 返回:
- the String value Works even if it's not a string (invokes
toString()). - 抛出:
Constants.ConstantException- if the field name wasn't found- 另请参阅:
-
asObject
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 analysing.- 参数:
code- the name of the field (nevernull)- 返回:
- the Object value
- 抛出:
Constants.ConstantException- if there's no such field
-
getNames
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
namePrefixwill be uppercased (in a locale-insensitive fashion) prior to the main logic of this method kicking in.- 参数:
namePrefix- prefix of the constant names to search (may benull)- 返回:
- the set of constant names
-
getNamesForProperty
Return all names of the group of constants for the given bean property name.- 参数:
propertyName- the name of the bean property- 返回:
- the set of values
- 另请参阅:
-
getNamesForSuffix
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
nameSuffixwill be uppercased (in a locale-insensitive fashion) prior to the main logic of this method kicking in.- 参数:
nameSuffix- suffix of the constant names to search (may benull)- 返回:
- the set of constant names
-
getValues
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
namePrefixwill be uppercased (in a locale-insensitive fashion) prior to the main logic of this method kicking in.- 参数:
namePrefix- prefix of the constant names to search (may benull)- 返回:
- the set of values
-
getValuesForProperty
Return all values of the group of constants for the given bean property name.- 参数:
propertyName- the name of the bean property- 返回:
- the set of values
- 另请参阅:
-
getValuesForSuffix
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
nameSuffixwill be uppercased (in a locale-insensitive fashion) prior to the main logic of this method kicking in.- 参数:
nameSuffix- suffix of the constant names to search (may benull)- 返回:
- the set of values
-
toCode
Look up the given value within the given group of constants.Will return the first match.
- 参数:
value- constant value to look upnamePrefix- prefix of the constant names to search (may benull)- 返回:
- the name of the constant field
- 抛出:
Constants.ConstantException- if the value wasn't found
-
getPrefixToUse
-
toCodeForProperty
public String toCodeForProperty(Object value, String propertyName) throws Constants.ConstantException Look up the given value within the group of constants for the given bean property name. Will return the first match.- 参数:
value- constant value to look uppropertyName- the name of the bean property- 返回:
- the name of the constant field
- 抛出:
Constants.ConstantException- if the value wasn't found- 另请参阅:
-
toCodeForSuffix
Look up the given value within the given group of constants.Will return the first match.
- 参数:
value- constant value to look upnameSuffix- suffix of the constant names to search (may benull)- 返回:
- the name of the constant field
- 抛出:
Constants.ConstantException- if the value wasn't found
-
propertyToConstantNamePrefix
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"- 参数:
propertyName- the name of the bean property- 返回:
- the corresponding constant name prefix
- 另请参阅:
-