Create PsiJavaFile from PsiFile(plain text)

Hi,

I apologize if this is really basic but, after much searching in the forums, I still haven't found the answer. I have some java source files that exist within my project's content root but outside of any source paths. When examining the virtual files associated to each of the java files, they are all identifed as PsiFile(plain text), and their language is TEXT.

How can I turn such a reference into a PsiJavaFile reference so that I can manipulate/query its structure? The ultimate goal is to obtain the package of the file so that I can resolve the proper parent folder and add it to the source path.

Thank you very much,

Sebastien

2 comments

PsiManager.gelElementsFactory().createFileFromText

Sebastien Gagnon wrote:

Hi,

I apologize if this is really basic but, after much searching in the forums, I still haven't found the answer. I have some java source files that exist within my project's content root but outside of any source paths. When examining the virtual files associated to each of the java files, they are all identifed as PsiFile(plain text), and their language is TEXT.

How can I turn such a reference into a PsiJavaFile reference so that I can manipulate/query its structure? The ultimate goal is to obtain the package of the file so that I can resolve the proper parent folder and add it to the source path.

Thank you very much,

Sebastien



--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0

Thanks Maxim, it worked great.

For the sake of completeness on this post, I offer this example code block which, given a "whatever.java" virtual file, will parse it into a PsiJavaFile if it's not already one.

The file "temp.java" is not created anywhere, it's simply a token name. The extension of the name, however, is important since it will indicate to the factory which parser to use.

Regards,

Sebastien

0

Please sign in to leave a comment.