Create a new PSIClass with Generic type parameters
Answered
Newbie question on IntelliJ plugin development.
I need to generate a parameterised class (Class with generics) given the name of the class and the name of the type parameter, but I can not find how to?
It seems PSIClass does not support generics.
Example
Given
String className = "MyClass";
String typeName = "T"
I would like to have a PSIClass or something that represents this:
public class MyClass<T> { ... }
Thanks!
Please sign in to leave a comment.
Have you tried PsiElementFactory.SERVICE.getInstance().createClassFromText()?
I asked that recently too: https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000059670-convert-string-with-generics-to-PsiType
Hi, Thanks for your replies! =)
It seems that `createClassFromText` might not work. Example:
If I use:
String classContent = "class Builder<T>{}";PsiClass myClass = PsiElementFactory.SERVICE.getInstance(project).createClassFromText(classContent, null);
Then myClass will have the name `_Dummy_` and won't have any type parameters.
What other options do I have?
Currently I am trying with `PsiSubsitutor.EMPTY`, but I can not find a good example to use it.
Thanks!
Oh I see, the API is misleading and inconsistent. Then I'd recommend to use PsiFileFactory.getInstance(...).createFileFromText("ClassName.java", JavaFileType.INSTANCE, "class ClassName<T> { ...}"), cast the result to PsiJavaFile and use its getClasses[0] as the result.
Thanks, It worked!
It is a bit unexpected that I need to create a file to be able to create a class though.
Anyway, Thank you very much for your help. I have solved my issue!