Injecting an IParser (yes, again...) [message #1096651] |
Wed, 28 August 2013 17:26 |
Wayne Campbell 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
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 #1097179 is a reply to message #1096667] |
Thu, 29 August 2013 10:32 |
Wayne Campbell 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
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
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03149 seconds