Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » toEnumerationType not working(Can't create Enums)
toEnumerationType not working [message #804190] Wed, 22 February 2012 09:59 Go to next message
Ingo Boegemann is currently offline Ingo BoegemannFriend
Messages: 7
Registered: February 2012
Junior Member
I'm trying to create an Enum class from my Model.
There is a method called "toEnumerationType" inside the JvmTypesBuilder class.
This seems to work correctly and creates an Enumeration but this is never actually written to the generated sources.

There is a bug referencing this issue from last November(364538) but it has no history nor is it assigned to anyone.

I would assume that generating Enums from a model is a common requirement and am wondering if I am missing here something?

Thanks in advance
Ingo
Re: toEnumerationType not working [message #804268 is a reply to message #804190] Wed, 22 February 2012 12:00 Go to previous message
Ingo Boegemann is currently offline Ingo BoegemannFriend
Messages: 7
Registered: February 2012
Junior Member
Hi again
I looked into the JvmModelGenerator code and confirmed that the generation of Enumerations is not implemented Sad

I therefore extended the JvmModelGenerator with my own class adding Enumeration types to it Wink
Naturally I would prefer to use an xText version rather than my own - please let me know how / whether you want to adapt my code for a further version?!
Any comments to any mistakes/misunderstandings/ommissions are extremely welcom to!

Below my current extension:
package com.wcg.casemanagement.dsl.jvmmodel

import org.eclipse.xtext.xbase.compiler.JvmModelGenerator
import org.eclipse.xtext.common.types.JvmEnumerationType
import org.eclipse.xtext.xbase.compiler.ImportManager
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.xtext.common.types.JvmGenericType
import org.eclipse.xtext.common.types.JvmEnumerationLiteral

class EnumAwareJvmModelGenerator extends JvmModelGenerator{
	
	def dispatch void internalDoGenerate(JvmEnumerationType type, IFileSystemAccess fsa) {
		fsa.generateFile(type.qualifiedName.replace('.','/')+".java", type.generateEnumType)
	}
	
	
	def CharSequence generateEnumType(JvmEnumerationType type) {
		val importManager = new ImportManager(true, type)
		val typeBody = generateBody(type, importManager)
		'''
			«IF type.packageName != null»package «type.packageName»;
			
			«ENDIF»
			«FOR i: importManager.imports AFTER "\n"»
				import «i»;
			«ENDFOR»
			«typeBody»
		'''
	}
	
	def generateBody(JvmEnumerationType it, ImportManager importManager) '''
		«it.generateJavaDoc»
		«it.annotations.generateAnnotations(importManager)»
		«it.generateModifier»enum «it.simpleName»«it.generateExtendsClause(importManager)»{
		  «FOR memberCode : it.members.map(m|m.generateMember(importManager)).filter(c|c!=null) SEPARATOR ','»
		    «memberCode»
		  «ENDFOR»
		}
	'''
	
	def generateExtendsClause(JvmEnumerationType it, ImportManager importManager) {
		if (superTypes.empty)
			return null

		val withoutObject = superTypes.filter( typeRef | typeRef.identifier != "java.lang.Object")
		val superClazz = withoutObject.filter(typeRef | typeRef.type instanceof JvmGenericType && !(typeRef.type as JvmGenericType).interface).head
		val superInterfaces = withoutObject.filter(typeRef | typeRef != superClazz)
		var result = ""
		if (superClazz != null) {
			result = "extends " + superClazz.serialize(importManager)+" "
		} 
		if (!superInterfaces.empty) {
			result = result + "implements " + superInterfaces.map( t | t.serialize(importManager)).join(", ") + " "
		}
		return result
		
	}
	
	def dispatch generateModifier(JvmEnumerationType it) '''
		«visibility.javaName»«IF abstract»abstract «ENDIF»«IF final»final «ENDIF»«IF ^static»static «ENDIF»'''
		
	def dispatch generateMember(JvmEnumerationLiteral lit, ImportManager importManager) '''
		«lit.generateJavaDoc»
		«IF !lit.annotations.empty»«lit.annotations.generateAnnotations(importManager)»«ENDIF»
		«lit.simpleName»
	'''
	

}

Previous Topic:How to get position of parsed token in input grammar
Next Topic:Xtend -> Java transformation bug? (scoping of type names)
Goto Forum:
  


Current Time: Thu Apr 09 18:19:27 GMT 2020

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

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

Back to the top