Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How can I infer jvm type which has super type which is derived from part of domain model
How can I infer jvm type which has super type which is derived from part of domain model [message #809201] Tue, 28 February 2012 15:43 Go to next message
Jeeeyul Lee is currently offline Jeeeyul LeeFriend
Messages: 117
Registered: July 2009
Location: Seoul
Senior Member

I'm trying JVM language Tutorial.

This great article has some missing parts:
* Generated java classes have no super type notations.
* Until satisfy some build paths, closures described in example will not work

So I'm finding missing parts, and trying other experiments.

below code are part of type inferrer:
result.superTypes += element.superType.cloneWithProxies


element is an instance of Entity ( in JVM language Tutorial)

above code works fine when super type is one of normal java classes.
so below DSL was compiled well as I suspected:
entity A extends ArrayList{

}


but with java class which is generated by same xtext building context
(a class which is derived from Entity):

entity A{

}

entity B extends A{

}


It throws an error:
java.lang.NullPointerException
	at java.util.regex.Matcher.getTextLength(Unknown Source)
	at java.util.regex.Matcher.reset(Unknown Source)
	at java.util.regex.Matcher.<init>(Unknown Source)
	at java.util.regex.Pattern.matcher(Unknown Source)
	at org.eclipse.xtext.xbase.compiler.ImportManager.allowsSimpleName(ImportManager.java:93)
	at org.eclipse.xtext.xbase.compiler.ImportManager.appendType(ImportManager.java:72)
	at org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable.appendType(StringBuilderBasedAppendable.java:101)
	at org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable.append(StringBuilderBasedAppendable.java:23)
	at org.eclipse.xtext.xbase.compiler.TypeReferenceSerializer.serialize(TypeReferenceSerializer.java:115)
	at org.eclipse.xtext.xbase.compiler.TypeReferenceSerializer.serialize(TypeReferenceSerializer.java:61)
	at org.eclipse.xtext.xbase.compiler.TypeReferenceSerializer.serialize(TypeReferenceSerializer.java:58)
	at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.serialize(JvmModelGenerator.java:1050)
	at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.generateExtendsClause(JvmModelGenerator.java:376)
	at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.generateBody(JvmModelGenerator.java:160)
	at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.generateType(JvmModelGenerator.java:96)
	at org.eclipse.xtext.xbase.compiler.JvmModelGenerator._internalDoGenerate(JvmModelGenerator.java:87)
	at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.internalDoGenerate(JvmModelGenerator.java:1105)
	at org.eclipse.xtext.xbase.compiler.JvmModelGenerator.doGenerate(JvmModelGenerator.java:76)
	at org.eclipse.xtext.builder.BuilderParticipant.handleChangedContents(BuilderParticipant.java:222)
	at org.eclipse.xtext.builder.BuilderParticipant.build(BuilderParticipant.java:168)
	at org.eclipse.xtext.builder.impl.RegistryBuilderParticipant.build(RegistryBuilderParticipant.java:60)
	at org.eclipse.xtext.builder.impl.XtextBuilder.doBuild(XtextBuilder.java:160)
	at org.eclipse.xtext.builder.impl.XtextBuilder.incrementalBuild(XtextBuilder.java:141)
	at org.eclipse.xtext.builder.impl.XtextBuilder.build(XtextBuilder.java:91)
	at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:728)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:199)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:239)
	at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:292)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:295)
	at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:351)
	at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:374)
	at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:143)
	at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:241)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)


At first, I thought it might be some problem with build dependency things.
But referencing types(derived form Entity model) as property(JvmField) seems fine.
I can't understand this difference.

Any help?

ps.
Generated java files are relatively hard to read, So I wanted to review xtend files(likes JvmTypeBuilder.xtend) which are delivered by plugin bundle rather than review derived java source file.

But because of french quotes and incorrect charset detection, So I had to see just broken source codes. And I couldn't find way to change charset.

of course, I can manually extract xtend files from jars, and I can see it with prefer charset, as you know, it's quite annoying. any suggestions?

[Updated on: Tue, 28 February 2012 15:48]

Report message to a moderator

Re: How can I infer jvm type which has super type which is derived from part of domain model [message #1002869 is a reply to message #809201] Sat, 19 January 2013 17:58 Go to previous messageGo to next message
Alan Alberghini is currently offline Alan AlberghiniFriend
Messages: 19
Registered: January 2013
Junior Member
Have you found a solution?
I'm getting stuck with a (I guess) similar issue in my DSL where I'm trying to generate a Java interface + a Java support Class (implementing that same interface) for every entity in my model.
I tried using both
it.superTypes +=  typeRefs.createTypeRef(typeRefs.findDeclaredType([interfaceName], entity))

and
it.superTypes += entity.newTypeRef([interfaceName])

where entity is an Entity object from my model and [interfaceName] is the complete qualified name of the interface (which is "accepted" beforehand by the inferrer), but I keep getting null JvmTypeReferences from both methods.
Do I need to explicitly "register" the interfaces/classes I'm generating before referring to them when setting the superTypes?
Re: How can I infer jvm type which has super type which is derived from part of domain model [message #1003113 is a reply to message #1002869] Sun, 20 January 2013 09:17 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 01/19/2013 06:58 PM, Alan Alberghini wrote:
> Have you found a solution?
> I'm getting stuck with a (I guess) similar issue in my DSL where I'm
> trying to generate a Java interface + a Java support Class (implementing
> that same interface) for every entity in my model.
> I tried using both
>
> it.superTypes +=
> typeRefs.createTypeRef(typeRefs.findDeclaredType([interfaceName], entity))
>
> and
>
> it.superTypes += entity.newTypeRef([interfaceName])
>
> where entity is an Entity object from my model and [interfaceName] is
> the complete qualified name of the interface (which is "accepted"
> beforehand by the inferrer), but I keep getting null JvmTypeReferences
> from both methods.
> Do I need to explicitly "register" the interfaces/classes I'm generating
> before referring to them when setting the superTypes?

Hi

I cannot test it at the moment:

- when you use toClass in the inferrer you get a JvmGeneritType
- to create a type reference for the superTypes you can use newTypeRef
which accepts a JvmDeclaredType (and JvmGenericType is a JvmDeclaredType)

I don't think instead that using a qualified name would work in this
context... thus, instead of using interfaceName, use the actual
JvmGenericType that you inferred for the interface

hope this helps
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it


Re: How can I infer jvm type which has super type which is derived from part of domain model [message #1003126 is a reply to message #1003113] Sun, 20 January 2013 10:14 Go to previous messageGo to next message
Alan Alberghini is currently offline Alan AlberghiniFriend
Messages: 19
Registered: January 2013
Junior Member
Lorenzo Bettini wrote on Sun, 20 January 2013 10:17

Hi

I cannot test it at the moment:

- when you use toClass in the inferrer you get a JvmGeneritType
- to create a type reference for the superTypes you can use newTypeRef
which accepts a JvmDeclaredType (and JvmGenericType is a JvmDeclaredType)

I don't think instead that using a qualified name would work in this
context... thus, instead of using interfaceName, use the actual
JvmGenericType that you inferred for the interface

hope this helps
Lorenzo

Thanks for your answer.
Unfortunately, I cannot find a newTypeRef method which accepts a JvmDeclaredType as argument (I'm using Xtext 2.2.1), as it only accepts a Class or a String argument.
Using the qualified name of the JvmGenericType actually seems to almost work, but there's still something not quite right.
Will post any news as soon as I find a way to make it work...

EDIT:
The type references seem to be working now (will post a complete working example after the last issue is solved), but I keep getting a lot of errors while editing the (text) model file: it seems like the validator (the default one, because I didn't specify a custom one yet) is triggered every time a small change is done to the model, which will leave it in a non-consistent state most of the times (open braces, statements not terminated etc.).
What triggers this "aggressive" behavior of the validator? Is there a way to tell the validator to chill-out for a little while? Smile

EDIT2:
Found out the culprit: I just needed some more null-checking inside the inferrer to prevent inferring null-entities (created in the middle of editing).
Will post a complete example/tutorial for reference soon.

[Updated on: Mon, 21 January 2013 08:31]

Report message to a moderator

Re: How can I infer jvm type which has super type which is derived from part of domain model [message #1004134 is a reply to message #1003126] Tue, 22 January 2013 16:21 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 01/20/2013 11:14 AM, Alan Alberghini wrote:
> Lorenzo Bettini wrote on Sun, 20 January 2013 10:17
>> Hi
>>
>> I cannot test it at the moment:
>>
>> - when you use toClass in the inferrer you get a JvmGeneritType
>> - to create a type reference for the superTypes you can use newTypeRef
>> which accepts a JvmDeclaredType (and JvmGenericType is a JvmDeclaredType)
>>
>> I don't think instead that using a qualified name would work in this
>> context... thus, instead of using interfaceName, use the actual
>> JvmGenericType that you inferred for the interface
>>
>> hope this helps
>> Lorenzo
>
> Thanks for your answer.
> Unfortunately, I cannot find a newTypeRef method which accepts a
> JvmDeclaredType as argument (I'm using Xtext 2.2.1), as it only accepts
> a Class or a String argument.
> Using the qualified name of the JvmGenericType actually seems to almost
> work, but there's still something not quite right.
> Will post any news as soon as I find a way to make it work...
>
>

I (and probably other people) would strongly suggest you to switch to
Xtext 2.3.1 where the inferrer and related classes are really much better :)

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it


Re: How can I infer jvm type which has super type which is derived from part of domain model [message #1005234 is a reply to message #1004134] Thu, 24 January 2013 17:28 Go to previous messageGo to next message
Alan Alberghini is currently offline Alan AlberghiniFriend
Messages: 19
Registered: January 2013
Junior Member
I can't upgrade because I need some components available only on Indigo, so I'm stuck with Xtext 2.2 for now.
Re: How can I infer jvm type which has super type which is derived from part of domain model [message #1005330 is a reply to message #1005234] Thu, 24 January 2013 22:06 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
Xtext and Xtend 2.3.x are working down to Eclipse 3.5, so your excuses
don't count ;-)

Am 24.01.13 18:28, schrieb Alan Alberghini:
> I can't upgrade because I need some components available only on Indigo,
> so I'm stuck with Xtext 2.2 for now.


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


---
Get professional support from the Xtext committers at www.typefox.io
Re: How can I infer jvm type which has super type which is derived from part of domain model [message #1005967 is a reply to message #1005330] Tue, 29 January 2013 12:31 Go to previous messageGo to next message
Alan Alberghini is currently offline Alan AlberghiniFriend
Messages: 19
Registered: January 2013
Junior Member
You're right, but I forgot to mention in my previous post that I need to use some 3rd-party plugins built using Xtext 2.1/2.2 that have some issues with Xtext 2.3, so that's why I'm stuck with an older Xtext version.
Re: How can I infer jvm type which has super type which is derived from part of domain model [message #1032689 is a reply to message #1003126] Wed, 03 April 2013 09:54 Go to previous messageGo to next message
Nils Schmidt is currently offline Nils SchmidtFriend
Messages: 2
Registered: April 2013
Junior Member
Hi Allan,

would you be so kind to share your solution (some example code).

I am trying to generate an Interface + Implementation from one Entity in my DSL. But I am not able to get the typeRef linking working. Seems the solution you found, should work in my case as well, so if you could share it, I would highly apreciate it!

Thanks and regards
Nils
Re: How can I infer jvm type which has super type which is derived from part of domain model [message #1033042 is a reply to message #1032689] Wed, 03 April 2013 19:52 Go to previous messageGo to next message
Alan Alberghini is currently offline Alan AlberghiniFriend
Messages: 19
Registered: January 2013
Junior Member
Sure, no problem Nils!
Will try to prepare something in the next few days after work.

Cheers
Alan
Re: How can I infer jvm type which has super type which is derived from part of domain model [message #1051432 is a reply to message #1033042] Sun, 28 April 2013 19:55 Go to previous messageGo to next message
Alan Alberghini is currently offline Alan AlberghiniFriend
Messages: 19
Registered: January 2013
Junior Member
Sorry for the long wait, Nils.
My solution basically boils down to:

  1. generate all interfaces first with a dedicated inferrer
  2. generate all support classes after the interfaces are generated

To link the support classes to the interfaces (in order to generate an "implements" declaration in the source code), one needs to add a JvmTypeReference to the "superTypes" list in the support class.
This is the way I get the type reference:
entity.eContainer.newTypeRef(String qualifiedName)

Here I use the container of the entity as context and retrieve a type reference to the implemented interface (generated on the first infer pass) by passing its fully qualified name to the newTypeRef method. Should you use a more recent version of Xtext, as Lorenzo pointed out before, you could use a newTypeRef method accepting a JvmDeclaredType, like the one returned by the "toInterface" method.
That's basically it. If you need further explanations with more code examples please tell me and I'll manage to add some more information.
icon7.gif  Re: How can I infer jvm type which has super type which is derived from part of domain model [message #1053599 is a reply to message #1051432] Mon, 06 May 2013 16:41 Go to previous messageGo to next message
Nils Schmidt is currently offline Nils SchmidtFriend
Messages: 2
Registered: April 2013
Junior Member
Thanks Allan, that helped a lot!
Re: How can I infer jvm type which has super type which is derived from part of domain model [message #1053827 is a reply to message #1051432] Tue, 07 May 2013 18:47 Go to previous message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 04/28/2013 09:55 PM, Alan Alberghini wrote:
> Sorry for the long wait, Nils.
> My solution basically boils down to:
>
> generate all interfaces first with a dedicated inferrer
> generate all support classes after the interfaces are generated
>
> To link the support classes to the interfaces (in order to generate an
> "implements" declaration in the source code), one needs to add a
> JvmTypeReference to the "superTypes" list in the support class.
> This is the way I get the type reference:
> entity.eContainer.newTypeRef(String qualifiedName)
> Here I use the container of the entity as context and retrieve a type
> reference to the implemented interface (generated on the first infer
> pass) by passing its fully qualified name to the newTypeRef method.
> Should you use a more recent version of Xtext, as Lorenzo pointed out
> before, you could use a newTypeRef method accepting a JvmDeclaredType,
> like the one returned by the "toInterface" method.
> That's basically it. If you need further explanations with more code
> examples please tell me and I'll manage to add some more information.
>

as a follow-up, in case, once you inferred interfaces, you need to refer
to them when inferring the classes, and you did not keep track of them,
you can still retrieve the JvmDeclaredType by using
IJvmModelAssociations, and then create the type reference, e.g.,


@Inject extension TypeReferences
@Inject extension IJvmModelAssociations

def associatedInterfaceType(EObject t) {
t.jvmElements.filter(typeof(JvmGenericType)).
filter[interface].head
}

def associatedInterface(TJTraitExpression t) {
t.associatedInterfaceType?.createTypeRef()
}

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it


Previous Topic:Formating with Xtend
Next Topic:How to add a library directory to a project in my DSL
Goto Forum:
  


Current Time: Fri Apr 19 23:11:25 GMT 2024

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

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

Back to the top