Is it possible to create a java class with JCodeModel in a plugin?

已回答

Hello everyone,

This may be a stupid question, but is it possible to write JCodeModel to a PSI File? 
I know that I can do:  codeModel.build(codeWriter), but in Idea Plugin all work with files
does it happen via VirtualFile or PsiFile?
Perhaps I should generate a Java class using Psi?

0

Hi Anastasiia,

You can generate a class with an external library and create a file from a text.

See https://plugins.jetbrains.com/docs/intellij/psi-files.html#how-do-i-create-a-psi-file

0

Hi Karol Lewandowski thank you! I used it

PsiDirectoryFactory.getInstance(project)
        .createDirectory(dir)
        .createFile(fileName)
        .getVirtualFile().setBinaryContent(jsonObject.getBytes(StandardCharsets.UTF_8));
0

Hi,

It is better to use PSI API (PsiFileFactory.createFileFromText() I linked). VirtualFile is a low-level API.

0

请先登录再写评论。