Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Switch case problem with JvmTypeReference(How to convert the reference to Java type?)
Switch case problem with JvmTypeReference [message #1798927] Tue, 27 November 2018 14:22 Go to next message
Karsten Wilken is currently offline Karsten WilkenFriend
Messages: 59
Registered: August 2016
Member
With the simple parser rule:
Column:
	'column' name=ValidID ':' columnType=JvmTypeReference ';'
;


Depending on the type I want to generate different strings. Sounds easy, but it doesn't seem to be that obvious.

Within the inferrer I use a switch statement:

switch column.columnType {
	Long: {
		list.add("xyz")
	}
	Integer: {
		list.add("abc")
	}
	default: {
		list.add("z")
	}
}


This leads to a obvious "Cannot cast from JvmTypeReference to Long".

How can I convert the jvmreference or the jvmtype of the reference to the corresponding java type?
Re: Switch case problem with JvmTypeReference [message #1798928 is a reply to message #1798927] Tue, 27 November 2018 14:30 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

Resolve the 'type' reference of the JvmTypeReference. That returns a JvmType. Use the qualifiedName of it to decide that it's a java.lang.Long. A direct usage of the Long type is not possible here, the JVM types are just a representation of the type system.
Re: Switch case problem with JvmTypeReference [message #1798930 is a reply to message #1798928] Tue, 27 November 2018 14:49 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
you may use something like

def static isTypeOf(JvmType type, Class<?> clazz) {
return clazz.canonicalName.equals(type.identifier)
}

or similar


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Switch case problem with JvmTypeReference [message #1798932 is a reply to message #1798930] Tue, 27 November 2018 15:02 Go to previous messageGo to next message
Karsten Wilken is currently offline Karsten WilkenFriend
Messages: 59
Registered: August 2016
Member
Thank you. The approach with the identifier was my fall back plan. I hoped there might be a more sophisticated way.
Re: Switch case problem with JvmTypeReference [message #1798934 is a reply to message #1798932] Tue, 27 November 2018 15:22 Go to previous message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

You should be able to use Long.name.toFullyQualifiedName with IQualifiedNameConverter if you prefer to use the class constant instead of Strings.
Previous Topic:Grammar parsing problem
Next Topic:Most convenient way to include third party dependencies
Goto Forum:
  


Current Time: Wed Apr 24 21:18:43 GMT 2024

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

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

Back to the top