DATACMNS-1126 - Polishing.

Moved PreferredConstructorDiscoverers into PreferredConstructorDiscoverer to hide more API. Renamed ClassGeneratingKotlinEntityInstantiator to KotlinClassGeneratingEntityInstantiator.

Use org.springframework.util.Assert instead of com.mysema.commons.lang.Assert.

Original pull request: #233.
This commit is contained in:
Oliver Gierke
2017-08-25 13:03:07 +02:00
committed by Mark Paluch
parent b05c4e74fb
commit 22b6971318
5 changed files with 190 additions and 202 deletions

View File

@@ -57,7 +57,7 @@ public class EntityInstantiators {
* @param customInstantiators must not be {@literal null}.
*/
public EntityInstantiators(Map<Class<?>, EntityInstantiator> customInstantiators) {
this(new ClassGeneratingKotlinEntityInstantiator(), customInstantiators);
this(new KotlinClassGeneratingEntityInstantiator(), customInstantiators);
}
/**

View File

@@ -30,16 +30,16 @@ import org.springframework.data.mapping.PreferredConstructor.Parameter;
import org.springframework.data.mapping.model.ParameterValueProvider;
import org.springframework.data.util.ReflectionUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Kotlin-specific extension to {@link ClassGeneratingEntityInstantiator} that adapts Kotlin constructors with
* defaulting.
*
* @author Mark Paluch
* @author Oliver Gierke
* @since 2.0
*/
public class ClassGeneratingKotlinEntityInstantiator extends ClassGeneratingEntityInstantiator {
public class KotlinClassGeneratingEntityInstantiator extends ClassGeneratingEntityInstantiator {
/*
* (non-Javadoc)
@@ -48,14 +48,18 @@ public class ClassGeneratingKotlinEntityInstantiator extends ClassGeneratingEnti
@Override
protected EntityInstantiator doCreateEntityInstantiator(PersistentEntity<?, ?> entity) {
if (ReflectionUtils.isKotlinClass(entity.getType()) && entity.getPersistenceConstructor() != null) {
PreferredConstructor<?, ?> constructor = entity.getPersistenceConstructor();
if (ReflectionUtils.isKotlinClass(entity.getType()) && constructor != null) {
PreferredConstructor<?, ?> defaultConstructor = new DefaultingKotlinConstructorResolver(entity)
.getDefaultConstructor();
if (defaultConstructor != null) {
return new DefaultingKotlinClassInstantiatorAdapter(createObjectInstantiator(entity, defaultConstructor),
entity.getPersistenceConstructor());
ObjectInstantiator instantiator = createObjectInstantiator(entity, defaultConstructor);
return new DefaultingKotlinClassInstantiatorAdapter(instantiator, constructor);
}
}
@@ -71,7 +75,7 @@ public class ClassGeneratingKotlinEntityInstantiator extends ClassGeneratingEnti
*/
static class DefaultingKotlinConstructorResolver {
@Nullable private final PreferredConstructor<?, ?> defaultConstructor;
private final @Nullable PreferredConstructor<?, ?> defaultConstructor;
@SuppressWarnings("unchecked")
DefaultingKotlinConstructorResolver(PersistentEntity<?, ?> entity) {
@@ -90,12 +94,14 @@ public class ClassGeneratingKotlinEntityInstantiator extends ClassGeneratingEnti
@Nullable
private static Constructor<?> resolveDefaultConstructor(PersistentEntity<?, ?> entity) {
if (entity.getPersistenceConstructor() == null) {
PreferredConstructor<?, ?> persistenceConstructor = entity.getPersistenceConstructor();
if (persistenceConstructor == null) {
return null;
}
Constructor<?> hit = null;
Constructor<?> constructor = entity.getPersistenceConstructor().getConstructor();
Constructor<?> constructor = persistenceConstructor.getConstructor();
for (Constructor<?> candidate : entity.getType().getDeclaredConstructors()) {
@@ -192,7 +198,10 @@ public class ClassGeneratingKotlinEntityInstantiator extends ClassGeneratingEnti
ParameterValueProvider<P> provider) {
PreferredConstructor<? extends T, P> preferredConstructor = entity.getPersistenceConstructor();
Assert.notNull(preferredConstructor, "PreferredConstructor must not be null!");
if (preferredConstructor == null) {
throw new IllegalArgumentException("PreferredConstructor must not be null!");
}
int[] defaulting = new int[(synthetic.getParameterCount() / 32) + 1];