Browse Source

DATACMNS-169 - Reworked simple type handling in mapping subsystem.

All simple type discovery is now done through a SimpletypeHolder instance that carries a few default types considered to be simple but is configurable to carry additional ones. The mapping subsystem uses an instance of that class to do simple type decisions and thus allows customizing what is to be considered a simple type by simply dependency injecting a value object.
pull/2/merge
Oliver Gierke 15 years ago
parent
commit
7a13657eb5
  1. 20
      spring-data-commons-core/src/main/java/org/springframework/data/mapping/AbstractMappingContext.java
  2. 12
      spring-data-commons-core/src/main/java/org/springframework/data/mapping/AbstractPersistentProperty.java
  3. 5
      spring-data-commons-core/src/main/java/org/springframework/data/mapping/AnnotationBasedPersistentProperty.java
  4. 90
      spring-data-commons-core/src/main/java/org/springframework/data/mapping/MappingBeanHelper.java
  5. 125
      spring-data-commons-core/src/main/java/org/springframework/data/mapping/SimpleTypeHolder.java
  6. 10
      spring-data-commons-core/src/test/java/org/springframework/data/mapping/MappingMetadataTests.java
  7. 79
      spring-data-commons-core/src/test/java/org/springframework/data/mapping/SimpleTypeHolderUnitTests.java

20
spring-data-commons-core/src/main/java/org/springframework/data/mapping/AbstractMappingContext.java

@ -65,15 +65,7 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<? @@ -65,15 +65,7 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
private List<Class<?>> customSimpleTypes = new ArrayList<Class<?>>();
private Set<? extends Class<?>> initialEntitySet = new HashSet<Class<?>>();
private boolean strict = false;
/**
* Sets types to be considered simple. That means these types will not be mapped recusively.
*
* @param customSimpleTypes the customSimpleTypes to set
*/
public void setCustomSimpleTypes(List<Class<?>> customSimpleTypes) {
this.customSimpleTypes = customSimpleTypes;
}
private SimpleTypeHolder simpleTypeHolder = new SimpleTypeHolder();
/*
* (non-Javadoc)
@ -103,6 +95,10 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<? @@ -103,6 +95,10 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
public void setStrict(boolean strict) {
this.strict = strict;
}
public void setSimpleTypeHolder(SimpleTypeHolder simpleTypes) {
this.simpleTypeHolder = simpleTypes;
}
/*
* (non-Javadoc)
@ -186,7 +182,7 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<? @@ -186,7 +182,7 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
PropertyDescriptor descriptor = descriptors.get(field.getName());
ReflectionUtils.makeAccessible(field);
P property = createPersistentProperty(field, descriptor, entity);
P property = createPersistentProperty(field, descriptor, entity, simpleTypeHolder);
if (property.isTransient()) {
return;
@ -273,7 +269,7 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<? @@ -273,7 +269,7 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
}
private TypeInformation<?> getTypeInformationIfNotSimpleType(TypeInformation<?> information) {
return information == null || MappingBeanHelper.isSimpleType(information.getType()) ? null : information;
return information == null || simpleTypeHolder.isSimpleType(information.getType()) ? null : information;
}
public List<Validator> getEntityValidators(E entity) {
@ -282,7 +278,7 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<? @@ -282,7 +278,7 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
protected abstract <T> E createPersistentEntity(TypeInformation<T> typeInformation);
protected abstract P createPersistentProperty(Field field, PropertyDescriptor descriptor, E owner);
protected abstract P createPersistentProperty(Field field, PropertyDescriptor descriptor, E owner, SimpleTypeHolder simpleTypeHolder);
public void afterPropertiesSet() {

12
spring-data-commons-core/src/main/java/org/springframework/data/mapping/AbstractPersistentProperty.java

@ -28,6 +28,7 @@ import org.springframework.data.mapping.model.Association; @@ -28,6 +28,7 @@ import org.springframework.data.mapping.model.Association;
import org.springframework.data.mapping.model.PersistentEntity;
import org.springframework.data.mapping.model.PersistentProperty;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.Assert;
/**
* Simple impementation of {@link PersistentProperty}.
@ -44,8 +45,12 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P> @@ -44,8 +45,12 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
protected final Field field;
protected final Association<P> association;
protected final PersistentEntity<?, P> owner;
private final SimpleTypeHolder simpleTypeHolder;
public AbstractPersistentProperty(Field field, PropertyDescriptor propertyDescriptor, PersistentEntity<?, P> owner) {
public AbstractPersistentProperty(Field field, PropertyDescriptor propertyDescriptor, PersistentEntity<?, P> owner, SimpleTypeHolder simpleTypeHolder) {
Assert.notNull(simpleTypeHolder);
this.name = field.getName();
this.rawType = field.getType();
this.information = owner.getTypeInformation().getProperty(this.name);
@ -53,6 +58,7 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P> @@ -53,6 +58,7 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
this.field = field;
this.association = isAssociation() ? createAssociation() : null;
this.owner = owner;
this.simpleTypeHolder = simpleTypeHolder;
}
protected abstract Association<P> createAssociation();
@ -127,9 +133,9 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P> @@ -127,9 +133,9 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
public boolean isComplexType() {
if (isCollection() || isArray()) {
return !MappingBeanHelper.isSimpleType(getComponentType());
return !simpleTypeHolder.isSimpleType(getComponentType());
} else {
return !MappingBeanHelper.isSimpleType(getType());
return !simpleTypeHolder.isSimpleType(getType());
}
}

5
spring-data-commons-core/src/main/java/org/springframework/data/mapping/AnnotationBasedPersistentProperty.java

@ -45,10 +45,9 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp @@ -45,10 +45,9 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
* @param propertyDescriptor
* @param owner
*/
public AnnotationBasedPersistentProperty(Field field,
PropertyDescriptor propertyDescriptor, PersistentEntity<?, P> owner) {
public AnnotationBasedPersistentProperty(Field field, PropertyDescriptor propertyDescriptor, PersistentEntity<?, P> owner, SimpleTypeHolder simpleTypeHolder) {
super(field, propertyDescriptor, owner);
super(field, propertyDescriptor, owner, simpleTypeHolder);
this.value = field.getAnnotation(Value.class);
field.isAnnotationPresent(Autowired.class);
}

90
spring-data-commons-core/src/main/java/org/springframework/data/mapping/MappingBeanHelper.java

@ -1,90 +0,0 @@ @@ -1,90 +0,0 @@
/*
* Copyright (c) 2011 by the original author(s).
*
* 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;
import java.util.Collections;
import java.util.Date;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* Helper class to set and retrieve bean values.
*
* @author Jon Brisbin <jbrisbin@vmware.com>
* @author Oliver Gierke
*/
public abstract class MappingBeanHelper {
private static final Set<Class<?>> simpleTypes = Collections.newSetFromMap(new ConcurrentHashMap<Class<?>, Boolean>());
static {
simpleTypes.add(boolean.class);
simpleTypes.add(boolean[].class);
simpleTypes.add(long.class);
simpleTypes.add(long[].class);
simpleTypes.add(short.class);
simpleTypes.add(short[].class);
simpleTypes.add(int.class);
simpleTypes.add(int[].class);
simpleTypes.add(byte.class);
simpleTypes.add(byte[].class);
simpleTypes.add(float.class);
simpleTypes.add(float[].class);
simpleTypes.add(double.class);
simpleTypes.add(double[].class);
simpleTypes.add(char.class);
simpleTypes.add(char[].class);
simpleTypes.add(Boolean.class);
simpleTypes.add(Long.class);
simpleTypes.add(Short.class);
simpleTypes.add(Integer.class);
simpleTypes.add(Byte.class);
simpleTypes.add(Float.class);
simpleTypes.add(Double.class);
simpleTypes.add(Character.class);
simpleTypes.add(String.class);
simpleTypes.add(Date.class);
simpleTypes.add(Locale.class);
simpleTypes.add(Class.class);
simpleTypes.add(Number.class);
}
/**
* Returns the set of types considered to be simple.
*
* @return
*/
public static Set<Class<?>> getSimpleTypes() {
return simpleTypes;
}
/**
* Returns whether the given type is considered a simple one.
*
* @param type
* @return
*/
public static boolean isSimpleType(Class<?> type) {
for (Class<?> clazz : simpleTypes) {
if (type == clazz || clazz.isAssignableFrom(type)) {
return true;
}
}
return type.isEnum();
}
}

125
spring-data-commons-core/src/main/java/org/springframework/data/mapping/SimpleTypeHolder.java

@ -0,0 +1,125 @@ @@ -0,0 +1,125 @@
/*
* Copyright (c) 2011 by the original author(s).
*
* 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;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import org.springframework.util.Assert;
/**
* Simple container to hold a set of types to be considered simple types.
*
* @author Oliver Gierke
*/
public class SimpleTypeHolder {
private static final Set<Class<?>> DEFAULTS = new HashSet<Class<?>>();
static {
DEFAULTS.add(boolean.class);
DEFAULTS.add(boolean[].class);
DEFAULTS.add(long.class);
DEFAULTS.add(long[].class);
DEFAULTS.add(short.class);
DEFAULTS.add(short[].class);
DEFAULTS.add(int.class);
DEFAULTS.add(int[].class);
DEFAULTS.add(byte.class);
DEFAULTS.add(byte[].class);
DEFAULTS.add(float.class);
DEFAULTS.add(float[].class);
DEFAULTS.add(double.class);
DEFAULTS.add(double[].class);
DEFAULTS.add(char.class);
DEFAULTS.add(char[].class);
DEFAULTS.add(Boolean.class);
DEFAULTS.add(Long.class);
DEFAULTS.add(Short.class);
DEFAULTS.add(Integer.class);
DEFAULTS.add(Byte.class);
DEFAULTS.add(Float.class);
DEFAULTS.add(Double.class);
DEFAULTS.add(Character.class);
DEFAULTS.add(String.class);
DEFAULTS.add(Date.class);
DEFAULTS.add(Locale.class);
DEFAULTS.add(Class.class);
DEFAULTS.add(Number.class);
}
private final Set<Class<?>> simpleTypes;
/**
* Creates a new {@link SimpleTypeHolder} containing the default types.
*
* @see #SimpleTypeHolder(Set, boolean)
*/
@SuppressWarnings("unchecked")
public SimpleTypeHolder() {
this(Collections.EMPTY_SET, true);
}
/**
* Creates a new {@link SimpleTypeHolder} to carry the given custom simple types. Registration of default simple types
* can be deactivated by passing {@literal false} for {@code registerDefaults}.
*
* @param customSimpleTypes
* @param registerDefaults
*/
public SimpleTypeHolder(Set<? extends Class<?>> customSimpleTypes, boolean registerDefaults) {
Assert.notNull(customSimpleTypes);
this.simpleTypes = new HashSet<Class<?>>(customSimpleTypes);
if (registerDefaults) {
this.simpleTypes.addAll(DEFAULTS);
}
}
/**
* Copy constructor to create a new {@link SimpleTypeHolder} that carries the given additional custom simple types.
*
* @param customSimpleTypes must not be {@literal null}
* @param source must not be {@literal null}
*/
public SimpleTypeHolder(Set<? extends Class<?>> customSimpleTypes, SimpleTypeHolder source) {
Assert.notNull(customSimpleTypes);
Assert.notNull(source);
this.simpleTypes = new HashSet<Class<?>>(customSimpleTypes);
this.simpleTypes.addAll(source.simpleTypes);
}
/**
* Returns whether the given type is considered a simple one.
*
* @param type
* @return
*/
public boolean isSimpleType(Class<?> type) {
for (Class<?> clazz : simpleTypes) {
if (type == clazz || clazz.isAssignableFrom(type)) {
return true;
}
}
return type.isEnum();
}
}

10
spring-data-commons-core/src/test/java/org/springframework/data/mapping/MappingMetadataTests.java

@ -73,8 +73,9 @@ public class MappingMetadataTests { @@ -73,8 +73,9 @@ public class MappingMetadataTests {
@Override
protected SampleProperty createPersistentProperty(Field field,
PropertyDescriptor descriptor,
MutablePersistentEntity<?, SampleProperty> owner) {
return new SamplePropertyImpl(field, descriptor, owner);
MutablePersistentEntity<?, SampleProperty> owner,
SimpleTypeHolder simpleTypeHolder) {
return new SamplePropertyImpl(field, descriptor, owner, simpleTypeHolder);
}
}
@ -82,9 +83,10 @@ public class MappingMetadataTests { @@ -82,9 +83,10 @@ public class MappingMetadataTests {
public SamplePropertyImpl(Field field,
PropertyDescriptor propertyDescriptor,
PersistentEntity<?, SampleProperty> owner) {
PersistentEntity<?, SampleProperty> owner,
SimpleTypeHolder simpleTypeHolder) {
super(field, propertyDescriptor, owner);
super(field, propertyDescriptor, owner, simpleTypeHolder);
}
@Override

79
spring-data-commons-core/src/test/java/org/springframework/data/mapping/SimpleTypeHolderUnitTests.java

@ -0,0 +1,79 @@ @@ -0,0 +1,79 @@
/*
* Copyright (c) 2011 by the original author(s).
*
* 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;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.util.Collections;
import java.util.HashSet;
import org.junit.Test;
/**
* Unit tests for {@link SimpleTypeHolder}.
*
* @author Oliver Gierke
*/
public class SimpleTypeHolderUnitTests {
@Test(expected = IllegalArgumentException.class)
public void rejectsNullCustomTypes() {
new SimpleTypeHolder(null, false);
}
@Test(expected = IllegalArgumentException.class)
public void rejectsNullOriginal() {
new SimpleTypeHolder(new HashSet<Class<?>>(), null);
}
@Test
public void addsDefaultTypes() {
SimpleTypeHolder holder = new SimpleTypeHolder();
assertThat(holder.isSimpleType(String.class), is(true));
}
@Test
public void doesNotAddDefaultConvertersIfConfigured() {
SimpleTypeHolder holder = new SimpleTypeHolder(new HashSet<Class<?>>(), false);
assertThat(holder.isSimpleType(String.class), is(false));
}
@Test
public void addsCustomTypesToSimpleOnes() {
SimpleTypeHolder holder = new SimpleTypeHolder(Collections.singleton(SimpleTypeHolder.class), true);
assertThat(holder.isSimpleType(SimpleTypeHolder.class), is(true));
assertThat(holder.isSimpleType(SimpleTypeHolderUnitTests.class), is(false));
}
@Test
public void createsHolderFromAnotherOneCorrectly() {
SimpleTypeHolder holder = new SimpleTypeHolder(Collections.singleton(SimpleTypeHolder.class), true);
SimpleTypeHolder second = new SimpleTypeHolder(Collections.singleton(SimpleTypeHolderUnitTests.class), holder);
assertThat(holder.isSimpleType(SimpleTypeHolder.class), is(true));
assertThat(holder.isSimpleType(SimpleTypeHolderUnitTests.class), is(false));
assertThat(second.isSimpleType(SimpleTypeHolder.class), is(true));
assertThat(second.isSimpleType(SimpleTypeHolderUnitTests.class), is(true));
}
}
Loading…
Cancel
Save