RFR: 8279598: Provide adapter from RandomGenerator to Random [v14]

Stuart Marks smarks at openjdk.java.net
Thu Mar 10 04:05:40 UTC 2022


On Sat, 5 Mar 2022 21:27:47 GMT, Marcono1234 <duke at openjdk.java.net> wrote:

> Would it make sense to override next(int) to always throw an exception? Even though it should not be possible for a user to call the method on the wrapper, it might be better to be on the safe side; for example in case a new Random method which depends on next(int) is added in the future (even though this is unlikely).

Heh, interesting. On the one hand, it can only be called reflectively, and doing so would be an odd thing to do, so who cares. I initially thought it would NPE immediately on `seed`, but it turns out that `seed` is actually initialized by the wrapper's call to the superclass constructor. So calling `next(int)` will actually advance the random state in the superclass, quite independently of the wrapped `RandomGenerator`. So yeah maybe having `next(int)` throw UOE might be reasonable. It's good documentation for the wrapper as well.

As an aside it might be reasonable to initialize `seed` to `null` in that constructor, since the seed should never be used. The NPE will fail-fast in the unlikely event that something ends up trying to use it.

-------------

PR: https://git.openjdk.java.net/jdk/pull/7001


More information about the core-libs-dev mailing list