Skip to content

Commit

Permalink
Ensure every null analysis annotation has a value defined when enabled
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <[email protected]>
  • Loading branch information
snjeza committed Jun 18, 2024
1 parent 22931df commit ad0245f
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -2529,7 +2529,7 @@ private String findTypeInProject(IJavaProject javaProject, String annotationType
*/
private Map<String, String> generateProjectNullAnalysisOptions(String nonnullType, String nullableType, String nonnullbydefaultType) {
Map<String, String> 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<String, String> defaultOptions = JavaCore.getDefaultOptions();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="lib/org-netbeans-api-annotations-common-RELEASE200.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions org.eclipse.jdt.ls.tests/projects/eclipse/testnullable3/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>testnullable2</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -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) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<IProject> 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);
}
}

}

0 comments on commit ad0245f

Please sign in to comment.