com.emarsys.ecommon.prefs.config.declaration
Class ClassConfigurationDeclaration

java.lang.Object
  extended by com.emarsys.ecommon.prefs.config.declaration.GenericConfigurationDeclaration
      extended by com.emarsys.ecommon.prefs.config.declaration.ClassConfigurationDeclaration
All Implemented Interfaces:
ConfigurationDeclaration

public class ClassConfigurationDeclaration
extends GenericConfigurationDeclaration

A ClassConfigurationDeclaration represents a ConfigurationDeclaration that is specified through public static final String fields of a Class. Thus it forms some kind of an in-code configuration declaration.

A ClassConfigurationDeclaration can be created out of any Object whose Class is annotated with DeclareConfiguration.
NOTE that every String constant of such a Class that is not excluded through the String[] Field specified by DeclareConfiguration.excludedNames() will be treated as a potential ISetting name, default value or fallback declaration.

Author:
Michael "kULO" Kulovits

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.emarsys.ecommon.prefs.config.ConfigurationDeclaration
ConfigurationDeclaration.DeclarationType
 
Field Summary
protected  DeclareConfiguration declarationAnnotation
           
protected  java.lang.Class<?> declarationClass
           
protected  java.lang.Object declarationInstance
           
protected  java.util.List<java.lang.String> excludedNames
           
 
Fields inherited from class com.emarsys.ecommon.prefs.config.declaration.GenericConfigurationDeclaration
defaults, fallbacks, name, settingNames, type
 
Constructor Summary
ClassConfigurationDeclaration(java.lang.Object declarationInstance)
           Creates a new ClassConfigurationDeclaration out of the passed "declaration object".
 
Method Summary
static boolean declaresConfiguration(java.lang.Class<?> clazz)
          Checks whether or not the passed Class declares a Configuration by checking if an DeclareConfiguration Annotation is present.
protected  ConfigurationDeclaration.DeclarationType getDeclarationType(java.lang.reflect.Field field)
           Determines the DeclarationType of the passed Field.
protected  java.util.List<java.lang.String> getExcludedNames()
           
protected  java.lang.Object getFieldValue(java.lang.String fieldName)
          Returns the value of the declarationInstance's Field with the passed fieldName.
protected  java.lang.String[] getSurrogateeNameAndValue(java.lang.reflect.Field surrogateField, java.lang.String suffix)
          Gets the name and value for the surrogate field (either a fallback setting or default value) passed.
protected  void parse()
          Will set default values for all names specified as public static final String constants in declarationClass whoose names are not excluded through DeclareConfiguration.excludedNames() and end with DeclareConfiguration.defaultSuffix().
protected  void setSettingName(java.lang.String fieldName)
          Adds the value of the Field of declarationClass that's referenced by the passed fieldName as a setting name.
 
Methods inherited from class com.emarsys.ecommon.prefs.config.declaration.GenericConfigurationDeclaration
getDefaults, getFallbacks, getName, getSettingNames, getType, setDefaultValue, setFallback, setName, setType
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

declarationInstance

protected final java.lang.Object declarationInstance

declarationClass

protected final java.lang.Class<?> declarationClass

declarationAnnotation

protected final DeclareConfiguration declarationAnnotation

excludedNames

protected java.util.List<java.lang.String> excludedNames
Constructor Detail

ClassConfigurationDeclaration

public ClassConfigurationDeclaration(java.lang.Object declarationInstance)
                              throws ConfigurationException

Creates a new ClassConfigurationDeclaration out of the passed "declaration object".

The passed Object's type has to be annotated with DeclareConfiguration.

See parse() for details on how the declarations parts are retrieved from the declaration instance's type.

Parameters:
declarationInstance -
Throws:
ConfigurationException
Method Detail

declaresConfiguration

public static boolean declaresConfiguration(java.lang.Class<?> clazz)
Checks whether or not the passed Class declares a Configuration by checking if an DeclareConfiguration Annotation is present.

Parameters:
clazz -
Returns:
true if the passed Class has a DeclareConfiguration annotation present, false otherwise.

parse

protected void parse()
              throws ConfigurationException
Will set default values for all names specified as public static final String constants in declarationClass whoose names are not excluded through DeclareConfiguration.excludedNames() and end with DeclareConfiguration.defaultSuffix(). The same will be provided for fallback ISettings that have constants ending with DeclareConfiguration.fallbackSuffix() defined.

Throws:
ConfigurationException - - on any parsing error

getFieldValue

protected java.lang.Object getFieldValue(java.lang.String fieldName)
                                  throws java.lang.IllegalArgumentException,
                                         java.lang.SecurityException,
                                         java.lang.IllegalAccessException,
                                         java.lang.NoSuchFieldException
Returns the value of the declarationInstance's Field with the passed fieldName.

Parameters:
fieldName -
Returns:
Throws:
java.lang.IllegalArgumentException
java.lang.SecurityException
java.lang.IllegalAccessException
java.lang.NoSuchFieldException

setSettingName

protected void setSettingName(java.lang.String fieldName)
Adds the value of the Field of declarationClass that's referenced by the passed fieldName as a setting name.

Parameters:
fieldName -

getSurrogateeNameAndValue

protected java.lang.String[] getSurrogateeNameAndValue(java.lang.reflect.Field surrogateField,
                                                       java.lang.String suffix)
                                                throws java.lang.Exception
Gets the name and value for the surrogate field (either a fallback setting or default value) passed.

for instance: if the Field representing MY_SETTING_DEFAULT together with declarationAnnotation's specified suffix for default values, see DeclareConfiguration.defaultSuffix(), will be passed the method will return {"MY_SETTING", valueOf(MY_SETTING_DEFAULT) }; the same is analogously valid for MY_SETTING_FALLBACK.

Parameters:
surrogateField -
suffix - specifies the surrogates suffix which distinguished it from the real setting.
Returns:
Throws:
java.lang.Exception

getExcludedNames

protected java.util.List<java.lang.String> getExcludedNames()

getDeclarationType

protected ConfigurationDeclaration.DeclarationType getDeclarationType(java.lang.reflect.Field field)

Determines the DeclarationType of the passed Field.

The type will be DeclarationType#UNKNOWN for any field that is not a String constant (public static final String)
It will be DeclarationType#EXCLUDED for any string constant whose name is in the excludedNames
or DeclarationType#DEFAULT_VALUE if the string constant's name ends with the default suffix
or DeclarationType#FALLBACK_NAME if the string constant's name ends with the fallback suffix.
Finally if the string constants field name is neither excluded nor a default or fallback definition then it will be considered as an ISetting's name.

Returns:


Copyright © 2010 emarsys AG. All Rights Reserved.