Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Instantiating instance of Java Class corresponding to JvmDeclaredType(Reflectively creating java.lang.Class from a JvmDeclaredType)
Instantiating instance of Java Class corresponding to JvmDeclaredType [message #1231565] Tue, 14 January 2014 19:24 Go to next message
Eclipse UserFriend
Hi,

I have following rule in my grammar (mydsl.xtext):
import "http://www.eclipse.org/xtext/common/JavaVMTypes" as jvmTypes
...

JvmTypeRef:
'ref' ':' jvmtype=[jvmTypes::JvmDeclaredType | FQN]
;

A sample instance file (say 01.mydsl) contains the following:
ref : org.example.try01.Class01

where 01.mydsl is in a general project in workspace that also has a java project having class org.example.try01.Class01

I need to create an instance of the java class org.example.try01.Class01 using reflection (are there other alternatives as well..?) while I compute scope for elements corresponding to other grammar rules.

Is there a way to get instance of java.lang.Class from instance of JvmDeclaredType...?

After reading a bit of xtext code, I feel what I need is an exact reverse of DeclaredTypeFactory#createType(final Class<?> clazz).

Note: jvmtype.eResource.URI gives me 'java:/Objects/org.example.try01.Class01', but I am not sure how to use this to instantiate java.lang.Class.
Re: Instantiating instance of Java Class corresponding to JvmDeclaredType [message #1231679 is a reply to message #1231565] Wed, 15 January 2014 02:28 Go to previous messageGo to next message
Eclipse UserFriend
So you want to run code within your Eclipse workbench, that sits in the
workspace? That can cause all kinds of trouble. Is that really needed?

The only way to do that is to create a URL Class Loader for the given
IJavaProject.

Am 15/01/14 01:24, schrieb Anil Bhatia:
> Hi,
>
> I have following rule in my grammar (mydsl.xtext):
> import "http://www.eclipse.org/xtext/common/JavaVMTypes" as jvmTypes
> ..
>
> JvmTypeRef:
> 'ref' ':' jvmtype=[jvmTypes::JvmDeclaredType | FQN]
> ;
>
> A sample instance file (say 01.mydsl) contains the following:
> ref : org.example.try01.Class01
>
> where 01.mydsl is in a general project in workspace that also has a java
> project having class org.example.try01.Class01
>
> I need to create an instance of the java class org.example.try01.Class01
> using reflection (are there other alternatives as well..?) while I
> compute scope for elements corresponding to other grammar rules.
>
> Is there a way to get instance of java.lang.Class from instance of
> JvmDeclaredType...?
>
> After reading a bit of xtext code, I feel what I need is an exact
> reverse of DeclaredTypeFactory#createType(final Class<?> clazz).
>
> Note: jvmtype.eResource.URI gives me
> 'java:/Objects/org.example.try01.Class01', but I am not sure how to use
> this to instantiate java.lang.Class.


--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de
Re: Instantiating instance of Java Class corresponding to JvmDeclaredType [message #1231742 is a reply to message #1231679] Wed, 15 January 2014 04:31 Go to previous messageGo to next message
Eclipse UserFriend
Hi Sven,

Thanks for the warning...
Basically I need the user to refer to some existing java class (java class could potentially be coming from another Java project in the same workspace, or from a jar file) from 01.xtext. Then, in order to compute the scope for some other domain elements, I need to instantiate this Java Class.

At this point I really do not mind have some extra steps in my workflow, so I am willing to export the Java class from project to some jar, if that helps.

Can you provide some more details, pointers, code snippet, on how to create instance of java.lang.Class from IJavaProject or for imported jar...? Thanks.

Re: Instantiating instance of Java Class corresponding to JvmDeclaredType [message #1232231 is a reply to message #1231742] Thu, 16 January 2014 08:33 Go to previous messageGo to next message
Eclipse UserFriend
Am 15/01/14 10:31, schrieb Anil Bhatia:
> Hi Sven,
>
> Thanks for the warning...
> Basically I need the user to refer to some existing java class (java
> class could potentially be coming from another Java project in the same
> workspace, or from a jar file) from 01.xtext. Then, in order to compute
> the scope for some other domain elements, I need to instantiate this
> Java Class.

What do you mean by 'compute the scope'. Xtext has support for
referencing Java types. Although they are no java.lang.Class instances,
they can be inspected such as that.

Wouldn't that be sufficient?

--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de
Re: Instantiating instance of Java Class corresponding to JvmDeclaredType [message #1232353 is a reply to message #1232231] Thu, 16 January 2014 13:55 Go to previous messageGo to next message
Eclipse UserFriend
Lets say for the moment, in my case, just inspecting via JvmDeclaredTypes is not sufficient.

Based on what I understood from documentation, I tried playing with ResourceDescriptionsProvider etc.

var index = resourceDescriptionsProvider.getResourceDescriptions(jvmtype.eResource)
var descr = index.getResourceDescription(jvmtype.eResource.URI)
manager.getVisibleContainers(descr, index).forEach[
it.resourceDescriptions.forEach[
println(it.URI)
]
]
but this does not yield anything, as descr is null.

I would be grateful if you could point me to any code snippets that demonstrate how to create instance of java.lang.Class from JvmDeclaredType.

Thanks
Anil
Re: Instantiating instance of Java Class corresponding to JvmDeclaredType [message #1232544 is a reply to message #1232353] Fri, 17 January 2014 02:05 Go to previous messageGo to next message
Eclipse UserFriend
We do that in Xtend when executing annotation processors.
See:

https://github.com/eclipse/xtext/blob/master/plugins/org.eclipse.xtend.ide/src/org/eclipse/xtend/ide/macro/JdtBasedProcessorProvider.xtend


Am 16/01/14 19:55, schrieb Anil Bhatia:
> Lets say for the moment, in my case, just inspecting via
> JvmDeclaredTypes is not sufficient.
>
> Based on what I understood from documentation, I tried playing with
> ResourceDescriptionsProvider etc.
>
> var index =
> resourceDescriptionsProvider.getResourceDescriptions(jvmtype.eResource)
> var descr = index.getResourceDescription(jvmtype.eResource.URI)
> manager.getVisibleContainers(descr, index).forEach[
> it.resourceDescriptions.forEach[
> println(it.URI)
> ]
> ]
> but this does not yield anything, as descr is null.
>
> I would be grateful if you could point me to any code snippets that
> demonstrate how to create instance of java.lang.Class from JvmDeclaredType.
>
> Thanks
> Anil


--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de
Re: Instantiating instance of Java Class corresponding to JvmDeclaredType [message #1233061 is a reply to message #1232544] Sat, 18 January 2014 08:47 Go to previous message
Eclipse UserFriend
Thanks so much for the excellent hint Sven, this is precisely what I was seeking.
Previous Topic:An error has occured See the log file ... .log
Next Topic:How to get existing Xtext project from google code sync to eclipse
Goto Forum:
  


Current Time: Mon Jul 14 12:55:00 EDT 2025

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

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

Back to the top