Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 367487

Summary: JvmConstructor body is generated only when it has parameters
Product: [Modeling] TMF Reporter: rjarana Mising name <rayco.arana>
Component: XtextAssignee: Jan Koehnlein <jan>
Status: CLOSED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: christian.dietrich.opensource, ingo.boegemann, jan, lorenzo.bettini, sven.efftinge, tmf.xtext-inbox
Version: 2.2.0Flags: jan: juno+
Target Milestone: M5   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description rjarana Mising name CLA 2011-12-23 06:28:03 EST
Build Identifier: 20110615-0604

When you create a default constructor with a body generated in code, it is ignored.

Debugging at JvmModelGenerator._generateMember(final JvmConstructor it,...), there's a condition that check if it hasn't any parameters, tries to check if it has a XExpression body and if not, exit the method.

The correct code must do the same that there is in generateBody method, where it also check if there is a CompilationStrategyAdapter for it.

Reproducible: Always
Comment 1 Jan Koehnlein CLA 2012-01-03 09:53:27 EST
Pushed to MASTER
Comment 2 Jan Koehnlein CLA 2012-01-03 11:10:41 EST
I added one more condition: If there is a default constructor without any implementation hint next to another constructor, it is generated with an empty body. We are able to link aginst it, so it has to exist in Java, too.
Comment 3 Lorenzo Bettini CLA 2012-01-13 07:14:26 EST
The same problem is there also in the presence of constructor parameters...
Comment 4 Ingo Boegemann CLA 2012-02-23 15:48:33 EST
(In reply to comment #3)
> The same problem is there also in the presence of constructor parameters...

The reason for the empty body lies in the fact that the JvmTypesBuilder already added the empty body to the constructor before applying the init procedure.

If a body was added it becomes the second adapter in the eAdapters and is hence ignored.
Extending the JvmTypesBuilder and commenting this out removed this particular problem for me:
public class ConstructorCapableJvmTypesBuilder extends JvmTypesBuilder {
	public JvmConstructor toConstructor(EObject sourceElement, String simpleName, Procedure1<JvmConstructor> init) {
		JvmConstructor constructor = TypesFactory.eINSTANCE.createJvmConstructor();
		constructor.setSimpleName(nullSaveName(simpleName));
//		setBody(constructor, new Function1<ImportManager, CharSequence>() {
//			public CharSequence apply(ImportManager p) {
//				return "{}";
//			}
//		});
		if (init != null && simpleName != null)
			init.apply(constructor);
		return associate(sourceElement, constructor);
	}
}
Comment 5 Sven Efftinge CLA 2012-02-23 16:13:54 EST
This has also already been fixed in current master.
Comment 6 Ingo Boegemann CLA 2012-02-23 16:15:24 EST
(In reply to comment #5)
> This has also already been fixed in current master.

Great! Looking forward to 2.3!
Comment 7 Karsten Thoms CLA 2017-09-19 17:03:35 EDT
Closing all bugs that were set to RESOLVED before Neon.0
Comment 8 Karsten Thoms CLA 2017-09-19 17:15:13 EDT
Closing all bugs that were set to RESOLVED before Neon.0