From 699d380af962841917efccf1c4c9b478b019d190 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 25 May 2023 14:24:44 +0200 Subject: [PATCH] Polishing. Add missing generics. Inline superfluous methods. See #1384 --- .../convert/MappingCassandraConverter.java | 36 +++++-------------- .../query/AbstractCassandraQuery.java | 17 ++------- .../query/AbstractReactiveCassandraQuery.java | 21 +++-------- .../CassandraRepositoryQuerySupport.java | 7 ++++ 4 files changed, 21 insertions(+), 60 deletions(-) diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/convert/MappingCassandraConverter.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/convert/MappingCassandraConverter.java index 5d2998da6..5e9cbda1b 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/convert/MappingCassandraConverter.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/convert/MappingCassandraConverter.java @@ -283,6 +283,7 @@ private Class transformClassToBeanClassLoaderClass(Class entity) { } @Override + @SuppressWarnings("unchecked") public R project(EntityProjection projection, Row row) { if (!projection.isProjection()) { @@ -665,7 +666,7 @@ private void writeWhereFromObject(Object source, Where sink, CassandraPersistent ? getMappingContext().getRequiredPersistentEntity(compositeIdProperty) : entity; - writeWhere(MapId.class.cast(id), sink, whereEntity); + writeWhere((MapId) id, sink, whereEntity); return; } @@ -725,18 +726,6 @@ private void writeWhere(MapId id, Where sink, CassandraPersistentEntity entit } } - private void writeWhere(ConvertingPropertyAccessor accessor, Where sink, CassandraPersistentEntity entity) { - - Assert.isTrue(entity.isCompositePrimaryKey(), - () -> String.format("Entity [%s] is not a composite primary key", entity.getName())); - - for (CassandraPersistentProperty property : entity) { - TypeCodec codec = getCodec(property); - Object value = accessor.getProperty(property, codec.getJavaType().getRawType()); - sink.put(property.getRequiredColumnName(), value); - } - } - private void writeTupleValue(ConvertingPropertyAccessor propertyAccessor, TupleValue tupleValue, CassandraPersistentEntity entity) { @@ -863,7 +852,7 @@ private Class getTargetType(CassandraPersistentProperty property) { */ @Nullable @SuppressWarnings("unchecked") - private T getWriteValue(CassandraPersistentProperty property, ConvertingPropertyAccessor propertyAccessor) { + private T getWriteValue(CassandraPersistentProperty property, ConvertingPropertyAccessor propertyAccessor) { ColumnType cassandraTypeDescriptor = cassandraTypeResolver.resolve(property); @@ -914,7 +903,7 @@ private Object getWriteValue(@Nullable Object value, ColumnType columnType) { return writeMapInternal((Map) value, columnType); } - TypeInformation type = TypeInformation.of((Class) value.getClass()); + TypeInformation type = TypeInformation.of((Class) value.getClass()); TypeInformation actualType = type.getRequiredActualType(); BasicCassandraPersistentEntity entity = getMappingContext().getPersistentEntity(actualType.getType()); @@ -989,7 +978,6 @@ private Object writeMapInternal(Map source, ColumnType type) { * @param requestedTargetType must not be {@literal null}. * @see org.springframework.data.cassandra.core.mapping.CassandraType */ - @SuppressWarnings("unchecked") @Nullable private Object getPotentiallyConvertedSimpleValue(@Nullable Object value, @Nullable Class requestedTargetType) { @@ -1237,7 +1225,7 @@ public T getParameterValue(Parameter paramet * Extension of {@link SpELExpressionParameterValueProvider} to recursively trigger value conversion on the raw * resolved SpEL value. */ - private class ConverterAwareSpELExpressionParameterValueProvider + private static class ConverterAwareSpELExpressionParameterValueProvider extends SpELExpressionParameterValueProvider { private final ConversionContext context; @@ -1450,16 +1438,8 @@ private CassandraPersistentProperty getPersistentProperty(String name, TypeInfor } } - private static class PropertyTranslatingPropertyAccessor implements PersistentPropertyAccessor { - - private final PersistentPropertyAccessor delegate; - private final PersistentPropertyTranslator propertyTranslator; - - private PropertyTranslatingPropertyAccessor(PersistentPropertyAccessor delegate, - PersistentPropertyTranslator propertyTranslator) { - this.delegate = delegate; - this.propertyTranslator = propertyTranslator; - } + private record PropertyTranslatingPropertyAccessor (PersistentPropertyAccessor delegate, + PersistentPropertyTranslator propertyTranslator) implements PersistentPropertyAccessor { static PersistentPropertyAccessor create(PersistentPropertyAccessor delegate, PersistentPropertyTranslator propertyTranslator) { @@ -1549,7 +1529,7 @@ static class MapPersistentPropertyAccessor implements PersistentPropertyAccessor Map map = new LinkedHashMap<>(); @Override - public void setProperty(PersistentProperty persistentProperty, Object o) { + public void setProperty(PersistentProperty persistentProperty, @Nullable Object o) { map.put(persistentProperty.getName(), o); } diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/query/AbstractCassandraQuery.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/query/AbstractCassandraQuery.java index 4d80f983c..097ccdee3 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/query/AbstractCassandraQuery.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/query/AbstractCassandraQuery.java @@ -17,8 +17,6 @@ import org.springframework.core.convert.converter.Converter; import org.springframework.data.cassandra.core.CassandraOperations; -import org.springframework.data.cassandra.core.convert.CassandraConverter; -import org.springframework.data.cassandra.core.mapping.CassandraMappingContext; import org.springframework.data.cassandra.repository.query.CassandraQueryExecution.CollectionExecution; import org.springframework.data.cassandra.repository.query.CassandraQueryExecution.ExistsExecution; import org.springframework.data.cassandra.repository.query.CassandraQueryExecution.ResultProcessingConverter; @@ -31,7 +29,6 @@ import org.springframework.data.repository.query.RepositoryQuery; import org.springframework.data.repository.query.ResultProcessor; import org.springframework.lang.Nullable; -import org.springframework.util.Assert; import com.datastax.oss.driver.api.core.cql.SimpleStatement; import com.datastax.oss.driver.api.core.cql.Statement; @@ -56,7 +53,7 @@ public abstract class AbstractCassandraQuery extends CassandraRepositoryQuerySup */ public AbstractCassandraQuery(CassandraQueryMethod queryMethod, CassandraOperations operations) { - super(queryMethod, toMappingContext(operations)); + super(queryMethod, operations.getConverter().getMappingContext()); this.operations = operations; } @@ -81,7 +78,7 @@ public Object execute(Object[] parameters) { Statement statement = createQuery(parameterAccessor); CassandraQueryExecution queryExecution = getExecution(parameterAccessor, - new ResultProcessingConverter(resultProcessor, toMappingContext(getOperations()), getEntityInstantiators())); + new ResultProcessingConverter(resultProcessor, getMappingContext(), getEntityInstantiators())); Class resultType = resolveResultType(resultProcessor); @@ -170,14 +167,4 @@ private CassandraQueryExecution getExecutionToWrap(CassandraParameterAccessor pa */ protected abstract boolean isModifyingQuery(); - private static CassandraConverter toConverter(CassandraOperations operations) { - - Assert.notNull(operations, "CassandraOperations must not be null"); - - return operations.getConverter(); - } - - private static CassandraMappingContext toMappingContext(CassandraOperations operations) { - return toConverter(operations).getMappingContext(); - } } diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/query/AbstractReactiveCassandraQuery.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/query/AbstractReactiveCassandraQuery.java index 2ff8df085..d9f3851fa 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/query/AbstractReactiveCassandraQuery.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/query/AbstractReactiveCassandraQuery.java @@ -22,8 +22,6 @@ import org.springframework.data.cassandra.ReactiveResultSet; import org.springframework.data.cassandra.core.CassandraOperations; import org.springframework.data.cassandra.core.ReactiveCassandraOperations; -import org.springframework.data.cassandra.core.convert.CassandraConverter; -import org.springframework.data.cassandra.core.mapping.CassandraMappingContext; import org.springframework.data.cassandra.repository.query.ReactiveCassandraQueryExecution.CollectionExecution; import org.springframework.data.cassandra.repository.query.ReactiveCassandraQueryExecution.ExistsExecution; import org.springframework.data.cassandra.repository.query.ReactiveCassandraQueryExecution.ResultProcessingConverter; @@ -33,7 +31,6 @@ import org.springframework.data.repository.query.ParameterAccessor; import org.springframework.data.repository.query.RepositoryQuery; import org.springframework.data.repository.query.ResultProcessor; -import org.springframework.util.Assert; import com.datastax.oss.driver.api.core.cql.SimpleStatement; @@ -58,7 +55,7 @@ public abstract class AbstractReactiveCassandraQuery extends CassandraRepository */ public AbstractReactiveCassandraQuery(ReactiveCassandraQueryMethod method, ReactiveCassandraOperations operations) { - super(method, getRequiredMappingContext(operations)); + super(method, operations.getConverter().getMappingContext()); this.operations = operations; } @@ -83,8 +80,8 @@ private Publisher executeLater(ReactiveCassandraParameterAccessor parame Mono statement = createQuery(parameterAccessor); ResultProcessor resultProcessor = getQueryMethod().getResultProcessor().withDynamicProjection(parameterAccessor); - ReactiveCassandraQueryExecution queryExecution = getExecution(parameterAccessor, new ResultProcessingConverter( - resultProcessor, getRequiredMappingContext(getReactiveCassandraOperations()), getEntityInstantiators())); + ReactiveCassandraQueryExecution queryExecution = getExecution(parameterAccessor, + new ResultProcessingConverter(resultProcessor, getMappingContext(), getEntityInstantiators())); Class resultType = resolveResultType(resultProcessor); @@ -94,7 +91,7 @@ private Publisher executeLater(ReactiveCassandraParameterAccessor parame private Class resolveResultType(ResultProcessor resultProcessor) { CassandraReturnedType returnedType = new CassandraReturnedType(resultProcessor.getReturnedType(), - getRequiredConverter(getReactiveCassandraOperations()).getCustomConversions()); + getReactiveCassandraOperations().getConverter().getCustomConversions()); return returnedType.getResultType(); } @@ -173,14 +170,4 @@ private ReactiveCassandraQueryExecution getExecutionToWrap(CassandraParameterAcc */ protected abstract boolean isModifyingQuery(); - private static CassandraConverter getRequiredConverter(ReactiveCassandraOperations operations) { - - Assert.notNull(operations, "ReactiveCassandraOperations must not be null"); - - return operations.getConverter(); - } - - private static CassandraMappingContext getRequiredMappingContext(ReactiveCassandraOperations operations) { - return getRequiredConverter(operations).getMappingContext(); - } } diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/query/CassandraRepositoryQuerySupport.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/query/CassandraRepositoryQuerySupport.java index 87641ffb1..48142f4d9 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/query/CassandraRepositoryQuerySupport.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/query/CassandraRepositoryQuerySupport.java @@ -49,6 +49,8 @@ public abstract class CassandraRepositoryQuerySupport implements RepositoryQuery private final QueryStatementCreator queryStatementCreator; + private final MappingContext, CassandraPersistentProperty> mappingContext; + /** * Create a new {@link AbstractCassandraQuery} from the given {@link CassandraQueryMethod} and * {@link CassandraOperations}. @@ -78,6 +80,7 @@ public CassandraRepositoryQuerySupport(CassandraQueryMethod queryMethod, this.queryMethod = queryMethod; this.instantiators = new EntityInstantiators(); this.queryStatementCreator = new QueryStatementCreator(queryMethod, mappingContext); + this.mappingContext = mappingContext; } @Override @@ -93,6 +96,10 @@ protected QueryStatementCreator getQueryStatementCreator() { return this.queryStatementCreator; } + protected MappingContext, CassandraPersistentProperty> getMappingContext() { + return mappingContext; + } + class CassandraReturnedType { private final ReturnedType returnedType;