IndexNotReadyException when add files/directories using ModuleBuilderListener

I have a custom module builder

public class CustomModuleBuilder extends JavaModuleBuilder implements ModuleBuilderListener

And I want all modules of this type to start out with a directory structure, class, format the class, and add two libraries.  My moduleCreated is below

@Override
public void moduleCreated(@NotNull Module module) {

    VirtualFile[] sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots();

    VirtualFile root = sourceRoots[0];

    PsiDirectory sourceDirectory = PsiManager.getInstance(module.getProject()).findDirectory(root);

    PsiDirectory classDirectory;

    classDirectory = PackageUtil.findOrCreateDirectoryForPackage(module, "com.my.custompackage", sourceDirectory, false);


    NOTE!! : The function createFileFromText(@NonNls @NotNull String fileName, @NotNull FileType fileType, @NotNull CharSequence text); failed becuase it said eventSystemEnabled was false

    PsiFile functionPsiFile = PsiFileFactory.getInstance(module.getProject()).createFileFromText(“CustomClsas.java", JavaFileType.INSTANCE, createCustomClassCode(), LocalTimeCounter.currentTime(), true);

    WriteCommandAction.runWriteCommandAction(module.getProject(), new Runnable() {

        @Override

        public void run() {

            try {

                classDirectory.add(functionPsiFile);

            } catch (IndexNotReadyException e) {

                e.printStackTrace();

                //Throws but works fine

            }

            //shorten class references, and add the necessary import statements

            PsiDocumentManager documentManager = PsiDocumentManager.getInstance(module.getProject());

            Document document = documentManager.getDocument(functionPsiFile);

            documentManager.commitDocument(document);

            PsiClass functionClass = ((PsiJavaFile) functionPsiFile).getClasses()[0];

            CodeStyleManager.getInstance(module.getProject()).reformat(functionClass);

            try {

                //This does not work becuase of thrown error

                JavaCodeStyleManager.getInstance(module.getProject()).shortenClassReferences(functionClass);

            } catch (IndexNotReadyException e){

                e.printStackTrace();

            }

        }

    });

}


The package, class directory, library and class all get created (Though adding the newly created class directory throws the exception as show, stack trace below).  But the class does not get it's references references shortened.  Both stack traces are very similar (and end in the same FileBasedIndexImple calls.  I have used the same code to format/shorted references in actions that are called via right-clicking on open class editor --> generate code --> format generated code. Incase you missed it, I'm not sure if the following was relavent

    NOTE!! : The function createFileFromText(@NonNls @NotNull String fileName, @NotNull FileType fileType, @NotNull CharSequence text); failed becuase it said eventSystemEnabled was false

    PsiFile functionPsiFile = PsiFileFactory.getInstance(module.getProject()).createFileFromText(“CustomClsas.java", JavaFileType.INSTANCE, createCustomClassCode(), LocalTimeCounter.currentTime(), true);


at com.intellij.util.indexing.FileBasedIndexImpl.handleDumbMode(FileBasedIndexImpl.java:855)
 at com.intellij.util.indexing.FileBasedIndexImpl.ensureUpToDate(FileBasedIndexImpl.java:804)
 at com.intellij.util.indexing.FileBasedIndexImpl.ensureUpToDate(FileBasedIndexImpl.java:788)
 at com.intellij.psi.stubs.StubIndexImpl.processElements(StubIndexImpl.java:250)
 at com.intellij.psi.stubs.StubIndexImpl.processElements(StubIndexImpl.java:238)
 at com.intellij.psi.stubs.StubIndex.process(StubIndex.java:76)
 at com.intellij.psi.stubs.StubIndex.process(StubIndex.java:95)
 at com.intellij.psi.stubs.StubIndexImpl.get(StubIndexImpl.java:227)
 at com.intellij.psi.stubs.StubIndex.getElements(StubIndex.java:144)
 at com.intellij.psi.stubs.StubIndex.getElements(StubIndex.java:134)
 at com.intellij.psi.impl.java.stubs.index.JavaFullClassNameIndex.get(JavaFullClassNameIndex.java:48)
 at com.intellij.psi.impl.file.impl.JavaFileManagerImpl.findClassInIndex(JavaFileManagerImpl.java:145)
 at com.intellij.psi.impl.file.impl.JavaFileManagerImpl.findClass(JavaFileManagerImpl.java:129)
 at com.intellij.psi.impl.PsiElementFinderImpl.findClass(PsiElementFinderImpl.java:54)
 at com.intellij.psi.impl.JavaPsiFacadeImpl.findClass(JavaPsiFacadeImpl.java:102)
 at com.intellij.psi.impl.PsiElementFactoryImpl.createTypeByFQClassName(PsiElementFactoryImpl.java:411)
 at com.intellij.psi.PsiType.getTypeByName(PsiType.java:147)
 at com.intellij.psi.PsiType.getJavaLangObject(PsiType.java:159)
 at com.intellij.psi.impl.PsiClassImplUtil.getSuperTypes(PsiClassImplUtil.java:902)
 at com.intellij.psi.impl.source.PsiClassImpl.getSuperTypes(PsiClassImpl.java:249)
 at com.intellij.psi.impl.PsiClassImplUtil.processSuperTypes(PsiClassImplUtil.java:774)
 at com.intellij.psi.impl.PsiClassImplUtil.processDeclarationsInClassNotCached(PsiClassImplUtil.java:709)
 at com.intellij.psi.impl.PsiClassImplUtil.processDeclarationsInClass(PsiClassImplUtil.java:478)
 at com.intellij.psi.impl.PsiClassImplUtil.processDeclarationsInClass(PsiClassImplUtil.java:448)
 at com.intellij.psi.impl.source.PsiClassImpl.processDeclarations(PsiClassImpl.java:488)
 at com.intellij.psi.scope.util.PsiScopesUtil.treeWalkUp(PsiScopesUtil.java:72)
 at com.intellij.psi.scope.util.PsiScopesUtil.treeWalkUp(PsiScopesUtil.java:54)
 at com.intellij.psi.scope.util.PsiScopesUtil.resolveAndWalk(PsiScopesUtil.java:215)
 at com.intellij.psi.scope.util.PsiScopesUtil.resolveAndWalk(PsiScopesUtil.java:146)
 at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl.resolve(PsiJavaCodeReferenceElementImpl.java:438)
 at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl.access$100(PsiJavaCodeReferenceElementImpl.java:55)
 at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl$OurGenericsResolver.resolve(PsiJavaCodeReferenceElementImpl.java:339)
 at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl$OurGenericsResolver.resolve(PsiJavaCodeReferenceElementImpl.java:331)
 at com.intellij.psi.impl.source.resolve.ResolveCache$4.compute(ResolveCache.java:191)
 at com.intellij.psi.impl.source.resolve.ResolveCache$4.compute(ResolveCache.java:188)
 at com.intellij.openapi.util.RecursionManager$2.doPreventingRecursion(RecursionManager.java:112)
 at com.intellij.psi.impl.source.resolve.ResolveCache.resolveWithCaching(ResolveCache.java:188)
 at com.intellij.psi.impl.PsiImplUtil.multiResolveImpl(PsiImplUtil.java:779)
 at com.intellij.psi.impl.PsiImplUtil.multiResolveImpl(PsiImplUtil.java:769)
 at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl.multiResolve(PsiJavaCodeReferenceElementImpl.java:366)
 at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl.advancedResolve(PsiJavaCodeReferenceElementImpl.java:359)
 at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl.resolve(PsiJavaCodeReferenceElementImpl.java:328)
 at com.intellij.psi.impl.source.tree.JavaTreeCopyHandler.encodeInformationInRef(JavaTreeCopyHandler.java:207)
 at com.intellij.psi.impl.source.tree.JavaTreeCopyHandler.encodeInformation(JavaTreeCopyHandler.java:141)
 at com.intellij.psi.impl.source.tree.ChangeUtil.encodeInformation(ChangeUtil.java:62)
 at com.intellij.psi.impl.source.tree.ChangeUtil.encodeInformation(ChangeUtil.java:69)
 at com.intellij.psi.impl.source.tree.ChangeUtil.encodeInformation(ChangeUtil.java:69)
 at com.intellij.psi.impl.source.tree.ChangeUtil.encodeInformation(ChangeUtil.java:69)
 at com.intellij.psi.impl.source.tree.ChangeUtil.encodeInformation(ChangeUtil.java:69)
 at com.intellij.psi.impl.source.tree.ChangeUtil.encodeInformation(ChangeUtil.java:69)
 at com.intellij.psi.impl.source.tree.ChangeUtil.encodeInformation(ChangeUtil.java:53)
 at com.intellij.psi.impl.source.tree.ChangeUtil.encodeInformation(ChangeUtil.java:47)
 at com.intellij.psi.impl.file.PsiDirectoryImpl.updateAddedFile(PsiDirectoryImpl.java:392)
 at com.intellij.psi.impl.file.PsiDirectoryImpl.add(PsiDirectoryImpl.java:456)
 at com.intellij.psi.impl.file.PsiJavaDirectoryImpl.add(PsiJavaDirectoryImpl.java:64)
 at com.custom.custom.CustomModuleBuilder$1.run(CustomModuleBuilder.java:70)
 at com.intellij.openapi.command.WriteCommandAction$4.run(WriteCommandAction.java:176)
 at com.intellij.openapi.command.WriteCommandAction$Simple.run(WriteCommandAction.java:166)
 at com.intellij.openapi.application.RunResult.run(RunResult.java:38)
 at com.intellij.openapi.command.WriteCommandAction$2$1.run(WriteCommandAction.java:114)
 at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:931)
 at com.intellij.openapi.command.WriteCommandAction$2.run(WriteCommandAction.java:111)
 at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:124)
 at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:99)
 at com.intellij.openapi.command.WriteCommandAction.performWriteCommandAction(WriteCommandAction.java:108)
 at com.intellij.openapi.command.WriteCommandAction.execute(WriteCommandAction.java:80)
 at com.intellij.openapi.command.WriteCommandAction.runWriteCommandAction(WriteCommandAction.java:173)
 at com.custom.custom.Custom.CustomModuleBuilder.moduleCreated(CustomModuleBuilder.java:66)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:497)
 at com.intellij.util.EventDispatcher.dispatch(EventDispatcher.java:88)
 at com.intellij.util.EventDispatcher.access$100(EventDispatcher.java:34)
 at com.intellij.util.EventDispatcher$1.invoke(EventDispatcher.java:68)
 at com.sun.proxy.$Proxy110.moduleCreated(Unknown Source)
 at com.intellij.ide.util.projectWizard.ModuleBuilder.onModuleInitialized(ModuleBuilder.java:279)
 at com.intellij.ide.util.projectWizard.ModuleBuilder.access$000(ModuleBuilder.java:56)
 at com.intellij.ide.util.projectWizard.ModuleBuilder$3$1.run(ModuleBuilder.java:311)
 at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:931)
 at com.intellij.ide.util.projectWizard.ModuleBuilder$3.run(ModuleBuilder.java:308)
 at com.intellij.ide.startup.impl.StartupManagerImpl$7.run(StartupManagerImpl.java:369)
 at com.intellij.util.ui.UIUtil.invokeLaterIfNeeded(UIUtil.java:2350)
 at com.intellij.ide.startup.impl.StartupManagerImpl.runWhenProjectIsInitialized(StartupManagerImpl.java:365)
 at com.intellij.ide.util.projectWizard.ModuleBuilder.createAndCommitIfNeeded(ModuleBuilder.java:305)
 at com.intellij.ide.util.projectWizard.ModuleBuilder$4.compute(ModuleBuilder.java:355)
 at com.intellij.ide.util.projectWizard.ModuleBuilder$4.compute(ModuleBuilder.java:352)
 at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:955)
 at com.intellij.ide.util.projectWizard.ModuleBuilder.commitModule(ModuleBuilder.java:352)
 at com.intellij.openapi.roots.ui.configuration.actions.NewModuleAction.createModuleFromWizard(NewModuleAction.java:82)
 at com.intellij.openapi.roots.ui.configuration.actions.NewModuleAction.actionPerformed(NewModuleAction.java:61)
 at com.intellij.openapi.actionSystem.ex.ActionUtil.performActionDumbAware(ActionUtil.java:182)
 at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter$1.run(ActionMenuItem.java:312)
 at com.intellij.openapi.wm.impl.FocusManagerImpl.runOnOwnContext(FocusManagerImpl.java:958)
 at com.intellij.openapi.wm.impl.IdeFocusManagerImpl.runOnOwnContext(IdeFocusManagerImpl.java:124)
 at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter.actionPerformed(ActionMenuItem.java:282)
 at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
 at com.intellij.openapi.actionSystem.impl.ActionMenuItem.fireActionPerformed(ActionMenuItem.java:109)
 at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
 at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
 at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:308)
 at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
 at com.apple.laf.ScreenMenuItemCheckbox.itemStateChanged(ScreenMenuItemCheckbox.java:193)
 at java.awt.CheckboxMenuItem.processItemEvent(CheckboxMenuItem.java:389)
 at java.awt.CheckboxMenuItem.processEvent(CheckboxMenuItem.java:357)
 at java.awt.MenuComponent.dispatchEventImpl(MenuComponent.java:351)
 at java.awt.MenuComponent.dispatchEvent(MenuComponent.java:339)
 at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:761)
 at java.awt.EventQueue.access$500(EventQueue.java:97)
 at java.awt.EventQueue$3.run(EventQueue.java:709)
 at java.awt.EventQueue$3.run(EventQueue.java:703)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
 at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
 at java.awt.EventQueue$4.run(EventQueue.java:731)
 at java.awt.EventQueue$4.run(EventQueue.java:729)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
 at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
 at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:734)
 at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:569)
 at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:382)
 at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
 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)

Any help would be appreciated!

Thanks,
Weston

1 comment
Comment actions Permalink

Please see IndexNotReadyException javadoc (which it tells you in the exception message :-)) to see solutions.

0

Please sign in to leave a comment.