Skip to content

Commit

Permalink
cleanup and another fix
Browse files Browse the repository at this point in the history
  • Loading branch information
msridhar committed Dec 12, 2024
1 parent 1569e53 commit 5ada6f0
Showing 1 changed file with 34 additions and 37 deletions.
71 changes: 34 additions & 37 deletions nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.lang.model.element.AnnotationMirror;
Expand Down Expand Up @@ -328,13 +329,13 @@ private static Stream<Attribute.TypeCompound> getTypeUseAnnotations(
return rawTypeAttributes.filter(
(t) ->
t.position.type.equals(TargetType.METHOD_RETURN)
&& isDirectTypeUseAnnotation(t, symbol, config));
&& (!onlyDirect || isDirectTypeUseAnnotation(t, symbol, config)));
} else {
// filter for annotations directly on the type
return rawTypeAttributes.filter(
t ->
targetTypeMatches(symbol, t.position)
&& (!onlyDirect || NullabilityUtil.isDirectTypeUseAnnotation(t, symbol, config)));
&& (!onlyDirect || isDirectTypeUseAnnotation(t, symbol, config)));
}
}

Expand Down Expand Up @@ -540,13 +541,35 @@ public static <T> T castToNonNull(@Nullable T obj) {
* otherwise
*/
public static boolean isArrayElementNullable(Symbol arraySymbol, Config config) {
// TODO remove copy-paste!!!!!
return checkArrayElementAnnotations(
arraySymbol,
config,
Nullness::isNullableAnnotation,
Nullness::hasNullableDeclarationAnnotation);
}

/**
* Checks if the annotations on the elements of some array symbol satisfy some predicate.
*
* @param arraySymbol the array symbol
* @param config NullAway configuration
* @param typeUseCheck the predicate to check the type-use annotations
* @param declarationCheck the predicate to check the declaration annotations (applied only to
* varargs symbols)
* @return true if the annotations on the elements of the array symbol satisfy the given
* predicates, false otherwise
*/
private static boolean checkArrayElementAnnotations(
Symbol arraySymbol,
Config config,
BiPredicate<String, Config> typeUseCheck,
BiPredicate<Symbol, Config> declarationCheck) {
if (getTypeUseAnnotations(arraySymbol, config, /* onlyDirect= */ false)
.anyMatch(
t -> {
for (TypeAnnotationPosition.TypePathEntry entry : t.position.location) {
if (entry.tag == TypeAnnotationPosition.TypePathEntryKind.ARRAY) {
if (Nullness.isNullableAnnotation(t.type.toString(), config)) {
if (typeUseCheck.test(t.type.toString(), config)) {
return true;
}
}
Expand All @@ -555,20 +578,10 @@ public static boolean isArrayElementNullable(Symbol arraySymbol, Config config)
})) {
return true;
}
// for (Attribute.TypeCompound t : arraySymbol.getRawTypeAttributes()) {
// for (TypeAnnotationPosition.TypePathEntry entry : t.position.location) {
// if (entry.tag == TypeAnnotationPosition.TypePathEntryKind.ARRAY) {
// if (Nullness.isNullableAnnotation(t.type.toString(), config)) {
// return true;
// }
// }
// }
// }
// For varargs symbols we also consider the elements to be @Nullable if there is a @Nullable
// declaration annotation on the parameter
// For varargs symbols we also check for declaration annotations on the parameter
// NOTE this flag check does not work for the varargs parameter of a method defined in bytecodes
if ((arraySymbol.flags() & Flags.VARARGS) != 0) {
return Nullness.hasNullableDeclarationAnnotation(arraySymbol, config);
return declarationCheck.test(arraySymbol, config);
}
return false;
}
Expand Down Expand Up @@ -597,27 +610,11 @@ public static boolean nullableVarargsElementsForSourceOrBytecode(
* otherwise
*/
public static boolean isArrayElementNonNull(Symbol arraySymbol, Config config) {
if (getTypeUseAnnotations(arraySymbol, config, /* onlyDirect= */ false)
.anyMatch(
t -> {
for (TypeAnnotationPosition.TypePathEntry entry : t.position.location) {
if (entry.tag == TypeAnnotationPosition.TypePathEntryKind.ARRAY) {
if (Nullness.isNonNullAnnotation(t.type.toString(), config)) {
return true;
}
}
}
return false;
})) {
return true;
}
// For varargs symbols we also consider the elements to be @NonNull if there is a @NonNull
// declaration annotation on the parameter
// NOTE this flag check does not work for the varargs parameter of a method defined in bytecodes
if ((arraySymbol.flags() & Flags.VARARGS) != 0) {
return Nullness.hasNonNullDeclarationAnnotation(arraySymbol, config);
}
return false;
return checkArrayElementAnnotations(
arraySymbol,
config,
Nullness::isNonNullAnnotation,
Nullness::hasNonNullDeclarationAnnotation);
}

/**
Expand Down

0 comments on commit 5ada6f0

Please sign in to comment.