diff --git a/nullaway/src/main/java/com/uber/nullaway/NullAway.java b/nullaway/src/main/java/com/uber/nullaway/NullAway.java index 1475283549..b81023214d 100644 --- a/nullaway/src/main/java/com/uber/nullaway/NullAway.java +++ b/nullaway/src/main/java/com/uber/nullaway/NullAway.java @@ -2082,6 +2082,8 @@ private Description handleInvocation( : Nullness.NONNULL); } } + + // perform generics checks for calls to annotated methods in JSpecify mode if (config.isJSpecifyMode()) { genericsChecks.compareGenericTypeParameterNullabilityForCall(methodSymbol, tree, state); if (!methodSymbol.getTypeParameters().isEmpty()) { @@ -2090,7 +2092,9 @@ private Description handleInvocation( } } - // Allow handlers to override the list of non-null argument positions + // Allow handlers to override the list of non-null argument positions. For a varargs parameter, + // this array holds the nullness of individual varargs arguments; the case of a varargs array is + // handled separately @Nullable Nullness[] finalArgumentPositionNullness = handler.onOverrideMethodInvocationParametersNullability( state.context, methodSymbol, isMethodAnnotated, argumentPositionNullness); @@ -2106,38 +2110,29 @@ private Description handleInvocation( } boolean argIsNonNull = Objects.equals(Nullness.NONNULL, finalArgumentPositionNullness[argPos]); - boolean mayActualBeNull = false; if (varArgsPassedAsArray) { // This is the case where an array is explicitly passed in the position of the // varargs parameter - // Only check for a nullable varargs array if the method is annotated, or a restrictive - // @NonNull annotation is present on the varargs array itself. In legacy mode, a - // declaration @NonNull annotation also implies the array is @NonNull. Also check for - // JetBrains @NotNull declaration annotation (due to - // https://github.com/uber/NullAway/issues/720) - VarSymbol formalParamSymbol = formalParams.get(formalParams.size() - 1); - boolean restrictiveNonNullVarargsArray = - config.acknowledgeRestrictiveAnnotations() - && Nullness.varargsArrayIsNonNull(formalParamSymbol, config); - boolean checkForNullableVarargsArray = - isMethodAnnotated - || (config.isLegacyAnnotationLocation() && argIsNonNull) - || restrictiveNonNullVarargsArray - || NullabilityUtil.hasJetBrainsNotNullDeclarationAnnotation(formalParamSymbol); - if (checkForNullableVarargsArray) { - // If varargs array itself is not @Nullable, cannot pass @Nullable array - if (restrictiveNonNullVarargsArray - || !Nullness.varargsArrayIsNullable(formalParams.get(argPos), config)) { - mayActualBeNull = mayBeNullExpr(state, actual); - } - } - } else { - if (!argIsNonNull) { - // argument can be @Nullable, so nothing to check - return; - } - mayActualBeNull = mayBeNullExpr(state, actual); + VarSymbol varargsFormalParam = formalParams.get(formalParams.size() - 1); + // when is the varargs array itself @NonNull? + // 1. For @NullMarked methods, as long as there is no @Nullable annotation + Nullness varargsArrayNullness = + (isMethodAnnotated && !Nullness.varargsArrayIsNullable(varargsFormalParam, config)) + ? Nullness.NONNULL + : null; + // 2. For @NullUnmarked methods, there should be an explicit @NonNull annotation. This + // is handled by the RestrictiveAnnotationHandler + // 3. Library model indicating it is @NonNull (handled in LibraryModelsHandler) + varargsArrayNullness = + handler.onOverrideMethodInvocationVarargsArrayNullability( + state.context, methodSymbol, isMethodAnnotated, varargsArrayNullness); + argIsNonNull = Objects.equals(varargsArrayNullness, Nullness.NONNULL); + } + if (!argIsNonNull) { + // argument can be @Nullable, so nothing to check + return; } + boolean mayActualBeNull = mayBeNullExpr(state, actual); if (mayActualBeNull) { String message = "passing @Nullable parameter '" diff --git a/nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java b/nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java index 12112b07a7..712978aebf 100644 --- a/nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java +++ b/nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java @@ -677,7 +677,7 @@ private static boolean checkArrayElementAnnotations( * Does the given symbol have a JetBrains @NotNull declaration annotation? Useful for workarounds * in light of https://github.com/uber/NullAway/issues/720 */ - public static boolean hasJetBrainsNotNullDeclarationAnnotation(Symbol.VarSymbol varSymbol) { + public static boolean hasJetBrainsNotNullDeclarationAnnotation(Symbol varSymbol) { // We explicitly ignore type-use annotations here, looking for @NotNull used as a // declaration annotation, which is why this logic is simpler than e.g. // NullabilityUtil.getAllAnnotationsForParameter. diff --git a/nullaway/src/main/java/com/uber/nullaway/Nullness.java b/nullaway/src/main/java/com/uber/nullaway/Nullness.java index c2c063ce52..afdccacb4a 100644 --- a/nullaway/src/main/java/com/uber/nullaway/Nullness.java +++ b/nullaway/src/main/java/com/uber/nullaway/Nullness.java @@ -315,7 +315,11 @@ public static boolean varargsArrayIsNullable(Symbol paramSymbol, Config config) public static boolean varargsArrayIsNonNull(Symbol paramSymbol, Config config) { return hasNonNullTypeUseAnnotation(paramSymbol, config) || (config.isLegacyAnnotationLocation() - && hasNonNullDeclarationAnnotation(paramSymbol, config)); + && hasNonNullDeclarationAnnotation(paramSymbol, config)) + // For Kotlin-generated class files, we treat the presence of a JetBrains @NotNull + // declaration annotation on a varargs parameter as indicating that the varargs array itself + // is non-null. See https://github.com/uber/NullAway/issues/720 + || NullabilityUtil.hasJetBrainsNotNullDeclarationAnnotation(paramSymbol); } /** Checks if the symbol has a {@code @Nullable} declaration annotation */ diff --git a/nullaway/src/main/java/com/uber/nullaway/handlers/CompositeHandler.java b/nullaway/src/main/java/com/uber/nullaway/handlers/CompositeHandler.java index f6ba27c09d..86de187238 100644 --- a/nullaway/src/main/java/com/uber/nullaway/handlers/CompositeHandler.java +++ b/nullaway/src/main/java/com/uber/nullaway/handlers/CompositeHandler.java @@ -157,6 +157,20 @@ public boolean onOverrideFieldNullability(Symbol field) { return argumentPositionNullness; } + @Override + public @Nullable Nullness onOverrideMethodInvocationVarargsArrayNullability( + Context context, + Symbol.MethodSymbol methodSymbol, + boolean isAnnotated, + @Nullable Nullness varargsArrayNullness) { + for (Handler h : handlers) { + varargsArrayNullness = + h.onOverrideMethodInvocationVarargsArrayNullability( + context, methodSymbol, isAnnotated, varargsArrayNullness); + } + return varargsArrayNullness; + } + @Override public boolean onOverrideMayBeNullExpr( NullAway analysis, diff --git a/nullaway/src/main/java/com/uber/nullaway/handlers/Handler.java b/nullaway/src/main/java/com/uber/nullaway/handlers/Handler.java index 0a1fd1621a..af551d6b17 100644 --- a/nullaway/src/main/java/com/uber/nullaway/handlers/Handler.java +++ b/nullaway/src/main/java/com/uber/nullaway/handlers/Handler.java @@ -209,7 +209,10 @@ default boolean onOverrideFieldNullability(Symbol field) { * @param argumentPositionNullness Nullness info for each argument position as computed by * upstream handlers and/or the base analysis. Some entries may be {@code null}, indicating * upstream handlers and the base analysis consider the parameter to be nullness-unknown, - * usually since the parameter is from unannotated code. + * usually since the parameter is from unannotated code. For a varargs parameter, the nullness + * for when individual parameters are passed in the varargs position will be stored in this + * array. See {@link #onOverrideMethodInvocationVarargsArrayNullability} for overriding the + * nullness of the varargs array itself. * @return The updated nullness info for each argument position, as computed by the current * handler. */ @@ -222,6 +225,32 @@ default boolean onOverrideFieldNullability(Symbol field) { return argumentPositionNullness; } + /** + * Called to potentially override the nullability of the array itself when a varargs method is + * invoked by passing an array in the varargs position. + * + *

This hook is intentionally separate from {@link + * #onOverrideMethodInvocationParametersNullability(Context, Symbol.MethodSymbol, boolean, + * Nullness[])}. A single parameter-slot nullness value is not expressive enough for varargs, + * where the array and its elements can have different nullability. + * + * @param context The current context. + * @param methodSymbol The method symbol for the method in question. + * @param isAnnotated A boolean flag indicating whether the called method is annotated + * @param varargsArrayNullness current value for the varargs array nullability, or {@code null} if + * upstream handlers and the base analysis consider the parameter to be nullness-unknown + * @return Updated explicit nullability for the varargs array, or {@code varargsArrayNullness} if + * the current handler does not provide an override. + */ + default @Nullable Nullness onOverrideMethodInvocationVarargsArrayNullability( + Context context, + Symbol.MethodSymbol methodSymbol, + boolean isAnnotated, + @Nullable Nullness varargsArrayNullness) { + // NoOp + return varargsArrayNullness; + } + /** * Called when the Dataflow analysis generates the initial NullnessStore for a method or lambda. * diff --git a/nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java b/nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java index 2350302aba..eb478ec9d6 100644 --- a/nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java +++ b/nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java @@ -26,6 +26,7 @@ import static com.uber.nullaway.LibraryModels.MethodRef.methodRef; import static com.uber.nullaway.Nullness.NONNULL; import static com.uber.nullaway.Nullness.NULLABLE; +import static com.uber.nullaway.librarymodel.NestedAnnotationInfo.TypePathEntry.Kind.ARRAY_ELEMENT; import static com.uber.nullaway.librarymodel.NestedAnnotationInfo.TypePathEntry.Kind.TYPE_ARGUMENT; import com.google.common.base.Preconditions; @@ -134,11 +135,22 @@ public NullnessHint onDataflowVisitFieldAccess( optimizedLibraryModels.nonNullParameters(methodSymbol); // For sanity check: $ nonNullParamsFromModel \cap nullableParamsFromModel $ should be empty Set allPositions = new HashSet<>(); + boolean varArgsMethod = methodSymbol.isVarArgs(); for (Integer nullParam : nullableParamsFromModel) { + if (varArgsMethod && nullParam == methodSymbol.getParameters().size() - 1) { + // For varargs, top-level library models describe the array itself. We handle that through + // the dedicated varargs-array hook so that element nullability can be modeled separately. + continue; + } allPositions.add(nullParam); argumentPositionNullness[nullParam] = NULLABLE; } for (Integer nonNullParam : nonNullParamsFromModel) { + if (varArgsMethod && nonNullParam == methodSymbol.getParameters().size() - 1) { + // See comment above: top-level nullability for the varargs parameter applies to the array, + // not to individually passed varargs elements. + continue; + } if (!allPositions.add(nonNullParam)) { // position was already marked as nullable throw new IllegalStateException( @@ -148,9 +160,61 @@ public NullnessHint onDataflowVisitFieldAccess( } argumentPositionNullness[nonNullParam] = NONNULL; } + if (varArgsMethod) { + ImmutableSetMultimap nestedAnnotations = + optimizedLibraryModels.nestedAnnotationsForMethods(methodSymbol); + int varargsIndex = methodSymbol.getParameters().size() - 1; + Nullness varargsContentsNullness = null; + for (NestedAnnotationInfo nestedAnnotation : nestedAnnotations.get(varargsIndex)) { + // check if it's for the array element + if (nestedAnnotation.typePath().size() == 1 + && nestedAnnotation.typePath().get(0).kind() == ARRAY_ELEMENT) { + // this is a nested annotation for the varargs array element, which applies when + // individual parameters are passed + if (varargsContentsNullness == null) { + varargsContentsNullness = + nestedAnnotation.annotation() == Annotation.NULLABLE ? NULLABLE : NONNULL; + argumentPositionNullness[varargsIndex] = varargsContentsNullness; + } else { + throw new IllegalStateException( + "varargs contents nullness set multiple times: " + + nestedAnnotations.get(varargsIndex)); + } + } + } + } return argumentPositionNullness; } + @Override + public @Nullable Nullness onOverrideMethodInvocationVarargsArrayNullability( + Context context, + Symbol.MethodSymbol methodSymbol, + boolean isAnnotated, + @Nullable Nullness varargsArrayNullness) { + Nullness modeledVarargsArrayNullness = null; + if (methodSymbol.isVarArgs()) { + int varargsIndex = methodSymbol.getParameters().size() - 1; + OptimizedLibraryModels optimizedLibraryModels = getOptLibraryModels(context); + boolean nullableFromModel = + optimizedLibraryModels.explicitlyNullableParameters(methodSymbol).contains(varargsIndex); + boolean nonNullFromModel = + optimizedLibraryModels.nonNullParameters(methodSymbol).contains(varargsIndex); + if (nullableFromModel && nonNullFromModel) { + throw new IllegalStateException( + String.format( + "Library models give conflicting varargs array nullability for method %s", + methodSymbol.getQualifiedName())); + } + if (nullableFromModel) { + modeledVarargsArrayNullness = NULLABLE; + } else if (nonNullFromModel) { + modeledVarargsArrayNullness = NONNULL; + } + } + return modeledVarargsArrayNullness != null ? modeledVarargsArrayNullness : varargsArrayNullness; + } + @Override public Nullness onOverrideMethodReturnNullability( Symbol.MethodSymbol methodSymbol, diff --git a/nullaway/src/main/java/com/uber/nullaway/handlers/RestrictiveAnnotationHandler.java b/nullaway/src/main/java/com/uber/nullaway/handlers/RestrictiveAnnotationHandler.java index f45f1defe6..2c63887ba9 100644 --- a/nullaway/src/main/java/com/uber/nullaway/handlers/RestrictiveAnnotationHandler.java +++ b/nullaway/src/main/java/com/uber/nullaway/handlers/RestrictiveAnnotationHandler.java @@ -134,6 +134,22 @@ private CodeAnnotationInfo getCodeAnnotationInfo(Context context) { return argumentPositionNullness; } + @Override + public @Nullable Nullness onOverrideMethodInvocationVarargsArrayNullability( + Context context, + Symbol.MethodSymbol methodSymbol, + boolean isAnnotated, + @Nullable Nullness varargsArrayNullness) { + if (methodSymbol.isVarArgs()) { + Symbol.VarSymbol varargsParamSymbol = + methodSymbol.getParameters().get(methodSymbol.getParameters().size() - 1); + if (Nullness.varargsArrayIsNonNull(varargsParamSymbol, config)) { + return Nullness.NONNULL; + } + } + return varargsArrayNullness; + } + @Override public Nullness onOverrideMethodReturnNullability( Symbol.MethodSymbol methodSymbol, diff --git a/test-java-lib/src/main/java/com/uber/lib/unannotated/NullMarkedVarargsWithModel.java b/test-java-lib/src/main/java/com/uber/lib/unannotated/NullMarkedVarargsWithModel.java new file mode 100644 index 0000000000..2a8601371b --- /dev/null +++ b/test-java-lib/src/main/java/com/uber/lib/unannotated/NullMarkedVarargsWithModel.java @@ -0,0 +1,11 @@ +package com.uber.lib.unannotated; + +/* @NullMarked */ +public class NullMarkedVarargsWithModel { + + public static void nullableContents(/* @Nullable */ String... args) {} + + public static void nullableArray(String /* @Nullable */... args) {} + + public static void bothNullable(/* @Nullable */ String /* @Nullable */... args) {} +} diff --git a/test-java-lib/src/main/java/com/uber/lib/unannotated/NullUnmarkedVarargsWithModel.java b/test-java-lib/src/main/java/com/uber/lib/unannotated/NullUnmarkedVarargsWithModel.java new file mode 100644 index 0000000000..fa9311b4fe --- /dev/null +++ b/test-java-lib/src/main/java/com/uber/lib/unannotated/NullUnmarkedVarargsWithModel.java @@ -0,0 +1,10 @@ +package com.uber.lib.unannotated; + +public class NullUnmarkedVarargsWithModel { + + public static void nonNullContents(/* @NonNull */ String... args) {} + + public static void nonNullArray(String /* @NonNull */... args) {} + + public static void bothNonNull(/* @NonNull */ String /* @NonNull */... args) {} +} diff --git a/test-library-models/src/main/java/com/uber/nullaway/testlibrarymodels/TestLibraryModels.java b/test-library-models/src/main/java/com/uber/nullaway/testlibrarymodels/TestLibraryModels.java index 6ef09cc046..e2149e9800 100644 --- a/test-library-models/src/main/java/com/uber/nullaway/testlibrarymodels/TestLibraryModels.java +++ b/test-library-models/src/main/java/com/uber/nullaway/testlibrarymodels/TestLibraryModels.java @@ -43,7 +43,15 @@ public ImmutableSetMultimap failIfNullParameters() { @Override public ImmutableSetMultimap explicitlyNullableParameters() { - return ImmutableSetMultimap.of(); + return ImmutableSetMultimap.of( + methodRef( + "com.uber.lib.unannotated.NullMarkedVarargsWithModel", + "nullableArray(java.lang.String...)"), + 0, + methodRef( + "com.uber.lib.unannotated.NullMarkedVarargsWithModel", + "bothNullable(java.lang.String...)"), + 0); } @Override @@ -54,6 +62,16 @@ public ImmutableSetMultimap nonNullParameters() { "com.uber.lib.unannotated.RestrictivelyAnnotatedFIWithModelOverride", "apply(java.lang.Object)"), 0) + .put( + methodRef( + "com.uber.lib.unannotated.NullUnmarkedVarargsWithModel", + "nonNullArray(java.lang.String...)"), + 0) + .put( + methodRef( + "com.uber.lib.unannotated.NullUnmarkedVarargsWithModel", + "bothNonNull(java.lang.String...)"), + 0) .build(); } @@ -154,7 +172,8 @@ public ImmutableSetMultimap typeVariablesWithNullableUpperBound public ImmutableSet nullMarkedClasses() { return ImmutableSet.of( "com.uber.lib.unannotated.ProviderNullMarkedViaModel", - "com.uber.lib.unannotated.NestedAnnots"); + "com.uber.lib.unannotated.NestedAnnots", + "com.uber.lib.unannotated.NullMarkedVarargsWithModel"); } @Override @@ -169,59 +188,103 @@ public ImmutableSetMultimap methodTypeVariablesWithNullableU @Override public ImmutableMap> nestedAnnotationsForMethods() { - return ImmutableMap.of( - methodRef("com.uber.lib.unannotated.NestedAnnots", "genericMethod(java.lang.Class)"), - ImmutableSetMultimap.of( - 0, - new NestedAnnotationInfo( - Annotation.NONNULL, ImmutableList.of(new TypePathEntry(TYPE_ARGUMENT, 0)))), - methodRef( - "com.uber.lib.unannotated.NestedAnnots", - "deeplyNested(com.uber.lib.unannotated.NestedAnnots>)"), - ImmutableSetMultimap.of( - 0, - new NestedAnnotationInfo( - Annotation.NULLABLE, - ImmutableList.of( - new TypePathEntry(TYPE_ARGUMENT, 0), new TypePathEntry(TYPE_ARGUMENT, 0)))), - methodRef("com.uber.lib.unannotated.NestedAnnots", "nestedArray1()"), - ImmutableSetMultimap.of( - -1, - new NestedAnnotationInfo( - Annotation.NULLABLE, - ImmutableList.of( - new TypePathEntry(TYPE_ARGUMENT, 0), - new TypePathEntry(ARRAY_ELEMENT, -1), - new TypePathEntry(TYPE_ARGUMENT, 0)))), - methodRef("com.uber.lib.unannotated.NestedAnnots", "nestedArray2()"), - ImmutableSetMultimap.of( - -1, - new NestedAnnotationInfo( - Annotation.NULLABLE, ImmutableList.of(new TypePathEntry(TYPE_ARGUMENT, 0)))), - methodRef( - "com.uber.lib.unannotated.NestedAnnots", - "wildcardUpper(com.uber.lib.unannotated.NestedAnnots)"), - ImmutableSetMultimap.of( - 0, - new NestedAnnotationInfo( - Annotation.NONNULL, - ImmutableList.of( - new TypePathEntry(TYPE_ARGUMENT, 0), new TypePathEntry(WILDCARD_BOUND, 0)))), - methodRef( - "com.uber.lib.unannotated.NestedAnnots", - "wildcardLower(com.uber.lib.unannotated.NestedAnnots)"), - ImmutableSetMultimap.of( - 0, - new NestedAnnotationInfo( - Annotation.NULLABLE, - ImmutableList.of( - new TypePathEntry(TYPE_ARGUMENT, 0), new TypePathEntry(WILDCARD_BOUND, 1)))), - methodRef( - "com.uber.lib.unannotated.NestedAnnots", - "multipleArgs(com.uber.lib.unannotated.NestedAnnots,com.uber.lib.unannotated.NestedAnnots)"), - ImmutableSetMultimap.of( - 1, - new NestedAnnotationInfo( - Annotation.NULLABLE, ImmutableList.of(new TypePathEntry(TYPE_ARGUMENT, 0))))); + return new ImmutableMap.Builder< + MethodRef, ImmutableSetMultimap>() + .put( + methodRef( + "com.uber.lib.unannotated.NestedAnnots", "genericMethod(java.lang.Class)"), + ImmutableSetMultimap.of( + 0, + new NestedAnnotationInfo( + Annotation.NONNULL, ImmutableList.of(new TypePathEntry(TYPE_ARGUMENT, 0))))) + .put( + methodRef( + "com.uber.lib.unannotated.NestedAnnots", + "deeplyNested(com.uber.lib.unannotated.NestedAnnots>)"), + ImmutableSetMultimap.of( + 0, + new NestedAnnotationInfo( + Annotation.NULLABLE, + ImmutableList.of( + new TypePathEntry(TYPE_ARGUMENT, 0), new TypePathEntry(TYPE_ARGUMENT, 0))))) + .put( + methodRef("com.uber.lib.unannotated.NestedAnnots", "nestedArray1()"), + ImmutableSetMultimap.of( + -1, + new NestedAnnotationInfo( + Annotation.NULLABLE, + ImmutableList.of( + new TypePathEntry(TYPE_ARGUMENT, 0), + new TypePathEntry(ARRAY_ELEMENT, -1), + new TypePathEntry(TYPE_ARGUMENT, 0))))) + .put( + methodRef("com.uber.lib.unannotated.NestedAnnots", "nestedArray2()"), + ImmutableSetMultimap.of( + -1, + new NestedAnnotationInfo( + Annotation.NULLABLE, ImmutableList.of(new TypePathEntry(TYPE_ARGUMENT, 0))))) + .put( + methodRef( + "com.uber.lib.unannotated.NestedAnnots", + "wildcardUpper(com.uber.lib.unannotated.NestedAnnots)"), + ImmutableSetMultimap.of( + 0, + new NestedAnnotationInfo( + Annotation.NONNULL, + ImmutableList.of( + new TypePathEntry(TYPE_ARGUMENT, 0), + new TypePathEntry(WILDCARD_BOUND, 0))))) + .put( + methodRef( + "com.uber.lib.unannotated.NestedAnnots", + "wildcardLower(com.uber.lib.unannotated.NestedAnnots)"), + ImmutableSetMultimap.of( + 0, + new NestedAnnotationInfo( + Annotation.NULLABLE, + ImmutableList.of( + new TypePathEntry(TYPE_ARGUMENT, 0), + new TypePathEntry(WILDCARD_BOUND, 1))))) + .put( + methodRef( + "com.uber.lib.unannotated.NestedAnnots", + "multipleArgs(com.uber.lib.unannotated.NestedAnnots,com.uber.lib.unannotated.NestedAnnots)"), + ImmutableSetMultimap.of( + 1, + new NestedAnnotationInfo( + Annotation.NULLABLE, ImmutableList.of(new TypePathEntry(TYPE_ARGUMENT, 0))))) + .put( + methodRef( + "com.uber.lib.unannotated.NullMarkedVarargsWithModel", + "nullableContents(java.lang.String...)"), + ImmutableSetMultimap.of( + 0, + new NestedAnnotationInfo( + Annotation.NULLABLE, ImmutableList.of(new TypePathEntry(ARRAY_ELEMENT, -1))))) + .put( + methodRef( + "com.uber.lib.unannotated.NullMarkedVarargsWithModel", + "bothNullable(java.lang.String...)"), + ImmutableSetMultimap.of( + 0, + new NestedAnnotationInfo( + Annotation.NULLABLE, ImmutableList.of(new TypePathEntry(ARRAY_ELEMENT, -1))))) + .put( + methodRef( + "com.uber.lib.unannotated.NullUnmarkedVarargsWithModel", + "nonNullContents(java.lang.String...)"), + ImmutableSetMultimap.of( + 0, + new NestedAnnotationInfo( + Annotation.NONNULL, ImmutableList.of(new TypePathEntry(ARRAY_ELEMENT, -1))))) + .put( + methodRef( + "com.uber.lib.unannotated.NullUnmarkedVarargsWithModel", + "bothNonNull(java.lang.String...)"), + ImmutableSetMultimap.of( + 0, + new NestedAnnotationInfo( + Annotation.NONNULL, ImmutableList.of(new TypePathEntry(ARRAY_ELEMENT, -1))))) + .build(); } } diff --git a/test-library-models/src/test/java/com/uber/nullaway/CustomLibraryModelsTests.java b/test-library-models/src/test/java/com/uber/nullaway/CustomLibraryModelsTests.java index c1b1f708ad..1dc7d3a3e1 100644 --- a/test-library-models/src/test/java/com/uber/nullaway/CustomLibraryModelsTests.java +++ b/test-library-models/src/test/java/com/uber/nullaway/CustomLibraryModelsTests.java @@ -30,7 +30,10 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +@RunWith(JUnit4.class) public class CustomLibraryModelsTests { private CompilationTestHelper makeLibraryModelsTestHelperWithArgs(List args) { diff --git a/test-library-models/src/test/java/com/uber/nullaway/VarargsLibraryModelsTests.java b/test-library-models/src/test/java/com/uber/nullaway/VarargsLibraryModelsTests.java new file mode 100644 index 0000000000..7c9196b50b --- /dev/null +++ b/test-library-models/src/test/java/com/uber/nullaway/VarargsLibraryModelsTests.java @@ -0,0 +1,95 @@ +package com.uber.nullaway; + +import com.google.errorprone.CompilationTestHelper; +import com.uber.nullaway.generics.JSpecifyJavacConfig; +import java.util.Arrays; +import java.util.List; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class VarargsLibraryModelsTests { + + private CompilationTestHelper makeLibraryModelsTestHelperWithArgs(List args) { + return CompilationTestHelper.newInstance(NullAway.class, getClass()).setArgs(args); + } + + @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Test + public void jspecifyModeNullMarked() { + makeLibraryModelsTestHelperWithArgs( + JSpecifyJavacConfig.withJSpecifyModeArgs( + Arrays.asList( + "-d", + temporaryFolder.getRoot().getAbsolutePath(), + "-XepOpt:NullAway:OnlyNullMarked=true"))) + .addSourceLines( + "Test.java", + """ + import com.uber.lib.unannotated.NullMarkedVarargsWithModel; + import org.jspecify.annotations.*; + @NullMarked + public class Test { + void testNegative() { + String x = null; + String[] y = null; + NullMarkedVarargsWithModel.nullableContents(x, x); + NullMarkedVarargsWithModel.nullableArray(y); + NullMarkedVarargsWithModel.bothNullable(x, x); + NullMarkedVarargsWithModel.bothNullable(y); + } + void testPositive() { + String x = null; + String[] y = null; + // BUG: Diagnostic contains: passing @Nullable parameter 'y' where @NonNull is required + NullMarkedVarargsWithModel.nullableContents(y); + // BUG: Diagnostic contains: passing @Nullable parameter 'x' where @NonNull is required + NullMarkedVarargsWithModel.nullableArray(x, x); + } + } + """) + .doTest(); + } + + @Test + public void jspecifyModeRestrictive() { + makeLibraryModelsTestHelperWithArgs( + JSpecifyJavacConfig.withJSpecifyModeArgs( + Arrays.asList( + "-d", + temporaryFolder.getRoot().getAbsolutePath(), + "-XepOpt:NullAway:OnlyNullMarked=true"))) + .addSourceLines( + "Test.java", + """ + import com.uber.lib.unannotated.NullUnmarkedVarargsWithModel; + import org.jspecify.annotations.*; + @NullMarked + public class Test { + void testNegative() { + String x = null; + String[] y = null; + NullUnmarkedVarargsWithModel.nonNullContents(y); + NullUnmarkedVarargsWithModel.nonNullArray(x, x); + } + void testPositive() { + String x = null; + String[] y = null; + // BUG: Diagnostic contains: passing @Nullable parameter 'y' where @NonNull is required + NullUnmarkedVarargsWithModel.nonNullArray(y); + // BUG: Diagnostic contains: passing @Nullable parameter 'x' where @NonNull is required + NullUnmarkedVarargsWithModel.nonNullContents(x, x); + // BUG: Diagnostic contains: passing @Nullable parameter 'y' where @NonNull is required + NullUnmarkedVarargsWithModel.bothNonNull(y); + // BUG: Diagnostic contains: passing @Nullable parameter 'x' where @NonNull is required + NullUnmarkedVarargsWithModel.bothNonNull(x, x); + } + } + """) + .doTest(); + } +}