Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Injecting an IParser (yes, again...)
Injecting an IParser (yes, again...) [message #1096651] Wed, 28 August 2013 17:26 Go to next message
Wayne Campbell is currently offline Wayne CampbellFriend
Messages: 11
Registered: June 2013
Junior Member
Hey folks,

i know this topic has been around a lot, so hopefully I don't get killed for asking Wink

Short version of my problem:
What is the correct way to inject an IParser from an XText project into my own Plugin, that itself is not a full RCP application?

---

Lengthy problem description:
I have trouble getting an Injector for IParser from within my own plugin. Imagine the following project setup:
- com.example.myRCPApplication
- com.example.myPlugin (extends myRCPApplication, uses myDsl)
- com.example.myDsl
- com.example.myDsl.sdk
- com.example.myDsl.ui

myPlugin contains a few UI elements that are contributed to myRCPApplication. One of the handlers for these UI elements should read in a file and process it using the Xtext parser.

I do understand that I must not use the standalone setup as this would kill my EMF registry. Instead, I should use MyDslExecutableExtensionFactory from myDsl.ui to get an IParser injected by Guice.

The code for my handler looks like this:
public class StartParsingClickedHandler extends AbstractHandler {

    @Inject
    private IParser parser;

    public StartParsingClickedHandler() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        Shell shell = new Shell(Display.getDefault());
        FileDialog dialog = new FileDialog(shell);
        dialog.open();

        String selectedPath = dialog.getFilterPath() + File.separator + dialog.getFileName();

        BufferedReader f;
        try {
            f = new BufferedReader(new FileReader(selectedPath));
            this.invokeParser(f);
        } catch (IOException e) {
            System.out.println("e.getMessage()");
        }
		
        return null;
    }

    public void invokeParser(Reader reader) {
        IParseResult result = parser.parse(reader);

        if (result.hasSyntaxErrors()) {
            System.out.println("Parse error");
        }
    }	
}


The plugin.xml of myPlugin looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            locationURI="menu:org.eclipse.ui.main.menu">
         <menu
               label="Import">
            <command
                  commandId="com.example.reader.mydsl.import"
                  style="push">
            </command>
         </menu>
      </menuContribution>
   </extension>
   <extension
         point="org.eclipse.ui.commands">
      <command
            id="com.example.reader.mydsl.import"
            name="Import">
      </command>
   </extension>
   <extension
         point="org.eclipse.ui.handlers">
      <handler
            class="com.example.myPlugin.handlers.ImportClickedHandler"
            commandId="com.example.reader.mydsl.import">
      </handler>
   </extension>
</plugin>


Now, if I run this myRCPApplication and I invoke the "Import" command, the corresponding handler is executed. Unfortunately, the call to parser.parse() results in a NullPointerException.

Obviously the IParser was not injected by Guice.

I have read a lot of documentation and found out, that adding "MyDslExecutableExtensionFactory:" to the handler's class should solve my problem. But it does not.

I have tried the following steps (in every possible combination - yay, Brute Force...):
- including the com.example.myDsl.ui plugin to my run configuration
- adding a dependency to com.example.myDsl.ui to myPlugin
- putting the plugin name in front of "MyDslExecutableExtensionFactory:"
- putting the package name in front of "MyDslExecutableExtensionFactory:"
- rotating myself vertically very rapidly while swearing like a trooper

Didn't help me in any way, I always got a NullPointerException when it came to the parser.parse() call.


I would really appreciate your help as I am hopelessly stuck here :\

Thanks in advance!
Re: Injecting an IParser (yes, again...) [message #1096663 is a reply to message #1096651] Wed, 28 August 2013 17:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi

the error is the plugin xml (you dont get guice without doing anything)

class="com.example.myPlugin.handlers.ImportClickedHandler"

should be

class="xyz.YourDslExecutableExtensionFactory:com.example.myPlugin.handlers.ImportClickedHandler"


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Injecting an IParser (yes, again...) [message #1096666 is a reply to message #1096663] Wed, 28 August 2013 17:49 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
P.S: if you have a extra plugin you have to create an extra factory.

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Injecting an IParser (yes, again...) [message #1096667 is a reply to message #1096666] Wed, 28 August 2013 17:50 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
maybe http://kthoms.wordpress.com/2011/09/28/moving-an-xtend-generator-into-its-own-plugin/ helps

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Injecting an IParser (yes, again...) [message #1097179 is a reply to message #1096667] Thu, 29 August 2013 10:32 Go to previous messageGo to next message
Wayne Campbell is currently offline Wayne CampbellFriend
Messages: 11
Registered: June 2013
Junior Member
Thanks for your quick reply. The tutorial you linked definetely points in the right direction.

Now I am not only running inte NullPointerExceptions, but also inte error messages produced by Guice. First of all, it is great to see Guice talking to me Smile

I managed to fix 6 out of 9 Guice error messages, but I have problems resolving the remaining 3.

---

First of all, a list of steps I performed:

1.) I implemented the following classes within my plugin project:

- MyPluginUiModule
Here I use the BuilderParticipant.class instead of the JavaProjectBasedBuilderParticipant.class, as the use of the latter one is deprecated.

- ExecutableExtensionFactory
I did not use all of the implementation details stated in the example, but used the MyDslExecutableExtensionFactory from the MyDsl.ui plugin as a template

- Activator
I just updated the existing one.


2.) I added the ExecutableExtensinFactory to my Handler definition within my plugin.xml:

   <extension
         point="org.eclipse.ui.handlers">
      <handler
            class="com.example.MyPlugin.internal.ExecutableExtensionFactory:com.example.MyPlugin.handlers.ImportClickedHandler"
            commandId="com.example.MyPlugin.import">
      </handler>
   </extension>


---

My RCP application starts normally, and when I trigger the command to execute my handler, Guice starts complaining. I get 3 errors, each of them stating basically the same error message:

"Error injecting constructor (as a result of a java.lang.IllegalStateException)"

1) Error injecting constructor, org.eclipse.emf.common.util.WrappedException: org.eclipse.xtext.resource.ClasspathUriResolutionException: java.lang.IllegalStateException: Couldn't find resource on classpath. URI was 'classpath:/com/example/MyDsl.xmi'
  at org.eclipse.xtext.common.services.TerminalsGrammarAccess.<init>(Unknown Source)
  at org.eclipse.xtext.common.services.TerminalsGrammarAccess.class(Unknown Source)
  while locating org.eclipse.xtext.common.services.TerminalsGrammarAccess
...
while locating org.eclipse.xtext.parser.IParser
    for field at com.example.MyPlugin.handlers.ImportClickedHandler.parser(Unknown Source)
  while locating com.example.MyPlugin.handlers.ImportClickedHandler
Caused by: org.eclipse.emf.common.util.WrappedException: org.eclipse.xtext.resource.ClasspathUriResolutionException: java.lang.IllegalStateException: Couldn't find resource on classpath. URI was 'classpath:/com/example/MyfDsl.xmi'
	at org.eclipse.xtext.parser.BaseEPackageAccess.loadResource(BaseEPackageAccess.java:57)
...

I omitted most of the output to keep things well-arranged - let me know if you need the complete call stack.
Well, I have no idea where to look at to resolve the issue...


Additionally, I get the following error messages from the OSGi framework:

!ENTRY org.eclipse.ui 4 4 2013-08-29 11:37:20.653
!MESSAGE The proxied handler for 'com.example.MyPlugin.internal.ExecutableExtensionFactory:com.example.MyPlugin.handlers.ImportClickedHandler' could not be loaded

!ENTRY org.eclipse.ui 4 0 2013-08-29 11:37:20.654
!MESSAGE The proxied handler for 'com.example.MyPlugin.internal.ExecutableExtensionFactory:com.example.MyPlugin.handlers.ImportClickedHandler' could not be loaded


These are obviously related to my handler specification in the plugin.xml, but I guess (!!) that these errors disappear as soon as Guice is happy with my project.


Again I would really appreciate your advice - thanks in advance!

[Updated on: Thu, 29 August 2013 10:33]

Report message to a moderator

Re: Injecting an IParser (yes, again...) [message #1097254 is a reply to message #1097179] Thu, 29 August 2013 12:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i guess you have to use the activator of yourdsl.ui module.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Injecting an IParser (yes, again...) [message #1098658 is a reply to message #1097254] Sat, 31 August 2013 12:39 Go to previous message
Wayne Campbell is currently offline Wayne CampbellFriend
Messages: 11
Registered: June 2013
Junior Member
Yep, you are absolutely right. I now use the activator vom the .ui plugin, and everything works perfectly.

Thank you very much!!


FYI, if someone runs into error messages stating that the MyDsl.xmi is missing: just run your Xtext workflow, it will be generated then Wink
Previous Topic:Modify the STRING Terminal
Next Topic:mwe2 Workflow passed, but Eclipse can't start my DSL-Editor
Goto Forum:
  


Current Time: Wed Apr 24 15:52:26 GMT 2024

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

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

Back to the top