Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to load resources stored in a plug-in ?
How to load resources stored in a plug-in ? [message #1297453] Tue, 15 April 2014 17:41 Go to next message
Gregoire Dupe is currently offline Gregoire DupeFriend
Messages: 75
Registered: September 2009
Location: France
Member
Hello,

I have an XText grammar referring to a java classes. I've tried to use JvmType and JvmTypeReference.

For my existing Ecore model "MyDsl", here is bellow my XText grammar :

grammar com.myCompany.myapp.metamodel.xtext.MyDsl with org.eclipse.xtext.xbase.Xbase

import "http://www.eclipse.org/xtext/common/JavaVMTypes" as jvmTypes
import "http://www.myCompany.com/myapp/myDsl/0.1/myDsl" 
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
[...]

Action returns Action:
       {Action}
       'Action'
       name=ID
       '{'
             ('javaClassName' javaClassName=QualifiedName)?
             ('javaClass' javaClassJvmType=[jvmTypes::JvmType|QualifiedName])?
             ('javaClassR' javaClassTypeRef=JvmTypeReference)?
[...]
       '}';
[...]


Here is a sample of an MyDsl file conforming to my grammar:
       Action MyAction {
             javaClass com.mycompany.myapp.tests.internal.sample1.MyAction
             javaClassR com.mycompany.myapp.tests.internal.sample1.MyAction
       }


The XText editor resolves the reference to the Java class, hyperlink navigation is possible by "ctrl + left click".
I'm trying to load an XText resource (conforming to the previous grammar) embedded in a plug-in with the following code :

private static ClassLoader getClassLoader() throws ClassNotFoundException {
       Bundle bundle = Activator.getDefault().getBundle();
// Activator is the activator class of the bundle containing the MyDsl file.
       Class<?> objectClass = bundle.loadClass("java.lang.Object");
       return objectClass.getClassLoader();
}
public Resource loadXtextResouce() throws Exception {
       WorkflowStandaloneSetup wfStandaloneSetup = new WorkflowStandaloneSetup();
       Injector injector = wfStandaloneSetup.createInjectorAndDoEMFRegistration();
       XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
       ClassLoader classLoader = getClassLoader();
       IJvmTypeProvider.Factory classpathTypeProviderFactory = new ClasspathTypeProviderFactory(classLoader);
       IJvmTypeProvider createTypeProvider = classpathTypeProviderFactory.createTypeProvider(resourceSet);
       resourceSet.setClasspathURIContext(createTypeProvider);
       resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
       URI uri = URI.createURI("platform:/plugin/com.mycompany.myapp.tests/model/myWorkflow1.workflow");
       return resourceSet.getResource(uri, true);
}


My problem is that my action references to:
- An instance of JvmVoidImplCustom through the reference 'javaClassJvmType'
- Null through the reference 'javaClassTypeRef'

Notice that the referred class is embedded in the same plug-in than the MyDsl file.

I assume that my XTextResourceSet is not initialized properly. Can anybody explain how to initialize an XTextResouceSet to be able to load resources stored in a plug-in?

Regards,
Grégoire
Re: How to load resources stored in a plug-in ? [message #1312690 is a reply to message #1297453] Thu, 24 April 2014 12:47 Go to previous messageGo to next message
Holger Schill is currently offline Holger SchillFriend
Messages: 71
Registered: July 2009
Member
The contract to for setting the context of the ResourceSet is Object but
what it needs is a IJavaElement or a classloader to load classes.

This is how we do it in the StandaloneBuilder shipped with Xtext's maven
support.

def protected void installTypeProvider(Iterable<String> classPathRoots,
XtextResourceSet resSet,
IndexedJvmTypeAccess typeAccess) {
val classLoader = createURLClassLoader(classPathRoots)
new ClasspathTypeProvider(classLoader, resSet, typeAccess)
resSet.setClasspathURIContext(classLoader);
}

def private URLClassLoader createURLClassLoader(Iterable<String>
classPathEntries) {
val classPathUrls = classPathEntries.map[str|new
File(str).toURI().toURL()]
return new URLClassLoader(classPathUrls)
}
Cheers,
Holger

On 15.04.14 19:41, Gregoire Dupe wrote:
> Hello,
>
> I have an XText grammar referring to a java classes. I've tried to use
> JvmType and JvmTypeReference.
> For my existing Ecore model "MyDsl", here is bellow my XText grammar :
>
>
> grammar com.myCompany.myapp.metamodel.xtext.MyDsl with
> org.eclipse.xtext.xbase.Xbase
>
> import "http://www.eclipse.org/xtext/common/JavaVMTypes" as jvmTypes
> import "http://www.myCompany.com/myapp/myDsl/0.1/myDsl" import
> "http://www.eclipse.org/emf/2002/Ecore" as ecore
> [...]
>
> Action returns Action:
> {Action}
> 'Action'
> name=ID
> '{'
> ('javaClassName' javaClassName=QualifiedName)?
> ('javaClass'
> javaClassJvmType=[jvmTypes::JvmType|QualifiedName])?
> ('javaClassR' javaClassTypeRef=JvmTypeReference)?
> [...]
> '}';
> [...]
>
>
> Here is a sample of an MyDsl file conforming to my grammar:
>
> Action MyAction {
> javaClass com.mycompany.myapp.tests.internal.sample1.MyAction
> javaClassR com.mycompany.myapp.tests.internal.sample1.MyAction
> }
>
>
> The XText editor resolves the reference to the Java class, hyperlink
> navigation is possible by "ctrl + left click".
> I'm trying to load an XText resource (conforming to the previous
> grammar) embedded in a plug-in with the following code :
>
>
> private static ClassLoader getClassLoader() throws ClassNotFoundException {
> Bundle bundle = Activator.getDefault().getBundle();
> // Activator is the activator class of the bundle containing the MyDsl
> file.
> Class<?> objectClass = bundle.loadClass("java.lang.Object");
> return objectClass.getClassLoader();
> }
> public Resource loadXtextResouce() throws Exception {
> WorkflowStandaloneSetup wfStandaloneSetup = new
> WorkflowStandaloneSetup();
> Injector injector =
> wfStandaloneSetup.createInjectorAndDoEMFRegistration();
> XtextResourceSet resourceSet =
> injector.getInstance(XtextResourceSet.class);
> ClassLoader classLoader = getClassLoader();
> IJvmTypeProvider.Factory classpathTypeProviderFactory = new
> ClasspathTypeProviderFactory(classLoader);
> IJvmTypeProvider createTypeProvider =
> classpathTypeProviderFactory.createTypeProvider(resourceSet);
> resourceSet.setClasspathURIContext(createTypeProvider);
> resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL,
> Boolean.TRUE);
> URI uri =
> URI.createURI("platform:/plugin/com.mycompany.myapp.tests/model/myWorkflow1.workflow");
>
> return resourceSet.getResource(uri, true);
> }
>
>
> My problem is that my action references to:
> - An instance of JvmVoidImplCustom through the reference
> 'javaClassJvmType'
> - Null through the reference 'javaClassTypeRef'
> Notice that the referred class is embedded in the same plug-in than the
> MyDsl file.
>
> I assume that my XTextResourceSet is not initialized properly. Can
> anybody explain how to initialize an XTextResouceSet to be able to load
> resources stored in a plug-in?
>
> Regards,
> Grégoire
>


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


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: How to load resources stored in a plug-in ? [message #1312691 is a reply to message #1297453] Thu, 24 April 2014 12:47 Go to previous messageGo to next message
Holger Schill is currently offline Holger SchillFriend
Messages: 71
Registered: July 2009
Member
The contract to for setting the context of the ResourceSet is Object but
what it needs is a IJavaElement or a classloader to load classes.

This is how we do it in the StandaloneBuilder shipped with Xtext's maven
support.

def protected void installTypeProvider(Iterable<String> classPathRoots,
XtextResourceSet resSet,
IndexedJvmTypeAccess typeAccess) {
val classLoader = createURLClassLoader(classPathRoots)
new ClasspathTypeProvider(classLoader, resSet, typeAccess)
resSet.setClasspathURIContext(classLoader);
}

def private URLClassLoader createURLClassLoader(Iterable<String>
classPathEntries) {
val classPathUrls = classPathEntries.map[str|new
File(str).toURI().toURL()]
return new URLClassLoader(classPathUrls)
}
Cheers,
Holger

On 15.04.14 19:41, Gregoire Dupe wrote:
> Hello,
>
> I have an XText grammar referring to a java classes. I've tried to use
> JvmType and JvmTypeReference.
> For my existing Ecore model "MyDsl", here is bellow my XText grammar :
>
>
> grammar com.myCompany.myapp.metamodel.xtext.MyDsl with
> org.eclipse.xtext.xbase.Xbase
>
> import "http://www.eclipse.org/xtext/common/JavaVMTypes" as jvmTypes
> import "http://www.myCompany.com/myapp/myDsl/0.1/myDsl" import
> "http://www.eclipse.org/emf/2002/Ecore" as ecore
> [...]
>
> Action returns Action:
> {Action}
> 'Action'
> name=ID
> '{'
> ('javaClassName' javaClassName=QualifiedName)?
> ('javaClass'
> javaClassJvmType=[jvmTypes::JvmType|QualifiedName])?
> ('javaClassR' javaClassTypeRef=JvmTypeReference)?
> [...]
> '}';
> [...]
>
>
> Here is a sample of an MyDsl file conforming to my grammar:
>
> Action MyAction {
> javaClass com.mycompany.myapp.tests.internal.sample1.MyAction
> javaClassR com.mycompany.myapp.tests.internal.sample1.MyAction
> }
>
>
> The XText editor resolves the reference to the Java class, hyperlink
> navigation is possible by "ctrl + left click".
> I'm trying to load an XText resource (conforming to the previous
> grammar) embedded in a plug-in with the following code :
>
>
> private static ClassLoader getClassLoader() throws ClassNotFoundException {
> Bundle bundle = Activator.getDefault().getBundle();
> // Activator is the activator class of the bundle containing the MyDsl
> file.
> Class<?> objectClass = bundle.loadClass("java.lang.Object");
> return objectClass.getClassLoader();
> }
> public Resource loadXtextResouce() throws Exception {
> WorkflowStandaloneSetup wfStandaloneSetup = new
> WorkflowStandaloneSetup();
> Injector injector =
> wfStandaloneSetup.createInjectorAndDoEMFRegistration();
> XtextResourceSet resourceSet =
> injector.getInstance(XtextResourceSet.class);
> ClassLoader classLoader = getClassLoader();
> IJvmTypeProvider.Factory classpathTypeProviderFactory = new
> ClasspathTypeProviderFactory(classLoader);
> IJvmTypeProvider createTypeProvider =
> classpathTypeProviderFactory.createTypeProvider(resourceSet);
> resourceSet.setClasspathURIContext(createTypeProvider);
> resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL,
> Boolean.TRUE);
> URI uri =
> URI.createURI("platform:/plugin/com.mycompany.myapp.tests/model/myWorkflow1.workflow");
>
> return resourceSet.getResource(uri, true);
> }
>
>
> My problem is that my action references to:
> - An instance of JvmVoidImplCustom through the reference
> 'javaClassJvmType'
> - Null through the reference 'javaClassTypeRef'
> Notice that the referred class is embedded in the same plug-in than the
> MyDsl file.
>
> I assume that my XTextResourceSet is not initialized properly. Can
> anybody explain how to initialize an XTextResouceSet to be able to load
> resources stored in a plug-in?
>
> Regards,
> Grégoire
>


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


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: How to load resources stored in a plug-in ? [message #1336584 is a reply to message #1312691] Tue, 06 May 2014 13:11 Go to previous message
Gregoire Dupe is currently offline Gregoire DupeFriend
Messages: 75
Registered: September 2009
Location: France
Member
Hello,

I understood my mistake: The right way to get the class loader is the following one:

	private static ClassLoader getClassLoader(final Bundle bundle) {
		/*
		 * Where plug-in is the activator class of the bundle containing the
		 * MyDsl file.
		 */
		final BundleWiring adapt = bundle.adapt(BundleWiring.class);
		return adapt.getClassLoader();
	}


Thanks for your help.

Regards,
Grégoire
Previous Topic:Xtext plugin and context menu handler issue
Next Topic:serializing: how to?
Goto Forum:
  


Current Time: Fri Dec 19 10:52:14 GMT 2014

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

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