[Xtend] Setting a class type variable [message #1736523] |
Thu, 30 June 2016 07:58 |
chris yo Messages: 146 Registered: February 2013 |
Senior Member |
|
|
I have an xcore file with the following classes:
class Information {
String sName
String birthday
String age
}
class Directory {
String name
String address
contains Information xInfo
}
The variables in the class Directory is set by xtext thru the grammar configuration by the user, except for xInfo. I need to set the xInfo after all the grammar has been read.
The xInfo variable is set during validation. So, I have an xtend method that sets this.
public static val aDirs = new ArrayList<Directory>
public static val aInfo = new ArrayList<Information>
def public setInfo()
{
for (m: aDirs)
{
val x = aInfo.findFirst[m.name.equals(SName)]
if (x != null)
m.xInfo = x //print also shows that this is correctly set.
}
}
def public printInfo()
{
for (m: aDirs)
{
if (m.xInfo == null) // ERROR: m.xInfo is null again!
{
// do nothing
}
}
}
With the two methods above, I called setInfo() and then printInfo(), but in printInfo(), xInfo variable is null again.
aDirs and aInfos are stored during transform.
Am I doing this right? How can we set a class type in xtend?
[Updated on: Thu, 30 June 2016 08:04] Report message to a moderator
|
|
|
|
Re: [Xtend] Setting a class type variable [message #1736528 is a reply to message #1736524] |
Thu, 30 June 2016 08:30 |
chris yo Messages: 146 Registered: February 2013 |
Senior Member |
|
|
Hi Christian,
Thanks for the tip!
One of the solution I thought of is to make a derived feature in xcore so I will have a minimal code change.
I modified the class Directory like this:
class Directory {
String name
String address
derived Information xInfo get {
MySingleton::instance.getInfo(name)
}
}
But I am getting an error saying that "The generic attribute type must not refer to a class".
Does this mean that its not possible to do it as a derived feature?
I am also thinking of using the containment/container reference but the xcore wiki did not state how to set it. I hope you have some idea how..?
[Updated on: Thu, 30 June 2016 08:31] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.04451 seconds