|
Re: toEnumerationType not working [message #804268 is a reply to message #804190] |
Wed, 22 February 2012 12:00 |
Ingo Boegemann 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
I therefore extended the JvmModelGenerator with my own class adding Enumeration types to it
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»
'''
}
|
|
|
Powered by
FUDForum. Page generated in 0.03383 seconds