Skip to content

Commit 68cbfb6

Browse files
christophstroblmp911de
authored andcommitted
Polishing.
A round of minor code style improvements. Original Pull Request: #3695
1 parent b67f551 commit 68cbfb6

15 files changed

+48
-69
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/domain/JpaSort.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,14 @@ public <A extends Attribute<S, U>, U> Path<S, U> dot(A attribute) {
281281
* @return
282282
*/
283283
public <P extends PluralAttribute<S, ?, U>, U> Path<S, U> dot(P attribute) {
284-
return new Path<S, U>(add(attribute));
284+
return new Path<>(add(attribute));
285285
}
286286

287287
private List<Attribute<?, ?>> add(Attribute<?, ?> attribute) {
288288

289289
Assert.notNull(attribute, "Attribute must not be null");
290290

291-
List<Attribute<?, ?>> newAttributes = new ArrayList<Attribute<?, ?>>(attributes.size() + 1);
291+
List<Attribute<?, ?>> newAttributes = new ArrayList<>(attributes.size() + 1);
292292
newAttributes.addAll(attributes);
293293
newAttributes.add(attribute);
294294
return newAttributes;

spring-data-jpa/src/main/java/org/springframework/data/jpa/mapping/JpaPersistentPropertyImpl.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,9 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
5757

5858
static {
5959

60-
Set<Class<? extends Annotation>> annotations = new HashSet<>();
61-
annotations.add(OneToMany.class);
62-
annotations.add(OneToOne.class);
63-
annotations.add(ManyToMany.class);
64-
annotations.add(ManyToOne.class);
60+
Set<Class<? extends Annotation>> annotations;
6561

66-
ASSOCIATION_ANNOTATIONS = Collections.unmodifiableSet(annotations);
62+
ASSOCIATION_ANNOTATIONS = Set.of(OneToMany.class, OneToOne.class, ManyToMany.class, ManyToOne.class);
6763

6864
annotations = new HashSet<>();
6965
annotations.add(Id.class);
@@ -107,7 +103,7 @@ public JpaPersistentPropertyImpl(JpaMetamodel metamodel, Property property,
107103
this.associationTargetType = detectAssociationTargetType();
108104
this.updateable = detectUpdatability();
109105

110-
this.isIdProperty = Lazy.of(() -> ID_ANNOTATIONS.stream().anyMatch(it -> isAnnotationPresent(it)) //
106+
this.isIdProperty = Lazy.of(() -> ID_ANNOTATIONS.stream().anyMatch(this::isAnnotationPresent) //
111107
|| metamodel.isSingleIdAttribute(getOwner().getType(), getName(), getType()));
112108
this.isEntity = Lazy.of(() -> metamodel.isMappedType(getActualType()));
113109
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* JPA specific support projection support.
3+
*/
4+
@org.springframework.lang.NonNullApi
5+
package org.springframework.data.jpa.projection;

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/AbstractStringBasedJpaQuery.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ public AbstractStringBasedJpaQuery(JpaQueryMethod method, EntityManager em, Stri
9292
return query.deriveCountQuery(method.getCountQueryProjection());
9393
});
9494

95-
this.countParameterBinder = Lazy.of(() -> {
96-
return this.createBinder(this.countQuery.get());
97-
});
95+
this.countParameterBinder = Lazy.of(() -> this.createBinder(this.countQuery.get()));
9896

9997
this.queryRewriter = queryRewriter;
10098

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JSqlParserQueryEnhancer.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ public JSqlParserQueryEnhancer(DeclaredQuery query) {
9898
/**
9999
* Parses a query string with JSqlParser.
100100
*
101-
* @param query the query to parse
101+
* @param sql the query to parse
102+
* @param classOfT the query to parse
102103
* @return the parsed query
103104
*/
104105
static <T extends Statement> T parseStatement(String sql, Class<T> classOfT) {
@@ -510,7 +511,7 @@ private static boolean onlyASingleColumnProjection(List<SelectItem<?>> projectio
510511
* </ul>
511512
*/
512513
enum ParsedType {
513-
DELETE, UPDATE, SELECT, INSERT, MERGE, OTHER;
514+
DELETE, UPDATE, SELECT, INSERT, MERGE, OTHER
514515
}
515516

516517
/**

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategy.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ private static class DeclaredQueryLookupStrategy extends AbstractQueryLookupStra
143143
*
144144
* @param em must not be {@literal null}.
145145
* @param queryMethodFactory must not be {@literal null}.
146-
* @param evaluationContextProvider must not be {@literal null}.
146+
* @param delegate must not be {@literal null}.
147+
* @param queryRewriterProvider must not be {@literal null}.
147148
*/
148149
public DeclaredQueryLookupStrategy(EntityManager em, JpaQueryMethodFactory queryMethodFactory,
149150
ValueExpressionDelegate delegate, QueryRewriterProvider queryRewriterProvider) {

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpqlQueryBuilder.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -848,9 +848,7 @@ public String getAlias(Origin source) {
848848
*/
849849
public String getAlias(Origin source) {
850850

851-
return aliases.computeIfAbsent(source, it -> JpqlQueryBuilder.getAlias(source.getName(), s -> {
852-
return !aliases.containsValue(s);
853-
}, () -> "join_" + (counter++)));
851+
return aliases.computeIfAbsent(source, it -> JpqlQueryBuilder.getAlias(source.getName(), s -> !aliases.containsValue(s), () -> "join_" + (counter++)));
854852
}
855853

856854
/**

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/KeysetScrollDelegate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public Sort createSort(Sort sort, JpaEntityInformation<?, ?> entity) {
158158
}
159159

160160
/**
161-
* Reverse scrolling variant applying {@link Direction#Backward}. In reverse scrolling, we need to flip directions for
161+
* Reverse scrolling variant applying {@link Direction#BACKWARD}. In reverse scrolling, we need to flip directions for
162162
* the actual query so that we do not get everything from the top position and apply the limit but rather flip the
163163
* sort direction, apply the limit and then reverse the result to restore the actual sort order.
164164
*/

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/ParameterBinding.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ static MethodInvocationArgument ofParameter(@Nullable String name, @Nullable Int
667667
/**
668668
* Creates a {@link MethodInvocationArgument} object for {@code position}.
669669
*
670-
* @param position the parameter position (1-based) from the method invocation.
670+
* @param parameter the parameter from the method invocation.
671671
* @return {@link MethodInvocationArgument} object for {@code position}.
672672
*/
673673
static MethodInvocationArgument ofParameter(Parameter parameter) {

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/ParameterMetadataProvider.java

+6-11
Original file line numberDiff line numberDiff line change
@@ -273,17 +273,12 @@ public Object prepare(@Nullable Object value) {
273273

274274
if (String.class.equals(parameterType) && !noWildcards) {
275275

276-
switch (type) {
277-
case STARTING_WITH:
278-
return String.format("%s%%", escape.escape(value.toString()));
279-
case ENDING_WITH:
280-
return String.format("%%%s", escape.escape(value.toString()));
281-
case CONTAINING:
282-
case NOT_CONTAINING:
283-
return String.format("%%%s%%", escape.escape(value.toString()));
284-
default:
285-
return value;
286-
}
276+
return switch (type) {
277+
case STARTING_WITH -> String.format("%s%%", escape.escape(value.toString()));
278+
case ENDING_WITH -> String.format("%%%s", escape.escape(value.toString()));
279+
case CONTAINING, NOT_CONTAINING -> String.format("%%%s%%", escape.escape(value.toString()));
280+
default -> value;
281+
};
287282
}
288283

289284
return Collection.class.isAssignableFrom(parameterType) //

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/QueryParameterSetterFactory.java

-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ static QueryParameterSetterFactory forSynthetic() {
9393
*
9494
* @param parser must not be {@literal null}.
9595
* @param evaluationContextProvider must not be {@literal null}.
96-
* @param parameters must not be {@literal null}.
9796
* @return a {@link QueryParameterSetterFactory} that can handle
9897
* {@link org.springframework.expression.spel.standard.SpelExpression}s.
9998
*/
@@ -170,7 +169,6 @@ private static class ExpressionBasedQueryParameterSetterFactory extends QueryPar
170169
/**
171170
* @param parser must not be {@literal null}.
172171
* @param evaluationContextProvider must not be {@literal null}.
173-
* @param parameters must not be {@literal null}.
174172
*/
175173
ExpressionBasedQueryParameterSetterFactory(ValueExpressionParser parser,
176174
ValueEvaluationContextProvider evaluationContextProvider) {

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/QueryTokenStream.java

+5
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,23 @@ static QueryTokenStream empty() {
4646

4747
/**
4848
* Creates a QueryTokenStream from a {@link QueryToken}.
49+
* @since 4.0
4950
*/
5051
static QueryTokenStream from(QueryToken token) {
5152
return QueryRenderer.from(Collections.singletonList(token));
5253
}
5354

5455
/**
5556
* Creates an token QueryRenderer from an AST {@link TerminalNode}.
57+
* @since 4.0
5658
*/
5759
static QueryTokenStream ofToken(TerminalNode node) {
5860
return from(QueryTokens.token(node));
5961
}
6062

6163
/**
6264
* Creates an token QueryRenderer from an AST {@link Token}.
65+
* @since 4.0
6366
*/
6467
static QueryTokenStream ofToken(Token node) {
6568
return from(QueryTokens.token(node));
@@ -148,6 +151,7 @@ default QueryToken getFirst() {
148151

149152
/**
150153
* @return the required first query token or throw {@link java.util.NoSuchElementException} if empty.
154+
* @since 4.0
151155
*/
152156
default QueryToken getRequiredFirst() {
153157

@@ -170,6 +174,7 @@ default QueryToken getLast() {
170174

171175
/**
172176
* @return the required last query token or throw {@link java.util.NoSuchElementException} if empty.
177+
* @since 4.0
173178
*/
174179
default QueryToken getRequiredLast() {
175180

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/SimpleJpaQuery.java

+8-18
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,13 @@ private void validateQuery(String query, String errorMessage, Object... argument
8484
return;
8585
}
8686

87-
EntityManager validatingEm = null;
88-
89-
try {
90-
validatingEm = getEntityManager().getEntityManagerFactory().createEntityManager();
91-
validatingEm.createQuery(query);
92-
93-
} catch (RuntimeException e) {
94-
95-
// Needed as there's ambiguities in how an invalid query string shall be expressed by the persistence provider
96-
// https://java.net/projects/jpa-spec/lists/jsr338-experts/archive/2012-07/message/17
97-
throw new IllegalArgumentException(String.format(errorMessage, arguments), e);
98-
99-
} finally {
100-
101-
if (validatingEm != null) {
102-
validatingEm.close();
103-
}
104-
}
87+
try (EntityManager validatingEm = getEntityManager().getEntityManagerFactory().createEntityManager()) {
88+
validatingEm.createQuery(query);
89+
} catch (RuntimeException e) {
90+
91+
// Needed as there's ambiguities in how an invalid query string shall be expressed by the persistence provider
92+
// https://java.net/projects/jpa-spec/lists/jsr338-experts/archive/2012-07/message/17
93+
throw new IllegalArgumentException(String.format(errorMessage, arguments), e);
94+
}
10595
}
10696
}

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/StringQuery.java

+9-17
Original file line numberDiff line numberDiff line change
@@ -401,25 +401,17 @@ String parseParameterBindingsOfQueryIntoBindingsAndReturnCleanedQuery(String que
401401
: ParameterOrigin.ofExpression(expression);
402402

403403
BindingIdentifier targetBinding = queryParameter;
404-
Function<BindingIdentifier, ParameterBinding> bindingFactory;
405-
switch (ParameterBindingType.of(typeSource)) {
404+
Function<BindingIdentifier, ParameterBinding> bindingFactory = switch (ParameterBindingType.of(typeSource)) {
405+
case LIKE -> {
406406

407-
case LIKE:
407+
Type likeType = LikeParameterBinding.getLikeTypeFrom(matcher.group(2));
408+
yield (identifier) -> new LikeParameterBinding(identifier, origin, likeType);
409+
}
410+
case IN -> (identifier) -> new InParameterBinding(identifier, origin); // fall-through we don't need a special parameter queryParameter for the given parameter.
411+
default -> (identifier) -> new ParameterBinding(identifier, origin);
412+
};
408413

409-
Type likeType = LikeParameterBinding.getLikeTypeFrom(matcher.group(2));
410-
bindingFactory = (identifier) -> new LikeParameterBinding(identifier, origin, likeType);
411-
break;
412-
413-
case IN:
414-
bindingFactory = (identifier) -> new InParameterBinding(identifier, origin);
415-
break;
416-
417-
case AS_IS: // fall-through we don't need a special parameter queryParameter for the given parameter.
418-
default:
419-
bindingFactory = (identifier) -> new ParameterBinding(identifier, origin);
420-
}
421-
422-
if (origin.isExpression()) {
414+
if (origin.isExpression()) {
423415
parameterBindings.register(bindingFactory.apply(queryParameter));
424416
} else {
425417
targetBinding = parameterBindings.register(queryParameter, origin, bindingFactory, parameterLabels);

spring-data-jpa/src/main/java/org/springframework/data/jpa/support/ClasspathScanningPersistenceUnitPostProcessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ private Set<String> scanForMappingFileLocations() {
193193
* @param uri
194194
* @return
195195
*/
196-
private static String getResourcePath(URI uri) throws IOException {
196+
private static String getResourcePath(URI uri) {
197197

198198
if (uri.isOpaque()) {
199199
// e.g. jar:file:/foo/lib/somelib.jar!/com/acme/orm.xml

0 commit comments

Comments
 (0)