316 improve exception message (#318)
* improvement 316 add .idea directory to .gitignore
.idea directory should not be checked and is auto generated by the IDE.
So the directory should be ignored by git.
Signed-off-by: Simulant <nfaupel.dev@gmail.com>
* improvement 316 improve Exception message for missing constructor
when the default constructor is missing, the class name is contained in
the error message so the issue is easier to fix by the user.
Added a unit test to ensure the class name is contained in the message.
Signed-off-by: Simulant <nfaupel.dev@gmail.com>
* improvement 316 add missing java doc for parameter
Signed-off-by: Simulant <nfaupel.dev@gmail.com>
* bugfix 316 assert JsonbException instead of RuntimeException
Signed-off-by: Simulant <nfaupel.dev@gmail.com>
diff --git a/.gitignore b/.gitignore
index 22b2a1a..283e0e3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,6 @@
/target/
.classpath
.project
+.idea/
.settings/
/.DS_Store
diff --git a/src/main/java/org/eclipse/yasson/internal/ReflectionUtils.java b/src/main/java/org/eclipse/yasson/internal/ReflectionUtils.java
index d4a3cb9..0eddfd3 100644
--- a/src/main/java/org/eclipse/yasson/internal/ReflectionUtils.java
+++ b/src/main/java/org/eclipse/yasson/internal/ReflectionUtils.java
@@ -210,7 +210,9 @@
* Get default no argument constructor of the class.
* @param clazz Class to get constructor from
* @param <T> Class generic type
- * @return constructor
+ * @param required if true, throws an exception if the default constructor is missing.
+ * If false, returns null in that case
+ * @return the constructor of the class, or null. Depending on required.
*/
public static <T> Constructor<T> getDefaultConstructor(Class<T> clazz, boolean required) {
Objects.requireNonNull(clazz);
diff --git a/src/main/java/org/eclipse/yasson/internal/serializer/ObjectDeserializer.java b/src/main/java/org/eclipse/yasson/internal/serializer/ObjectDeserializer.java
index 9ced476..44763cb 100644
--- a/src/main/java/org/eclipse/yasson/internal/serializer/ObjectDeserializer.java
+++ b/src/main/java/org/eclipse/yasson/internal/serializer/ObjectDeserializer.java
@@ -87,9 +87,15 @@
}
final Class<?> rawType = ReflectionUtils.getRawType(getRuntimeType());
final JsonbCreator creator = getClassModel().getClassCustomization().getCreator();
- instance = creator != null ? createInstance((Class<T>) rawType, creator)
- : ReflectionUtils.createNoArgConstructorInstance((Constructor<T>) getClassModel().getDefaultConstructor());
-
+ if (creator != null) {
+ instance = createInstance((Class<T>) rawType, creator);
+ } else {
+ Constructor<T> defaultConstructor = (Constructor<T>) getClassModel().getDefaultConstructor();
+ if (defaultConstructor == null) {
+ throw new JsonbException(Messages.getMessage(MessageKeys.NO_DEFAULT_CONSTRUCTOR, rawType));
+ }
+ instance = ReflectionUtils.createNoArgConstructorInstance(defaultConstructor);
+ }
//values must be set in order, in which they appears in JSON by spec
values.forEach((key, wrapper) -> {
//skip creator values
diff --git a/src/test/java/org/eclipse/yasson/internal/serializer/ObjectDeserializerTest.java b/src/test/java/org/eclipse/yasson/internal/serializer/ObjectDeserializerTest.java
new file mode 100644
index 0000000..e9cbe4c
--- /dev/null
+++ b/src/test/java/org/eclipse/yasson/internal/serializer/ObjectDeserializerTest.java
@@ -0,0 +1,40 @@
+package org.eclipse.yasson.internal.serializer;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import javax.json.bind.Jsonb;
+import javax.json.bind.JsonbBuilder;
+import javax.json.bind.JsonbException;
+
+public class ObjectDeserializerTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ @Test
+ public void testGetInstanceExceptionShouldContainClassNameOnMissingConstructor() {
+ expectedException.expect(JsonbException.class);
+ expectedException.expectMessage(DummyDeserializationClass.class.getName());
+
+ Jsonb jsonb = JsonbBuilder.create();
+ jsonb.fromJson("{\"key\":\"value\"}", DummyDeserializationClass.class);
+ }
+
+ public static class DummyDeserializationClass {
+ private String key;
+
+ public DummyDeserializationClass(String key) {
+ this.key = key;
+ }
+
+ public String getKey() {
+ return key;
+ }
+
+ public void setKey(String key) {
+ this.key = key;
+ }
+ }
+}