Skip to content

Commit 8d06711

Browse files
snjezargrunber
authored andcommitted
Ensure every null analysis annotation has a value defined when enabled
Signed-off-by: Snjezana Peco <[email protected]>
1 parent 22931df commit 8d06711

File tree

7 files changed

+65
-2
lines changed

7 files changed

+65
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ bin/
55
**/lib/
66
!org.eclipse.jdt.ls.tests/projects/singlefile/simple/lib/
77
!org.eclipse.jdt.ls.tests/projects/eclipse/testnullable2/lib/
8+
!org.eclipse.jdt.ls.tests/projects/eclipse/testnullable3/lib/
89
!org.eclipse.jdt.ls.tests/projects/maven/salut5/node_modules/
910
!org.eclipse.jdt.ls.tests/fakejdk2/17a/bin
1011
!org.eclipse.jdt.ls.tests/fakejdk2/17a/lib

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2455,7 +2455,7 @@ private boolean hasAnnotationNullAnalysisTypes() {
24552455
String nonnullType = getAnnotationType(javaProject, this.nonnullTypes, nonnullClasspathStorage);
24562456
String nullableType = getAnnotationType(javaProject, this.nullableTypes, nullableClasspathStorage);
24572457
String nonnullbydefaultTypes = getAnnotationType(javaProject, this.nonnullbydefaultTypes, nonnullbydefaultClasspathStorage);
2458-
if (nonnullType != null || nullableType != null || nonnullbydefaultTypes != null) {
2458+
if (nonnullType != null && nullableType != null && nonnullbydefaultTypes != null) {
24592459
return true;
24602460
}
24612461
}
@@ -2529,7 +2529,7 @@ private String findTypeInProject(IJavaProject javaProject, String annotationType
25292529
*/
25302530
private Map<String, String> generateProjectNullAnalysisOptions(String nonnullType, String nullableType, String nonnullbydefaultType) {
25312531
Map<String, String> options = new HashMap<>();
2532-
if (nonnullType == null && nullableType == null && nonnullbydefaultType == null) {
2532+
if (nonnullType == null || nullableType == null || nonnullbydefaultType == null) {
25332533
options.put(JavaCore.COMPILER_ANNOTATION_NULL_ANALYSIS, "disabled");
25342534
// set default values
25352535
Hashtable<String, String> defaultOptions = JavaCore.getDefaultOptions();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
4+
<classpathentry kind="src" path="src"/>
5+
<classpathentry kind="lib" path="lib/org-netbeans-api-annotations-common-RELEASE200.jar"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>testnullable2</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.sample;
2+
import javax.annotation.Nonnull;
3+
public class Main {
4+
public static void main(String[] args) {
5+
Test.foo(null);
6+
}
7+
}
8+
class Test {
9+
static void foo(@Nonnull Test a) { }
10+
}

org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/preferences/NullAnalysisTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package org.eclipse.jdt.ls.core.internal.preferences;
1414

1515
import static org.junit.Assert.assertEquals;
16+
import static org.junit.Assert.assertFalse;
1617
import static org.junit.Assert.assertNotNull;
1718
import static org.junit.Assert.assertNull;
1819
import static org.junit.Assert.assertTrue;
@@ -227,4 +228,31 @@ public void testNonnullbyDefault() throws Exception {
227228
}
228229
}
229230

231+
// https://github.com/redhat-developer/vscode-java/issues/3387
232+
@Test
233+
public void testMissingNonNull() throws Exception {
234+
try {
235+
this.preferenceManager.getPreferences().setNonnullTypes(List.of("javax.annotation.Nonnull", "org.eclipse.jdt.annotation.NonNull"));
236+
this.preferenceManager.getPreferences().setNullableTypes(List.of("javax.annotation.Nullable", "org.eclipse.jdt.annotation.Nullable"));
237+
this.preferenceManager.getPreferences().setNonnullbydefaultTypes(List.of("org.eclipse.jdt.annotation.NonNullByDefault", "javax.annotation.ParametersAreNonnullByDefault"));
238+
this.preferenceManager.getPreferences().setNullAnalysisMode(FeatureStatus.automatic);
239+
List<IProject> projects = importProjects("eclipse/testnullable3");
240+
IProject project = projects.get(0);
241+
assertIsJavaProject(project);
242+
boolean updated = this.preferenceManager.getPreferences().updateAnnotationNullAnalysisOptions();
243+
assertFalse(updated);
244+
projects = importProjects("eclipse/testnullable2");
245+
project = projects.get(0);
246+
assertIsJavaProject(project);
247+
updated = this.preferenceManager.getPreferences().updateAnnotationNullAnalysisOptions();
248+
assertTrue(updated);
249+
} finally {
250+
this.preferenceManager.getPreferences().setNonnullTypes(Collections.emptyList());
251+
this.preferenceManager.getPreferences().setNullableTypes(Collections.emptyList());
252+
this.preferenceManager.getPreferences().setNonnullbydefaultTypes(Collections.emptyList());
253+
this.preferenceManager.getPreferences().updateAnnotationNullAnalysisOptions();
254+
this.preferenceManager.getPreferences().setNullAnalysisMode(FeatureStatus.disabled);
255+
}
256+
}
257+
230258
}

0 commit comments

Comments
 (0)