diff --git a/bosk-core/src/main/java/io/vena/bosk/Bosk.java b/bosk-core/src/main/java/io/vena/bosk/Bosk.java index 60f37de9..d6db44b3 100644 --- a/bosk-core/src/main/java/io/vena/bosk/Bosk.java +++ b/bosk-core/src/main/java/io/vena/bosk/Bosk.java @@ -650,7 +650,11 @@ public final class ReadContext implements AutoCloseable { private ReadContext() { originalRoot = rootSnapshot.get(); if (originalRoot == null) { - snapshot = currentRoot; + try { + snapshot = requireNonNull(currentRoot); + } catch (NullPointerException e) { + throw new IllegalStateException("Bosk constructor has not yet finished; cannot create a ReadContext", e); + } rootSnapshot.set(snapshot); LOGGER.trace("New {}", this); } else { diff --git a/bosk-core/src/test/java/io/vena/bosk/BoskConstructorTest.java b/bosk-core/src/test/java/io/vena/bosk/BoskConstructorTest.java index 675494e9..db1e7ce1 100644 --- a/bosk-core/src/test/java/io/vena/bosk/BoskConstructorTest.java +++ b/bosk-core/src/test/java/io/vena/bosk/BoskConstructorTest.java @@ -128,6 +128,16 @@ void defaultRoot_matches() { } } + @Test + void readContextDuringDriverFactory_throws() { + assertThrows(IllegalStateException.class, ()->{ + new Bosk<>("readContext", SimpleTypes.class, newEntity(), (b,d) -> { + try (val __ = b.readContext()) {} + return d; + }); + }); + } + //////////////// // // Helpers