Skip to content

Commit

Permalink
There is no way to refresh the classpath after adding a library to gr…
Browse files Browse the repository at this point in the history
…adle

Signed-off-by: Snjezana Peco <[email protected]>
  • Loading branch information
snjeza committed Dec 29, 2024
1 parent 1d41dde commit 784267b
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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());
}
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<GradleBuild> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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<IProject> 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);
}
}

}

0 comments on commit 784267b

Please sign in to comment.