Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » DSL Type Parameter is not used in extends
DSL Type Parameter is not used in extends [message #1857599] Thu, 16 February 2023 13:50 Go to next message
Thomas Olbrich is currently offline Thomas OlbrichFriend
Messages: 2
Registered: February 2023
Junior Member
In my DSL a Entity should be extended with a Java-class

***************************
	Entity:
		'entity' name=ValidID
    		('<' typeParameters+=JvmTypeParameter (',' typeParameters+=JvmTypeParameter)* '>')?
    		('extends' superType=JvmTypeReference)?

***************************

In the inferrer the entity-class is generated with following code:

***************************
	def dispatch void infer(Entity entity, IJvmDeclaredTypeAcceptor acceptor, boolean isPrelinkingPhase) {
		acceptor.accept(entity.toClass(entity.fullyQualifiedName)) [ entityType |
			for ( typeParameter : entity.typeParameters) {
				entityType.typeParameters += typeParameter.cloneWithProxies
			}
			if (entity.superType !== null) {
				entityType.superTypes += entity.superType.cloneWithProxies
			}
			else {
				entityType.superTypes += Object.typeRef
			}

***************************

Wenn a entity with type-parameter is defined and the type-parameter is used as type-argument of the super-class, than as type-argument a questionmark ? is generated:

*** generated: ************************
public class Test<T extends Number> extends ArrayList<?> {

***************************

the dsl-code was
***************************
	entity Test<T extends Number> extends ArrayList<T> {}

***************************

Question: how does the inferrer have to be adjusted so that the type argument of the super class is generated correctly with the type-argument T

*** should be generated: ************************
public class Test<T extends Number> extends ArrayList<T> {

***************************


Re: DSL Type Parameter is not used in extends [message #1857611 is a reply to message #1857599] Thu, 16 February 2023 20:14 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14736
Registered: July 2009
Senior Member
hi, i tried this with domain model example in xtext/master and am getting

public class Test<T extends Number> extends ArrayList<T> {

=> can you provide reproducing example and unit test?

@Test def void compareGeneratedJava() throws Exception {
		'''
			import java.util.ArrayList
			
			entity Test<T extends Number> extends ArrayList<T> {}
		'''.compile[assertEquals('''
			import java.util.ArrayList;
			import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;
			
			@SuppressWarnings("all")
			public class Test<T extends Number> extends ArrayList<T> {
			  @Override
			  public String toString() {
			    String result = new ToStringBuilder(this).addAllFields().toString();
			    return result;
			  }
			}
		'''.toString, getSingleGeneratedCode)
		]
	}


class DomainmodelJvmModelInferrer extends AbstractModelInferrer {

	@Inject extension JvmTypesBuilder
	@Inject extension IQualifiedNameProvider
	@Inject extension DomainmodelJvmModelHelper
	@Inject extension IJvmModelAssociations
	@Inject extension IJvmModelAssociator

	def dispatch infer(Entity entity, extension IJvmDeclaredTypeAcceptor acceptor, boolean prelinkingPhase) {
		accept(entity.toClass( entity.fullyQualifiedName )) [
			documentation = entity.documentation
			for ( typeParameter : entity.typeParameters) {
				it.typeParameters += typeParameter.cloneWithProxies
			}
			if (entity.superType !== null) {
				it.superTypes += entity.superType.cloneWithProxies
			}
			else {
				it.superTypes += Object.typeRef
			}

			// let's add a default constructor
			members += entity.toConstructor []

			// now let's go over the features
			for ( f : entity.features ) {
.....


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: DSL Type Parameter is not used in extends [message #1857613 is a reply to message #1857611] Thu, 16 February 2023 21:16 Go to previous messageGo to next message
Thomas Olbrich is currently offline Thomas OlbrichFriend
Messages: 2
Registered: February 2023
Junior Member
Please excuse me for giving the wrong example.
If an entity with a type parameter is specified as the super class, <?> is generated
 
package tommy.o.mvvm.dsl.test {

	entity TstEntityChildWithTypeParameter<T extends Number> {
		type : T
	}
	
	entity TstEntityChildWithTypeParameterSub<T extends Number> extends TstEntityChildWithTypeParameter<T> {
	}
}


compiles to

package tommy.o.mvvm.dsl.test;

@SuppressWarnings("all")
public class TstEntityChildWithTypeParameterSub<T extends Number> extends TstEntityChildWithTypeParameter<?> {

...

Re: DSL Type Parameter is not used in extends [message #1857628 is a reply to message #1857613] Fri, 17 February 2023 09:11 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14736
Registered: July 2009
Senior Member
still cannot reproduce

MULTIPLE FILES WERE GENERATED

File 1 : /myProject/./src-gen/tommy/o/mvvm/dsl/test/TstEntityChildWithTypeParameter.java

package tommy.o.mvvm.dsl.test;

import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

@SuppressWarnings("all")
public class TstEntityChildWithTypeParameter<T extends Number> {
private T type;

public T getType() {
return this.type;
}

public void setType(final T type) {
this.type = type;
}

@Override
public String toString() {
String result = new ToStringBuilder(this).addAllFields().toString();
return result;
}
}

File 2 : /myProject/./src-gen/tommy/o/mvvm/dsl/test/TstEntityChildWithTypeParameterSub.java

package tommy.o.mvvm.dsl.test;

import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

@SuppressWarnings("all")
public class TstEntityChildWithTypeParameterSub<T extends Number> extends TstEntityChildWithTypeParameter<T> {
@Override
public String toString() {
String result = new ToStringBuilder(this).addAllFields().toString();
return result;
}
}



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: DSL Type Parameter is not used in extends [message #1857629 is a reply to message #1857628] Fri, 17 February 2023 09:12 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14736
Registered: July 2009
Senior Member
=> can you please provide a reproducer. maybe you have something else in your env that causes this

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: DSL Type Parameter is not used in extends [message #1857631 is a reply to message #1857613] Fri, 17 February 2023 09:34 Go to previous message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Thomas,

I pasted the grammar snippet you provided into the DomainModel example:

Entity:
	'entity' name=ValidID
	('<' typeParameters+=JvmTypeParameter (',' typeParameters+=JvmTypeParameter)* '>')? 
	'extends' superType=JvmParameterizedTypeReference)? '{'
		features+=Feature*
	'}';


And added these lines to the inferred

for ( typeParameter : entity.typeParameters) {
	it.typeParameters += typeParameter.cloneWithProxies
}


The compiled code for my sample model looks good.

package somePackage {
  entity A<T extends Number> {
	t: T
  }
  entity B<T extends Number> extends A<T> {}  	
}


package somePackage;

import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

@SuppressWarnings("all")
public class B<T extends Number> extends A<T> {
  public B() {
  }

  public B(final Procedure1<B> initializer) {
    initializer.apply(this);
  }

  @Override
  public String toString() {
    String result = new ToStringBuilder(this).addAllFields().toString();
    return result;
  }
}



I'm afraid you need to provide more information. Indeally a self-contained example with a unit test. If you have that available, please file a ticket on https://github.com/eclipse/xtext-extras.

[Updated on: Fri, 17 February 2023 09:34]

Report message to a moderator

Previous Topic:InMemoryJavaCompiler: test for Java 8 compatibility
Next Topic: Xtext 2.30.0.M3 is out
Goto Forum:
  


Current Time: Wed Dec 11 22:36:03 GMT 2024

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

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

Back to the top