Skip to content

Commit ed4edba

Browse files
committed
Polishing.
Formatting. Original pull request #2000 See #1998
1 parent 6dab06a commit ed4edba

9 files changed

+175
-161
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2025 the original author or authors.
2+
* Copyright 2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,32 +23,38 @@
2323
import org.springframework.util.Assert;
2424

2525
/**
26-
* Abstract {@link RowMapper} that delegates the actual mapping logic to a {@link AbstractDelegatingRowMapper#delegate delegate}
26+
* Abstract {@link RowMapper} that delegates the actual mapping logic to a {@link AbstractDelegatingRowMapper#delegate
27+
* delegate}
2728
*
2829
* @author Mikhail Polivakha
30+
* @since 4.0
2931
*/
3032
public abstract class AbstractDelegatingRowMapper<T> implements RowMapper<T> {
3133

32-
private final RowMapper<T> delegate;
34+
private final RowMapper<T> delegate;
3335

34-
protected AbstractDelegatingRowMapper(RowMapper<T> delegate) {
35-
Assert.notNull(delegate, "Delegating RowMapper cannot be null");
36+
protected AbstractDelegatingRowMapper(RowMapper<T> delegate) {
3637

37-
this.delegate = delegate;
38-
}
38+
Assert.notNull(delegate, "Delegating RowMapper cannot be null");
3939

40-
@Override
41-
public T mapRow(ResultSet rs, int rowNum) throws SQLException {
42-
T intermediate = delegate.mapRow(rs, rowNum);
43-
return postProcessMapping(intermediate);
44-
}
40+
this.delegate = delegate;
41+
}
4542

46-
/**
47-
* The post-processing callback for implementations.
48-
*
49-
* @return the mapped entity after applying post-processing logic
50-
*/
51-
protected T postProcessMapping(@Nullable T object) {
52-
return object;
53-
}
43+
@Override
44+
@Nullable
45+
public T mapRow(ResultSet rs, int rowNum) throws SQLException {
46+
47+
T intermediate = delegate.mapRow(rs, rowNum);
48+
return postProcessMapping(intermediate);
49+
}
50+
51+
/**
52+
* The post-processing callback for implementations.
53+
*
54+
* @return the mapped entity after applying post-processing logic
55+
*/
56+
@Nullable
57+
protected T postProcessMapping(@Nullable T object) {
58+
return object;
59+
}
5460
}

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/AbstractJdbcQuery.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.springframework.data.jdbc.repository.query;
1717

18-
import java.sql.ResultSet;
19-
import java.sql.SQLException;
2018
import java.util.List;
2119
import java.util.function.Supplier;
2220
import java.util.stream.Stream;
@@ -156,7 +154,7 @@ private <T> JdbcQueryExecution<T> createSingleReadingQueryExecution(ResultSetExt
156154
* @deprecated Use {@link org.springframework.data.jdbc.repository.query.RowMapperFactory} instead
157155
*/
158156
@Deprecated(forRemoval = true, since = "3.4.4")
159-
public interface RowMapperFactory extends org.springframework.data.jdbc.repository.query.RowMapperFactory { }
157+
public interface RowMapperFactory extends org.springframework.data.jdbc.repository.query.RowMapperFactory {}
160158

161159
/**
162160
* Delegating {@link RowMapper} that reads a row into {@code T} and converts it afterwards into {@code Object}.
@@ -166,8 +164,8 @@ public interface RowMapperFactory extends org.springframework.data.jdbc.reposito
166164
* @deprecated use {@link org.springframework.data.jdbc.repository.query.ConvertingRowMapper} instead
167165
*/
168166
@Deprecated(forRemoval = true, since = "3.4.4")
169-
protected static class ConvertingRowMapper<T> extends
170-
org.springframework.data.jdbc.repository.query.ConvertingRowMapper {
167+
protected static class ConvertingRowMapper<T>
168+
extends org.springframework.data.jdbc.repository.query.ConvertingRowMapper {
171169

172170
@SuppressWarnings("unchecked")
173171
public ConvertingRowMapper(RowMapper<T> delegate, Converter<Object, Object> converter) {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2025 the original author or authors.
2+
* Copyright 2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,35 +25,41 @@
2525
import org.springframework.lang.Nullable;
2626

2727
/**
28-
* Delegating {@link RowMapper} implementation that applies post-processing logic
29-
* after the {@link RowMapper#mapRow(ResultSet, int)}. In particular, it emits the
30-
* {@link AfterConvertEvent} event and invokes the {@link AfterConvertCallback} callbacks.
28+
* Delegating {@link RowMapper} implementation that applies post-processing logic after the
29+
* {@link RowMapper#mapRow(ResultSet, int)}. In particular, it emits the {@link AfterConvertEvent} event and invokes the
30+
* {@link AfterConvertCallback} callbacks.
3131
*
3232
* @author Mark Paluch
3333
* @author Mikhail Polivakha
34+
* @since 4.0
3435
*/
3536
public class CallbackCapableRowMapper<T> extends AbstractDelegatingRowMapper<T> {
3637

37-
private final ApplicationEventPublisher publisher;
38-
private final @Nullable EntityCallbacks callbacks;
38+
private final ApplicationEventPublisher publisher;
39+
private final @Nullable EntityCallbacks callbacks;
3940

40-
public CallbackCapableRowMapper(RowMapper<T> delegate, ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks) {
41-
super(delegate);
42-
this.publisher = publisher;
43-
this.callbacks = callbacks;
44-
}
41+
public CallbackCapableRowMapper(RowMapper<T> delegate, ApplicationEventPublisher publisher,
42+
@Nullable EntityCallbacks callbacks) {
4543

46-
@Override
47-
public T postProcessMapping(@Nullable T object) {
48-
if (object != null) {
44+
super(delegate);
4945

50-
publisher.publishEvent(new AfterConvertEvent<>(object));
46+
this.publisher = publisher;
47+
this.callbacks = callbacks;
48+
}
5149

52-
if (callbacks != null) {
53-
return callbacks.callback(AfterConvertCallback.class, object);
54-
}
50+
@Override
51+
@Nullable
52+
public T postProcessMapping(@Nullable T object) {
5553

56-
}
57-
return object;
58-
}
54+
if (object != null) {
55+
56+
publisher.publishEvent(new AfterConvertEvent<>(object));
57+
58+
if (callbacks != null) {
59+
return callbacks.callback(AfterConvertCallback.class, object);
60+
}
61+
62+
}
63+
return object;
64+
}
5965
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2025 the original author or authors.
2+
* Copyright 2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,24 +20,26 @@
2020
import org.springframework.lang.Nullable;
2121

2222
/**
23-
* Delegating {@link RowMapper} that reads a row into {@code T} and converts it afterwards into {@code Object}.
23+
* Delegating {@link RowMapper} that reads a row into {@code T} and converts it afterward into {@code Object}.
2424
*
2525
* @author Mark Paluch
2626
* @author Mikhail Polivakha
27-
*
28-
* @since 2.3
27+
* @since 4.0
2928
*/
3029
public class ConvertingRowMapper extends AbstractDelegatingRowMapper<Object> {
3130

32-
private final Converter<Object, Object> converter;
31+
private final Converter<Object, Object> converter;
32+
33+
public ConvertingRowMapper(RowMapper<Object> delegate, Converter<Object, Object> converter) {
34+
35+
super(delegate);
3336

34-
public ConvertingRowMapper(RowMapper<Object> delegate, Converter<Object, Object> converter) {
35-
super(delegate);
36-
this.converter = converter;
37-
}
37+
this.converter = converter;
38+
}
3839

39-
@Override
40-
public Object postProcessMapping(@Nullable Object object) {
41-
return object != null ? converter.convert(object) : null;
42-
}
40+
@Override
41+
@Nullable
42+
public Object postProcessMapping(@Nullable Object object) {
43+
return object != null ? converter.convert(object) : null;
44+
}
4345
}

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/DefaultRowMapperFactory.java

+42-43
Original file line numberDiff line numberDiff line change
@@ -27,64 +27,63 @@
2727
import org.springframework.jdbc.core.SingleColumnRowMapper;
2828

2929
/**
30-
* Default implementation of {@link RowMapperFactory}. Honors the custom mappings defined
31-
* in {@link QueryMappingConfiguration}.
30+
* Default implementation of {@link RowMapperFactory}. Honors the custom mappings defined in
31+
* {@link QueryMappingConfiguration}.
3232
* <p>
33-
* This implementation is not capable of loading the {@link RowMapper} or {@link ResultSetExtractor}
34-
* by reference via corresponding methods from {@link RowMapperFactory}.
33+
* This implementation is not capable of loading the {@link RowMapper} or {@link ResultSetExtractor} by reference via
34+
* corresponding methods from {@link RowMapperFactory}.
3535
*
3636
* @implNote Public APIs of this class are thread-safe.
3737
* @author Mikhail Polivakha
38+
* @since 4.0
3839
*/
3940
public class DefaultRowMapperFactory implements RowMapperFactory {
4041

41-
private final RelationalMappingContext context;
42-
private final JdbcConverter converter;
43-
private final QueryMappingConfiguration queryMappingConfiguration;
44-
private final EntityCallbacks entityCallbacks;
45-
private final ApplicationEventPublisher publisher;
42+
private final RelationalMappingContext context;
43+
private final JdbcConverter converter;
44+
private final QueryMappingConfiguration queryMappingConfiguration;
45+
private final EntityCallbacks entityCallbacks;
46+
private final ApplicationEventPublisher publisher;
4647

47-
public DefaultRowMapperFactory(
48-
RelationalMappingContext context,
49-
JdbcConverter converter,
50-
QueryMappingConfiguration queryMappingConfiguration,
51-
EntityCallbacks entityCallbacks,
52-
ApplicationEventPublisher publisher
53-
) {
54-
this.context = context;
55-
this.converter = converter;
56-
this.queryMappingConfiguration = queryMappingConfiguration;
57-
this.entityCallbacks = entityCallbacks;
58-
this.publisher = publisher;
59-
}
48+
public DefaultRowMapperFactory(RelationalMappingContext context, JdbcConverter converter,
49+
QueryMappingConfiguration queryMappingConfiguration, EntityCallbacks entityCallbacks,
50+
ApplicationEventPublisher publisher) {
6051

61-
@Override
62-
@SuppressWarnings("unchecked")
63-
public RowMapper<Object> create(Class<?> returnedObjectType) {
52+
this.context = context;
53+
this.converter = converter;
54+
this.queryMappingConfiguration = queryMappingConfiguration;
55+
this.entityCallbacks = entityCallbacks;
56+
this.publisher = publisher;
57+
}
6458

65-
RelationalPersistentEntity<?> persistentEntity = context.getPersistentEntity(returnedObjectType);
59+
@Override
60+
@SuppressWarnings("unchecked")
61+
public RowMapper<Object> create(Class<?> returnedObjectType) {
6662

67-
if (persistentEntity == null) {
68-
return (RowMapper<Object>) SingleColumnRowMapper.newInstance(returnedObjectType,
69-
converter.getConversionService());
70-
}
63+
RelationalPersistentEntity<?> persistentEntity = context.getPersistentEntity(returnedObjectType);
7164

72-
return (RowMapper<Object>) determineDefaultMapper(returnedObjectType);
73-
}
65+
if (persistentEntity == null) {
7466

75-
private RowMapper<?> determineDefaultMapper(Class<?> returnedObjectType) {
67+
return (RowMapper<Object>) SingleColumnRowMapper.newInstance(returnedObjectType,
68+
converter.getConversionService());
69+
}
7670

77-
RowMapper<?> configuredQueryMapper = queryMappingConfiguration.getRowMapper(returnedObjectType);
71+
return (RowMapper<Object>) determineDefaultMapper(returnedObjectType);
72+
}
7873

79-
if (configuredQueryMapper != null) {
80-
return configuredQueryMapper;
81-
}
74+
private RowMapper<?> determineDefaultMapper(Class<?> returnedObjectType) {
8275

83-
EntityRowMapper<?> defaultEntityRowMapper = new EntityRowMapper<>( //
84-
context.getRequiredPersistentEntity(returnedObjectType), //
85-
converter //
86-
);
76+
RowMapper<?> configuredQueryMapper = queryMappingConfiguration.getRowMapper(returnedObjectType);
8777

88-
return new CallbackCapableRowMapper<>(defaultEntityRowMapper, publisher, entityCallbacks);
89-
}
78+
if (configuredQueryMapper != null) {
79+
return configuredQueryMapper;
80+
}
81+
82+
EntityRowMapper<?> defaultEntityRowMapper = new EntityRowMapper<>( //
83+
context.getRequiredPersistentEntity(returnedObjectType), //
84+
converter //
85+
);
86+
87+
return new CallbackCapableRowMapper<>(defaultEntityRowMapper, publisher, entityCallbacks);
88+
}
9089
}

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/PartTreeJdbcQuery.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ public PartTreeJdbcQuery(RelationalMappingContext context, JdbcQueryMethod query
9797
* @since 2.3
9898
*/
9999
public PartTreeJdbcQuery(RelationalMappingContext context, JdbcQueryMethod queryMethod, Dialect dialect,
100-
JdbcConverter converter, NamedParameterJdbcOperations operations, org.springframework.data.jdbc.repository.query.RowMapperFactory rowMapperFactory) {
100+
JdbcConverter converter, NamedParameterJdbcOperations operations,
101+
org.springframework.data.jdbc.repository.query.RowMapperFactory rowMapperFactory) {
101102

102103
super(queryMethod, operations);
103104

@@ -160,13 +161,13 @@ private JdbcQueryExecution<?> getQueryExecution(ResultProcessor processor,
160161
JdbcQueryExecution<?> queryExecution = getJdbcQueryExecution(extractor, rowMapper);
161162

162163
if (getQueryMethod().isSliceQuery()) {
163-
//noinspection unchecked
164+
// noinspection unchecked
164165
return new SliceQueryExecution<>((JdbcQueryExecution<Collection<Object>>) queryExecution, accessor.getPageable());
165166
}
166167

167168
if (getQueryMethod().isPageQuery()) {
168169

169-
//noinspection unchecked
170+
// noinspection unchecked
170171
return new PageQueryExecution<>((JdbcQueryExecution<Collection<Object>>) queryExecution, accessor.getPageable(),
171172
() -> {
172173

@@ -291,7 +292,8 @@ class CachedRowMapperFactory implements Supplier<RowMapper<?>> {
291292
private final Lazy<RowMapper<?>> rowMapper;
292293
private final Function<ResultProcessor, RowMapper<?>> rowMapperFunction;
293294

294-
public CachedRowMapperFactory(PartTree tree, org.springframework.data.jdbc.repository.query.RowMapperFactory rowMapperFactory, RelationalConverter converter,
295+
public CachedRowMapperFactory(PartTree tree,
296+
org.springframework.data.jdbc.repository.query.RowMapperFactory rowMapperFactory, RelationalConverter converter,
295297
ResultProcessor defaultResultProcessor) {
296298

297299
this.rowMapperFunction = processor -> {
@@ -301,8 +303,8 @@ public CachedRowMapperFactory(PartTree tree, org.springframework.data.jdbc.repos
301303
}
302304
Converter<Object, Object> resultProcessingConverter = new ResultProcessingConverter(processor,
303305
converter.getMappingContext(), converter.getEntityInstantiators());
304-
return new org.springframework.data.jdbc.repository.query.ConvertingRowMapper(rowMapperFactory.create(processor.getReturnedType().getDomainType()),
305-
resultProcessingConverter);
306+
return new org.springframework.data.jdbc.repository.query.ConvertingRowMapper(
307+
rowMapperFactory.create(processor.getReturnedType().getDomainType()), resultProcessingConverter);
306308
};
307309

308310
this.rowMapper = Lazy.of(() -> this.rowMapperFunction.apply(defaultResultProcessor));

0 commit comments

Comments
 (0)