From be8daa526815111b6e6be94f9e2faa3c6c49dfb0 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 7 Sep 2011 11:44:50 +0200 Subject: [PATCH] Removed obsolete MongoBeanWrapper and MongoPropertyDescriptors. --- .../core/convert/MongoBeanWrapper.java | 93 ------- .../convert/MongoPropertyDescriptors.java | 244 ------------------ .../core/mapping/MongoSimpleTypes.java | 21 +- .../MongoRepositoryFactoryBean.java | 6 +- 4 files changed, 19 insertions(+), 345 deletions(-) delete mode 100644 spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoBeanWrapper.java delete mode 100644 spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoPropertyDescriptors.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoBeanWrapper.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoBeanWrapper.java deleted file mode 100644 index 709c31c25..000000000 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoBeanWrapper.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright 2011 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.mongodb.core.convert; - -import static org.springframework.beans.PropertyAccessorFactory.forBeanPropertyAccess; -import static org.springframework.beans.PropertyAccessorFactory.forDirectFieldAccess; - -import org.springframework.beans.BeanWrapper; -import org.springframework.beans.ConfigurablePropertyAccessor; -import org.springframework.beans.NotWritablePropertyException; -import org.springframework.core.convert.ConversionService; -import org.springframework.data.mongodb.core.convert.MongoPropertyDescriptors.MongoPropertyDescriptor; -import org.springframework.util.Assert; - -/** - * Custom Mongo specific {@link BeanWrapper} to allow access to bean properties via {@link MongoPropertyDescriptor}s. - * - * @author Oliver Gierke - */ -class MongoBeanWrapper { - - private final ConfigurablePropertyAccessor accessor; - private final MongoPropertyDescriptors descriptors; - private final boolean fieldAccess; - - /** - * Creates a new {@link MongoBeanWrapper} for the given target object and {@link ConversionService}. - * - * @param target - * @param conversionService - * @param fieldAccess - */ - public MongoBeanWrapper(Object target, ConversionService conversionService, boolean fieldAccess) { - - Assert.notNull(target); - Assert.notNull(conversionService); - - this.fieldAccess = fieldAccess; - this.accessor = fieldAccess ? forDirectFieldAccess(target) : forBeanPropertyAccess(target); - this.accessor.setConversionService(conversionService); - this.descriptors = new MongoPropertyDescriptors(target.getClass()); - } - - /** - * Returns all {@link MongoPropertyDescriptors.MongoPropertyDescriptor}s for the underlying target object. - * - * @return - */ - public MongoPropertyDescriptors getDescriptors() { - return this.descriptors; - } - - /** - * Returns the value of the underlying object for the given property. - * - * @param descriptor - * @return - */ - public Object getValue(MongoPropertyDescriptors.MongoPropertyDescriptor descriptor) { - Assert.notNull(descriptor); - return accessor.getPropertyValue(descriptor.getName()); - } - - /** - * Sets the property of the underlying object to the given value. - * - * @param descriptor - * @param value - */ - public void setValue(MongoPropertyDescriptors.MongoPropertyDescriptor descriptor, Object value) { - Assert.notNull(descriptor); - try { - accessor.setPropertyValue(descriptor.getName(), value); - } catch (NotWritablePropertyException e) { - if (!fieldAccess) { - throw e; - } - } - } -} \ No newline at end of file diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoPropertyDescriptors.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoPropertyDescriptors.java deleted file mode 100644 index c579c66aa..000000000 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoPropertyDescriptors.java +++ /dev/null @@ -1,244 +0,0 @@ -/* - * Copyright 2011 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.mongodb.core.convert; - -import java.beans.PropertyDescriptor; -import java.lang.reflect.Method; -import java.lang.reflect.Type; -import java.math.BigInteger; -import java.util.*; - -import org.bson.types.ObjectId; -import org.springframework.beans.BeanUtils; -import org.springframework.util.Assert; -import org.springframework.util.ReflectionUtils; - -/** - * An iterable of {@link MongoPropertyDescriptor}s that allows dedicated access to the {@link MongoPropertyDescriptor} - * that captures the id-property. - * - * @author Oliver Gierke - */ -public class MongoPropertyDescriptors implements Iterable { - - private final Collection descriptors; - private final MongoPropertyDescriptors.MongoPropertyDescriptor idDescriptor; - - /** - * Creates the {@link MongoPropertyDescriptors} for the given type. - * - * @param type - */ - public MongoPropertyDescriptors(Class type) { - - Assert.notNull(type); - Set descriptors = new HashSet(); - MongoPropertyDescriptors.MongoPropertyDescriptor idDesciptor = null; - - for (PropertyDescriptor candidates : BeanUtils.getPropertyDescriptors(type)) { - MongoPropertyDescriptor descriptor = new MongoPropertyDescriptors.MongoPropertyDescriptor(candidates, type); - descriptors.add(descriptor); - if (descriptor.isIdProperty()) { - idDesciptor = descriptor; - } - } - - this.descriptors = Collections.unmodifiableSet(descriptors); - this.idDescriptor = idDesciptor; - } - - /** - * Returns the {@link MongoPropertyDescriptor} for the id property. - * - * @return the idDescriptor - */ - public MongoPropertyDescriptors.MongoPropertyDescriptor getIdDescriptor() { - return idDescriptor; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Iterable#iterator() - */ - public Iterator iterator() { - return descriptors.iterator(); - } - - /** - * Simple value object to have a more suitable abstraction for MongoDB specific property handling. - * - * @author Oliver Gierke - */ - public static class MongoPropertyDescriptor { - - public static Collection> SUPPORTED_ID_CLASSES; - - static { - Set> classes = new HashSet>(); - classes.add(ObjectId.class); - classes.add(String.class); - classes.add(BigInteger.class); - SUPPORTED_ID_CLASSES = Collections.unmodifiableCollection(classes); - } - - private static final String ID_PROPERTY = "id"; - static final String ID_KEY = "_id"; - - private final PropertyDescriptor delegate; - private final Class owningType; - - /** - * Creates a new {@link MongoPropertyDescriptor} for the given {@link PropertyDescriptor}. - * - * @param descriptor - * @param owningType - */ - public MongoPropertyDescriptor(PropertyDescriptor descriptor, Class owningType) { - Assert.notNull(descriptor); - this.delegate = descriptor; - this.owningType = owningType; - } - - /** - * Returns whether the property is the id-property. Will be identified by name for now ({@value #ID_PROPERTY}). - * - * @return - */ - public boolean isIdProperty() { - return ID_PROPERTY.equals(delegate.getName()) || ID_KEY.equals(delegate.getName()); - } - - /** - * Returns whether the property is of one of the supported id types. Currently we support {@link String}, - * {@link ObjectId} and {@link BigInteger}. - * - * @return - */ - public boolean isOfIdType() { - return SUPPORTED_ID_CLASSES.contains(delegate.getPropertyType()); - } - - /** - * Returns the key that shall be used for mapping. Will return {@value #ID_KEY} for the id property and the plain - * name for all other ones. - * - * @return - */ - public String getKeyToMap() { - return isIdProperty() ? ID_KEY : delegate.getName(); - } - - /** - * Returns the name of the property. - * - * @return - */ - public String getName() { - return delegate.getName(); - } - - /** - * Returns whether the underlying property is actually mappable. By default this will exclude the {@literal class} - * property and only include properties with a getter. - * - * @return - */ - public boolean isMappable() { - - boolean isNotClassAttribute = !delegate.getName().equals("class"); - boolean hasGetter = delegate.getReadMethod() != null; - boolean hasField = ReflectionUtils.findField(owningType, delegate.getName()) != null; - - return isNotClassAttribute && hasGetter && hasField; - } - - /** - * Returns the plain property type. - * - * @return - */ - public Class getPropertyType() { - return delegate.getPropertyType(); - } - - /** - * Returns the type type to be set. Will return the setter method's type and fall back to the getter method's return - * type in case no setter is available. Useful for further (generics) inspection. - * - * @return - */ - public Type getTypeToSet() { - - Method method = delegate.getWriteMethod(); - return method == null ? delegate.getReadMethod().getGenericReturnType() : method.getGenericParameterTypes()[0]; - } - - /** - * Returns whther we describe a {@link Map}. - * - * @return - */ - public boolean isMap() { - return Map.class.isAssignableFrom(getPropertyType()); - } - - /** - * Returns whether the descriptor is for a collection. - * - * @return - */ - public boolean isCollection() { - return Collection.class.isAssignableFrom(getPropertyType()); - } - - /** - * Returns whether the descriptor is for an {@link Enum}. - * - * @return - */ - public boolean isEnum() { - return Enum.class.isAssignableFrom(getPropertyType()); - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { - if (obj == this) { - return true; - } - if (obj == null || !getClass().equals(obj.getClass())) { - return false; - } - MongoPropertyDescriptor that = (MongoPropertyDescriptor) obj; - return that.delegate.equals(this.delegate); - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { - return delegate.hashCode(); - } - } -} \ No newline at end of file diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoSimpleTypes.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoSimpleTypes.java index 96c9a6d6b..4e31d7249 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoSimpleTypes.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoSimpleTypes.java @@ -15,6 +15,8 @@ */ package org.springframework.data.mongodb.core.mapping; +import java.math.BigInteger; +import java.util.Collections; import java.util.HashSet; import java.util.Set; @@ -31,14 +33,23 @@ import com.mongodb.DBRef; */ public abstract class MongoSimpleTypes { - private static final Set> MONGO_SIMPLE_TYPES = new HashSet>(); - + public static final Set> SUPPORTED_ID_CLASSES; + static { - MONGO_SIMPLE_TYPES.add(DBRef.class); - MONGO_SIMPLE_TYPES.add(ObjectId.class); - MONGO_SIMPLE_TYPES.add(CodeWScope.class); + Set> classes = new HashSet>(); + classes.add(ObjectId.class); + classes.add(String.class); + classes.add(BigInteger.class); + SUPPORTED_ID_CLASSES = Collections.unmodifiableSet(classes); + + Set> simpleTypes = new HashSet>(); + simpleTypes.add(DBRef.class); + simpleTypes.add(ObjectId.class); + simpleTypes.add(CodeWScope.class); + MONGO_SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes); } + private static final Set> MONGO_SIMPLE_TYPES; public static final SimpleTypeHolder HOLDER = new SimpleTypeHolder(MONGO_SIMPLE_TYPES, true); private MongoSimpleTypes() { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoRepositoryFactoryBean.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoRepositoryFactoryBean.java index 9a8b9b54b..8385b9f73 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoRepositoryFactoryBean.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoRepositoryFactoryBean.java @@ -29,10 +29,10 @@ import org.springframework.data.domain.Sort; import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.data.mongodb.core.convert.MongoPropertyDescriptors.MongoPropertyDescriptor; import org.springframework.data.mongodb.core.index.Index; import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; +import org.springframework.data.mongodb.core.mapping.MongoSimpleTypes; import org.springframework.data.mongodb.core.query.Order; import org.springframework.data.querydsl.QueryDslPredicateExecutor; import org.springframework.data.repository.Repository; @@ -239,9 +239,9 @@ public class MongoRepositoryFactoryBean, S, ID exten protected void validate(RepositoryMetadata metadata) { Class idClass = metadata.getIdClass(); - if (!MongoPropertyDescriptor.SUPPORTED_ID_CLASSES.contains(idClass)) { + if (!MongoSimpleTypes.SUPPORTED_ID_CLASSES.contains(idClass)) { throw new IllegalArgumentException(String.format("Unsupported id class! Only %s are supported!", - StringUtils.collectionToCommaDelimitedString(MongoPropertyDescriptor.SUPPORTED_ID_CLASSES))); + StringUtils.collectionToCommaDelimitedString(MongoSimpleTypes.SUPPORTED_ID_CLASSES))); } }