Last active
May 24, 2024 06:48
-
-
Save nbhusare/aa53500271f58c832ed56a47cdb1f5af to your computer and use it in GitHub Desktop.
Xtext - Creating a Generic type reference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Approach 01 | |
@Inject private TypeReferences typeReferences; | |
private LightweightTypeReference getGenericType(ITypeComputationState state, Class<?> t, Class<?>... t1) { | |
var resourceSet = state.getReferenceOwner().getContextResourceSet(); | |
var rawType = typeReferences.getTypeForName(t, resourceSet).getType(); | |
var typeParam = typeReferences.getTypeForName(t1[0], resourceSet); | |
return state.getReferenceOwner() | |
.toLightweightTypeReference(typeReferences.createTypeRef(rawType, typeParam);) | |
} | |
// Approach 02 | |
@Inject private JvmTypeReferenceBuilder.Factory typeRefBuilderFactory; | |
private LightweightTypeReference getGenericType(ITypeComputationState state, Class<?> t, Class<?>... t1) { | |
var jvmTypeReferenceBuilder = typeRefBuilderFactory.create(resourceSet); | |
var parameterizedTypeRef = jvmTypeReferenceBuilder.typeRef(t, jvmTypeReferenceBuilder.typeRef(t1[0])); | |
return state.getReferenceOwner().toLightweightTypeReference(parameterizedTypeRef); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment