-
Sub-task
-
Resolution: Not an Issue
-
P2
-
None
-
8
j.u.s.Stream.collect(Collector) throws NPE if Collector returns null-combiner or null-supplier or null-function or null-accumulator.
In spec we have next assertion: "Unless otherwise noted, passing a null argument to any stream method may result in a NullPointerException."
But formally in this case the argument of the method is not null.
Minimized test:
----------------------------------------------------------------------------------
public class Test {
public static void main(String[] args) {
printNPEIfIs(true, false, false, false, "supplier");
printNPEIfIs(false, true, false, false, "accumulator");
printNPEIfIs(false, false, true, false, "combiner");
printNPEIfIs(false, false, false, true, "function");
}
private static void printNPEIfIs(boolean s, boolean a, boolean c, boolean f, String whatNull) {
try {
Arrays.stream(new Object[] {1, 2, 3, 4, 5, 6, 7, 8, 10}).parallel().collect(new NPECollector(s, a, c, f));
} catch (NullPointerException e) {
System.out.println("NPE if " + whatNull + " is null");
}
}
static class NPECollector implements Collector<Object, Collection<Object>, Collection<Object>> {
boolean nullSupplier;
boolean nullAccum;
boolean nullComb;
boolean nullFunct;
public NPECollector(boolean nullSuppier, boolean nullAccum, boolean nullComb,
boolean nullFunct) {
this.nullSupplier = nullSuppier;
this.nullAccum = nullAccum;
this.nullComb = nullComb;
this.nullFunct = nullFunct;
}
@Override
public Supplier<Collection<Object>> supplier() {
return nullSupplier ? null : ArrayList::new;
}
@Override
public BiConsumer<Collection<Object>, Object> accumulator() {
return nullAccum ? null : Collection::add;
}
@Override
public BinaryOperator<Collection<Object>> combiner() {
return nullComb ? null : (l, r) -> {
l.addAll(r);
return l;
};
}
@Override
public Function<Collection<Object>, Collection<Object>> finisher() {
return nullFunct ? null : collect -> collect;
}
@Override
public Set<Collector.Characteristics> characteristics() {
return Collections.emptySet();
}
}
}
---------------------------------------------------------------------------------------------------
Output:
------------------------------------------------------------------------------
NPE if supplier is null
NPE if accumulator is null
NPE if combiner is null
NPE if function is null
In spec we have next assertion: "Unless otherwise noted, passing a null argument to any stream method may result in a NullPointerException."
But formally in this case the argument of the method is not null.
Minimized test:
----------------------------------------------------------------------------------
public class Test {
public static void main(String[] args) {
printNPEIfIs(true, false, false, false, "supplier");
printNPEIfIs(false, true, false, false, "accumulator");
printNPEIfIs(false, false, true, false, "combiner");
printNPEIfIs(false, false, false, true, "function");
}
private static void printNPEIfIs(boolean s, boolean a, boolean c, boolean f, String whatNull) {
try {
Arrays.stream(new Object[] {1, 2, 3, 4, 5, 6, 7, 8, 10}).parallel().collect(new NPECollector(s, a, c, f));
} catch (NullPointerException e) {
System.out.println("NPE if " + whatNull + " is null");
}
}
static class NPECollector implements Collector<Object, Collection<Object>, Collection<Object>> {
boolean nullSupplier;
boolean nullAccum;
boolean nullComb;
boolean nullFunct;
public NPECollector(boolean nullSuppier, boolean nullAccum, boolean nullComb,
boolean nullFunct) {
this.nullSupplier = nullSuppier;
this.nullAccum = nullAccum;
this.nullComb = nullComb;
this.nullFunct = nullFunct;
}
@Override
public Supplier<Collection<Object>> supplier() {
return nullSupplier ? null : ArrayList::new;
}
@Override
public BiConsumer<Collection<Object>, Object> accumulator() {
return nullAccum ? null : Collection::add;
}
@Override
public BinaryOperator<Collection<Object>> combiner() {
return nullComb ? null : (l, r) -> {
l.addAll(r);
return l;
};
}
@Override
public Function<Collection<Object>, Collection<Object>> finisher() {
return nullFunct ? null : collect -> collect;
}
@Override
public Set<Collector.Characteristics> characteristics() {
return Collections.emptySet();
}
}
}
---------------------------------------------------------------------------------------------------
Output:
------------------------------------------------------------------------------
NPE if supplier is null
NPE if accumulator is null
NPE if combiner is null
NPE if function is null
- relates to
-
JDK-8025909 Lambda Library Spec Updates
- Closed