From e4471d0042c7ed565cb4e1bde095e3c62c8a3862 Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 30 Oct 2024 11:32:50 +0300 Subject: [PATCH] Use JsonIOException for unchecked exceptions instead of RuntimeException in ConstructorConstructor --- .../com/google/gson/internal/ConstructorConstructor.java | 6 ++---- .../test/java/com/google/gson/functional/ObjectTest.java | 3 +-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java b/gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java index 7a7dc70b8d..83f26e8f3d 100644 --- a/gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java +++ b/gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java @@ -270,15 +270,13 @@ private static ObjectConstructor newDefaultConstructor( // Note: InstantiationException should be impossible because check at start of method made // sure that class is not abstract catch (InstantiationException e) { - throw new RuntimeException( + throw new JsonIOException( "Failed to invoke constructor '" + ReflectionHelper.constructorToString(constructor) + "' with no args", e); } catch (InvocationTargetException e) { - // TODO: don't wrap if cause is unchecked? - // TODO: JsonParseException ? - throw new RuntimeException( + throw new JsonIOException( "Failed to invoke constructor '" + ReflectionHelper.constructorToString(constructor) + "' with no args", diff --git a/gson/src/test/java/com/google/gson/functional/ObjectTest.java b/gson/src/test/java/com/google/gson/functional/ObjectTest.java index 5812daf8f1..1996f221c6 100644 --- a/gson/src/test/java/com/google/gson/functional/ObjectTest.java +++ b/gson/src/test/java/com/google/gson/functional/ObjectTest.java @@ -727,10 +727,9 @@ static class ClassWithStaticFinalField { @Test public void testThrowingDefaultConstructor() { - // TODO: Adjust this once Gson throws more specific exception type var e = assertThrows( - RuntimeException.class, () -> gson.fromJson("{}", ClassWithThrowingConstructor.class)); + JsonIOException.class, () -> gson.fromJson("{}", ClassWithThrowingConstructor.class)); assertThat(e) .hasMessageThat() .isEqualTo(