Changing the document contents and CodeStyleManager.reformat() problem


Hi all,

I've struck a problem with changing the contents of a class file - I'm trying to get IDEA to create
me a PsiClass file (so that it "knows" about it), but then replace the contents of the file with
some java source code from an external file (which has been generated by other means). This all
seems to be fine.

However, when I try to reformat it, it throws a NullPointerException. Here's my code:

PsiClass testClass = rootDir.findSubdirectory("test").createClass("TestClass");
final PsiDocumentManager docManager = PsiDocumentManager.getInstance(getProject());
Document doc = docManager.getDocument(testClass.getContainingFile());
doc.replaceString(0, doc.getText().length(), {java source from an external file});
docManager.commitDocument(doc);
CodeStyleManager.getInstance(getProject()).reformat(testClass.getContainingFile());



It's almost as if replacing the document contents screws up IDEA's Psi model of the class file, and
when reformat() is called, it doesn't have a model to work with. reformat() works fine if I don't
change the contents of the document in the PsiClass file.

Anyone have any idea why this is happening? Am I going about this in the wrong way?

This whole procedure is being performed within a Application.runWriteAction() which in turn is
inside a CommandProcessor.executeCommand().

Thanks in advance for any help.

Regards,
Corey.


-


EXCEPTION THROWN -



java.lang.NullPointerException
at com.intellij.psi.impl.l.a(l.java:9)
at com.intellij.psi.impl.source.g.c.reformat(c.java:11)
at plugin.test.TestAction.CreateTestClasses(TestAction.java:116)
at plugin.test.TestAction.access$100(TestAction.java:33)
at plugin.test.TestAction$2.run(TestAction.java:55)
at com.intellij.openapi.application.b.b.runWriteAction(b.java:38)
at plugin.test.TestAction$1.run(TestAction.java:50)
at com.intellij.openapi.command.impl.a.executeCommand(a.java:18)
at plugin.test.TestAction.actionPerformed(TestAction.java:45)
at plugin.common.actions.AbstractAction.actionPerformed(AbstractAction.java:63)
at com.intellij.openapi.actionSystem.b.n$b_.actionPerformed(n$b_.java:10)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)
at com.intellij.openapi.actionSystem.b.n.fireActionPerformed(n.java:17)
at com.intellij.ui.c.a.h.doClick(h.java:146)
at com.intellij.ui.c.a.h.access$300(h.java:77)
at com.intellij.ui.c.a.h$a_.mouseReleased(h$a_.java:4)
at java.awt.Component.processMouseEvent(Component.java:5100)
at java.awt.Component.processEvent(Component.java:4897)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
at java.awt.Container.dispatchEventImpl(Container.java:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at com.intellij.ide.q.b(q.java:101)
at com.intellij.ide.q.a(q.java:59)
at com.intellij.ide.q.dispatchEvent(q.java:46)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

2 comments
Comment actions Permalink


Sudden thought just after sending this - does the PsiClass and/or PsiFile handle become stale when
the contents of the Document are changed? I might try re-fetching it ...

Would still appreciate any feedback from anyone if there's a better way to do this.

Thanks.

Regards,
Corey.



Corey Yeatman wrote:


Hi all,

I've struck a problem with changing the contents of a class file - I'm
trying to get IDEA to create me a PsiClass file (so that it "knows"
about it), but then replace the contents of the file with some java
source code from an external file (which has been generated by other
means). This all seems to be fine.

However, when I try to reformat it, it throws a NullPointerException.
Here's my code:

PsiClass testClass =
rootDir.findSubdirectory("test").createClass("TestClass");
final PsiDocumentManager docManager =
PsiDocumentManager.getInstance(getProject());
Document doc = docManager.getDocument(testClass.getContainingFile());
doc.replaceString(0, doc.getText().length(), {java source from an
external file});
docManager.commitDocument(doc);
CodeStyleManager.getInstance(getProject()).reformat(testClass.getContainingFile());




It's almost as if replacing the document contents screws up IDEA's Psi
model of the class file, and when reformat() is called, it doesn't have
a model to work with. reformat() works fine if I don't change the
contents of the document in the PsiClass file.

Anyone have any idea why this is happening? Am I going about this in
the wrong way?

This whole procedure is being performed within a
Application.runWriteAction() which in turn is inside a
CommandProcessor.executeCommand().

Thanks in advance for any help.

Regards,
Corey.


---- EXCEPTION THROWN ----

java.lang.NullPointerException
at com.intellij.psi.impl.l.a(l.java:9)
at com.intellij.psi.impl.source.g.c.reformat(c.java:11)
at plugin.test.TestAction.CreateTestClasses(TestAction.java:116)
at plugin.test.TestAction.access$100(TestAction.java:33)
at plugin.test.TestAction$2.run(TestAction.java:55)
at com.intellij.openapi.application.b.b.runWriteAction(b.java:38)
at plugin.test.TestAction$1.run(TestAction.java:50)
at com.intellij.openapi.command.impl.a.executeCommand(a.java:18)
at plugin.test.TestAction.actionPerformed(TestAction.java:45)
at
plugin.common.actions.AbstractAction.actionPerformed(AbstractAction.java:63)

at
com.intellij.openapi.actionSystem.b.n$b_.actionPerformed(n$b_.java:10)
at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)
at
com.intellij.openapi.actionSystem.b.n.fireActionPerformed(n.java:17)
at com.intellij.ui.c.a.h.doClick(h.java:146)
at com.intellij.ui.c.a.h.access$300(h.java:77)
at com.intellij.ui.c.a.h$a_.mouseReleased(h$a_.java:4)
at java.awt.Component.processMouseEvent(Component.java:5100)
at java.awt.Component.processEvent(Component.java:4897)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
at
java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)
at
java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
at java.awt.Container.dispatchEventImpl(Container.java:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at com.intellij.ide.q.b(q.java:101)
at com.intellij.ide.q.a(q.java:59)
at com.intellij.ide.q.dispatchEvent(q.java:46)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)

at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)

at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

0
Comment actions Permalink

Corey Yeatman wrote:


Sudden thought just after sending this - does the PsiClass and/or PsiFile
handle become stale when
the contents of the Document are changed? I might try re-fetching it ...


Exactly.


Would still appreciate any feedback from anyone if there's a better way to
do this.


The best way is to create a file (not a class) in a directory, replace its
text in document, and then invoke reformat on that file.
PsiFile is guaranteed not to invalidate on its document changes.

Friendly,
Dmitry


Thanks.

Regards,
Corey.



Corey Yeatman wrote:

>>
>> Hi all,
>>
>> I've struck a problem with changing the contents of a class file - I'm
>> trying to get IDEA to create me a PsiClass file (so that it "knows"
>> about it), but then replace the contents of the file with some java
>> source code from an external file (which has been generated by other
>> means). This all seems to be fine.
>>
>> However, when I try to reformat it, it throws a NullPointerException.
>> Here's my code:
>>
>> PsiClass testClass =
>> rootDir.findSubdirectory("test").createClass("TestClass");
>> final PsiDocumentManager docManager =
>> PsiDocumentManager.getInstance(getProject());
>> Document doc = docManager.getDocument(testClass.getContainingFile());
>> doc.replaceString(0, doc.getText().length(), {java source from an
>> external file});
>> docManager.commitDocument(doc);
>>
CodeStyleManager.getInstance(getProject()).reformat(testClass.getContainingFile());
>>
>>
>>
>>
>> It's almost as if replacing the document contents screws up IDEA's Psi
>> model of the class file, and when reformat() is called, it doesn't have
>> a model to work with. reformat() works fine if I don't change the
>> contents of the document in the PsiClass file.
>>
>> Anyone have any idea why this is happening? Am I going about this in
>> the wrong way?
>>
>> This whole procedure is being performed within a
>> Application.runWriteAction() which in turn is inside a
>> CommandProcessor.executeCommand().
>>
>> Thanks in advance for any help.
>>
>> Regards,
>> Corey.
>>
>>
>> -


EXCEPTION THROWN -


>>
>> java.lang.NullPointerException
>> at com.intellij.psi.impl.l.a(l.java:9)
>> at com.intellij.psi.impl.source.g.c.reformat(c.java:11)
>> at plugin.test.TestAction.CreateTestClasses(TestAction.java:116)
>> at plugin.test.TestAction.access$100(TestAction.java:33)
>> at plugin.test.TestAction$2.run(TestAction.java:55)
>> at com.intellij.openapi.application.b.b.runWriteAction(b.java:38)
>> at plugin.test.TestAction$1.run(TestAction.java:50)
>> at com.intellij.openapi.command.impl.a.executeCommand(a.java:18)
>> at plugin.test.TestAction.actionPerformed(TestAction.java:45)
>> at
>>
plugin.common.actions.AbstractAction.actionPerformed(AbstractAction.java:63)
>>
>> at
>> com.intellij.openapi.actionSystem.b.n$b_.actionPerformed(n$b_.java:10)
>> at
>> javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)
>> at
>> com.intellij.openapi.actionSystem.b.n.fireActionPerformed(n.java:17)
>> at com.intellij.ui.c.a.h.doClick(h.java:146)
>> at com.intellij.ui.c.a.h.access$300(h.java:77)
>> at com.intellij.ui.c.a.h$a_.mouseReleased(h$a_.java:4)
>> at java.awt.Component.processMouseEvent(Component.java:5100)
>> at java.awt.Component.processEvent(Component.java:4897)
>> at java.awt.Container.processEvent(Container.java:1569)
>> at java.awt.Component.dispatchEventImpl(Component.java:3615)
>> at java.awt.Container.dispatchEventImpl(Container.java:1627)
>> at java.awt.Component.dispatchEvent(Component.java:3477)
>> at
>> java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
>> at
>> java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)
>> at
>> java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
>> at java.awt.Container.dispatchEventImpl(Container.java:1613)
>> at java.awt.Window.dispatchEventImpl(Window.java:1606)
>> at java.awt.Component.dispatchEvent(Component.java:3477)
>> at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
>> at com.intellij.ide.q.b(q.java:101)
>> at com.intellij.ide.q.a(q.java:59)
>> at com.intellij.ide.q.dispatchEvent(q.java:46)
>> at
>>
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
>>
>> at
>>
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
>>
>> at
>> java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
>> at
>> java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
>> at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

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

0

Please sign in to leave a comment.