diff --git a/.gitignore b/.gitignore index aa39c96c08..fc78d75716 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ bin/ **/lib/ !org.eclipse.jdt.ls.tests/projects/singlefile/simple/lib/ !org.eclipse.jdt.ls.tests/projects/eclipse/testnullable2/lib/ +!org.eclipse.jdt.ls.tests/projects/eclipse/testnullable3/lib/ !org.eclipse.jdt.ls.tests/projects/maven/salut5/node_modules/ !org.eclipse.jdt.ls.tests/fakejdk2/17a/bin !org.eclipse.jdt.ls.tests/fakejdk2/17a/lib diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java index a0a0620ae0..88bc6fcc0e 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java @@ -2455,7 +2455,7 @@ private boolean hasAnnotationNullAnalysisTypes() { String nonnullType = getAnnotationType(javaProject, this.nonnullTypes, nonnullClasspathStorage); String nullableType = getAnnotationType(javaProject, this.nullableTypes, nullableClasspathStorage); String nonnullbydefaultTypes = getAnnotationType(javaProject, this.nonnullbydefaultTypes, nonnullbydefaultClasspathStorage); - if (nonnullType != null || nullableType != null || nonnullbydefaultTypes != null) { + if (nonnullType != null && nullableType != null && nonnullbydefaultTypes != null) { return true; } } @@ -2529,7 +2529,7 @@ private String findTypeInProject(IJavaProject javaProject, String annotationType */ private Map generateProjectNullAnalysisOptions(String nonnullType, String nullableType, String nonnullbydefaultType) { Map options = new HashMap<>(); - if (nonnullType == null && nullableType == null && nonnullbydefaultType == null) { + if (nonnullType == null || nullableType == null || nonnullbydefaultType == null) { options.put(JavaCore.COMPILER_ANNOTATION_NULL_ANALYSIS, "disabled"); // set default values Hashtable defaultOptions = JavaCore.getDefaultOptions(); diff --git a/org.eclipse.jdt.ls.tests/projects/eclipse/testnullable3/.classpath b/org.eclipse.jdt.ls.tests/projects/eclipse/testnullable3/.classpath new file mode 100644 index 0000000000..0781177cda --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/eclipse/testnullable3/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/org.eclipse.jdt.ls.tests/projects/eclipse/testnullable3/.project b/org.eclipse.jdt.ls.tests/projects/eclipse/testnullable3/.project new file mode 100644 index 0000000000..cac8291a30 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/eclipse/testnullable3/.project @@ -0,0 +1,17 @@ + + + testnullable2 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/org.eclipse.jdt.ls.tests/projects/eclipse/testnullable3/lib/org-netbeans-api-annotations-common-RELEASE200.jar b/org.eclipse.jdt.ls.tests/projects/eclipse/testnullable3/lib/org-netbeans-api-annotations-common-RELEASE200.jar new file mode 100644 index 0000000000..cb8b9e4edc Binary files /dev/null and b/org.eclipse.jdt.ls.tests/projects/eclipse/testnullable3/lib/org-netbeans-api-annotations-common-RELEASE200.jar differ diff --git a/org.eclipse.jdt.ls.tests/projects/eclipse/testnullable3/src/org/sample/Main.java b/org.eclipse.jdt.ls.tests/projects/eclipse/testnullable3/src/org/sample/Main.java new file mode 100644 index 0000000000..a72ec21aec --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/eclipse/testnullable3/src/org/sample/Main.java @@ -0,0 +1,10 @@ +package org.sample; +import javax.annotation.Nonnull; +public class Main { + public static void main(String[] args) { + Test.foo(null); + } +} +class Test { + static void foo(@Nonnull Test a) { } +} \ No newline at end of file diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/preferences/NullAnalysisTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/preferences/NullAnalysisTest.java index ddce1d54ae..78cc55f558 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/preferences/NullAnalysisTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/preferences/NullAnalysisTest.java @@ -13,6 +13,7 @@ package org.eclipse.jdt.ls.core.internal.preferences; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -227,4 +228,31 @@ public void testNonnullbyDefault() throws Exception { } } + // https://github.com/redhat-developer/vscode-java/issues/3387 + @Test + public void testMissingNonNull() throws Exception { + try { + this.preferenceManager.getPreferences().setNonnullTypes(List.of("javax.annotation.Nonnull", "org.eclipse.jdt.annotation.NonNull")); + this.preferenceManager.getPreferences().setNullableTypes(List.of("javax.annotation.Nullable", "org.eclipse.jdt.annotation.Nullable")); + this.preferenceManager.getPreferences().setNonnullbydefaultTypes(List.of("org.eclipse.jdt.annotation.NonNullByDefault", "javax.annotation.ParametersAreNonnullByDefault")); + this.preferenceManager.getPreferences().setNullAnalysisMode(FeatureStatus.automatic); + List projects = importProjects("eclipse/testnullable3"); + IProject project = projects.get(0); + assertIsJavaProject(project); + boolean updated = this.preferenceManager.getPreferences().updateAnnotationNullAnalysisOptions(); + assertFalse(updated); + projects = importProjects("eclipse/testnullable2"); + project = projects.get(0); + assertIsJavaProject(project); + updated = this.preferenceManager.getPreferences().updateAnnotationNullAnalysisOptions(); + assertTrue(updated); + } finally { + this.preferenceManager.getPreferences().setNonnullTypes(Collections.emptyList()); + this.preferenceManager.getPreferences().setNullableTypes(Collections.emptyList()); + this.preferenceManager.getPreferences().setNonnullbydefaultTypes(Collections.emptyList()); + this.preferenceManager.getPreferences().updateAnnotationNullAnalysisOptions(); + this.preferenceManager.getPreferences().setNullAnalysisMode(FeatureStatus.disabled); + } + } + }