Skip to content

Commit

Permalink
Merge pull request #642 from jglick/List.of
Browse files Browse the repository at this point in the history
Replacing `Arrays.asList` with `List.of` can introduce `NullPointerException`
  • Loading branch information
jglick committed Dec 15, 2022
2 parents 2be4c99 + 7c908a0 commit 4f58de0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ Object sanitizeArrayAndRecordMutation(@NonNull Object[] objects, @CheckForNull E
this.isUnmodifiedBySanitization = false;
return NotStoredReason.OVERSIZE_VALUE;
}
List inputList = List.of(objects);
List<Object> inputList = Arrays.asList(objects);
Object sanitized = sanitizeListAndRecordMutation(inputList, variables);
if (sanitized == inputList) { // Works because if not mutated, we return original input instance
return objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static <E> Iterator<E> iterator(Set<E> set) {

public static <E> Iterator<E> iterator(E[] array) {
// TODO as above
return new Itr<>(List.of(array));
return new Itr<>(Arrays.asList(array));
}

public static <E> Iterator<E> iterator(Deque deque) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,18 @@ public class IteratorHackTest {
r.assertBuildStatusSuccess(r.waitForCompletion(b));
});
}

@Issue("https://github.com/jenkinsci/workflow-cps-plugin/pull/627#issuecomment-1352869571")
@Test public void arrayIterator() throws Exception {
rr.then(r -> {
WorkflowJob p = r.createProject(WorkflowJob.class);
p.setDefinition(new CpsFlowDefinition(
"Object[] arr = [1, null, 2]\n" +
"for (def elt : arr) {\n" +
" echo(/iterating on $elt/)\n" +
"}\n", false));
rr.j.assertLogContains("iterating on null", rr.j.buildAndAssertSuccess(p));
});
}

}

0 comments on commit 4f58de0

Please sign in to comment.