[SOLVED] Invalid injection for JavaElementDelegateAdapter [message #1761040] |
Fri, 05 May 2017 14:25 |
|
Dear all.
I would like to have the "Run as Java Application" on my DSL script.
I have added the following lines in my plugin.xml:
<extension point="org.eclipse.core.runtime.adapters">
<factory
class="io.sarl.eclipse.SARLEclipseExecutableExtensionFactory:io.sarl.eclipse.launching.shortcuts.SARLJavaElementDelegateAdapterFactory"
adaptableType="org.eclipse.ui.IFileEditorInput">
<adapter type="org.eclipse.xtext.xbase.ui.launching.JavaElementDelegateMainLaunch"/>
</factory>
<factory
class="io.sarl.eclipse.SARLEclipseExecutableExtensionFactory:io.sarl.eclipse.launching.shortcuts.SARLJavaElementDelegateAdapterFactory"
adaptableType="org.eclipse.ui.IEditorPart">
<adapter type="org.eclipse.xtext.xbase.ui.launching.JavaElementDelegateMainLaunch"/>
</factory>
<factory
class="io.sarl.eclipse.SARLEclipseExecutableExtensionFactory:io.sarl.eclipse.launching.shortcuts.SARLJavaElementDelegateAdapterFactory"
adaptableType="org.eclipse.core.resources.IResource">
<adapter type="org.eclipse.xtext.xbase.ui.launching.JavaElementDelegateMainLaunch"/>
</factory>
<factory
class="io.sarl.eclipse.SARLEclipseExecutableExtensionFactory:io.sarl.eclipse.launching.shortcuts.SARLJavaElementDelegateAdapterFactory"
adaptableType="org.eclipse.xtext.xbase.ui.launching.JavaElementDelegateMainLaunch">
<adapter type="org.eclipse.jdt.core.IJavaElement"/>
</factory>
</extension>
<extension
point="org.eclipse.debug.ui.launchShortcuts">
<shortcut
class="io.sarl.eclipse.SARLEclipseExecutableExtensionFactory:org.eclipse.xtext.xbase.ui.launching.JavaApplicationLaunchShortcut"
icon="platform:/plugin/org.eclipse.jdt.debug.ui/icons/full/etool16/java_app.gif"
id="io.sarl.eclipse.launching.shortcuts.LaunchJavaApplicationShortcut"
label="%launch.application"
helpContextId="org.eclipse.jdt.debug.ui.shortcut_local_java_application"
modes="run,debug">
<contextualLaunch>
<contextLabel mode="run" label="%launch.application.runas" />
<contextLabel mode="debug" label="%launch.application.debugas" />
<enablement>
<with variable="selection">
<count value="1"/>
<iterate ifEmpty="false" operator="or">
<test property="io.sarl.eclipse.hasMain"/>
</iterate>
</with>
</enablement>
</contextualLaunch>
<configurationType
id="org.eclipse.jdt.launching.localJavaApplication">
</configurationType>
<description
description="%launch.application.runas.description"
mode="run">
</description>
<description
description="%launch.application.debugas.description"
mode="debug">
</description>
</shortcut>
</extension>
When I run my product, and click on "Run As Java Application", I obtain a error message indicating that no main function was found.
When I debug my product with a breakpoint into JavaApplicationLaunchShortcut, the instance of JavaElementDelegateAdapter that is used for founding the main function is injected with Xtend elements, not with the elements for my DSL.
Note that Xtend is included in my product too.
I have defined adapter factories that seem to be never invoked.
What is going wrong?
Thank you by advance.
Stéphane.
[Updated on: Sat, 06 May 2017 08:37] Report message to a moderator
|
|
|
|
|
|
Re: Invalid injection for JavaElementDelegateAdapterFactory [message #1761046 is a reply to message #1761044] |
Fri, 05 May 2017 15:00 |
|
The initial requester is "org.eclipse.xtext.xbase.ui.launching.JavaApplicationLaunchShortcut", which do:
LaunchShortcutUtil.replaceWithJavaElementDelegates((IStructuredSelection) selection, JavaElementDelegateMainLaunch.class);
The "replaceWithJavaElementDelegates" invokes "getAdapter" with the "JavaElementDelegateMainLaunch.class" as parameter.
The replied adapter is not null, but the injected fields inside have objects injected by the Xtend module, not my DSL module (specifically, the file extension is "xtend", not "sarl"), the IJvmModelAssociations is the one from Xtend module not from my DSL module.
Obviously, I have defined a adapter factory that is not invoked. I certainly means that the adapter factory provided by Xtend are still active.
Regarding the editor, I did not change the things generated by the MWE2 scripts.
[Updated on: Fri, 05 May 2017 15:08] Report message to a moderator
|
|
|
|
|
Re: Invalid injection for JavaElementDelegateAdapterFactory [message #1761080 is a reply to message #1761050] |
Sat, 06 May 2017 08:36 |
|
For who has a similar issue:
1) Define a specific delegate class for enabling you to define your own adapters.
public class MyJavaElementDelegateMainLaunch extends org.eclipse.xtext.xbase.ui.launching.JavaElementDelegateMainLaunch {
}
2) Define the factory:
public class MyJavaElementDelegateAdapterFactory extends org.eclipse.xtext.xbase.ui.launching.JavaElementDelegateAdapterFactory {
@Inject
private Provider<MyJavaElementDelegateMainLaunch> mainDelegateProvider;
@Override
public Object getAdapter(Object adaptableObject, @SuppressWarnings("rawtypes") Class adapterType) {
JavaElementDelegate result = null;
if (MyJavaElementDelegateMainLaunch.class.equals(adapterType)) {
result = this.mainDelegateProvider.get();
}
if (result != null) {
if (adaptableObject instanceof IFileEditorInput) {
result.initializeWith((IFileEditorInput) adaptableObject);
return result;
}
if (adaptableObject instanceof IResource) {
result.initializeWith((IResource) adaptableObject);
return result;
}
if (adaptableObject instanceof IEditorPart) {
result.initializeWith((IEditorPart) adaptableObject);
return result;
}
}
return super.getAdapter(adaptableObject, adapterType);
}
}
3) Define the shortcut:
public class LaunchMainShortcut extends org.eclipse.jdt.debug.ui.launchConfigurations.JavaApplicationLaunchShortcut {
@Override
public void launch(ISelection selection, String mode) {
if (selection instanceof IStructuredSelection) {
final IStructuredSelection newSelection = LaunchShortcutUtil
.replaceWithJavaElementDelegates((IStructuredSelection) selection, MyJavaElementDelegateMainLaunch.class);
super.launch(newSelection, mode);
}
}
@Override
public void launch(IEditorPart editor, String mode) {
final SarlJavaElementDelegateMainLaunch javaElementDelegate = editor.getAdapter(MyJavaElementDelegateMainLaunch.class);
if (javaElementDelegate != null) {
super.launch(new StructuredSelection(javaElementDelegate), mode);
} else {
super.launch(editor, mode);
}
}
}
4) Update the plugin.xml file:
<extension point="org.eclipse.core.runtime.adapters">
<factory
class="mypackage.MyEclipseExecutableExtensionFactory:mypackage.MyJavaElementDelegateAdapterFactory"
adaptableType="org.eclipse.ui.IFileEditorInput">
<adapter type="mypackage.MyJavaElementDelegateMainLaunch"/>
</factory>
<factory
class="mypackage.MyEclipseExecutableExtensionFactory:mypackage.MyJavaElementDelegateAdapterFactory"
adaptableType="org.eclipse.ui.IEditorPart">
<adapter type="mypackage.MyJavaElementDelegateMainLaunch"/>
</factory>
<factory
class="mypackage.MyEclipseExecutableExtensionFactory:mypackage.MyJavaElementDelegateAdapterFactory"
adaptableType="org.eclipse.core.resources.IResource">
<adapter type="mypackage.MyJavaElementDelegateMainLaunch"/>
</factory>
<factory
class="mypackage.MyEclipseExecutableExtensionFactory:mypackage.MyJavaElementDelegateAdapterFactory"
adaptableType="org.eclipse.jdt.core.IJavaElement">
<adapter type="mypackage.MyJavaElementDelegateMainLaunch"/>
</factory>
<factory
class="mypackage.MyEclipseExecutableExtensionFactory:mypackage.MyJavaElementDelegateAdapterFactory"
adaptableType="mypackage.MyJavaElementDelegateMainLaunch">
<adapter type="org.eclipse.jdt.core.IJavaElement"/>
</factory>
</extension>
<extension
point="org.eclipse.debug.ui.launchShortcuts">
<shortcut
class="mypackage.MyEclipseExecutableExtensionFactory:mypackage.LaunchMainShortcut"
icon="..."
id="..."
label="..."
modes="run,debug">
<contextualLaunch>
<contextLabel mode="run" label="..." />
<contextLabel mode="debug" label="..." />
<enablement>
<with variable="selection">
<count value="1"/>
<iterate ifEmpty="false" operator="or">
<test property="mypackage.hasMain"/>
</iterate>
</with>
</enablement>
</contextualLaunch>
</shortcut>
</extension>
The enablement condition is specific to my project. You should update and adapt to your own project.
|
|
|
Powered by
FUDForum. Page generated in 0.03190 seconds