ArrayBackedEvent will throw ClassCastException when the backing type is primitive.
kvverti opened this issue ยท 3 comments
The two calls of Array.newInstance
will return primitive array types if this.type
represents a primitive type. The unchecked casts to T[]
(erased to Object[]
) will subsequently fail.
This issue is most likely academic, since the intended types for T
will be functional interface types, but the issue exists in theory.
I suggest checking whether the backing type is primitive in the constructor (via Class#isPrimitive
) and throwing IllegalArgumentException
if that is the case.
Well, to really create this, you need extremely edgy code like this:
Event<Integer> event = EventFactory.createArrayBacked(int.class/* or Integer.TYPE */, invokers -> {
/* computes and return an integer, who the heck knows how it's calculated*/
});
Meanwhile, this works:
Event<Integer> event = EventFactory.createArrayBacked(Integer.class, invokers -> {
/* computes and return an integer, who the heck knows how it's calculated*/
});
This is really a non-issue.
EventFactory.createArrayBacked(int.class, intArray -> 0);
is enough to crash. This will be solved by #1369 anyway.
With #1369 merged, this will now cause a crash in the event ctor... Good enough imo.