Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Generating enums with methods (and fields)
Generating enums with methods (and fields) [message #947690] Wed, 17 October 2012 10:20 Go to next message
Alexey Romanov is currently offline Alexey RomanovFriend
Messages: 263
Registered: May 2010
Senior Member
I am trying to generate a enum which contains additional methods, so from
enum Gender { MALE = 1, FEMALE = 2 }

I get
enum Gender { 
     MALE,
     FEMALE;

     int getValue() {
         switch (this) {
             case MALE: return 1;
             case FEMALE: return 2;
             default: throw new IllegalArgumentException();
         }
     }
}

This is my inferrer code:
def dispatch void infer(EnumDef enumDef, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
		val enumName = enumDef.fullyQualifiedName.toString
		acceptor.accept(enumDef.toEnumerationType(enumName) [
		]).initializeLater [
			documentation = enumDef.documentation
			val intTypeRef = enumDef.newTypeRef(typeof(int))
			
			for (const1 : enumDef.enumConsts) {
				members += const1.toEnumerationLiteral(const1.name)
			}
			
			members += enumDef.toMethod("getValue", intTypeRef) [
				body = [append('''
				switch (this) {
					«FOR const1 : enumDef.enumConsts»
					case «const1.name»: return «const1.id»;
					«ENDFOR»
					default: throw new IllegalArgumentException("Constant " + name() + " is not an «enumName»");
				}
				''')]
			]
		]
	}

The problem is that the generated code lacks ";" after FEMALE and doesn't work Sad Is this a bug or is there a way to do this correctly?

Ideally I'd also like to add a "int value" field to the enum, but there doesn't seem to be a way to set constructor arguments from "toEnumerationLiteral".
Re: Generating enums with methods (and fields) [message #949139 is a reply to message #947690] Thu, 18 October 2012 18:31 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13613
Registered: July 2009
Senior Member
Hi,

first it would be nice if you'd file a bug against JvmModelGenerator/JvmTypesBuilder
(1) to end the list of literals with a semikolon (see JvmModelGenerator def dispatch generateBody(JvmEnumerationType it, ITreeAppendable appendable) )
(2) to support a proper constructor and the possibility to call it when declaring the literals

btw (1) would be the place to hook into for your workaround

~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
Re: Generating enums with methods (and fields) [message #949620 is a reply to message #949139] Fri, 19 October 2012 06:00 Go to previous message
Alexey Romanov is currently offline Alexey RomanovFriend
Messages: 263
Registered: May 2010
Senior Member
I found both bugs already filed: https://bugs.eclipse.org/bugs/show_bug.cgi?id=377002 https://bugs.eclipse.org/bugs/show_bug.cgi?id=372771 Workaround was easy to write once I knew where to look, thanks!
Previous Topic:alternate between assignment and terminal
Next Topic:How to visualize grammar generated from Xtext in AntlrWorks
Goto Forum:
  


Current Time: Mon Sep 28 10:56:23 GMT 2020

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

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

Back to the top