Cannot run Code Smell Detector in File Change Event anymore
Hi
I use the
CodeSmellDetector findCodeSmells() in an Event that is fired after a Java class has changed. This worked until now 2018
IntelliJ release. The CodeSmellDetector is complaining that the the method findCodeSmells() must be executed in Read Only
'java.lang.RuntimeException: Must not run under write action'.
How do I run this in a Read Only thread?
This is the stack trace:
Must not run under write action
java.lang.RuntimeException: Must not run under write action
at com.intellij.openapi.vcs.impl.CodeSmellDetectorImpl.findCodeSmells(CodeSmellDetectorImpl.java:109)
at com.headwire.aem.tooling.intellij.communication.ContentResourceChangeListener.executeMakeInUIThread(ContentResourceChangeListener.java:292)
at com.headwire.aem.tooling.intellij.communication.ContentResourceChangeListener.lambda$executeMake$0(ContentResourceChangeListener.java:276)
at com.intellij.openapi.application.ReadAction.lambda$run$1(ReadAction.java:43)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:927)
at com.intellij.openapi.application.ReadAction.compute(ReadAction.java:47)
at com.intellij.openapi.application.ReadAction.run(ReadAction.java:43)
at com.headwire.aem.tooling.intellij.communication.ContentResourceChangeListener.executeMake(ContentResourceChangeListener.java:275)
at com.headwire.aem.tooling.intellij.communication.ContentResourceChangeListener.access$500(ContentResourceChangeListener.java:73)
at com.headwire.aem.tooling.intellij.communication.ContentResourceChangeListener$2.contentsChanged(ContentResourceChangeListener.java:164)
at sun.reflect.GeneratedMethodAccessor83.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.util.EventDispatcher.dispatch(EventDispatcher.java:99)
at com.intellij.util.EventDispatcher.access$200(EventDispatcher.java:35)
at com.intellij.util.EventDispatcher$2.invoke(EventDispatcher.java:79)
at com.sun.proxy.$Proxy18.contentsChanged(Unknown Source)
at com.intellij.openapi.vfs.impl.BulkVirtualFileListenerAdapter.fireAfter(BulkVirtualFileListenerAdapter.java:65)
at com.intellij.openapi.vfs.impl.BulkVirtualFileListenerAdapter.after(BulkVirtualFileListenerAdapter.java:56)
at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.util.messages.impl.MessageBusConnectionImpl.deliverMessage(MessageBusConnectionImpl.java:117)
at com.intellij.util.messages.impl.MessageBusImpl.doPumpMessages(MessageBusImpl.java:426)
at com.intellij.util.messages.impl.MessageBusImpl.pumpWaitingBuses(MessageBusImpl.java:387)
at com.intellij.util.messages.impl.MessageBusImpl.pumpMessages(MessageBusImpl.java:376)
at com.intellij.util.messages.impl.MessageBusImpl.sendMessage(MessageBusImpl.java:357)
at com.intellij.util.messages.impl.MessageBusImpl.access$200(MessageBusImpl.java:43)
at com.intellij.util.messages.impl.MessageBusImpl$2.invoke(MessageBusImpl.java:208)
at com.sun.proxy.$Proxy16.after(Unknown Source)
at com.intellij.openapi.vfs.newvfs.persistent.PersistentFSImpl$2.close(PersistentFSImpl.java:657)
at com.intellij.openapi.vfs.VirtualFile.setBinaryContent(VirtualFile.java:570)
at com.intellij.openapi.vfs.newvfs.impl.VirtualFileImpl.setBinaryContent(VirtualFileImpl.java:166)
at com.intellij.openapi.fileEditor.impl.LoadTextUtil.write(LoadTextUtil.java:418)
at com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl.lambda$doSaveDocumentInWriteAction$4(FileDocumentManagerImpl.java:440)
at com.intellij.pom.core.impl.PomModelImpl.guardPsiModificationsIn(PomModelImpl.java:406)
at com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl.doSaveDocumentInWriteAction(FileDocumentManagerImpl.java:429)
at com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl.lambda$doSaveDocument$2(FileDocumentManagerImpl.java:392)
at com.intellij.openapi.application.WriteAction.run(WriteAction.java:90)
at com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl.doSaveDocument(FileDocumentManagerImpl.java:392)
at com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl.saveAllDocuments(FileDocumentManagerImpl.java:314)
at com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl.saveAllDocuments(FileDocumentManagerImpl.java:292)
at com.intellij.openapi.components.impl.stores.StoreUtil.saveDocumentsAndProjectsAndApp(StoreUtil.java:109)
at com.intellij.ide.actions.SaveAllAction.actionPerformed(SaveAllAction.java:23)
at com.intellij.openapi.actionSystem.ex.ActionUtil$1.run(ActionUtil.java:220)
at com.intellij.openapi.actionSystem.ex.ActionUtil.performActionDumbAware(ActionUtil.java:237)
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher$1.performAction(IdeKeyEventDispatcher.java:578)
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.lambda$processAction$2(IdeKeyEventDispatcher.java:627)
at com.intellij.openapi.application.TransactionGuardImpl.performUserActivity(TransactionGuardImpl.java:195)
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.processAction(IdeKeyEventDispatcher.java:626)
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.inInitState(IdeKeyEventDispatcher.java:478)
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.dispatchKeyEvent(IdeKeyEventDispatcher.java:212)
at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:702)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:395)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
This is the relevant code:
if("java".equalsIgnoreCase(file.getExtension())) {
//AS TODO: In order to use the Code Snell Detector this needs to be invoked in a Read Only Thread but part of the Dispatcher Thread
ReadAction.run(() ->
executeMakeInUIThread(event)
);
}
}
}
private void executeMakeInUIThread(final VirtualFileEvent event) {
if(project.isInitialized() && !project.isDisposed() && project.isOpen()) {
final CompilerManager compilerManager = CompilerManager.getInstance(project);
if(!compilerManager.isCompilationActive() &&
!compilerManager.isExcludedFromCompilation(event.getFile()) // &&
) {
// Check first if there are no errors in the code
CodeSmellDetector codeSmellDetector = CodeSmellDetector.getInstance(project);
boolean isOk = true;
if(codeSmellDetector != null) {
List<CodeSmellInfo> codeSmellInfoList = codeSmellDetector.findCodeSmells(Arrays.asList(event.getFile()));
Please sign in to leave a comment.
Hello Andreas,
Do you use "Code Smell Detector" plugin?
Hello Yaroslav
No, I just use the com.intellij.openapi.vcs.CodeSmellDetector.CodeSmellDetector.getInstance(project); in my plugin.
What I want to know is how to I make the thread read-only. ReadAction.run() is making sure I can read but I leave the thread write enabled which caused the CodeSmellDetector to bark.