Add interface and abstract check in ConstructorLibrary.newDefaultConstructor
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.ArrayList;
@@ -90,8 +91,10 @@ public ConstructorLibrary(Map<Type, InstanceCreator<?>> instanceCreators) {
private <T> ObjectConstructor<T> newUnsafeAllocator(TypeInfo<T> typeInfo) {
return new ObjectConstructor<T>() {
private final UnsafeAllocator unsafeAllocator = UnsafeAllocator.create();
+
@SuppressWarnings("unchecked")
- @Override public T construct() {
+ @Override
+ public T construct() {
try {
Object newInstance = unsafeAllocator.newInstance(typeInfo.getRawType());
return (T) newInstance;
@@ -104,8 +107,14 @@ public ConstructorLibrary(Map<Type, InstanceCreator<?>> instanceCreators) {
}
private <T> ObjectConstructor<T> newDefaultConstructor(Class<? super T> rawType) {
- @SuppressWarnings("unchecked") // T is the same raw type as is requested
- Constructor<T> constructor = (Constructor<T>) Arrays.stream(rawType.getDeclaredConstructors())
+ if (rawType.isInterface() || Modifier.isAbstract(rawType.getModifiers())) {
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
+ // T is the same raw type as is requested
+ Constructor<T> constructor =
+ (Constructor<T>) Arrays.stream(rawType.getDeclaredConstructors())
.min(Comparator.comparingInt(c -> c.getParameterTypes().length))
.orElse(null);
