DATACMNS-1200 - Polishing.

Throw MappingInstantiationException from KotlinClassGeneratingEntityInstantiator if instantiation fails to align behavior with ClassGeneratingEntityInstantiator. Report Kotlin constructor instead of Java constructor if available.

Original pull request: #255.
This commit is contained in:
Mark Paluch
2017-10-18 14:48:56 +02:00
committed by Oliver Gierke
parent 78bd8fb0f7
commit 506fe37aea
3 changed files with 54 additions and 3 deletions

View File

@@ -15,6 +15,9 @@
*/
package org.springframework.data.mapping.model;
import kotlin.reflect.KFunction;
import kotlin.reflect.jvm.ReflectJvmMapping;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
@@ -22,6 +25,7 @@ import java.util.Optional;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PreferredConstructor;
import org.springframework.data.util.ReflectionUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;
@@ -88,12 +92,28 @@ public class MappingInstantiationException extends RuntimeException {
}
return String.format(TEXT_TEMPLATE, it.getType().getName(),
constructor.map(c -> c.getConstructor().toString()).orElse("NO_CONSTRUCTOR"), //
constructor.map(c -> toString(c)).orElse("NO_CONSTRUCTOR"), //
String.join(",", toStringArgs));
}).orElse(defaultMessage);
}
private static String toString(PreferredConstructor<?, ?> preferredConstructor) {
Constructor<?> constructor = preferredConstructor.getConstructor();
if (ReflectionUtils.isSupportedKotlinClass(constructor.getDeclaringClass())) {
KFunction<?> kotlinFunction = ReflectJvmMapping.getKotlinFunction(constructor);
if (kotlinFunction != null) {
return kotlinFunction.toString();
}
}
return constructor.toString();
}
/**
* Returns the type of the entity that was attempted to instantiate.
*