Browse Source

DATACMNS-134 - Changed instantation infrastructure to be able to instantiate member types.

In case a member class is not static the compiler will either create a constructor with a parameter of the enclosing type or alter the existing ones to add a first parameter of that type. Thus when instantiating the class we need to detect that case and hand in the parent object as parameter value.

Updated PreferredConstructor and Parameter abstractions to allow detecting this case as well as the ParameterValueProvider abstraction to capture the parent object.
pull/14/head
Oliver Gierke 14 years ago
parent
commit
c2fd1e255a
  1. 31
      spring-data-commons-core/src/main/java/org/springframework/data/mapping/PreferredConstructor.java
  2. 14
      spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/PersistentEntityParameterValueProvider.java
  3. 2
      spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java
  4. 31
      spring-data-commons-core/src/test/java/org/springframework/data/convert/ReflectionEntityInstantiatorUnitTest.java
  5. 26
      spring-data-commons-core/src/test/java/org/springframework/data/mapping/PreferredConstructorDiscovererUnitTests.java
  6. 73
      spring-data-commons-core/src/test/java/org/springframework/data/mapping/model/PersistentEntityParameterValueProviderUnitTests.java

31
spring-data-commons-core/src/main/java/org/springframework/data/mapping/PreferredConstructor.java

@ -36,7 +36,7 @@ import org.springframework.util.StringUtils; @@ -36,7 +36,7 @@ import org.springframework.util.StringUtils;
public class PreferredConstructor<T, P extends PersistentProperty<P>> {
private final Constructor<T> constructor;
private final List<Parameter<?, P>> parameters;
private final List<Parameter<Object, P>> parameters;
/**
* Creates a new {@link PreferredConstructor} from the given {@link Constructor} and {@link Parameter}s.
@ -44,7 +44,7 @@ public class PreferredConstructor<T, P extends PersistentProperty<P>> { @@ -44,7 +44,7 @@ public class PreferredConstructor<T, P extends PersistentProperty<P>> {
* @param constructor
* @param parameters
*/
public PreferredConstructor(Constructor<T> constructor, Parameter<?, P>... parameters) {
public PreferredConstructor(Constructor<T> constructor, Parameter<Object, P>... parameters) {
Assert.notNull(constructor);
Assert.notNull(parameters);
@ -68,7 +68,7 @@ public class PreferredConstructor<T, P extends PersistentProperty<P>> { @@ -68,7 +68,7 @@ public class PreferredConstructor<T, P extends PersistentProperty<P>> {
*
* @return
*/
public Iterable<Parameter<?, P>> getParameters() {
public Iterable<Parameter<Object, P>> getParameters() {
return parameters;
}
@ -121,6 +121,25 @@ public class PreferredConstructor<T, P extends PersistentProperty<P>> { @@ -121,6 +121,25 @@ public class PreferredConstructor<T, P extends PersistentProperty<P>> {
return false;
}
/**
* Returns whether the given {@link Parameter} is one referring to an enclosing class. That is in case the class this
* {@link PreferredConstructor} belongs to is a member class actually. If that's the case the compiler creates a first
* constructor argument of the enclosing class type.
*
* @param parameter must not be {@literal null}.
* @return
*/
public boolean isEnclosingClassParameter(Parameter<?, P> parameter) {
Assert.notNull(parameter);
if (parameters.isEmpty()) {
return false;
}
return parameters.get(0).equals(parameter) && parameter.isEnclosingClassParameter();
}
/**
* Value object to represent constructor parameters.
*
@ -220,5 +239,11 @@ public class PreferredConstructor<T, P extends PersistentProperty<P>> { @@ -220,5 +239,11 @@ public class PreferredConstructor<T, P extends PersistentProperty<P>> {
P referencedProperty = entity == null ? null : entity.getPersistentProperty(name);
return property == null ? false : property.equals(referencedProperty);
}
private boolean isEnclosingClassParameter() {
Class<T> owningType = entity.getType();
return owningType.isMemberClass() && type.getType().equals(owningType.getEnclosingClass());
}
}
}

14
spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/PersistentEntityParameterValueProvider.java

@ -17,6 +17,7 @@ package org.springframework.data.mapping.model; @@ -17,6 +17,7 @@ package org.springframework.data.mapping.model;
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.util.Assert;
@ -33,6 +34,7 @@ public class PersistentEntityParameterValueProvider<P extends PersistentProperty @@ -33,6 +34,7 @@ public class PersistentEntityParameterValueProvider<P extends PersistentProperty
private final PersistentEntity<?, P> entity;
private final PropertyValueProvider<P> provider;
private final Object parent;
private SpELExpressionEvaluator spELEvaluator;
@ -42,14 +44,17 @@ public class PersistentEntityParameterValueProvider<P extends PersistentProperty @@ -42,14 +44,17 @@ public class PersistentEntityParameterValueProvider<P extends PersistentProperty
*
* @param entity must not be {@literal null}.
* @param provider must not be {@literal null}.
* @param parent the parent object being created currently, can be {@literal null}.
*/
public PersistentEntityParameterValueProvider(PersistentEntity<?, P> entity, PropertyValueProvider<P> provider) {
public PersistentEntityParameterValueProvider(PersistentEntity<?, P> entity, PropertyValueProvider<P> provider,
Object parent) {
Assert.notNull(entity);
Assert.notNull(provider);
this.entity = entity;
this.provider = provider;
this.parent = parent;
}
/**
@ -66,12 +71,19 @@ public class PersistentEntityParameterValueProvider<P extends PersistentProperty @@ -66,12 +71,19 @@ public class PersistentEntityParameterValueProvider<P extends PersistentProperty
* (non-Javadoc)
* @see org.springframework.data.mapping.model.ParameterValueProvider#getParameterValue(org.springframework.data.mapping.PreferredConstructor.Parameter)
*/
@SuppressWarnings("unchecked")
public <T> T getParameterValue(Parameter<T, P> parameter) {
if (spELEvaluator != null && parameter.hasSpelExpression()) {
return spELEvaluator.evaluate(parameter.getSpelExpression());
}
PreferredConstructor<?, P> constructor = entity.getPersistenceConstructor();
if (constructor.isEnclosingClassParameter(parameter)) {
return (T) parent;
}
P property = entity.getPersistentProperty(parameter.getName());
return provider.getPropertyValue(property);

2
spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java

@ -107,7 +107,7 @@ public class PreferredConstructorDiscoverer<T, P extends PersistentProperty<P>> @@ -107,7 +107,7 @@ public class PreferredConstructorDiscoverer<T, P extends PersistentProperty<P>>
}
String[] parameterNames = nameDiscoverer.getParameterNames(constructor);
Parameter<?, P>[] parameters = new Parameter[parameterTypes.size()];
Parameter<Object, P>[] parameters = new Parameter[parameterTypes.size()];
Annotation[][] parameterAnnotations = constructor.getParameterAnnotations();
for (int i = 0; i < parameterTypes.size(); i++) {

31
spring-data-commons-core/src/test/java/org/springframework/data/convert/ReflectionEntityInstantiatorUnitTest.java

@ -15,6 +15,7 @@ @@ -15,6 +15,7 @@
*/
package org.springframework.data.convert;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.springframework.data.convert.ReflectionEntityInstantiator.*;
@ -23,12 +24,16 @@ import org.junit.Test; @@ -23,12 +24,16 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.convert.ReflectionEntityInstantiatorUnitTest.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.ParameterValueProvider;
import org.springframework.data.mapping.model.PreferredConstructorDiscoverer;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.test.util.ReflectionTestUtils;
/**
* Unit tests for {@link ReflectionEntityInstantiator}.
@ -78,10 +83,36 @@ public class ReflectionEntityInstantiatorUnitTest<P extends PersistentProperty<P @@ -78,10 +83,36 @@ public class ReflectionEntityInstantiatorUnitTest<P extends PersistentProperty<P
verify(provider, times(1)).getParameterValue((Parameter) constructor.getParameters().iterator().next());
}
/**
* @see DATACMNS-134
*/
@Test
public void createsInnerClassInstanceCorrectly() {
BasicPersistentEntity<Inner, P> entity = new BasicPersistentEntity<Inner, P>(ClassTypeInformation.from(Inner.class));
PreferredConstructor<Inner, P> constructor = entity.getPersistenceConstructor();
Parameter<Object, P> parameter = constructor.getParameters().iterator().next();
Object outer = new Outer();
when(provider.getParameterValue(parameter)).thenReturn(outer);
Inner instance = INSTANCE.createInstance(entity, provider);
assertThat(instance, is(notNullValue()));
assertThat(ReflectionTestUtils.getField(instance, "this$1"), is(outer));
}
static class Foo {
Foo(String foo) {
}
}
static class Outer {
class Inner {
}
}
}

26
spring-data-commons-core/src/test/java/org/springframework/data/mapping/PreferredConstructorDiscovererUnitTests.java

@ -23,7 +23,10 @@ import java.util.Iterator; @@ -23,7 +23,10 @@ import java.util.Iterator;
import org.junit.Test;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.mapping.PreferredConstructor.Parameter;
import org.springframework.data.mapping.PreferredConstructorDiscovererUnitTests.Outer.Inner;
import org.springframework.data.mapping.model.BasicPersistentEntity;
import org.springframework.data.mapping.model.PreferredConstructorDiscoverer;
import org.springframework.data.util.ClassTypeInformation;
/**
* Unit tests for {@link PreferredConstructorDiscoverer}.
@ -76,13 +79,27 @@ public class PreferredConstructorDiscovererUnitTests<P extends PersistentPropert @@ -76,13 +79,27 @@ public class PreferredConstructorDiscovererUnitTests<P extends PersistentPropert
assertThat(constructor.isExplicitlyAnnotated(), is(true));
assertThat(constructor.hasParameters(), is(true));
Iterator<Parameter<?, P>> parameters = constructor.getParameters().iterator();
Iterator<Parameter<Object, P>> parameters = constructor.getParameters().iterator();
Parameter<?, P> parameter = parameters.next();
assertThat(parameter.getType().getType(), typeCompatibleWith(Long.class));
assertThat(parameters.hasNext(), is(false));
}
/**
* @see DATACMNS-134
*/
@Test
public void discoversInnerClassConstructorCorrectly() {
PersistentEntity<Inner, P> entity = new BasicPersistentEntity<Inner, P>(ClassTypeInformation.from(Inner.class));
PreferredConstructorDiscoverer<Inner, P> discoverer = new PreferredConstructorDiscoverer<Inner, P>(entity);
PreferredConstructor<Inner, P> constructor = discoverer.getConstructor();
Parameter<?, P> parameter = constructor.getParameters().iterator().next();
assertThat(constructor.isEnclosingClassParameter(parameter), is(true));
}
static class EntityWithoutConstructor {
}
@ -123,4 +140,11 @@ public class PreferredConstructorDiscovererUnitTests<P extends PersistentPropert @@ -123,4 +140,11 @@ public class PreferredConstructorDiscovererUnitTests<P extends PersistentPropert
public ClassWithMultipleConstructorsAndAnnotation(Long value) {
}
}
static class Outer {
class Inner {
}
}
}

73
spring-data-commons-core/src/test/java/org/springframework/data/mapping/model/PersistentEntityParameterValueProviderUnitTests.java

@ -0,0 +1,73 @@ @@ -0,0 +1,73 @@
/*
* Copyright 2012 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
*
* http://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.model;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.util.Iterator;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
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.PersistentEntityParameterValueProviderUnitTests.Outer.Inner;
import org.springframework.data.util.ClassTypeInformation;
/**
* Unit tests for {@link PersistentEntityParameterValueProvider}.
*
* @author Oliver Gierke
*/
@RunWith(MockitoJUnitRunner.class)
public class PersistentEntityParameterValueProviderUnitTests<P extends PersistentProperty<P>> {
@Mock
PropertyValueProvider<P> propertyValueProvider;
/**
* @see DATACMNS-134
*/
@Test
public void usesParentObjectAsImplicitFirstConstructorArgument() {
Object outer = new Outer();
PersistentEntity<Inner, P> entity = new BasicPersistentEntity<Inner, P>(ClassTypeInformation.from(Inner.class));
PreferredConstructor<Inner, P> constructor = entity.getPersistenceConstructor();
Iterator<Parameter<Object, P>> iterator = constructor.getParameters().iterator();
ParameterValueProvider<P> provider = new PersistentEntityParameterValueProvider<P>(entity, propertyValueProvider,
outer);
assertThat(provider.getParameterValue(iterator.next()), is(outer));
assertThat(provider.getParameterValue(iterator.next()), is(nullValue()));
assertThat(iterator.hasNext(), is(false));
}
static class Outer {
class Inner {
Inner(Object myObject) {
}
}
}
}
Loading…
Cancel
Save