Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [SOLVED] Invalid injection for JavaElementDelegateAdapter
[SOLVED] Invalid injection for JavaElementDelegateAdapter [message #1761040] Fri, 05 May 2017 14:25 Go to next message
Stéphane Galland is currently offline Stéphane GallandFriend
Messages: 123
Registered: July 2014
Location: Belfort, France
Senior Member
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 #1761041 is a reply to message #1761040] Fri, 05 May 2017 14:31 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
this looks similar to https://bugs.eclipse.org/bugs/show_bug.cgi?id=457313 and https://bugs.eclipse.org/bugs/show_bug.cgi?id=471773

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Invalid injection for JavaElementDelegateAdapterFactory [message #1761042 is a reply to message #1761041] Fri, 05 May 2017 14:35 Go to previous messageGo to next message
Stéphane Galland is currently offline Stéphane GallandFriend
Messages: 123
Registered: July 2014
Location: Belfort, France
Senior Member
Indeed, it is. As soon as I understand, I need to provide my own launching classes because Xtend plugin already provide the adapters that are used by the Xbase classes.

Thank you.
Re: Invalid injection for JavaElementDelegateAdapterFactory [message #1761044 is a reply to message #1761042] Fri, 05 May 2017 14:41 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
good question. have never tried this myself.

which is the class that asks for the JavaElementDelegateAdapter? is it the editor?
if yes did you try to bind a subclass of xbaseeditor,
and maybe changed edit part to your editor class in the plugin xml


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Invalid injection for JavaElementDelegateAdapterFactory [message #1761046 is a reply to message #1761044] Fri, 05 May 2017 15:00 Go to previous messageGo to next message
Stéphane Galland is currently offline Stéphane GallandFriend
Messages: 123
Registered: July 2014
Location: Belfort, France
Senior Member
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 #1761049 is a reply to message #1761046] Fri, 05 May 2017 15:15 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
no the qeustion is:

that the the thing that is adapted? the iresource the edior ????

i assume that all adaper factories are asked,
but the first one wins


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Invalid injection for JavaElementDelegateAdapterFactory [message #1761050 is a reply to message #1761049] Fri, 05 May 2017 15:18 Go to previous messageGo to next message
Stéphane Galland is currently offline Stéphane GallandFriend
Messages: 123
Registered: July 2014
Location: Belfort, France
Senior Member
I have tried on IResource and on the editor. It runs in similar ways both.

I agree with your assumption. I will implements a new adapter type that extends the original one. But because it will be a different type, it will be found first by the adapter manager.
Re: Invalid injection for JavaElementDelegateAdapterFactory [message #1761080 is a reply to message #1761050] Sat, 06 May 2017 08:36 Go to previous message
Stéphane Galland is currently offline Stéphane GallandFriend
Messages: 123
Registered: July 2014
Location: Belfort, France
Senior Member
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.
Previous Topic:Import generation in model file
Next Topic:Xtext 2.12 and Guava
Goto Forum:
  


Current Time: Tue Apr 16 22:08:38 GMT 2024

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

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

Back to the top