Create a method with a return type with a type parameter
Hi
I'm developing a plugin for idea to add some methods, based on class annotations, and I'm trying to create a static method that return a class with a type parameter, something like this,
public static <T> Test<T> narrowK(Object hkt) {
return (Test<T>) hkt;
}
But I couldn't find the way to do it using the API:
PsiElementFactory factory = PsiElementFactory.getInstance(project);
LightMethodBuilder narrowK = new LightMethodBuilder(PsiManager.getInstance(project), "narrowK");
narrowK.setContainingClass(clazz);
LightTypeParameterBuilder typeParameter = new LightTypeParameterBuilder("T", narrowK, 0);
narrowK.addTypeParameter(typeParameter);
narrowK.addModifier(PsiModifier.PUBLIC);
narrowK.addModifier(PsiModifier.STATIC);
PsiClassType returnType = factory.createType(clazz);
narrowK.setMethodReturnType(returnType);
narrowK.addParameter("hkt", factory.createTypeByFQClassName(OBJECT));
return narrowK;
I've tried also to create the method using `factory.createMethodFromText()` but it didn't work.
Thanks!
Please sign in to leave a comment.
What is "clazz"? It doesn't seem to contain "<T>" type parameter.