|
A Simple Bean Language
The language allows to create Java objects by reflection.
The compiles code of this language
into a corresponding object.
- Creation by constructors. Example:
URL url = (URL) BeanCompiler.compile(URL.class, "URL(http, localhost,8080, '/index.html')");
- Creation by static methods. Example:
Object object = BeanCompiler.compile(String.class, "java.lang.System.getProperty('user.name')");
- Defining attributes via setters. Example:
DecimalFormat format
= (DecimalFormat) BeanCompiler.compile(DecimalFormat.class,
"DecimalFormat{multiplier=42,negativePrefix='a',parseBigDecimal=true}");
- Collection arguments. Example:
DateFormatSymbols symbols
= (DateFormatSymbols) BeanCompiler.compile(DateFormatSymbols.class,
"DateFormatSymbols{weekdays = [M,T,W,T,F,S,S]}");
- Recursive definitions: Constructor and method arguments are objects defined
by this language. Example:
DecimalFormat format
= (DecimalFormat) BeanCompiler.compile(DecimalFormat.class,
"DecimalFormat('0.00', DecimalFormatSymbols{decimalSeparator=*})");
- Short class names instead of fully qualified class names are recognized in
many places as the above examples show.
bean: QUOTED_TEXT | (METHOD_NAME_OR_STRING arguments? properties?) | collection;
arguments: '(' (bean (',' bean)* )? ')';
properties: '{' (property (',' property)* )? '}';
property: POPERTY_NAME '=' bean;
collection: '[' (bean (',' bean)* )? ']';
Where QUOTED_TEXT is a sequence of any characters enclosed in single quotes.
METHOD_NAME_OR_STRING and PROPERTY_NAME are any sequence of characters
excluding white spaces and terminal symbol characters which appear in the above syntax definition.
The backslash character '\' can be used at any place to escape the next character.
Note, that white spaces are ignored except in QUOTED_TEXT.
The uses knowledge about classes
(gained by reflection) to decides how to interpret a METHOD_NAME_OR_STRING.
For example, if the argument of a constructor is of type double or Double
it try to parses the string as a floating point number.
If the compiler expects an object which is neither a String nor a
wrapper object of a primitive type it tries to find a constructor or a static method.
It tests the following hypotheses about METHOD_NAME_OR_STRING:
- It is a static method. That is, it doesn't end with a dot character '.'
but contains at least one '.' and the character after the last '.' is
a lower-case letter reflecting the standard convention that a Java method
should start with a lower-case letter. If a static method has been detected the string
before the last '.' is interpreted as the fully-qualified class name.
- It is a constructor of a fully-qualified Java class.
- It is a constructor for class from a list of expected Java classes. The end
of the fully-qualified name of one class of this list has to be matched by
METHOD_NAME_OR_STRING. This list may have more then one element
because of ambiguity due to overloaded methods or constructors.
|