Skip to content

Commit

Permalink
Do not forget existing project options when setting null analysis opt…
Browse files Browse the repository at this point in the history
…ions (#2299)

Signed-off-by: Shi Chen <chenshi@microsoft.com>
  • Loading branch information
CsCherrYY committed Oct 28, 2022
1 parent 9b1b3e5 commit e110f57
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2055,16 +2055,23 @@ public boolean updateAnnotationNullAnalysisOptions(IJavaProject javaProject) {
if (javaProject.getElementName().equals(ProjectsManager.DEFAULT_PROJECT_NAME)) {
return false;
}
Map<String, String> projectOptions = javaProject.getOptions(true);
if (projectOptions == null) {
Map<String, String> projectInheritOptions = javaProject.getOptions(true);
if (projectInheritOptions == null) {
return false;
}
String nonnullType = getAnnotationType(javaProject, this.nonnullTypes, nonnullClasspathStorage);
String nullableType = getAnnotationType(javaProject, this.nullableTypes, nullableClasspathStorage);
Map<String, String> projectNullAnalysisOptions = generateProjectNullAnalysisOptions(nonnullType, nullableType);
boolean shouldUpdate = !projectNullAnalysisOptions.entrySet().stream().allMatch(e -> e.getValue().equals(projectOptions.get(e.getKey())));
boolean shouldUpdate = !projectNullAnalysisOptions.entrySet().stream().allMatch(e -> e.getValue().equals(projectInheritOptions.get(e.getKey())));
if (shouldUpdate) {
javaProject.setOptions(projectNullAnalysisOptions);
// get existing project options
Map<String, String> projectOptions = javaProject.getOptions(false);
if (projectOptions != null) {
projectOptions.putAll(projectNullAnalysisOptions);
javaProject.setOptions(projectOptions);
} else {
return false;
}
}
return shouldUpdate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.ProjectUtils;
import org.eclipse.jdt.ls.core.internal.ResourceUtils;
import org.eclipse.jdt.ls.core.internal.handlers.BuildWorkspaceHandler;
import org.eclipse.jdt.ls.core.internal.managers.AbstractGradleBasedTest;
Expand Down Expand Up @@ -120,4 +123,28 @@ public void testNullAnalysisDisabled() throws Exception {
assertEquals(3, warningMarkers.size());
assertNoErrors(project);
}

@Test
public void testKeepExistingProjectOptions() throws Exception {
try {
this.preferenceManager.getPreferences().setNonnullTypes(ImmutableList.of("javax.annotation.Nonnull", "org.eclipse.jdt.annotation.NonNull"));
this.preferenceManager.getPreferences().setNullableTypes(ImmutableList.of("javax.annotation.Nullable", "org.eclipse.jdt.annotation.Nullable"));
IProject project = importGradleProject("null-analysis");
assertIsJavaProject(project);
if (this.preferenceManager.getPreferences().updateAnnotationNullAnalysisOptions()) {
BuildWorkspaceHandler buildWorkspaceHandler = new BuildWorkspaceHandler(JavaLanguageServerPlugin.getProjectsManager());
buildWorkspaceHandler.buildWorkspace(true, new NullProgressMonitor());
}
IJavaProject javaProject = ProjectUtils.getJavaProject(project);
// sourceCompatibility = '11' defined in project null-analysis build.gradle
assertEquals("11", javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, false));
assertEquals("11", javaProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, false));
assertEquals("11", javaProject.getOption(JavaCore.COMPILER_SOURCE, false));

} finally {
this.preferenceManager.getPreferences().setNonnullTypes(Collections.emptyList());
this.preferenceManager.getPreferences().setNullableTypes(Collections.emptyList());
this.preferenceManager.getPreferences().updateAnnotationNullAnalysisOptions();
}
}
}

0 comments on commit e110f57

Please sign in to comment.