Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
A round of minor code style improvements.

Original Pull Request: #3695
  • Loading branch information
christophstrobl committed Dec 20, 2024
1 parent 61725bc commit c150ddc
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,14 @@ public <A extends Attribute<S, U>, U> Path<S, U> dot(A attribute) {
* @return
*/
public <P extends PluralAttribute<S, ?, U>, U> Path<S, U> dot(P attribute) {
return new Path<S, U>(add(attribute));
return new Path<>(add(attribute));
}

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

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

List<Attribute<?, ?>> newAttributes = new ArrayList<Attribute<?, ?>>(attributes.size() + 1);
List<Attribute<?, ?>> newAttributes = new ArrayList<>(attributes.size() + 1);
newAttributes.addAll(attributes);
newAttributes.add(attribute);
return newAttributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -107,7 +103,7 @@ public JpaPersistentPropertyImpl(JpaMetamodel metamodel, Property property,
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()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* JPA specific support projection support.
*/
@org.springframework.lang.NonNullApi
package org.springframework.data.jpa.projection;
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ public AbstractStringBasedJpaQuery(JpaQueryMethod method, EntityManager em, Stri
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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public JSqlParserQueryEnhancer(DeclaredQuery query) {
/**
* 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) {
Expand Down Expand Up @@ -502,7 +503,7 @@ private static boolean onlyASingleColumnProjection(List<SelectItem<?>> projectio
* </ul>
*/
enum ParsedType {
DELETE, UPDATE, SELECT, INSERT, MERGE, OTHER;
DELETE, UPDATE, SELECT, INSERT, MERGE, OTHER
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ private static class DeclaredQueryLookupStrategy extends AbstractQueryLookupStra
*
* @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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -848,9 +848,7 @@ public String getAlias(Origin source) {
*/
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++)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public Sort createSort(Sort sort, JpaEntityInformation<?, ?> entity) {
}

/**
* 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ static MethodInvocationArgument ofParameter(@Nullable String name, @Nullable Int
/**
* 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,17 +273,12 @@ public Object prepare(@Nullable Object value) {

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) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ static QueryParameterSetterFactory forSynthetic() {
*
* @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.
*/
Expand Down Expand Up @@ -170,7 +169,6 @@ private static class ExpressionBasedQueryParameterSetterFactory extends QueryPar
/**
* @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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,23 @@ static QueryTokenStream empty() {

/**
* Creates a QueryTokenStream from a {@link QueryToken}.
* @since 4.0
*/
static QueryTokenStream from(QueryToken token) {
return QueryRenderer.from(Collections.singletonList(token));
}

/**
* Creates an token QueryRenderer from an AST {@link TerminalNode}.
* @since 4.0
*/
static QueryTokenStream ofToken(TerminalNode node) {
return from(QueryTokens.token(node));
}

/**
* Creates an token QueryRenderer from an AST {@link Token}.
* @since 4.0
*/
static QueryTokenStream ofToken(Token node) {
return from(QueryTokens.token(node));
Expand Down Expand Up @@ -148,6 +151,7 @@ default QueryToken getFirst() {

/**
* @return the required first query token or throw {@link java.util.NoSuchElementException} if empty.
* @since 4.0
*/
default QueryToken getRequiredFirst() {

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

/**
* @return the required last query token or throw {@link java.util.NoSuchElementException} if empty.
* @since 4.0
*/
default QueryToken getRequiredLast() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,13 @@ private void validateQuery(String query, String errorMessage, Object... argument
return;
}

EntityManager validatingEm = null;

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();
}
}
try (EntityManager 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -304,25 +304,17 @@ String parseParameterBindingsOfQueryIntoBindingsAndReturnCleanedQuery(String que
: 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private Set<String> scanForMappingFileLocations() {
* @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
Expand Down

0 comments on commit c150ddc

Please sign in to comment.