Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message when abstract class cannot be constructed #1814

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -99,6 +100,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;
}

final Constructor<? super T> constructor;
try {
constructor = rawType.getDeclaredConstructor();
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, true);
ObjectConstructor<AbstractClass> constructor = constructorFactory.get(TypeToken.get(AbstractClass.class));
try {
constructor.construct();
fail("Expected exception");
} catch (RuntimeException exception) {
assertEquals(
"Unable to create instance of class com.google.gson.internal.ConstructorConstructorTest$AbstractClass. "
+ "Registering an InstanceCreator or a TypeAdapter for this type, or adding a no-args constructor may fix this problem.",
exception.getMessage()
);
}
}

@Test
public void testGet_Interface() {
ConstructorConstructor constructorFactory = new ConstructorConstructor(NO_INSTANCE_CREATORS, true);
ObjectConstructor<Interface> constructor = constructorFactory.get(TypeToken.get(Interface.class));
try {
constructor.construct();
fail("Expected exception");
} catch (RuntimeException exception) {
assertEquals(
"Unable to create instance of interface com.google.gson.internal.ConstructorConstructorTest$Interface. "
+ "Registering an InstanceCreator or a TypeAdapter for this type, or adding a no-args constructor may fix this problem.",
exception.getMessage()
);
}
}
}