Create java class from text?

Whats the best way to create a java file and add raw text contents to it?

I noticed these two methods, but first one doesn't give a location to put the file, and the second one I'm missing context too. Am I missing some docs on this? So what are the steps to get the file into the project?

PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText(name, StdFileTypes.JAVA, contents);
PsiElementFactory.SERVICE.getInstance(project).createClassFromText(...)

Brandon

2 comments
Comment actions Permalink

How I did it:

PsiDirectory[] createdNameTokensPackageDirectories = createdNameTokensPackage.getDirectories();
PsiFile element = PsiFileFactory.getInstance(project).createFileFromText(nameFile, JavaFileType.INSTANCE, contents);
PsiElement createdNameTokensClass = createdNameTokensPackageDirectories[0].add(element);

0
Comment actions Permalink

Hi Brandon,

I happen to come across same problem. Glad to see your post.

Could you please tell me what is "createdNameTokensPackage" in your comment above ?

0

Please sign in to leave a comment.