public class BeanConfig extends Object
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. Supplied string values are
interpreted using the following rules:
null
" represents null.valueOf(String)
method returning
instances of the required class is present on that class,
the supplied value string is fed to it.to<ReqClass>Instance(String)
method, where <ReqClass>
is the unqualified name
of the required class, exists on the context class, the supplied value
string is fed to it.ConfigMethod
Modifier and Type | Class and Description |
---|---|
static class |
BeanConfig.Setting
Represents an object configuration operation.
|
Constructor and Description |
---|
BeanConfig(String baseTxt,
String configTxt)
Constructs a BeanConfig with a basic object representation and
a string providing configuration information.
|
Modifier and Type | Method and Description |
---|---|
static 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.
|
static <T> T |
decodeTypedValue(Class<T> clazz,
String txt,
Object target)
Attempts to interpret a string as an instance of a given class.
|
String |
getBaseText()
Returns the basic object name for this object.
|
String |
getConfigText()
Returns configuration specification.
|
static String |
getMethodUsage(Method configMethod)
Returns a usage string corresponding to a given configuration method.
|
BeanConfig.Setting[] |
getSettings()
Returns the parsed settings for this object, as extracted
from the configuration text.
|
static void |
main(String[] args) |
static BeanConfig |
parseSpec(String txt)
Parses an object specification to produce a BeanConfig instance.
|
public BeanConfig(String baseTxt, String configTxt)
name1=value1,name2=value2,...
;
if other forms are used, a LoadException with a helpful
error message will be generated as appropriate by relevant method
invocations.baseTxt
- basic object name or representationconfigTxt
- configuration text, normally as supplied
in trailing parenthesispublic String getBaseText()
public String getConfigText()
public BeanConfig.Setting[] getSettings() throws LoadException
LoadException
- if the config text was badly-formedpublic void configBean(Object target) throws LoadException
target
- object to configureLoadException
- with a helpful error message
if the settings don't workpublic static BeanConfig parseSpec(String txt)
base-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.txt
- input textpublic static void applySetting(Object target, BeanConfig.Setting setting) throws ReflectiveOperationException
target
- object to configuresetting
- setting to applyRuntimeException
- in case of various problems with converting
the supplied string to the required typeReflectiveOperationException
- in case of some reflection troublepublic static String getMethodUsage(Method configMethod)
configMethod
- mutator methodpublic static <T> T decodeTypedValue(Class<T> clazz, String txt, Object target)
clazz
- required output classtxt
- text representation of valuetarget
- object in the context of which interpretation is doneRuntimeException
- if decoding is unsuccessfulpublic static void main(String[] args) throws ReflectiveOperationException, LoadException
Copyright © 2025 Central Laboratory of the Research Councils. All Rights Reserved.