How to solve java.lang.Throwable: Assertion failed: anchorBefore == null || anchorBefore.getTreeParent() == parent ?

I am developing a IDEA plugin to add an annotation for a field, the code like this:

WriteCommandAction.runWriteCommandAction(psiClass.getProject(), new Runnable() {
@Override
public void run() {
psiField.addBefore(psiAnnotation, psiField);
}
});

When I use the plugin, it can add an annotation for a field, but the IDEA throws ecxeption like this:

 

java.lang.Throwable: Assertion failed: anchorBefore == null || anchorBefore.getTreeParent() == parent
at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:163)
at com.intellij.psi.impl.source.tree.CompositeElement.addChild(CompositeElement.java:605)
at com.intellij.psi.impl.source.tree.CompositeElement.addChildren(CompositeElement.java:706)
at com.intellij.psi.impl.source.codeStyle.CodeEditUtil.addChildren(CodeEditUtil.java:67)
at com.intellij.psi.impl.source.tree.CompositeElement.addInternal(CompositeElement.java:481)
at com.intellij.psi.impl.source.JavaStubPsiElement.addBefore(JavaStubPsiElement.java:83)

 

So, I want to know how to solve it.

6

You can try:

psiField.addBefore(psiAnnotation, psiField.getFirstChild());
0

请先登录再写评论。