Class BeanConfig
The idea is that objects can be configured from user-supplied text strings that configure bean-like properties. For instance an object may be specified on the command line or using a system property with a syntax like:
"java.util.Date(year=212,day=1)"which would construct a Date object using its no-arg constructor, and then configure the bean-like "year" property by reflectively invoking the
setYear
and setDay
methods
of the Date class.
Where present, a ConfigMethod
annotation on the
relevant mutator methods is used to provide aliased property names,
as well as to improve LoadException error messages when
property setting fails.
The configuration syntax is fairly straightforward;
the format is a comma-separated list of name=value
settings within a pair of parentheses. Obvious serializations are
supported for numeric and boolean values; string values are unquoted;
commas may be backslash-escaped; and symbolic values are permitted
for Enums and static public members on the value class or
created object class.
- Since:
- 11 Sep 2020
- Author:
- Mark Taylor
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Represents an object configuration operation. -
Constructor Summary
ConstructorsConstructorDescriptionBeanConfig
(String baseTxt, String configTxt) Constructs a BeanConfig with a basic object representation and a string providing configuration information. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
applySetting
(Object target, BeanConfig.Setting setting) Applies a given setting to an object.void
configBean
(Object target) Attempts to apply this object's configuration setttings to a supplied target object.Returns the basic object name for this object.Returns configuration specification.static String
getMethodUsage
(Method configMethod) Returns a usage string corresponding to a given configuration method.Returns the parsed settings for this object, as extracted from the configuration text.static void
static BeanConfig
Parses an object specification to produce a BeanConfig instance.
-
Constructor Details
-
BeanConfig
Constructs a BeanConfig with a basic object representation and a string providing configuration information. The baseTxt could be a classname or something else. The configTxt should normally be of the formname1=value1,name2=value2,...
; if other forms are used, a LoadException with a helpful error message will be generated as appropriate by relevant method invocations.- Parameters:
baseTxt
- basic object name or representationconfigTxt
- configuration text, normally as supplied in trailing parenthesis
-
-
Method Details
-
getBaseText
Returns the basic object name for this object. May be a classname or something else. This should not be null.- Returns:
- basic representation
-
getConfigText
Returns configuration specification. May be null if there was no trailing parenthesis.- Returns:
- configuration text, nominally of the form "n1=v1,n2=v2,...",
-
getSettings
Returns the parsed settings for this object, as extracted from the configuration text. The list may be empty.- Returns:
- configuration settings
- Throws:
LoadException
- if the config text was badly-formed
-
configBean
Attempts to apply this object's configuration setttings to a supplied target object.- Parameters:
target
- object to configure- Throws:
LoadException
- with a helpful error message if the settings don't work
-
parseSpec
Parses an object specification to produce a BeanConfig instance. The specification is of the formbase-name(n1=v1,n2=v2,...)
. If there is no well-formed parenthesised config-string at the end, the returned object has a base-name that is the whole of the input string, and no config text.- Parameters:
txt
- input text- Returns:
- BaseConfig object, which may or may not have configuration information
-
applySetting
public static void applySetting(Object target, BeanConfig.Setting setting) throws ReflectiveOperationException Applies a given setting to an object.- Parameters:
target
- object to configuresetting
- setting to apply- Throws:
RuntimeException
- in case of various problems with converting the supplied string to the required typeReflectiveOperationException
- in case of some reflection trouble
-
getMethodUsage
Returns a usage string corresponding to a given configuration method.- Parameters:
configMethod
- mutator method- Returns:
- short user-directed usage text
-
main
-