类 Constants
- java.lang.Object
- org.springframework.core.Constants
public class Constants extends Object
This class can be used to parse other classes containing constant definitions in public static final members. TheasXXXX
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 wrappingFoo.class
will return the constant value of 66 from itsasNumber
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.
- 从以下版本开始:
- 16.03.2003
- 作者:
- Rod Johnson, Juergen Hoeller
嵌套类概要
嵌套类 修饰符和类型 类 说明 static class
Constants.ConstantException
Exception thrown when theConstants
class is asked for an invalid constant name.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 Number
asNumber(String code)
Return a constant value cast to a Number.Object
asObject(String 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 analysing.String
asString(String code)
Return a constant value as a String.String
getClassName()
Return the name of the analyzed class.protected Map<String,Object>
getFieldCache()
Exposes the field cache to subclasses: a Map from String field name to object value.Set<String>
getNames(String namePrefix)
Return all names of the given group of constants.Set<String>
getNamesForProperty(String propertyName)
Return all names of the group of constants for the given bean property name.Set<String>
getNamesForSuffix(String nameSuffix)
Return all names of the given group of constants.int
getSize()
Return the number of constants exposed.Set<Object>
getValues(String namePrefix)
Return all values of the given group of constants.Set<Object>
getValuesForProperty(String propertyName)
Return all values of the group of constants for the given bean property name.Set<Object>
getValuesForSuffix(String nameSuffix)
Return all values of the given group of constants.String
propertyToConstantNamePrefix(String propertyName)
Convert the given bean property name to a constant name prefix.String
toCode(Object value, String namePrefix)
Look up the given value within the given group of constants.String
toCodeForProperty(Object value, String propertyName)
Look up the given value within the group of constants for the given bean property name.String
toCodeForSuffix(Object value, String nameSuffix)
Look up the given value within the given group of constants.
构造器详细资料
Constants
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.
- 参数:
clazz
- the class to analyze- 抛出:
IllegalArgumentException
- if the suppliedclazz
isnull
方法详细资料
getClassName
public final String getClassName()
Return the name of the analyzed class.
getSize
public final int getSize()
Return the number of constants exposed.
getFieldCache
protected final Map<String,Object> getFieldCache()
Exposes the field cache to subclasses: a Map from String field name to object value.
asNumber
public Number asNumber(String code) throws Constants.ConstantException
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- 另请参阅:
asObject(java.lang.String)
asString
public String asString(String code) throws Constants.ConstantException
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(java.lang.String)
asObject
public Object asObject(String code) throws Constants.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 analysing.- 参数:
code
- the name of the field (nevernull
)- 返回:
- the Object value
- 抛出:
Constants.ConstantException
- if there's no such field
getNames
public Set<String> getNames(@Nullable 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.- 参数:
namePrefix
- prefix of the constant names to search (may benull
)- 返回:
- the set of constant names
getNamesForProperty
public Set<String> getNamesForProperty(String propertyName)
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
- 另请参阅:
propertyToConstantNamePrefix(java.lang.String)
getNamesForSuffix
public Set<String> getNamesForSuffix(@Nullable 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.- 参数:
nameSuffix
- suffix of the constant names to search (may benull
)- 返回:
- the set of constant names
getValues
public Set<Object> getValues(@Nullable 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.- 参数:
namePrefix
- prefix of the constant names to search (may benull
)- 返回:
- the set of values
getValuesForProperty
public Set<Object> getValuesForProperty(String propertyName)
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
- 另请参阅:
propertyToConstantNamePrefix(java.lang.String)
getValuesForSuffix
public Set<Object> getValuesForSuffix(@Nullable 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.- 参数:
nameSuffix
- suffix of the constant names to search (may benull
)- 返回:
- the set of values
toCode
public String toCode(Object value, @Nullable String namePrefix) throws Constants.ConstantException
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
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- 另请参阅:
propertyToConstantNamePrefix(java.lang.String)
toCodeForSuffix
public String toCodeForSuffix(Object value, @Nullable String nameSuffix) throws Constants.ConstantException
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
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"- 参数:
propertyName
- the name of the bean property- 返回:
- the corresponding constant name prefix
- 另请参阅:
getValuesForProperty(java.lang.String)
,toCodeForProperty(java.lang.Object, java.lang.String)