From 573c02ddd8de95a965150794ce33e07a97431d6a Mon Sep 17 00:00:00 2001 From: Snjezana Peco Date: Sun, 6 Oct 2024 18:53:51 +0200 Subject: [PATCH] Intermittent Class Loading Issues with Java Core Packages --- .../internal/JavaLanguageServerPlugin.java | 24 ++++++++++ .../internal/handlers/JDTLanguageServer.java | 3 ++ .../internal/managers/ProjectsManager.java | 4 ++ .../managers/StandardProjectsManager.java | 46 +++++++++++++++++++ 4 files changed, 77 insertions(+) diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JavaLanguageServerPlugin.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JavaLanguageServerPlugin.java index 9f248da082..d7ddb1d9c0 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JavaLanguageServerPlugin.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JavaLanguageServerPlugin.java @@ -32,11 +32,14 @@ import org.eclipse.core.net.proxy.IProxyService; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Plugin; import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; import org.eclipse.core.runtime.preferences.DefaultScope; +import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.WorkingCopyOwner; import org.eclipse.jdt.core.manipulation.JavaManipulation; import org.eclipse.jdt.internal.codeassist.impl.AssistOptions; @@ -174,6 +177,27 @@ public void start(BundleContext bundleContext) throws Exception { if (System.getProperty(AssistOptions.PROPERTY_SubstringMatch) == null) { System.setProperty(AssistOptions.PROPERTY_SubstringMatch, "false"); } + // https://github.com/redhat-developer/vscode-java/issues/3797 + Job initializeAfterLoad = new Job("Initialize After Load") { + + @Override + protected IStatus run(IProgressMonitor monitor) { + setPriority(Job.SHORT); + try { + JavaCore.initializeAfterLoad(monitor); + } catch (CoreException e) { + logException(e); + try { + JavaCore.rebuildIndex(monitor); + } catch (CoreException e1) { + logException(e); + } + } + return Status.OK_STATUS; + } + + }; + initializeAfterLoad.schedule(); } private void disableServices() { diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/JDTLanguageServer.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/JDTLanguageServer.java index ea799410f3..3f6f56a3df 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/JDTLanguageServer.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/JDTLanguageServer.java @@ -329,6 +329,9 @@ public IStatus run(IProgressMonitor monitor) { // https://github.com/redhat-developer/vscode-java/issues/3637 - delay registerWatchers pm.registerWatchers(); debugTrace(">> watchers registered"); + // https://github.com/redhat-developer/vscode-java/issues/3797 + pm.checkIndexes(); + debugTrace(">> indexes checked"); pm.projectsBuildFinished(monitor); telemetryManager.onBuildFinished(System.currentTimeMillis()); workspaceDiagnosticsHandler.publishDiagnostics(monitor); diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/ProjectsManager.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/ProjectsManager.java index 75b1d3dfa1..821d3645dc 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/ProjectsManager.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/ProjectsManager.java @@ -888,4 +888,8 @@ private void clearDiagnostics(IProject project) throws CoreException { } } } + + public void checkIndexes() { + } + } diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/StandardProjectsManager.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/StandardProjectsManager.java index bb93a6bd1b..a89a8ffbc2 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/StandardProjectsManager.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/StandardProjectsManager.java @@ -57,6 +57,7 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status; @@ -67,6 +68,11 @@ import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.search.IJavaSearchConstants; +import org.eclipse.jdt.core.search.IJavaSearchScope; +import org.eclipse.jdt.core.search.SearchEngine; +import org.eclipse.jdt.core.search.SearchPattern; +import org.eclipse.jdt.core.search.TypeNameRequestor; import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; import org.eclipse.jdt.launching.AbstractVMInstall; import org.eclipse.jdt.launching.IVMInstall; @@ -703,4 +709,44 @@ public void projectsBuildFinished(IProgressMonitor monitor) { public boolean isBuildFinished() { return buildFinished; } + + @Override + public void checkIndexes() { + Job job = new Job(("Check Indexes")) { + + @Override + protected IStatus run(IProgressMonitor monitor) { + IJavaProject[] javaProjects = ProjectUtils.getJavaProjects(); + if (javaProjects == null || javaProjects.length <= 0) { + return Status.OK_STATUS; + } + SearchEngine engine = new SearchEngine(); + IJavaSearchScope scope = SearchEngine.createWorkspaceScope(); + boolean found[] = { false }; + try { + engine.searchAllTypeNames("java.lang".toCharArray(), SearchPattern.R_EXACT_MATCH, "String".toCharArray(), //$NON-NLS-1$ + SearchPattern.R_PATTERN_MATCH | SearchPattern.R_CASE_SENSITIVE, IJavaSearchConstants.CLASS, scope, new TypeNameRequestor() { + @Override + public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) { + found[0] = true; + } + }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, monitor); + } catch (JavaModelException e) { + // search failed + } catch (OperationCanceledException e) { + return Status.CANCEL_STATUS; + } + if (!found[0]) { + try { + JavaLanguageServerPlugin.logInfo("rebuild indexes"); + JavaCore.rebuildIndex(monitor); + } catch (CoreException e) { + JavaLanguageServerPlugin.logException(e); + } + } + return Status.OK_STATUS; + } + }; + job.schedule(); + } }