Skip to content

Commit

Permalink
Avoid NPE in IsEmpty matcher (#978)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 authored Jan 20, 2024
1 parent f12474e commit dc65917
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
// XXX: Also recognize empty builders and `emptyBuilder.build()` invocations.
public final class IsEmpty implements Matcher<ExpressionTree> {
private static final long serialVersionUID = 1L;
private static final Integer ZERO = 0;
private static final Pattern EMPTY_INSTANCE_FACTORY_METHOD_PATTERN = Pattern.compile("empty.*");
private static final Matcher<Tree> EMPTY_COLLECTION_CONSTRUCTOR_ARGUMENT =
anyOf(isPrimitiveType(), isSubtypeOf(Comparator.class));
Expand Down Expand Up @@ -145,7 +146,7 @@ private static boolean isEmptyArrayCreation(ExpressionTree tree) {

NewArrayTree newArray = (NewArrayTree) tree;
return (!newArray.getDimensions().isEmpty()
&& ASTHelpers.constValue(newArray.getDimensions().get(0), Integer.class) == 0)
&& ZERO.equals(ASTHelpers.constValue(newArray.getDimensions().get(0), Integer.class)))
|| (newArray.getInitializers() != null && newArray.getInitializers().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,75 +55,80 @@ void matches() {
" return new int[][] {{0}};",
" }",
"",
" Random negative5() {",
" int[] negative5() {",
" int i = hashCode();",
" return new int[i];",
" }",
"",
" Random negative6() {",
" return new Random();",
" }",
"",
" List<Integer> negative6() {",
" List<Integer> negative7() {",
" return new ArrayList<>(ImmutableList.of(1));",
" }",
"",
" Map<Integer, Integer> negative7() {",
" Map<Integer, Integer> negative8() {",
" return new HashMap<>(ImmutableMap.of(1, 2));",
" }",
"",
" Set<Integer> negative8() {",
" Set<Integer> negative9() {",
" return new HashSet<>(ImmutableList.of(1));",
" }",
"",
" Map<Integer, Integer> negative9() {",
" Map<Integer, Integer> negative10() {",
" return new LinkedHashMap<>(ImmutableMap.of(1, 2));",
" }",
"",
" Set<Integer> negative10() {",
" Set<Integer> negative11() {",
" return new LinkedHashSet<>(ImmutableList.of(1));",
" }",
"",
" List<Integer> negative11() {",
" List<Integer> negative12() {",
" return new LinkedList<>(ImmutableList.of(1));",
" }",
"",
" Map<Integer, Integer> negative12() {",
" Map<Integer, Integer> negative13() {",
" return new HashMap<>(ImmutableMap.of(1, 2));",
" }",
"",
" Set<Integer> negative13() {",
" Set<Integer> negative14() {",
" return new HashSet<>(ImmutableList.of(1));",
" }",
"",
" List<Integer> negative14() {",
" List<Integer> negative15() {",
" return new Vector<>(ImmutableList.of(1));",
" }",
"",
" ImmutableList<Integer> negative15() {",
" ImmutableList<Integer> negative16() {",
" return ImmutableList.of(1);",
" }",
"",
" ImmutableSet<Integer> negative16() {",
" ImmutableSet<Integer> negative17() {",
" return ImmutableSet.of(1);",
" }",
"",
" ImmutableMap<Integer, Integer> negative17() {",
" ImmutableMap<Integer, Integer> negative18() {",
" return ImmutableMap.of(1, 2);",
" }",
"",
" ImmutableSetMultimap<Integer, Integer> negative18() {",
" ImmutableSetMultimap<Integer, Integer> negative19() {",
" return ImmutableSetMultimap.of(1, 2);",
" }",
"",
" List<Integer> negative19() {",
" List<Integer> negative20() {",
" return List.of(1);",
" }",
"",
" Map<Integer, Integer> negative20() {",
" Map<Integer, Integer> negative21() {",
" return Map.of(1, 2);",
" }",
"",
" Set<Integer> negative21() {",
" Set<Integer> negative22() {",
" return Set.of(1);",
" }",
"",
" Stream<Integer> negative22() {",
" Stream<Integer> negative23() {",
" return Stream.of(1);",
" }",
"",
Expand Down

0 comments on commit dc65917

Please sign in to comment.