Exception, when saving new PsiJavaFile

I'm creating new PsiJavaFile, using some template by calling method:
PsiJavaFile testPsiFile = (PsiJavaFile) elementFactory.createFileFromText("some.java", templateContent);

VirtualFile testDirVF = psiFile.getVirtualFile().getFileSystem().findFileByPath(someDir.getCanonicalPath());

PsiDirectory psiDirectory = psiManager.findDirectory(testDirVF);

try {
psiDirectory.add(testPsiFile);
} catch (Exception e1) {
// ingore
}

So, the testFile is saving, but an internal error occused:

Assertion failed: Write access is allowed inside write-action only (see com.intellij.openapi.application.Application.runWriteAction())
java.lang.Throwable
at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:89)
at com.intellij.openapi.application.impl.ApplicationImpl.assertWriteAccessAllowed(ApplicationImpl.java:215)
at com.intellij.openapi.editor.impl.DocumentImpl.a(DocumentImpl.java:162)
at com.intellij.openapi.editor.impl.DocumentImpl.a(DocumentImpl.java:18)
at com.intellij.openapi.editor.impl.DocumentImpl.replaceString(DocumentImpl.java:8)
at com.intellij.openapi.editor.impl.DocumentImpl$5.run(DocumentImpl.java:1)
at com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:103)
at com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:89)
at com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:136)
at com.intellij.openapi.editor.impl.DocumentImpl.setText(DocumentImpl.java:153)
at com.intellij.psi.impl.file.PsiDirectoryImpl.add(PsiDirectoryImpl.java:292)
at ru.andremoniy.plugins.idea.autoseleniumtest.AutoSeleniumTestProject.createTestFile(AutoSeleniumTestProject.java:152)
at ru.andremoniy.plugins.idea.autoseleniumtest.AutoSeleniumTestProject.access$000(AutoSeleniumTestProject.java:35)
at ru.andremoniy.plugins.idea.autoseleniumtest.AutoSeleniumTestProject$OkButtonActionListener.actionPerformed(AutoSeleniumTestProject.java:104)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5806)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2440)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at com.intellij.ide.IdeEventQueue.c(IdeEventQueue.java:131)
at com.intellij.ide.IdeEventQueue.b(IdeEventQueue.java:18)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:136)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)



java.lang.Throwable
at com.intellij.openapi.diagnostic.Logger.error(Logger.java:48)
at com.intellij.openapi.editor.impl.DocumentImpl.b(DocumentImpl.java:68)
at com.intellij.openapi.editor.impl.DocumentImpl.a(DocumentImpl.java:308)
at com.intellij.openapi.editor.impl.DocumentImpl.replaceString(DocumentImpl.java:8)
at com.intellij.openapi.editor.impl.DocumentImpl$5.run(DocumentImpl.java:1)
at com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:103)
at com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:89)
at com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:136)
at com.intellij.openapi.editor.impl.DocumentImpl.setText(DocumentImpl.java:153)
at com.intellij.psi.impl.file.PsiDirectoryImpl.add(PsiDirectoryImpl.java:292)
at ru.andremoniy.plugins.idea.autoseleniumtest.AutoSeleniumTestProject.createTestFile(AutoSeleniumTestProject.java:152)
at ru.andremoniy.plugins.idea.autoseleniumtest.AutoSeleniumTestProject.access$000(AutoSeleniumTestProject.java:35)
at ru.andremoniy.plugins.idea.autoseleniumtest.AutoSeleniumTestProject$OkButtonActionListener.actionPerformed(AutoSeleniumTestProject.java:104)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5806)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2440)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at com.intellij.ide.IdeEventQueue.c(IdeEventQueue.java:131)
at com.intellij.ide.IdeEventQueue.b(IdeEventQueue.java:18)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:136)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

3 comments
Comment actions Permalink

Just wrap that code inside a block like this:

ApplicationManager.getApplication().runWriteAction(
new Runnable() {
public void run() {
// your code here
}
);

Also, if you creating a new class from a template, you might want to take a look at the PsiDirectory.createClass(name, templateName) method.

0
Comment actions Permalink

Hello Andremoniy,

Actually the exception message gives you all the information that you need
to fix the problem. I'll repeat what it says in other words: You need to
wrap the operation you perform in ApplicationManager.getApplication().runWriteAction().

I'm creating new PsiJavaFile, using some template by calling method:
PsiJavaFile testPsiFile = (PsiJavaFile)
elementFactory.createFileFromText("some.java", templateContent);
VirtualFile testDirVF =
psiFile.getVirtualFile().getFileSystem().findFileByPath(someDir.getCan
onicalPath());

PsiDirectory psiDirectory = psiManager.findDirectory(testDirVF);

try {
psiDirectory.add(testPsiFile);
} catch (Exception e1) {
// ingore
}
So, the testFile is saving, but an internal error occused:

Assertion failed: Write access is allowed inside write-action only
(see com.intellij.openapi.application.Application.runWriteAction())
java.lang.Throwable
at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:89)
at
com.intellij.openapi.application.impl.ApplicationImpl.assertWriteAcces
sAllowed(ApplicationImpl.java:215)
at
com.intellij.openapi.editor.impl.DocumentImpl.a(DocumentImpl.java:162)
at
com.intellij.openapi.editor.impl.DocumentImpl.a(DocumentImpl.java:18)
at
com.intellij.openapi.editor.impl.DocumentImpl.replaceString(DocumentIm
pl.java:8)
at
com.intellij.openapi.editor.impl.DocumentImpl$5.run(DocumentImpl.java:
1)
at
com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(
CommandProcessorImpl.java:103)
at
com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(
CommandProcessorImpl.java:89)
at
com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(
CommandProcessorImpl.java:136)
at
com.intellij.openapi.editor.impl.DocumentImpl.setText(DocumentImpl.jav
a:153)
at
com.intellij.psi.impl.file.PsiDirectoryImpl.add(PsiDirectoryImpl.java:
292)
at
ru.andremoniy.plugins.idea.autoseleniumtest.AutoSeleniumTestProject.cr
eateTestFile(AutoSeleniumTestProject.java:152)
at
ru.andremoniy.plugins.idea.autoseleniumtest.AutoSeleniumTestProject.ac
cess$000(AutoSeleniumTestProject.java:35)
at
ru.andremoniy.plugins.idea.autoseleniumtest.AutoSeleniumTestProject$Ok
ButtonActionListener.actionPerformed(AutoSeleniumTestProject.java:104)
at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:199
5)
at
javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java
:2318)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.
java:387)
at
javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonLi
stener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5806)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at
java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2440)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at com.intellij.ide.IdeEventQueue.c(IdeEventQueue.java:131)
at com.intellij.ide.IdeEventQueue.b(IdeEventQueue.java:18)
at
com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:136)
at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThrea
d.java:273)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.j
ava:183)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThrea
d.java:173)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
java.lang.Throwable
at com.intellij.openapi.diagnostic.Logger.error(Logger.java:48)
at
com.intellij.openapi.editor.impl.DocumentImpl.b(DocumentImpl.java:68)
at
com.intellij.openapi.editor.impl.DocumentImpl.a(DocumentImpl.java:308)
at
com.intellij.openapi.editor.impl.DocumentImpl.replaceString(DocumentIm
pl.java:8)
at
com.intellij.openapi.editor.impl.DocumentImpl$5.run(DocumentImpl.java:
1)
at
com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(
CommandProcessorImpl.java:103)
at
com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(
CommandProcessorImpl.java:89)
at
com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(
CommandProcessorImpl.java:136)
at
com.intellij.openapi.editor.impl.DocumentImpl.setText(DocumentImpl.jav
a:153)
at
com.intellij.psi.impl.file.PsiDirectoryImpl.add(PsiDirectoryImpl.java:
292)
at
ru.andremoniy.plugins.idea.autoseleniumtest.AutoSeleniumTestProject.cr
eateTestFile(AutoSeleniumTestProject.java:152)
at
ru.andremoniy.plugins.idea.autoseleniumtest.AutoSeleniumTestProject.ac
cess$000(AutoSeleniumTestProject.java:35)
at
ru.andremoniy.plugins.idea.autoseleniumtest.AutoSeleniumTestProject$Ok
ButtonActionListener.actionPerformed(AutoSeleniumTestProject.java:104)
at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:199
5)
at
javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java
:2318)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.
java:387)
at
javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonLi
stener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5806)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at
java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2440)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at com.intellij.ide.IdeEventQueue.c(IdeEventQueue.java:131)
at com.intellij.ide.IdeEventQueue.b(IdeEventQueue.java:18)
at
com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:136)
at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThrea
d.java:273)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.j
ava:183)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThrea
d.java:173)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Hugo, Dmitry, thank you very much, it is really helpful answer.

0

Please sign in to leave a comment.