Polishing.

A round of minor code style improvements.

Original Pull Request: #3695
This commit is contained in:
Christoph Strobl
2024-12-19 14:07:53 +01:00
committed by Mark Paluch
parent 9f36e39a66
commit a7e28aa94b
14 changed files with 45 additions and 66 deletions

View File

@@ -57,13 +57,9 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
static {
Set<Class<? extends Annotation>> annotations = new HashSet<>();
annotations.add(OneToMany.class);
annotations.add(OneToOne.class);
annotations.add(ManyToMany.class);
annotations.add(ManyToOne.class);
Set<Class<? extends Annotation>> annotations;
ASSOCIATION_ANNOTATIONS = Collections.unmodifiableSet(annotations);
ASSOCIATION_ANNOTATIONS = Set.of(OneToMany.class, OneToOne.class, ManyToMany.class, ManyToOne.class);
annotations = new HashSet<>();
annotations.add(Id.class);
@@ -107,7 +103,7 @@ class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPer
this.associationTargetType = detectAssociationTargetType();
this.updateable = detectUpdatability();
this.isIdProperty = Lazy.of(() -> ID_ANNOTATIONS.stream().anyMatch(it -> isAnnotationPresent(it)) //
this.isIdProperty = Lazy.of(() -> ID_ANNOTATIONS.stream().anyMatch(this::isAnnotationPresent) //
|| metamodel.isSingleIdAttribute(getOwner().getType(), getName(), getType()));
this.isEntity = Lazy.of(() -> metamodel.isMappedType(getActualType()));
}

View File

@@ -0,0 +1,5 @@
/**
* JPA specific support projection support.
*/
@org.springframework.lang.NonNullApi
package org.springframework.data.jpa.projection;

View File

@@ -98,9 +98,7 @@ abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery {
return query.deriveCountQuery(method.getCountQueryProjection());
});
this.countParameterBinder = Lazy.of(() -> {
return this.createBinder(this.countQuery.get());
});
this.countParameterBinder = Lazy.of(() -> this.createBinder(this.countQuery.get()));
this.queryRewriter = queryRewriter;

View File

@@ -102,7 +102,8 @@ public class JSqlParserQueryEnhancer implements QueryEnhancer {
/**
* Parses a query string with JSqlParser.
*
* @param query the query to parse
* @param sql the query to parse
* @param classOfT the query to parse
* @return the parsed query
*/
static <T extends Statement> T parseStatement(String sql, Class<T> classOfT) {
@@ -560,7 +561,7 @@ public class JSqlParserQueryEnhancer implements QueryEnhancer {
* </ul>
*/
enum ParsedType {
DELETE, UPDATE, SELECT, INSERT, MERGE, OTHER;
DELETE, UPDATE, SELECT, INSERT, MERGE, OTHER
}
/**

View File

@@ -143,7 +143,8 @@ public final class JpaQueryLookupStrategy {
*
* @param em must not be {@literal null}.
* @param queryMethodFactory must not be {@literal null}.
* @param evaluationContextProvider must not be {@literal null}.
* @param delegate must not be {@literal null}.
* @param queryRewriterProvider must not be {@literal null}.
*/
public DeclaredQueryLookupStrategy(EntityManager em, JpaQueryMethodFactory queryMethodFactory,
ValueExpressionDelegate delegate, QueryRewriterProvider queryRewriterProvider) {

View File

@@ -848,9 +848,7 @@ public final class JpqlQueryBuilder {
*/
public String getAlias(Origin source) {
return aliases.computeIfAbsent(source, it -> JpqlQueryBuilder.getAlias(source.getName(), s -> {
return !aliases.containsValue(s);
}, () -> "join_" + (counter++)));
return aliases.computeIfAbsent(source, it -> JpqlQueryBuilder.getAlias(source.getName(), s -> !aliases.containsValue(s), () -> "join_" + (counter++)));
}
/**

View File

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

View File

@@ -667,7 +667,7 @@ class ParameterBinding {
/**
* Creates a {@link MethodInvocationArgument} object for {@code position}.
*
* @param position the parameter position (1-based) from the method invocation.
* @param parameter the parameter from the method invocation.
* @return {@link MethodInvocationArgument} object for {@code position}.
*/
static MethodInvocationArgument ofParameter(Parameter parameter) {

View File

@@ -273,17 +273,12 @@ public class ParameterMetadataProvider {
if (String.class.equals(parameterType) && !noWildcards) {
switch (type) {
case STARTING_WITH:
return String.format("%s%%", escape.escape(value.toString()));
case ENDING_WITH:
return String.format("%%%s", escape.escape(value.toString()));
case CONTAINING:
case NOT_CONTAINING:
return String.format("%%%s%%", escape.escape(value.toString()));
default:
return value;
}
return switch (type) {
case STARTING_WITH -> String.format("%s%%", escape.escape(value.toString()));
case ENDING_WITH -> String.format("%%%s", escape.escape(value.toString()));
case CONTAINING, NOT_CONTAINING -> String.format("%%%s%%", escape.escape(value.toString()));
default -> value;
};
}
return Collection.class.isAssignableFrom(parameterType) //

View File

@@ -93,7 +93,6 @@ abstract class QueryParameterSetterFactory {
*
* @param parser must not be {@literal null}.
* @param evaluationContextProvider must not be {@literal null}.
* @param parameters must not be {@literal null}.
* @return a {@link QueryParameterSetterFactory} that can handle
* {@link org.springframework.expression.spel.standard.SpelExpression}s.
*/
@@ -170,7 +169,6 @@ abstract class QueryParameterSetterFactory {
/**
* @param parser must not be {@literal null}.
* @param evaluationContextProvider must not be {@literal null}.
* @param parameters must not be {@literal null}.
*/
ExpressionBasedQueryParameterSetterFactory(ValueExpressionParser parser,
ValueEvaluationContextProvider evaluationContextProvider) {

View File

@@ -46,6 +46,7 @@ interface QueryTokenStream extends Streamable<QueryToken> {
/**
* Creates a QueryTokenStream from a {@link QueryToken}.
* @since 4.0
*/
static QueryTokenStream from(QueryToken token) {
return QueryRenderer.from(Collections.singletonList(token));
@@ -53,6 +54,7 @@ interface QueryTokenStream extends Streamable<QueryToken> {
/**
* Creates an token QueryRenderer from an AST {@link TerminalNode}.
* @since 4.0
*/
static QueryTokenStream ofToken(TerminalNode node) {
return from(QueryTokens.token(node));
@@ -60,6 +62,7 @@ interface QueryTokenStream extends Streamable<QueryToken> {
/**
* Creates an token QueryRenderer from an AST {@link Token}.
* @since 4.0
*/
static QueryTokenStream ofToken(Token node) {
return from(QueryTokens.token(node));
@@ -148,6 +151,7 @@ interface QueryTokenStream extends Streamable<QueryToken> {
/**
* @return the required first query token or throw {@link java.util.NoSuchElementException} if empty.
* @since 4.0
*/
default QueryToken getRequiredFirst() {
@@ -170,6 +174,7 @@ interface QueryTokenStream extends Streamable<QueryToken> {
/**
* @return the required last query token or throw {@link java.util.NoSuchElementException} if empty.
* @since 4.0
*/
default QueryToken getRequiredLast() {

View File

@@ -84,23 +84,13 @@ final class SimpleJpaQuery extends AbstractStringBasedJpaQuery {
return;
}
EntityManager validatingEm = null;
try (EntityManager validatingEm = getEntityManager().getEntityManagerFactory().createEntityManager()) {
validatingEm.createQuery(query);
} catch (RuntimeException e) {
try {
validatingEm = getEntityManager().getEntityManagerFactory().createEntityManager();
validatingEm.createQuery(query);
} catch (RuntimeException e) {
// Needed as there's ambiguities in how an invalid query string shall be expressed by the persistence provider
// https://java.net/projects/jpa-spec/lists/jsr338-experts/archive/2012-07/message/17
throw new IllegalArgumentException(String.format(errorMessage, arguments), e);
} finally {
if (validatingEm != null) {
validatingEm.close();
}
}
// Needed as there's ambiguities in how an invalid query string shall be expressed by the persistence provider
// https://java.net/projects/jpa-spec/lists/jsr338-experts/archive/2012-07/message/17
throw new IllegalArgumentException(String.format(errorMessage, arguments), e);
}
}
}

View File

@@ -401,25 +401,17 @@ class StringQuery implements DeclaredQuery {
: ParameterOrigin.ofExpression(expression);
BindingIdentifier targetBinding = queryParameter;
Function<BindingIdentifier, ParameterBinding> bindingFactory;
switch (ParameterBindingType.of(typeSource)) {
Function<BindingIdentifier, ParameterBinding> bindingFactory = switch (ParameterBindingType.of(typeSource)) {
case LIKE -> {
case LIKE:
Type likeType = LikeParameterBinding.getLikeTypeFrom(matcher.group(2));
yield (identifier) -> new LikeParameterBinding(identifier, origin, likeType);
}
case IN -> (identifier) -> new InParameterBinding(identifier, origin); // fall-through we don't need a special parameter queryParameter for the given parameter.
default -> (identifier) -> new ParameterBinding(identifier, origin);
};
Type likeType = LikeParameterBinding.getLikeTypeFrom(matcher.group(2));
bindingFactory = (identifier) -> new LikeParameterBinding(identifier, origin, likeType);
break;
case IN:
bindingFactory = (identifier) -> new InParameterBinding(identifier, origin);
break;
case AS_IS: // fall-through we don't need a special parameter queryParameter for the given parameter.
default:
bindingFactory = (identifier) -> new ParameterBinding(identifier, origin);
}
if (origin.isExpression()) {
if (origin.isExpression()) {
parameterBindings.register(bindingFactory.apply(queryParameter));
} else {
targetBinding = parameterBindings.register(queryParameter, origin, bindingFactory, parameterLabels);

View File

@@ -193,7 +193,7 @@ public class ClasspathScanningPersistenceUnitPostProcessor
* @param uri
* @return
*/
private static String getResourcePath(URI uri) throws IOException {
private static String getResourcePath(URI uri) {
if (uri.isOpaque()) {
// e.g. jar:file:/foo/lib/somelib.jar!/com/acme/orm.xml