Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to get scope for jvm type or pojo
How to get scope for jvm type or pojo [message #1065188] Mon, 24 June 2013 16:28 Go to next message
Ian McDevitt is currently offline Ian McDevittFriend
Messages: 70
Registered: December 2012
Location: Belfast
Member
This is a simplified version of what I am trying to do. I declare an object with a type and then use its members in method calls. The DSL could look like this
NEW t1 as java.util.Timer
CALL t1.cancel

but I get an error line under cancel, so I have tried to set the right scope.

I have done this before when the type is a DSL type, it is straight forward to add the operations of the type to the scope as shown in other posts such as http://stackoverflow.com/questions/7791871/xtext-example-of-a-scoped-object.

But when the type is a normal Java class (not created in the DSL) then I am stuck. I believe it can be done as I see it in Xbase but I cannot get it to work as all the methods for working with Scopes seem to require EObjects not Objects.

Here is an example grammar, but I have also tried using feature=XMemberFeatureCall and other Xbase rules without success.
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.xbase.Xbase
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
import "http://www.eclipse.org/xtext/common/JavaVMTypes" as types

Model:	elements+=(NewPojo|Operation)*;
	
NewPojo: 'NEW' name=ID 'as' type=JvmTypeReference ;

Operation: 'CALL' target=[NewPojo] '.' feature=[types::JvmIdentifiableElement] ; 


I usually get "couldn't resolve reference to JvmIdentifiableElement 'cancel'". I know the 'cancel' method is a JVM operation so I want to set the scope as the members of the type of the target, but I can't see how to get from a Method[] to a list of EObjectDescriptions. Is there a utility?

Re: How to get scope for jvm type or pojo [message #1065193 is a reply to message #1065188] Mon, 24 June 2013 17:37 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

have a look at the class Scopes.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to get scope for jvm type or pojo [message #1065214 is a reply to message #1065193] Mon, 24 June 2013 19:57 Go to previous messageGo to next message
Ian McDevitt is currently offline Ian McDevittFriend
Messages: 70
Registered: December 2012
Location: Belfast
Member
It's funny how it starts working after you ask someone. I find now that getting the contents from the JvmType (from the JvmTypeReference) is now giving me a list of EObjects,
target.type.type.eContents
Somehow I had been getting an empty list every time before. Not sure what I did differently this time but I can now get the methods from the jvm type as EObjects.

I will work with Scopes::scopeFor and others. It is creating a scope list with the methods in it but something else is not working as I still get the error. I have a custom scope_Operation_feature method so I'll debug on a bit more myself now I've got the first problem solved.
Thanks
Ian
Re: How to get scope for jvm type or pojo [message #1065860 is a reply to message #1065214] Thu, 27 June 2013 22:31 Go to previous messageGo to next message
Ian McDevitt is currently offline Ian McDevittFriend
Messages: 70
Registered: December 2012
Location: Belfast
Member
For anyone with a similar problem, I have got the rest of this working now but not quite as first expected.

In the getScope method of my delegate scope provider, which extends XImportSectionNamespaceScopeProvider, I check that it's looking to scope an OPERATION__FEATURE reference (otherwise I get cyclic exceptions if it's looking for the OPERATION_TARGET) and I get the list of pojo members from the target type as EObjects:
context.target.type.type.eContents.filter(typeof(JvmFeature))
(the first 'type' is a typeRef).

So far so good.

But when I used
Scopes::scopeFor
it gave me an empty scope. This seems to be a result of Scopes::scopedElementsFor producing null qualified names when transforming the EObjects to EObjectDescriptions.

So I did my own transform on the list using
EObjectDescription::create
to get the list as object descriptions and then used
MapBasedScope::createScope
and that did the job nicely.


(edited to replace XbaseImportedNamespaceScopeProvider with XImportSectionNamespaceScopeProvider, as I've recently migrated to 2.4 where it's been deprecated - so now my content assist works again!)

[Updated on: Thu, 27 June 2013 23:08]

Report message to a moderator

Re: How to get scope for jvm type or pojo [message #1065871 is a reply to message #1065860] Fri, 28 June 2013 05:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi please Note that you may have to use the scopefor Method. With namecomputation Since the Method may not have a properste Named name

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to get scope for jvm type or pojo [message #1092148 is a reply to message #1065871] Thu, 22 August 2013 11:44 Go to previous messageGo to next message
Eclipse UserFriend
Great, EObjectDescription::create worked for my Problem too. Before that, I tried to use the nameComputation Function but didn't succeed under xtend scope provider. Is there any code snippet to solve this under xtend with xtext 2.4.2?
Re: How to get scope for jvm type or pojo [message #1095983 is a reply to message #1092148] Tue, 27 August 2013 20:11 Go to previous messageGo to next message
Ian McDevitt is currently offline Ian McDevittFriend
Messages: 70
Registered: December 2012
Location: Belfast
Member
I did change to use scopeFor and a name computation, but I also also stopped using the xbase scope provider. I'll see if I can dig out the code to see if it helps any.
Re: How to get scope for jvm type or pojo [message #1096076 is a reply to message #1095983] Tue, 27 August 2013 23:44 Go to previous message
Ian McDevitt is currently offline Ian McDevittFriend
Messages: 70
Registered: December 2012
Location: Belfast
Member
This was what I had at the time as an example of using a trivial naming function in the scope function to get elements from a JvmTypeReference. It was mainly to help me understand how things were working rather than best practice but it may point you to something in your own code.
var Function<JvmFeature, QualifiedName> getNameOfJvmFeature = [ QualifiedName::create(it.simpleName) ]
val jvmFeatures = aTypeRef.type.eContents.filter(typeof(JvmFeature))
scope = Scopes::scopeFor(jvmFeatures, getNameOfJvmImplFeature, scope)
Previous Topic:Obtain a Resource of a model source file (to compile imports in code generation)
Next Topic:Cannot use literal in following ID terminal rule
Goto Forum:
  


Current Time: Thu Apr 25 11:31:44 GMT 2024

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

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

Back to the top