DATACMNS-1639 - Introduce InstantiationAwarePropertyAccessor.
We now ship and use a PropertyAccessor implementation by default that will fall back to use the persistence constructor and creating a new instance to "set" a property. This allows completely immutable types, ommitting previously needed wither methods, e.g. to populate automatically generated identifier values. This is implemented by using the EntityInstantiator available and a ParameterValueProvider that returns the value of the properties of the current instance but replacing the one for the value to be set. This requires moving EntityInstantiators, the EntityInstantiator SPI as well as all existing implementations (reflective, class generating and the Kotlin one) into the mapping.model package. Legacy instances stay around in deprecated form to avoid breaking existing clients (other store modules as well as user implementations of EntityInstantiator). The newly introduced implementations stay package protected as their sole users are now colocated in the same package. Access from the legacy types is handled via InternalEntityInstantiatorFactory, which is introduced in deprecated from for removal alongside the actual deprecations.
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mapping;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import lombok.Value;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.convert.EntityInstantiators;
|
||||
import org.springframework.data.mapping.context.SampleMappingContext;
|
||||
import org.springframework.data.mapping.context.SamplePersistentProperty;
|
||||
import org.springframework.data.mapping.model.InstantiationAwarePropertyAccessor;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
public class InstantiationAwarePersistentPropertyAccessorUnitTests {
|
||||
|
||||
@Test
|
||||
public void testname() {
|
||||
|
||||
EntityInstantiators instantiators = new EntityInstantiators();
|
||||
SampleMappingContext context = new SampleMappingContext();
|
||||
|
||||
PersistentEntity<Object, SamplePersistentProperty> entity = context.getRequiredPersistentEntity(Sample.class);
|
||||
|
||||
PersistentPropertyAccessor<Sample> accessor = entity.getPropertyAccessor(new Sample("Dave", "Matthews", 42));
|
||||
|
||||
PersistentPropertyAccessor<Sample> wrapper = new InstantiationAwarePropertyAccessor<>(accessor,
|
||||
instantiators);
|
||||
|
||||
wrapper.setProperty(entity.getRequiredPersistentProperty("firstname"), "Oliver August");
|
||||
|
||||
assertThat(wrapper.getBean()).isEqualTo(new Sample("Oliver August", "Matthews", 42));
|
||||
}
|
||||
|
||||
@Value
|
||||
static class Sample {
|
||||
|
||||
String firstname, lastname;
|
||||
int age;
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,6 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.data.annotation.AccessType;
|
||||
import org.springframework.data.annotation.AccessType.Type;
|
||||
@@ -196,8 +195,14 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
|
||||
PersistentPropertyAccessor accessor = entity.getPropertyAccessor(value);
|
||||
|
||||
assertThat(accessor).isNotInstanceOf(BeanWrapper.class);
|
||||
assertThat(accessor.getClass().getName()).contains("_Accessor_");
|
||||
assertThat(accessor.getBean()).isEqualTo(value);
|
||||
|
||||
assertThat(accessor).isInstanceOfSatisfying(InstantiationAwarePropertyAccessor.class, it -> {
|
||||
|
||||
PersistentPropertyAccessor delegate = (PersistentPropertyAccessor) ReflectionTestUtils.getField(it, "delegate");
|
||||
|
||||
assertThat(delegate.getClass().getName()).contains("_Accessor_");
|
||||
assertThat(delegate.getBean()).isEqualTo(value);
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATACMNS-596
|
||||
|
||||
@@ -13,9 +13,10 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.convert;
|
||||
package org.springframework.data.mapping.model;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.data.util.ClassTypeInformation.from;
|
||||
|
||||
@@ -29,16 +30,12 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.data.classloadersupport.HidingClassLoader;
|
||||
import org.springframework.data.convert.ClassGeneratingEntityInstantiator.ObjectInstantiator;
|
||||
import org.springframework.data.convert.ClassGeneratingEntityInstantiatorUnitTests.Outer.Inner;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.mapping.PreferredConstructor;
|
||||
import org.springframework.data.mapping.PreferredConstructor.Parameter;
|
||||
import org.springframework.data.mapping.model.BasicPersistentEntity;
|
||||
import org.springframework.data.mapping.model.MappingInstantiationException;
|
||||
import org.springframework.data.mapping.model.ParameterValueProvider;
|
||||
import org.springframework.data.mapping.model.PreferredConstructorDiscoverer;
|
||||
import org.springframework.data.mapping.model.ClassGeneratingEntityInstantiator.ObjectInstantiator;
|
||||
import org.springframework.data.mapping.model.ClassGeneratingEntityInstantiatorUnitTests.Outer.Inner;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
@@ -363,11 +360,10 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
|
||||
public void shouldUseReflectionIfFrameworkTypesNotVisible() throws Exception {
|
||||
|
||||
HidingClassLoader classLoader = HidingClassLoader.hide(ObjectInstantiator.class);
|
||||
classLoader.excludePackage("org.springframework.data.mapping.model");
|
||||
classLoader.excludePackage("org.springframework.data.mapping");
|
||||
|
||||
// require type from different package to meet visibility quirks
|
||||
Class<?> entityType = classLoader
|
||||
.loadClass("org.springframework.data.mapping.model.PersistentPropertyAccessorTests$ClassLoaderTest");
|
||||
Class<?> entityType = classLoader.loadClass("org.springframework.data.mapping.Person");
|
||||
|
||||
prepareMocks(entityType);
|
||||
|
||||
@@ -139,7 +139,10 @@ public class ClassGeneratingPropertyAccessorFactoryDatatypeTests {
|
||||
.getRequiredPersistentEntity(bean.getClass());
|
||||
|
||||
assertThat(ReflectionTestUtils.getField(persistentEntity, "propertyAccessorFactory"))
|
||||
.isInstanceOf(ClassGeneratingPropertyAccessorFactory.class);
|
||||
.isInstanceOfSatisfying(InstantiationAwarePropertyAccessorFactory.class, it -> {
|
||||
assertThat(ReflectionTestUtils.getField(it, "delegate"))
|
||||
.isInstanceOf(ClassGeneratingPropertyAccessorFactory.class);
|
||||
});
|
||||
}
|
||||
|
||||
private PersistentPropertyAccessor getPersistentPropertyAccessor(Object bean) {
|
||||
|
||||
@@ -150,7 +150,7 @@ public class ClassGeneratingPropertyAccessorFactoryTests {
|
||||
|
||||
assertThat(getProperty(new Dummy(), "dummy"))
|
||||
.satisfies(property -> assertThatExceptionOfType(UnsupportedOperationException.class)//
|
||||
.isThrownBy(() -> getPersistentPropertyAccessor(bean).getProperty(property)));
|
||||
.isThrownBy(() -> getPersistentPropertyAccessor(bean).getProperty(property)));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-809
|
||||
@@ -158,7 +158,7 @@ public class ClassGeneratingPropertyAccessorFactoryTests {
|
||||
|
||||
assertThat(getProperty(new Dummy(), "dummy"))
|
||||
.satisfies(property -> assertThatExceptionOfType(UnsupportedOperationException.class)//
|
||||
.isThrownBy(() -> getPersistentPropertyAccessor(bean).setProperty(property, Optional.empty())));
|
||||
.isThrownBy(() -> getPersistentPropertyAccessor(bean).setProperty(property, Optional.empty())));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-809
|
||||
@@ -168,7 +168,10 @@ public class ClassGeneratingPropertyAccessorFactoryTests {
|
||||
.getRequiredPersistentEntity(bean.getClass());
|
||||
|
||||
assertThat(ReflectionTestUtils.getField(persistentEntity, "propertyAccessorFactory"))
|
||||
.isInstanceOf(ClassGeneratingPropertyAccessorFactory.class);
|
||||
.isInstanceOfSatisfying(InstantiationAwarePropertyAccessorFactory.class, it -> {
|
||||
assertThat(ReflectionTestUtils.getField(it, "delegate"))
|
||||
.isInstanceOf(ClassGeneratingPropertyAccessorFactory.class);
|
||||
});
|
||||
}
|
||||
|
||||
private PersistentPropertyAccessor getPersistentPropertyAccessor(Object bean) {
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.convert;
|
||||
package org.springframework.data.mapping.model;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
@@ -55,8 +55,7 @@ public class EntityInstantiatorsUnitTests {
|
||||
|
||||
doReturn(String.class).when(entity).getType();
|
||||
|
||||
Map<Class<?>, EntityInstantiator> customInstantiators = Collections
|
||||
.singletonMap(String.class, customInstantiator);
|
||||
Map<Class<?>, EntityInstantiator> customInstantiators = Collections.singletonMap(String.class, customInstantiator);
|
||||
|
||||
EntityInstantiators instantiators = new EntityInstantiators(customInstantiators);
|
||||
assertThat(instantiators.getInstantiatorFor(entity)).isEqualTo(customInstantiator);
|
||||
@@ -67,8 +66,8 @@ public class EntityInstantiatorsUnitTests {
|
||||
|
||||
doReturn(Object.class).when(entity).getType();
|
||||
|
||||
Map<Class<?>, EntityInstantiator> customInstantiators = Collections
|
||||
.singletonMap(String.class, ReflectionEntityInstantiator.INSTANCE);
|
||||
Map<Class<?>, EntityInstantiator> customInstantiators = Collections.singletonMap(String.class,
|
||||
ReflectionEntityInstantiator.INSTANCE);
|
||||
|
||||
EntityInstantiators instantiators = new EntityInstantiators(customInstantiator, customInstantiators);
|
||||
instantiators.getInstantiatorFor(entity);
|
||||
@@ -76,7 +75,6 @@ public class EntityInstantiatorsUnitTests {
|
||||
assertThat(instantiators.getInstantiatorFor(entity)).isEqualTo(customInstantiator);
|
||||
|
||||
doReturn(String.class).when(entity).getType();
|
||||
assertThat(instantiators.getInstantiatorFor(entity))
|
||||
.isEqualTo((EntityInstantiator) ReflectionEntityInstantiator.INSTANCE);
|
||||
assertThat(instantiators.getInstantiatorFor(entity)).isEqualTo(ReflectionEntityInstantiator.INSTANCE);
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.convert;
|
||||
package org.springframework.data.mapping.model;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
@@ -30,14 +30,13 @@ import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.PreferredConstructor.Parameter;
|
||||
import org.springframework.data.mapping.context.SampleMappingContext;
|
||||
import org.springframework.data.mapping.context.SamplePersistentProperty;
|
||||
import org.springframework.data.mapping.model.BasicPersistentEntity;
|
||||
import org.springframework.data.mapping.model.ParameterValueProvider;
|
||||
import org.springframework.data.mapping.model.With32Args;
|
||||
import org.springframework.data.mapping.model.With33Args;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
/**
|
||||
* Unit test to verify correct object instantiation using Kotlin defaulting via {@link KotlinClassGeneratingEntityInstantiator}.
|
||||
* Unit test to verify correct object instantiation using Kotlin defaulting via
|
||||
* {@link KotlinClassGeneratingEntityInstantiator}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@@ -51,7 +50,9 @@ public class ParameterizedKotlinInstantiatorUnitTests {
|
||||
private final String propertyUnderTestName;
|
||||
private final EntityInstantiator entityInstantiator;
|
||||
|
||||
public ParameterizedKotlinInstantiatorUnitTests(PersistentEntity<Object, SamplePersistentProperty> entity, int propertyCount, int propertyUnderTestIndex, String propertyUnderTestName, EntityInstantiator entityInstantiator, String label) {
|
||||
public ParameterizedKotlinInstantiatorUnitTests(PersistentEntity<Object, SamplePersistentProperty> entity,
|
||||
int propertyCount, int propertyUnderTestIndex, String propertyUnderTestName,
|
||||
EntityInstantiator entityInstantiator, String label) {
|
||||
this.entity = entity;
|
||||
this.propertyCount = propertyCount;
|
||||
this.propertyUnderTestIndex = propertyUnderTestIndex;
|
||||
@@ -76,13 +77,16 @@ public class ParameterizedKotlinInstantiatorUnitTests {
|
||||
return fixtures;
|
||||
}
|
||||
|
||||
private static List<Object[]> createFixture(SampleMappingContext context, Class<?> entityType, int propertyCount, EntityInstantiator entityInstantiator) {
|
||||
private static List<Object[]> createFixture(SampleMappingContext context, Class<?> entityType, int propertyCount,
|
||||
EntityInstantiator entityInstantiator) {
|
||||
|
||||
BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntity = context.getPersistentEntity(entityType);
|
||||
|
||||
return IntStream.range(0, propertyCount).mapToObj(i -> {
|
||||
|
||||
return new Object[]{persistentEntity, propertyCount, i, Integer.toString(i), entityInstantiator, String.format("Property %d for %s using %s", i, entityType.getSimpleName(), entityInstantiator.getClass().getSimpleName())};
|
||||
return new Object[] { persistentEntity, propertyCount, i, Integer.toString(i), entityInstantiator,
|
||||
String.format("Property %d for %s using %s", i, entityType.getSimpleName(),
|
||||
entityInstantiator.getClass().getSimpleName()) };
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@@ -13,11 +13,12 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.convert;
|
||||
package org.springframework.data.mapping.model;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.data.convert.ReflectionEntityInstantiator.*;
|
||||
import static org.springframework.data.mapping.model.ReflectionEntityInstantiator.*;
|
||||
import static org.springframework.data.util.ClassTypeInformation.from;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
@@ -28,15 +29,11 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.data.convert.ReflectionEntityInstantiatorUnitTests.Outer.Inner;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.mapping.PreferredConstructor;
|
||||
import org.springframework.data.mapping.PreferredConstructor.Parameter;
|
||||
import org.springframework.data.mapping.model.BasicPersistentEntity;
|
||||
import org.springframework.data.mapping.model.MappingInstantiationException;
|
||||
import org.springframework.data.mapping.model.ParameterValueProvider;
|
||||
import org.springframework.data.mapping.model.PreferredConstructorDiscoverer;
|
||||
import org.springframework.data.mapping.model.ReflectionEntityInstantiatorUnitTests.Outer.Inner;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
Reference in New Issue
Block a user