unresolved jvmTypeReference as generic type parameter [message #1778699] |
Wed, 20 December 2017 10:20 |
paul lu Messages: 43 Registered: April 2013 |
Member |
|
|
Hi,
I reproduced the case with a simpler example (xtext version 2.12),
test DSL file:
test.mydsl
element MyElement : String
collection MyCollection of MyElement
The expected Java is :
public class MyCollection implements Collection<String>{
..
}
where MyCollection is a collection type of the type of the MyElement.
The simple case reproduces the resolution of the jvm type in the following pattern:
For MyElement: String is resolved as :
MyElement --typeRef--> String
For MyCollection: String is resolved as :
MyCollection --elementRef--> MyElement ---typeRef--> String
It should have something to do with cyclic resolution, so actually I got the following generated Java:
@SuppressWarnings("all")
public class MyCollection implements /* Collection<Object> */ {
private String foo;
private /* Collection<Object> */Object foos;
}
Separating the files solves the problem only in Maven build and only when
the file containing "element MyElement : String" is loaded first by naming the file with something like aaaaa.mydsl (and another file named zzzz.mydsl)
I failed to get the expected result in Eclipse either.
the grammar:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.xbase.annotations.XbaseWithAnnotations
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
Model:
elements+=Entity*;
Entity:
Element | Collection;
Element:
'element' name=ID ':' typeRef=JvmTypeReference;
Collection:
'collection' name=ID 'of' elementRef=[Element];
Jvm Inferrer:
class MyDslJvmModelInferrer extends AbstractModelInferrer {
@Inject extension JvmTypesBuilder
def dispatch void infer(Element element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
acceptor.accept(element.toClass(element.name))[
// it.superTypes += element.typeRef.type.typeRef()
it.superTypes += element.typeRef.cloneWithProxies
]
}
def dispatch void infer(Collection collection, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
acceptor.accept(collection.toClass(collection.name))[
// it.superTypes += "java.util.Collection".typeRef(
// collection.elementRef.typeRef.type.typeRef
// )
it.superTypes += "java.util.Collection".typeRef(
collection.elementRef.typeRef.cloneWithProxies
)
it.members += collection.toField("foo", collection.elementRef.typeRef.cloneWithProxies)
it.members += collection.toField("foos", "java.util.Collection".typeRef(
collection.elementRef.typeRef.cloneWithProxies
))
]
}
}
It looks like the jvmType gets wrong only when used as type parameter,
private /* Collection<Object> */Object foos;
it gets right in :
private String foo
Unit test, grammar, and jvm inferrer are also attached.
Thanks
|
|
|
|
|
|
|
|
Re: unresolved jvmTypeReference as generic type parameter [message #1778745 is a reply to message #1778741] |
Wed, 20 December 2017 17:09 |
|
why always element.typeRef.cloneWithProxies
class MyDslJvmModelInferrer extends AbstractModelInferrer {
@Inject extension JvmTypesBuilder
def dispatch void infer(Element element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
acceptor.accept(element.toClass(element.name))[
it.superTypes += "java.util.function.Supplier".typeRef(
element.typeRef
)
it.members += element.toField("foo", element.typeRef )
]
}
def dispatch void infer(Collection collection, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
acceptor.accept(collection.toClass(collection.name))[
it.superTypes += "java.util.Collection".typeRef(
collection.elementRef.typeRef.qualifiedName.typeRef
)
]
}
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03540 seconds