Enum problems: Creating enum via PsiElementFactory.createFileFromText fails


I'm using IntelliJ 6.0.4 (build 6148). In my unit tests I create PsiJavaFiles containing classes or interfaces and this works perfectly. However, when doing the same for an enum the following happens:

PsiElementFactory psiElementFactory = getPsiManager().getElementFactory();
PsiJavaFile definedEnum = (PsiJavaFile) psiElementFactory.createFileFromText(
"package p1.p2;\n\n" +
"public enum Test {\n" +

When viewing the children of the PsiJavaFile, the following content is shown:

definedEnum.getChildren() = {com.intellij.psi.PsiElement[7]@8557}
= {com.intellij.psi.impl.source.tree.java.PsiPackageStatementImpl@8558}"PsiPackageStatement:p1.p2"
= {com.intellij.psi.impl.source.PsiImportListImpl@8559}"PsiImportList"
= {com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl@8560}"PsiWhiteSpace"
= {com.intellij.psi.impl.source.PsiModifierListImpl@8561}"PsiModifierList:public"
* = {com.intellij.psi.impl.source.tree.PsiErrorElementImpl@8562}"PsiErrorElement:'class' or 'interface' expected"
= {com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl@8563}"PsiWhiteSpace"
= {com.intellij.psi.impl.source.tree.PsiErrorElementImpl@8564}"PsiErrorElement:'class' or 'interface' expected"*

Is this a bug in IntelliJ? If not, how can I successfully create an enum programmatically (for test purposes only)?

Thanks in advance,

Raymond Brandon

Comment actions Permalink

In te test you should set language level to JDK_50 to enable parsing enums.

Comment actions Permalink

Thanks Eugene that solved it!


Please sign in to leave a comment.