diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleBuildSupport.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleBuildSupport.java index 0543dd66c3..e7ec9e9961 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleBuildSupport.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleBuildSupport.java @@ -27,6 +27,7 @@ import org.eclipse.buildship.core.GradleBuild; import org.eclipse.buildship.core.GradleCore; import org.eclipse.buildship.core.internal.CorePlugin; +import org.eclipse.buildship.core.internal.DefaultGradleBuild; import org.eclipse.buildship.core.internal.launch.GradleClasspathProvider; import org.eclipse.buildship.core.internal.preferences.PersistentModel; import org.eclipse.buildship.core.internal.util.file.FileUtils; @@ -106,7 +107,17 @@ public void update(IProject project, boolean force, IProgressMonitor monitor) th || (settingsFile.exists() && JavaLanguageServerPlugin.getDigestStore().updateDigest(settingsFile.toPath())) || (buildKtsFile.exists() && JavaLanguageServerPlugin.getDigestStore().updateDigest(buildKtsFile.toPath())) || (settingsKtsFile.exists() && JavaLanguageServerPlugin.getDigestStore().updateDigest(settingsKtsFile.toPath())); - if (isRoot || shouldUpdate) { + // https://github.com/redhat-developer/vscode-java/issues/3893 + shouldUpdate = isRoot || shouldUpdate; + if (!shouldUpdate) { + if (force && gradleBuild instanceof DefaultGradleBuild defaultGradleBuild) { + org.eclipse.buildship.core.internal.configuration.BuildConfiguration gradleConfig = defaultGradleBuild.getBuildConfig(); + if (!gradleConfig.isAutoSync()) { + shouldUpdate = true; + } + } + } + if (shouldUpdate) { gradleBuild.synchronize(monitor); syncAnnotationProcessingConfiguration(gradleBuild, monitor); } diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradlePreferenceChangeListener.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradlePreferenceChangeListener.java index 7628feb686..35388f2ae1 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradlePreferenceChangeListener.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradlePreferenceChangeListener.java @@ -17,6 +17,9 @@ import java.nio.file.Paths; import java.util.Objects; +import org.eclipse.buildship.core.BuildConfiguration; +import org.eclipse.buildship.core.GradleBuild; +import org.eclipse.buildship.core.GradleCore; import org.eclipse.buildship.core.GradleDistribution; import org.eclipse.buildship.core.WrapperGradleDistribution; import org.eclipse.buildship.core.internal.CorePlugin; @@ -31,6 +34,7 @@ import org.eclipse.jdt.ls.core.internal.ProjectUtils; import org.eclipse.jdt.ls.core.internal.preferences.IPreferencesChangeListener; import org.eclipse.jdt.ls.core.internal.preferences.Preferences; +import org.eclipse.jdt.ls.core.internal.preferences.Preferences.FeatureStatus; import org.eclipse.jdt.ls.internal.gradle.checksums.ValidationResult; import org.eclipse.jdt.ls.internal.gradle.checksums.WrapperValidator; @@ -79,6 +83,20 @@ public void preferencesChange(Preferences oldPreferences, Preferences newPrefere } } } + boolean updateBuildConfigurationChanged = !Objects.equals(oldPreferences.getUpdateBuildConfigurationStatus(), newPreferences.getUpdateBuildConfigurationStatus()); + if (updateBuildConfigurationChanged) { + if (newPreferences.getUpdateBuildConfigurationStatus().equals(FeatureStatus.automatic)) { + for (IProject project : ProjectUtils.getGradleProjects()) { + ProjectConfiguration configuration = CorePlugin.configurationManager().loadProjectConfiguration(project); + if (!configuration.getBuildConfiguration().isAutoSync()) { + String projectPath = project.getLocation().toFile().getAbsolutePath(); + BuildConfiguration buildConfiguration = GradleProjectImporter.getBuildConfiguration(Paths.get(projectPath)); + GradleBuild gradleBuild = GradleCore.getWorkspace().createBuild(buildConfiguration); + gradleBuild.synchronize(new NullProgressMonitor()); + } + } + } + } } } 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 5eb3f89bfb..b38e3a33b3 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 @@ -40,6 +40,9 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import org.eclipse.buildship.core.GradleBuild; +import org.eclipse.buildship.core.GradleCore; +import org.eclipse.buildship.core.internal.DefaultGradleBuild; import org.eclipse.core.internal.preferences.EclipsePreferences; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; @@ -263,7 +266,22 @@ public void fileChanged(String uriString, CHANGE_TYPE changeType) { // The sync task is handled by Buildship when sync.auto is turned on, // except for the annotation processing configuration updating. // See https://github.com/redhat-developer/vscode-java/issues/2673 - GradleBuildSupport.syncAnnotationProcessingConfiguration(project, new NullProgressMonitor()); + // See https://github.com/redhat-developer/vscode-java/issues/3893 + Optional build = GradleCore.getWorkspace().getBuild(project); + boolean syncAnnotationProcessing = true; + if (build.isPresent()) { + GradleBuild gradleBuild = build.get(); + if (gradleBuild instanceof DefaultGradleBuild defaultGradleBuild) { + org.eclipse.buildship.core.internal.configuration.BuildConfiguration gradleConfig = defaultGradleBuild.getBuildConfig(); + if (!gradleConfig.isAutoSync()) { + updateProject(project, true); + syncAnnotationProcessing = false; + } + } + } + if (syncAnnotationProcessing) { + GradleBuildSupport.syncAnnotationProcessingConfiguration(project, new NullProgressMonitor()); + } return; } updateProject(project, true); diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/GradleBuildSupportTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/GradleBuildSupportTest.java index 9f19f55ab4..7b5282a586 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/GradleBuildSupportTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/GradleBuildSupportTest.java @@ -15,12 +15,22 @@ import static org.eclipse.jdt.ls.core.internal.ResourceUtils.getContent; import static org.eclipse.jdt.ls.core.internal.ResourceUtils.setContent; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import java.io.InputStream; import java.net.URI; +import java.util.List; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.ls.core.internal.ProjectUtils; +import org.eclipse.jdt.ls.core.internal.WorkspaceHelper; +import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager.CHANGE_TYPE; +import org.eclipse.jdt.ls.core.internal.preferences.Preferences.FeatureStatus; import org.junit.Test; /** @@ -62,4 +72,37 @@ public void testUpdate() throws Exception { assertEquals("1.8", ProjectUtils.getJavaSourceLevel(project)); } + // https://github.com/redhat-developer/vscode-java/issues/3893 + @Test + public void testUpdateModule() throws Exception { + FeatureStatus oldSettings = preferenceManager.getPreferences().getUpdateBuildConfigurationStatus(); + try { + preferenceManager.getPreferences().setUpdateBuildConfigurationStatus(FeatureStatus.disabled); + List projects = importProjects("gradle/sample"); + assertEquals(2, projects.size()); // app, sample + IProject root = WorkspaceHelper.getProject("sample"); + assertIsGradleProject(root); + IProject project = WorkspaceHelper.getProject("app"); + assertIsGradleProject(project); + assertIsJavaProject(project); + IJavaProject javaProject = JavaCore.create(project); + IType type = javaProject.findType("org.apache.commons.lang3.StringUtils"); + assertNull(type); + IFile build2 = project.getFile("/build.gradle2"); + InputStream contents = build2.getContents(); + IFile build = project.getFile("/build.gradle"); + build.setContents(contents, true, false, null); + projectsManager.fileChanged(build.getRawLocation().toPath().toUri().toString(), CHANGE_TYPE.CHANGED); + waitForBackgroundJobs(); + type = javaProject.findType("org.apache.commons.lang3.StringUtils"); + assertNull(type); + projectsManager.updateProject(project, true); + waitForBackgroundJobs(); + type = javaProject.findType("org.apache.commons.lang3.StringUtils"); + assertNotNull(type); + } finally { + preferenceManager.getPreferences().setUpdateBuildConfigurationStatus(oldSettings); + } + } + }