PSI exception

What am I doing wrong? This is while performing PSI operation on XML file.
Here's the code:

public class SurroundWithTagFix implements LocalQuickFix {
private final PsiElement parent;
private PsiElement firstChild;
private PsiElement lastChild;
private String tagName;

public SurroundWithTagFix(PsiElement parent, PsiElement firstChild,
PsiElement lastChild, String tagName) {
this.parent = parent;
this.firstChild = firstChild;
this.lastChild = lastChild;
this.tagName = tagName;
}

public String getName() {
return "Surround with '" + tagName + "'";
}

public void applyFix(Project project, ProblemDescriptor descriptor) {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
PsiElementFactory elfactory =
parent.getManager().getElementFactory();
try {
StringBuilder builder = new StringBuilder(
parent.getTextRange().getLength());
for (PsiElement next = firstChild;;
next = next.getNextSibling()) {
assert next != null;
builder.append(next.getText());
if (next == lastChild) break;
}
XmlTag tag = elfactory.createTagFromText(
"<" + tagName + ">" + builder.toString()
+ "</" + tagName + ">");
parent.addAfter(tag, lastChild);
parent.deleteChildRange(firstChild, lastChild);
} catch (IncorrectOperationException e) {
throw new IllegalStateException(e);
}
}
});
}

public String getFamilyName() {
return null;
}
}

Here's the exception, it happens every time:

Assertion failed
java.lang.Throwable
at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:77)
at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:85)
at com.intellij.psi.impl.source.tree.ChangeUtil.addChild(ChangeUtil.java:20)
at
com.intellij.psi.impl.source.tree.CompositeElement.addChild(CompositeElement.java:209)
at com.intellij.psi.impl.source.tree.ChangeUtil.addChildren(ChangeUtil.java:205)
at
com.intellij.psi.impl.source.tree.CompositeElement.addChildren(CompositeElement.java:228)
at com.intellij.psi.impl.source.codeStyle.CodeEditUtil.a(CodeEditUtil.java:501)
at
com.intellij.psi.impl.source.codeStyle.CodeEditUtil.addChildren(CodeEditUtil.java:341)
at
com.intellij.psi.impl.source.tree.CompositeElement.addInternal(CompositeElement.java:63)
at com.intellij.psi.impl.source.xml.XmlTagImpl.access$1001(XmlTagImpl.java:179)
at
com.intellij.psi.impl.source.xml.XmlTagImpl$GenericInsertTransaction.runInner(XmlTagImpl.java:7)
at com.intellij.pom.impl.PomTransactionBase.run(PomTransactionBase.java:43)
at com.intellij.pom.core.impl.PomModelImpl.runTransaction(PomModelImpl.java:81)
at com.intellij.psi.impl.source.xml.XmlTagImpl.a(XmlTagImpl.java:171)
at com.intellij.psi.impl.source.xml.XmlTagImpl.addInternal(XmlTagImpl.java:229)
at
com.intellij.psi.impl.source.tree.CompositePsiElement.addAfter(CompositePsiElement.java:20)
at net.kano.gxpeditor.fixes.SurroundWithTagFix$1.run(SurroundWithTagFix.java:79)
at
com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:139)
at net.kano.gxpeditor.fixes.SurroundWithTagFix.applyFix(SurroundWithTagFix.java:64)
at com.intellij.codeInspection.ex.QuickFixWrapper.invoke(QuickFixWrapper.java:4)
at
com.intellij.codeInsight.intention.impl.IntentionHintComponent$ListPopupRunnable$1$1$1.run(IntentionHintComponent.java:1)
at
com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:139)
at
com.intellij.codeInsight.intention.impl.IntentionHintComponent$ListPopupRunnable$1$1$2.run(IntentionHintComponent.java:1)
at
com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:109)
at
com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:20)
at
com.intellij.codeInsight.intention.impl.IntentionHintComponent$ListPopupRunnable$1$1.run(IntentionHintComponent.java:5)
at
com.intellij.openapi.application.impl.LaterInvocatorEx$FlushQueue.run(LaterInvocatorEx.java:3)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:25)
at com.intellij.ide.IdeEventQueue.a(IdeEventQueue.java:128)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:62)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:267)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:182)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)



java.lang.Throwable
at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:77)
at
com.intellij.psi.impl.SharedPsiElementImplUtil.findAndDecodeFirstChildAfterAddition(SharedPsiElementImplUtil.java:26)
at
com.intellij.psi.impl.source.tree.CompositePsiElement.addAfter(CompositePsiElement.java:73)
at net.kano.gxpeditor.fixes.SurroundWithTagFix$1.run(SurroundWithTagFix.java:79)
at
com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:139)
at net.kano.gxpeditor.fixes.SurroundWithTagFix.applyFix(SurroundWithTagFix.java:64)
at com.intellij.codeInspection.ex.QuickFixWrapper.invoke(QuickFixWrapper.java:4)
at
com.intellij.codeInsight.intention.impl.IntentionHintComponent$ListPopupRunnable$1$1$1.run(IntentionHintComponent.java:1)
at
com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:139)
at
com.intellij.codeInsight.intention.impl.IntentionHintComponent$ListPopupRunnable$1$1$2.run(IntentionHintComponent.java:1)
at
com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:109)
at
com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:20)
at
com.intellij.codeInsight.intention.impl.IntentionHintComponent$ListPopupRunnable$1$1.run(IntentionHintComponent.java:5)
at
com.intellij.openapi.application.impl.LaterInvocatorEx$FlushQueue.run(LaterInvocatorEx.java:3)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:25)
at com.intellij.ide.IdeEventQueue.a(IdeEventQueue.java:128)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:62)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:267)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:182)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)



java.lang.NullPointerException
at com.intellij.psi.impl.source.codeStyle.CodeEditUtil.a(CodeEditUtil.java:506)
at com.intellij.psi.impl.source.codeStyle.CodeEditUtil.a(CodeEditUtil.java:468)
at com.intellij.psi.impl.source.codeStyle.CodeEditUtil.a(CodeEditUtil.java:201)
at com.intellij.psi.impl.source.codeStyle.CodeEditUtil.a(CodeEditUtil.java:126)
at
com.intellij.psi.impl.source.codeStyle.CodeEditUtil.addChildren(CodeEditUtil.java:341)
at
com.intellij.psi.impl.source.tree.CompositeElement.addInternal(CompositeElement.java:63)
at com.intellij.psi.impl.source.xml.XmlTagImpl.access$1001(XmlTagImpl.java:179)
at
com.intellij.psi.impl.source.xml.XmlTagImpl$GenericInsertTransaction.runInner(XmlTagImpl.java:7)
at com.intellij.pom.impl.PomTransactionBase.run(PomTransactionBase.java:43)
at com.intellij.pom.core.impl.PomModelImpl.runTransaction(PomModelImpl.java:81)
at com.intellij.psi.impl.source.xml.XmlTagImpl.a(XmlTagImpl.java:171)
at com.intellij.psi.impl.source.xml.XmlTagImpl.addInternal(XmlTagImpl.java:229)
at
com.intellij.psi.impl.source.tree.CompositePsiElement.addAfter(CompositePsiElement.java:20)
at net.kano.gxpeditor.fixes.SurroundWithTagFix$1.run(SurroundWithTagFix.java:79)
at
com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:139)
at net.kano.gxpeditor.fixes.SurroundWithTagFix.applyFix(SurroundWithTagFix.java:64)
at com.intellij.codeInspection.ex.QuickFixWrapper.invoke(QuickFixWrapper.java:4)
at
com.intellij.codeInsight.intention.impl.IntentionHintComponent$ListPopupRunnable$1$1$1.run(IntentionHintComponent.java:1)
at
com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:139)
at
com.intellij.codeInsight.intention.impl.IntentionHintComponent$ListPopupRunnable$1$1$2.run(IntentionHintComponent.java:1)
at
com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:109)
at
com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:20)
at
com.intellij.codeInsight.intention.impl.IntentionHintComponent$ListPopupRunnable$1$1.run(IntentionHintComponent.java:5)
at
com.intellij.openapi.application.impl.LaterInvocatorEx$FlushQueue.run(LaterInvocatorEx.java:3)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:25)
at com.intellij.ide.IdeEventQueue.a(IdeEventQueue.java:128)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:62)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:267)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:182)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)



java.lang.NullPointerException
at
com.intellij.psi.impl.SharedPsiElementImplUtil.findAndDecodeFirstChildAfterAddition(SharedPsiElementImplUtil.java:21)
at
com.intellij.psi.impl.source.tree.CompositePsiElement.addAfter(CompositePsiElement.java:73)
at net.kano.gxpeditor.fixes.SurroundWithTagFix$1.run(SurroundWithTagFix.java:79)
at
com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:139)
at net.kano.gxpeditor.fixes.SurroundWithTagFix.applyFix(SurroundWithTagFix.java:64)
at com.intellij.codeInspection.ex.QuickFixWrapper.invoke(QuickFixWrapper.java:4)
at
com.intellij.codeInsight.intention.impl.IntentionHintComponent$ListPopupRunnable$1$1$1.run(IntentionHintComponent.java:1)
at
com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:139)
at
com.intellij.codeInsight.intention.impl.IntentionHintComponent$ListPopupRunnable$1$1$2.run(IntentionHintComponent.java:1)
at
com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:109)
at
com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:20)
at
com.intellij.codeInsight.intention.impl.IntentionHintComponent$ListPopupRunnable$1$1.run(IntentionHintComponent.java:5)
at
com.intellij.openapi.application.impl.LaterInvocatorEx$FlushQueue.run(LaterInvocatorEx.java:3)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:25)
at com.intellij.ide.IdeEventQueue.a(IdeEventQueue.java:128)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:62)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:267)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:182)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)


3 comments
Comment actions Permalink

Hello Keith,

KL> What am I doing wrong? This is while performing PSI operation on XML
KL> file. Here's the code:

If I understand correctly, this assertion fires because 'lastChild' is not
actually a child of 'parent'.

--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com
"Develop with pleasure!"


0
Comment actions Permalink

Okay I'll look into it. Do you think the assertion could be made more clear so
others can quickly fiix their code?

Dmitry Jemerov (JetBrains) wrote:

Hello Keith,

KL> What am I doing wrong? This is while performing PSI operation on XML
KL> file. Here's the code:

If I understand correctly, this assertion fires because 'lastChild' is
not actually a child of 'parent'.

0
Comment actions Permalink

Hello Keith,

There are hundreds of places in IDEA code where parameter checks and/or assertions
can be added or improved to make life easier for plugin developers... We'll
see what we can do about improving this situation.

KL> Okay I'll look into it. Do you think the assertion could be made
KL> more clear so others can quickly fiix their code?
KL>
KL> Dmitry Jemerov (JetBrains) wrote:
KL>
>> Hello Keith,
>>
>> KL> What am I doing wrong? This is while performing PSI operation on
>> XML KL> file. Here's the code:
>>
>> If I understand correctly, this assertion fires because 'lastChild'
>> is not actually a child of 'parent'.
>>
--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com
"Develop with pleasure!"


0

Please sign in to leave a comment.