From 8cdbf0634fca17a47ca40ea7ed8c88e74c73fff6 Mon Sep 17 00:00:00 2001 From: Amit Mishra Date: Sun, 28 Jun 2026 15:10:53 +0530 Subject: [PATCH] Throw JsonSyntaxException instead of IllegalStateException for non-numeric AtomicIntegerArray elements Symmetric to #3038: in.nextInt() throws IllegalStateException for any non-numeric token (null, boolean, etc.), which previously propagated unwrapped since only NumberFormatException was caught. Fixes #3047 This PR was written primarily by Claude Code; I reviewed the change and ran the test suite before submitting. --- .../google/gson/internal/bind/TypeAdapters.java | 2 +- .../functional/JavaUtilConcurrentAtomicTest.java | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java index 6e0f85ad6d..d6375b2352 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java +++ b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java @@ -344,7 +344,7 @@ public AtomicIntegerArray read(JsonReader in) throws IOException { try { int integer = in.nextInt(); list.add(integer); - } catch (NumberFormatException e) { + } catch (NumberFormatException | IllegalStateException e) { throw new JsonSyntaxException(e); } } diff --git a/gson/src/test/java/com/google/gson/functional/JavaUtilConcurrentAtomicTest.java b/gson/src/test/java/com/google/gson/functional/JavaUtilConcurrentAtomicTest.java index 41f999ec82..5d618afb85 100644 --- a/gson/src/test/java/com/google/gson/functional/JavaUtilConcurrentAtomicTest.java +++ b/gson/src/test/java/com/google/gson/functional/JavaUtilConcurrentAtomicTest.java @@ -88,6 +88,22 @@ public void testAtomicIntegerArray() { assertThat(json).isEqualTo("[10,13,14]"); } + @Test + public void testAtomicIntegerArrayWithNullElement() { + JsonSyntaxException e = + assertThrows( + JsonSyntaxException.class, () -> gson.fromJson("[1,null,3]", AtomicIntegerArray.class)); + assertThat(e).hasCauseThat().isInstanceOf(IllegalStateException.class); + } + + @Test + public void testAtomicIntegerArrayWithNonNumericElement() { + JsonSyntaxException e = + assertThrows( + JsonSyntaxException.class, () -> gson.fromJson("[1,true,3]", AtomicIntegerArray.class)); + assertThat(e).hasCauseThat().isInstanceOf(IllegalStateException.class); + } + @Test public void testAtomicLongArray() { AtomicLongArray target = gson.fromJson("[10, 13, 14]", AtomicLongArray.class);