|
Re: Resolving type parameters in the Jvm types [message #1159179 is a reply to message #1152927] |
Mon, 28 October 2013 10:50 |
Victor Noël Messages: 112 Registered: June 2010 |
Senior Member |
|
|
Hi,
Considering a JvmTypeReference R1 in a JvmType T1 with type parameters (for example ChangeListener<T> fron the addListener method declared in ObservableValue<T>) and considering a JvmTypeReference R2 of the JvmType (for example ObservableValue<String>), if I want to get R1 with type parameters substituted (thus to get ChangeListener<String>), I do that:
val mapping = new ConstraintAwareTypeArgumentCollector(containerTypeRef.owner).getTypeParameterMapping(containerTypeRef)
new StandardTypeParameterSubstitutor(mapping, containerTypeRef.owner).substitute(tr)
containerTypeRef is a LightweightTypeReference created from R2 (check XBaseValidator to see how to create such a LightweightTypeReference, there is a helper method there).
tr is a LightweightTypeReference created from R1.
Problem with that is that it does not take the hierarchy of classes into account (if for example R1 is actually defined in a super class of T1 but you only have R2 a reference to T1).
Here is my raw code for it, it contains things that are specific to my grammar, to check who owns the reference (and thus the type parameters that must be substituted:
private def LightweightTypeReference resolveType(LightweightTypeReference portTypeRef, LightweightTypeReference containerTypeRef, Port port) {
val ac = containerTypeRef.type.associatedAbstractComponent
val tr = if (!(ac.provides.contains(port) || ac.requires.contains(port)) && ac.specializes != null) {
val nptr = ac.specializes.toLightweightTypeReference(port.eResource)
resolveType(portTypeRef, nptr, port)
} else {
portTypeRef
}
val mapping = new ConstraintAwareTypeArgumentCollector(containerTypeRef.owner).getTypeParameterMapping(containerTypeRef)
new StandardTypeParameterSubstitutor(mapping, containerTypeRef.owner).substitute(tr)
}
|
|
|
Powered by
FUDForum. Page generated in 0.03195 seconds