Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Generating complex enum literals
Generating complex enum literals [message #905703] Thu, 30 August 2012 23:18 Go to next message
Jens-Uwe Moeller is currently offline Jens-Uwe MoellerFriend
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.
Re: Generating complex enum literals [message #905838 is a reply to message #905703] Fri, 31 August 2012 07:14 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13667
Registered: July 2009
Senior Member
Hi,

if i have a look at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.generateEnumLiteral(JvmEnumerationLiteral, ITreeAppendable, boolean)
this is not possible currently => you have to do something yourself

(and/or file an enhancement request)

~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:JvmFormalParameter rule ambigouous?
Next Topic:StyledTextXtextAdapter and minimum required extension-points
Goto Forum:
  


Current Time: Wed Dec 02 22:05:58 GMT 2020

Powered by FUDForum. Page generated in 0.01993 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top