Generating complex enum literals [message #905703] |
Thu, 30 August 2012 23:18 |
Jens-Uwe Moeller Messages: 12 Registered: March 2012 Location: Hamburg |
Junior Member |
|
|
Hello folks,
Java allows to use complex enum literals, which might have data associated (here a and b) and associated constant-specific methods (here myfct).
These things should be generated from my DSL
public enum color {
RED( 3, 1234L) {int myfct(int p) {return 17*p;}},
BLUE(6, 12L) {int myfct(int p) {return 13*p;}};
private final int a;
private final long b;
private color(final int a, final long b) {
this.a = a;
this.b = b;
}
abstract int myfct(int p);
}
To get the enum type color I can use toEnumerationType.
To get the constants RED and BLUE as simle Literals, I can use toEnumerationLiteral,
each of which will be added tho the members-List.
toField makes the fields a and b, and toConstructor helps with
building the constructor color.
This is straight forward. But I do not get it to work using the initializer of the
toEnumerationLiteral to add the field values resp. the initialization parameters to the
JvmEnumerationLiteral.
Here is one of my trial:
...acceptor.accept(
e.toEnumerationType('color', null)
).initializeLater [
var jvmLiteral = e.toEnumerationLiteral('BLUE')[
setInitializer([ it.append('''whatsoever''') ])
]
members += jvmLiteral
]
For the values belonging to a and b I tried to use toParameter, but these values
are no parameters. toField or toMethod seems not to work either.
Is my idea possible at all? I ask, because I miss any ELISTs within the initializer for JvmEnumerationLiteral that can bear these values or concrete methods.
If it should work, can you give an example.
Jens
PS: I am using xtext 2.3.
|
|
|
|
Powered by
FUDForum. Page generated in 0.03327 seconds