Skip to content

Commit

Permalink
Improve error message when abstract class cannot be constructed
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcono1234 committed Nov 5, 2020
1 parent ceae88b commit 2bc8b69
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Expand Up @@ -18,6 +18,7 @@

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayDeque;
Expand Down Expand Up @@ -97,6 +98,11 @@ public <T> ObjectConstructor<T> get(TypeToken<T> typeToken) {
}

private <T> ObjectConstructor<T> newDefaultConstructor(Class<? super T> rawType) {
// Cannot invoke constructor of abstract class
if (Modifier.isAbstract(rawType.getModifiers())) {
return null;
}

try {
final Constructor<? super T> constructor = rawType.getDeclaredConstructor();
if (!constructor.isAccessible()) {
Expand Down Expand Up @@ -225,6 +231,7 @@ private <T> ObjectConstructor<T> newUnsafeAllocator(
Object newInstance = unsafeAllocator.newInstance(rawType);
return (T) newInstance;
} catch (Exception e) {
// TODO: JsonParseException ?
throw new RuntimeException(("Unable to invoke no-args constructor for " + type + ". "
+ "Registering an InstanceCreator with Gson for this type may fix this problem."), e);
}
Expand Down
@@ -0,0 +1,59 @@
package com.google.gson.internal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Map;

import org.junit.Test;

import com.google.gson.InstanceCreator;
import com.google.gson.reflect.TypeToken;

public class ConstructorConstructorTest {
private static final Map<Type, InstanceCreator<?>> NO_INSTANCE_CREATORS = Collections.emptyMap();

private abstract static class AbstractClass {
@SuppressWarnings("unused")
public AbstractClass() { }
}
private interface Interface { }

/**
* Verify that ConstructorConstructor does not try to invoke no-arg constructor
* of abstract class.
*/
@Test
public void testGet_AbstractClassNoArgConstructor() {
ConstructorConstructor constructorFactory = new ConstructorConstructor(NO_INSTANCE_CREATORS);
ObjectConstructor<AbstractClass> constructor = constructorFactory.get(TypeToken.get(AbstractClass.class));
try {
constructor.construct();
fail("Expected exception");
} catch (RuntimeException exception) {
assertEquals(
"Unable to invoke no-args constructor for " + AbstractClass.class
+ ". Registering an InstanceCreator with Gson for this type may fix this problem.",
exception.getMessage()
);
}
}

@Test
public void testGet_Interface() {
ConstructorConstructor constructorFactory = new ConstructorConstructor(NO_INSTANCE_CREATORS);
ObjectConstructor<Interface> constructor = constructorFactory.get(TypeToken.get(Interface.class));
try {
constructor.construct();
fail("Expected exception");
} catch (RuntimeException exception) {
assertEquals(
"Unable to invoke no-args constructor for " + Interface.class
+ ". Registering an InstanceCreator with Gson for this type may fix this problem.",
exception.getMessage()
);
}
}
}

0 comments on commit 2bc8b69

Please sign in to comment.