Skip to content

Commit ee350f5

Browse files
committed
Add better comments, refine naming.
1 parent 8a0447a commit ee350f5

File tree

2 files changed

+112
-15
lines changed

2 files changed

+112
-15
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/aot/JdbcCodeBlocks.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ private CodeBlock update(Builder builder, Class<?> returnType) {
706706
it.add("getJdbcOperations().update($L, $L)", queryVariableName, parameterSourceVariableName);
707707
});
708708

709-
builder.add(ReturnStatement.returning(returnType) //
709+
builder.add(ReturnStatement.forType(returnType) //
710710
.whenBoolean("$L != 0", result) //
711711
.whenBoxedLong("(long) $L", result) //
712712
.otherwise("$L", result)//
@@ -726,7 +726,7 @@ private CodeBlock delete(Builder builder, String rowMapper, String result, TypeN
726726

727727
builder.addStatement("$L.forEach(getOperations()::delete)", result);
728728

729-
builder.add(ReturnStatement.returning(returnType) //
729+
builder.add(ReturnStatement.forType(returnType) //
730730
.when(Collection.class.isAssignableFrom(context.getReturnType().toClass()), "($T) convertMany($L, $T.class)",
731731
context.getReturnTypeName(), result, queryResultType) //
732732
.when(context.getRepositoryInformation().getDomainType(),
@@ -744,7 +744,7 @@ private CodeBlock count(Builder builder, String result, Class<?> returnType, Typ
744744
builder.addStatement("$1T $2L = queryForObject($3L, $4L, new $5T<>($1T.class))", Number.class, result,
745745
queryVariableName, parameterSourceVariableName, SingleColumnRowMapper.class);
746746

747-
builder.add(ReturnStatement.returning(returnType) //
747+
builder.add(ReturnStatement.forType(returnType) //
748748
.number(result) //
749749
.otherwise("($T) convertOne($L, $T.class)", context.getReturnTypeName(), result, queryResultType) //
750750
.build());

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/aot/ReturnStatement.java

Lines changed: 109 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,57 +20,150 @@
2020
import org.springframework.util.ClassUtils;
2121

2222
/**
23+
* Utility for building return statements for code generation purposes.
24+
* <p>
25+
* Provides a fluent API to construct return statements based on the given return type. Typically used in AOT scenarios
26+
* to generate code fragments for repository methods.
27+
*
2328
* @author Mark Paluch
2429
*/
2530
class ReturnStatement {
2631

27-
public static ReturnStatementBuilder returning(Class<?> returnType) {
32+
/**
33+
* Create a builder for a return statement targeting the given return type.
34+
*
35+
* @param returnType the method return type
36+
* @return a new {@code ReturnStatementBuilder}
37+
*/
38+
public static ReturnStatementBuilder forType(Class<?> returnType) {
2839
return new ReturnStatementBuilder(returnType);
2940
}
3041

42+
/**
43+
* Builder for constructing return statements based on the target return type.
44+
*/
3145
static class ReturnStatementBuilder {
3246

3347
private final Class<?> returnType;
3448
private final CodeBlock.Builder builder = CodeBlock.builder();
3549
private boolean hasReturn = false;
3650

51+
/**
52+
* Create a new builder for the given return type.
53+
*
54+
* @param returnType the method return type
55+
*/
3756
private ReturnStatementBuilder(Class<?> returnType) {
38-
3957
this.returnType = returnType;
58+
59+
// consider early return cases for Void and void.
4060
whenBoxed(Void.class, "null");
41-
hasReturn = ReflectionUtils.isVoid(returnType);
61+
this.hasReturn = ReflectionUtils.isVoid(returnType);
62+
}
63+
64+
/**
65+
* Add return statements for numeric types if the given {@code resultToReturn} points to a {@link Number}. Considers
66+
* primitive and boxed {@code int} and {@code long} type return paths and that {@code resultToReturn} can be
67+
* {@literal null}.
68+
*
69+
* @param resultToReturn the argument or variable name holding the result.
70+
* @return {@code this} builder.
71+
*/
72+
public ReturnStatementBuilder number(String resultToReturn) {
73+
return whenBoxedLong("$1L != null ? $1L.longValue() : null", resultToReturn)
74+
.whenLong("$1L != null ? $1L.longValue() : 0L", resultToReturn)
75+
.whenBoxedInteger("$1L != null ? $1L.intValue() : null", resultToReturn)
76+
.whenInt("$1L != null ? $1L.intValue() : 0", resultToReturn);
4277
}
4378

79+
/**
80+
* Add a return statement if the return type is boolean (primitive or box type).
81+
*
82+
* @param format the code format string.
83+
* @param args the format arguments.
84+
* @return {@code this} builder.
85+
*/
4486
public ReturnStatementBuilder whenBoolean(String format, Object... args) {
4587
return when(returnType == boolean.class || returnType == Boolean.class, format, args);
4688
}
4789

90+
/**
91+
* Add a return statement if the return type is {@link Long} (boxed {@code long} type).
92+
*
93+
* @param format the code format string.
94+
* @param args the format arguments.
95+
* @return {@code this} builder.
96+
*/
4897
public ReturnStatementBuilder whenBoxedLong(String format, Object... args) {
4998
return whenBoxed(long.class, format, args);
5099
}
51100

101+
/**
102+
* Add a return statement if the return type is a primitive {@code long} type.
103+
*
104+
* @param format the code format string.
105+
* @param args the format arguments.
106+
* @return {@code this} builder.
107+
*/
52108
public ReturnStatementBuilder whenLong(String format, Object... args) {
53109
return when(returnType == long.class, format, args);
54110
}
55111

112+
/**
113+
* Add a return statement if the return type is {@link Integer} (boxed {@code int} type).
114+
*
115+
* @param format the code format string.
116+
* @param args the format arguments.
117+
* @return {@code this} builder.
118+
*/
56119
public ReturnStatementBuilder whenBoxedInteger(String format, Object... args) {
57120
return whenBoxed(int.class, format, args);
58121
}
59122

123+
/**
124+
* Add a return statement if the return type is a primitive {@code int} type.
125+
*
126+
* @param format the code format string.
127+
* @param args the format arguments.
128+
* @return {@code this} builder.
129+
*/
60130
public ReturnStatementBuilder whenInt(String format, Object... args) {
61131
return when(returnType == int.class, format, args);
62132
}
63133

134+
/**
135+
* Add a return statement if the return type matches the given boxed wrapper type.
136+
*
137+
* @param primitiveOrWrapper the primitive or wrapper type.
138+
* @param format the code format string.
139+
* @param args the format arguments.
140+
* @return {@code this} builder.
141+
*/
64142
public ReturnStatementBuilder whenBoxed(Class<?> primitiveOrWrapper, String format, Object... args) {
65-
66143
Class<?> primitiveWrapper = ClassUtils.resolvePrimitiveIfNecessary(primitiveOrWrapper);
67144
return when(returnType == primitiveWrapper, format, args);
68145
}
69146

147+
/**
148+
* Add a return statement if the declared return type is assignable from the given {@code returnType}.
149+
*
150+
* @param returnType the candidate return type.
151+
* @param format the code format string.
152+
* @param args the format arguments
153+
* @return {@code this} builder.
154+
*/
70155
public ReturnStatementBuilder when(Class<?> returnType, String format, Object... args) {
71156
return when(this.returnType.isAssignableFrom(returnType), format, args);
72157
}
73158

159+
/**
160+
* Add a return statement if the given condition is {@code true}.
161+
*
162+
* @param condition the condition to evaluate.
163+
* @param format the code format string.
164+
* @param args the format arguments.
165+
* @return {@code this} builder.
166+
*/
74167
public ReturnStatementBuilder when(boolean condition, String format, Object... args) {
75168

76169
if (hasReturn) {
@@ -85,18 +178,22 @@ public ReturnStatementBuilder when(boolean condition, String format, Object... a
85178
return this;
86179
}
87180

181+
/**
182+
* Add a fallback return statement if no previous return statement was added.
183+
*
184+
* @param format the code format string.
185+
* @param args the format arguments.
186+
* @return {@code this} builder.
187+
*/
88188
public ReturnStatementBuilder otherwise(String format, Object... args) {
89189
return when(!hasReturn, format, args);
90190
}
91191

92-
public ReturnStatementBuilder number(String resultVariableName) {
93-
94-
return whenBoxedLong("$1L != null ? $1L.longValue() : null", resultVariableName) //
95-
.whenLong("$1L != null ? $1L.longValue() : 0L", resultVariableName) //
96-
.whenBoxedInteger("$1L != null ? $1L.intValue() : null", resultVariableName) //
97-
.whenInt("$1L != null ? $1L.intValue() : 0", resultVariableName);
98-
}
99-
192+
/**
193+
* Build the code block representing the return statement.
194+
*
195+
* @return the resulting {@code CodeBlock}
196+
*/
100197
public CodeBlock build() {
101198
return builder.build();
102199
}

0 commit comments

Comments
 (0)