Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Support @NonNullApi and @NonNullFields on package #1095

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions nullaway/src/main/java/com/uber/nullaway/CodeAnnotationInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ private static boolean fromAnnotatedPackage(
String className = outermostClassSymbol.getQualifiedName().toString();
Symbol.PackageSymbol enclosingPackage = ASTHelpers.enclosingPackage(outermostClassSymbol);
if (!config.fromExplicitlyAnnotatedPackage(className)
&& !(enclosingPackage != null
&& hasDirectAnnotationWithSimpleName(
enclosingPackage, NullabilityUtil.NULLMARKED_SIMPLE_NAME))) {
&& !(enclosingPackage != null && annotatedAsNullMarkedPackage(enclosingPackage))) {
// By default, unknown code is unannotated unless @NullMarked or configured as annotated by
// package name
return false;
Expand All @@ -104,6 +102,21 @@ && hasDirectAnnotationWithSimpleName(
return true;
}

private static final String NONNULLAPI_NAME = "org.springframework.lang.NonNullApi";
private static final String NONNULLFIELDS_NAME = "org.springframework.lang.NonNullFields";

private static boolean annotatedAsNullMarkedPackage(Symbol.PackageSymbol enclosingPackage) {
return hasDirectAnnotationWithSimpleName(
enclosingPackage, NullabilityUtil.NULLMARKED_SIMPLE_NAME)
|| (packageHasAnnotation(enclosingPackage, NONNULLAPI_NAME)
&& packageHasAnnotation(enclosingPackage, NONNULLFIELDS_NAME));
}

private static boolean packageHasAnnotation(Symbol sym, String annotationClass) {
return sym.getAnnotationMirrors().stream()
.anyMatch(a -> a.type.tsym.getQualifiedName().contentEquals(annotationClass));
}

/**
* Check if a symbol comes from generated code.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,44 @@ public void nullMarkedPackageLevel() {
.doTest();
}

@Test
public void springNullMarkedPackage() {
defaultCompilationHelper
.addSourceLines(
"package-info.java",
"@NonNullApi @NonNullFields package com.example.thirdparty;",
"import org.springframework.lang.NonNullApi;",
"import org.springframework.lang.NonNullFields;")
.addSourceLines(
"ThirdPartyAnnotatedUtils.java",
"package com.example.thirdparty;",
"import org.jspecify.annotations.Nullable;",
"public class ThirdPartyAnnotatedUtils {",
" public static String toStringOrDefault(@Nullable Object o1, String s) {",
" if (o1 != null) {",
" return o1.toString();",
" }",
" return s;",
" }",
"}")
.addSourceLines(
"Test.java",
"package com.uber;",
"import com.example.thirdparty.ThirdPartyAnnotatedUtils;",
"public class Test {",
" public static void test(Object o) {",
" // Safe: passing @NonNull on both args",
" ThirdPartyAnnotatedUtils.toStringOrDefault(o, \"default\");",
" // Safe: first arg is @Nullable",
" ThirdPartyAnnotatedUtils.toStringOrDefault(null, \"default\");",
" // Unsafe: @NullMarked means the second arg is @NonNull by default, not @Nullable",
" // BUG: Diagnostic contains: passing @Nullable parameter",
" ThirdPartyAnnotatedUtils.toStringOrDefault(o, null);",
" }",
"}")
.doTest();
}

@Test
public void nullMarkedPackageEnablesChecking() {
defaultCompilationHelper
Expand Down
Loading