Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using new features available in recent Java versions. #3053

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@
*/
package org.springframework.data.jpa.convert;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.From;
import jakarta.persistence.criteria.Path;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import jakarta.persistence.metamodel.Attribute;
import jakarta.persistence.metamodel.Attribute.PersistentAttributeType;
import jakarta.persistence.metamodel.ManagedType;
import jakarta.persistence.metamodel.SingularAttribute;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
Expand All @@ -45,6 +34,17 @@
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.From;
import jakarta.persistence.criteria.Path;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import jakarta.persistence.metamodel.Attribute;
import jakarta.persistence.metamodel.Attribute.PersistentAttributeType;
import jakarta.persistence.metamodel.ManagedType;
import jakarta.persistence.metamodel.SingularAttribute;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate this PR as a way to update some of our coding constructions, taking advantage of Java 17 features.

There is certainly a lot of noise generating in the re-ordering of import statements. I can deal with this, but for future PR's, you may want to look into adopting https://github.com/spring-projects/spring-data-build/blob/main/etc/ide/README.md. We have settings configured for both Eclipse and IntelliJ so that we all have the same import statement/static import policies, and avoid generating too much churn just from that.

/**
* {@link QueryByExamplePredicateBuilder} creates a single {@link CriteriaBuilder#and(Predicate...)} combined
* {@link Predicate} for a given {@link Example}. <br />
Expand All @@ -57,6 +57,7 @@
* @author Oliver Gierke
* @author Jens Schauder
* @author Greg Turnquist
* @author Christian Wörz
* @since 1.10
*/
public class QueryByExamplePredicateBuilder {
Expand Down Expand Up @@ -186,35 +187,24 @@ static List<Predicate> getPredicates(String path, CriteriaBuilder cb, Path<?> fr
}

switch (exampleAccessor.getStringMatcherForPath(currentPath)) {

case DEFAULT:
case EXACT:
predicates.add(cb.equal(expression, attributeValue));
break;
case CONTAINING:
predicates.add(cb.like( //
expression, //
"%" + escapeCharacter.escape(attributeValue.toString()) + "%", //
escapeCharacter.getEscapeCharacter() //
case DEFAULT, EXACT -> predicates.add(cb.equal(expression, attributeValue));
case CONTAINING -> predicates.add(cb.like( //
expression, //
"%" + escapeCharacter.escape(attributeValue.toString()) + "%", //
escapeCharacter.getEscapeCharacter() //
));
break;
case STARTING:
predicates.add(cb.like(//
expression, //
escapeCharacter.escape(attributeValue.toString()) + "%", //
escapeCharacter.getEscapeCharacter()) //
case STARTING -> predicates.add(cb.like(//
expression, //
escapeCharacter.escape(attributeValue.toString()) + "%", //
escapeCharacter.getEscapeCharacter()) //
);
break;
case ENDING:
predicates.add(cb.like( //
expression, //
"%" + escapeCharacter.escape(attributeValue.toString()), //
escapeCharacter.getEscapeCharacter()) //
case ENDING -> predicates.add(cb.like( //
expression, //
"%" + escapeCharacter.escape(attributeValue.toString()), //
escapeCharacter.getEscapeCharacter()) //
);
break;
default:
throw new IllegalArgumentException(
"Unsupported StringMatcher " + exampleAccessor.getStringMatcherForPath(currentPath));
default -> throw new IllegalArgumentException(
"Unsupported StringMatcher " + exampleAccessor.getStringMatcherForPath(currentPath));
}
} else {
predicates.add(cb.equal(from.get(attribute), attributeValue));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
*/
package org.springframework.data.jpa.domain;

import jakarta.persistence.metamodel.Attribute;
import jakarta.persistence.metamodel.PluralAttribute;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -28,11 +25,8 @@
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import jakarta.persistence.metamodel.Attribute;
import jakarta.persistence.metamodel.PluralAttribute;

/**
* Sort option for queries that wraps JPA meta-model {@link Attribute}s for sorting.
Expand All @@ -42,13 +36,14 @@
* @author Christoph Strobl
* @author David Madden
* @author Jens Schauder
* @author Christian Wörz
*/
public class JpaSort extends Sort {

private static final long serialVersionUID = 1L;

private JpaSort(Direction direction, List<Path<?, ?>> paths) {
this(Collections.<Order>emptyList(), direction, paths);
this(Collections.<Order> emptyList(), direction, paths);
}

private JpaSort(List<Order> orders, @Nullable Direction direction, List<Path<?, ?>> paths) {
Expand Down Expand Up @@ -80,7 +75,7 @@ public static JpaSort of(JpaSort.Path<?, ?>... paths) {
/**
* Creates a new {@link JpaSort} for the given direction and attributes.
*
* @param direction the sorting direction.
* @param direction the sorting direction.
* @param attributes must not be {@literal null} or empty.
*/
public static JpaSort of(Direction direction, Attribute<?, ?>... attributes) {
Expand All @@ -91,7 +86,7 @@ public static JpaSort of(Direction direction, Attribute<?, ?>... attributes) {
* Creates a new {@link JpaSort} for the given direction and {@link Path}s.
*
* @param direction the sorting direction.
* @param paths must not be {@literal null} or empty.
* @param paths must not be {@literal null} or empty.
*/
public static JpaSort of(Direction direction, Path<?, ?>... paths) {
return new JpaSort(direction, Arrays.asList(paths));
Expand All @@ -100,7 +95,7 @@ public static JpaSort of(Direction direction, Path<?, ?>... paths) {
/**
* Returns a new {@link JpaSort} with the given sorting criteria added to the current one.
*
* @param direction can be {@literal null}.
* @param direction can be {@literal null}.
* @param attributes must not be {@literal null}.
* @return
*/
Expand All @@ -115,7 +110,7 @@ public JpaSort and(@Nullable Direction direction, Attribute<?, ?>... attributes)
* Returns a new {@link JpaSort} with the given sorting criteria added to the current one.
*
* @param direction can be {@literal null}.
* @param paths must not be {@literal null}.
* @param paths must not be {@literal null}.
* @return
*/
public JpaSort and(@Nullable Direction direction, Path<?, ?>... paths) {
Expand All @@ -134,7 +129,7 @@ public JpaSort and(@Nullable Direction direction, Path<?, ?>... paths) {
/**
* Returns a new {@link JpaSort} with the given sorting criteria added to the current one.
*
* @param direction can be {@literal null}.
* @param direction can be {@literal null}.
* @param properties must not be {@literal null} or empty.
* @return
*/
Expand All @@ -152,7 +147,7 @@ public JpaSort andUnsafe(@Nullable Direction direction, String... properties) {
orders.add(new JpaOrder(direction, property));
}

return new JpaSort(orders, direction, Collections.<Path<?, ?>>emptyList());
return new JpaSort(orders, direction, Collections.<Path<?, ?>> emptyList());
}

/**
Expand Down Expand Up @@ -223,7 +218,7 @@ public static JpaSort unsafe(String... properties) {
/**
* Creates new unsafe {@link JpaSort} based on given {@link Direction} and properties.
*
* @param direction must not be {@literal null}.
* @param direction must not be {@literal null}.
* @param properties must not be {@literal null} or empty.
* @return
*/
Expand All @@ -239,7 +234,7 @@ public static JpaSort unsafe(Direction direction, String... properties) {
/**
* Creates new unsafe {@link JpaSort} based on given {@link Direction} and properties.
*
* @param direction must not be {@literal null}.
* @param direction must not be {@literal null}.
* @param properties must not be {@literal null} or empty.
* @return
*/
Expand Down Expand Up @@ -286,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 Expand Up @@ -331,7 +326,7 @@ public static class JpaOrder extends Order {
* {@link Sort#DEFAULT_DIRECTION}
*
* @param direction can be {@literal null}, will default to {@link Sort#DEFAULT_DIRECTION}.
* @param property must not be {@literal null}.
* @param property must not be {@literal null}.
*/
private JpaOrder(@Nullable Direction direction, String property) {
this(direction, property, NullHandling.NATIVE);
Expand All @@ -341,16 +336,16 @@ private JpaOrder(@Nullable Direction direction, String property) {
* Creates a new {@link Order} instance. if order is {@literal null} then order defaults to
* {@link Sort#DEFAULT_DIRECTION}.
*
* @param direction can be {@literal null}, will default to {@link Sort#DEFAULT_DIRECTION}.
* @param property must not be {@literal null}.
* @param direction can be {@literal null}, will default to {@link Sort#DEFAULT_DIRECTION}.
* @param property must not be {@literal null}.
* @param nullHandlingHint can be {@literal null}, will default to {@link NullHandling#NATIVE}.
*/
private JpaOrder(@Nullable Direction direction, String property, NullHandling nullHandlingHint) {
this(direction, property, false, nullHandlingHint, true);
}

private JpaOrder(@Nullable Direction direction, String property, boolean ignoreCase, NullHandling nullHandling,
boolean unsafe) {
boolean unsafe) {

super(direction, property, ignoreCase, nullHandling);
this.unsafe = unsafe;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@
*/
package org.springframework.data.jpa.domain;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaDelete;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;

import java.io.Serializable;
import java.util.Arrays;
import java.util.stream.StreamSupport;

import org.springframework.lang.Nullable;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;

/**
* Specification in the sense of Domain Driven Design.
*
Expand All @@ -37,6 +36,7 @@
* @author Mark Paluch
* @author Jens Schauder
* @author Daniel Shuy
* @author Christian Wörz
*/
public interface Specification<T> extends Serializable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@
*/
package org.springframework.data.jpa.mapping;

import jakarta.persistence.*;
import jakarta.persistence.metamodel.Metamodel;

import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.springframework.core.annotation.AnnotationUtils;
Expand All @@ -37,6 +33,9 @@
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import jakarta.persistence.*;
import jakarta.persistence.metamodel.Metamodel;

/**
* {@link JpaPersistentProperty} implementation using a JPA {@link Metamodel}.
*
Expand All @@ -46,38 +45,17 @@
* @author Christoph Strobl
* @author Mark Paluch
* @author Erik Pellizzon
* @author Christian Wörz
* @since 1.3
*/
class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPersistentProperty>
implements JpaPersistentProperty {

private static final Collection<Class<? extends Annotation>> ASSOCIATION_ANNOTATIONS;
private static final Collection<Class<? extends Annotation>> ID_ANNOTATIONS;
private static final Collection<Class<? extends Annotation>> UPDATEABLE_ANNOTATIONS;

static {

Set<Class<? extends Annotation>> annotations = new HashSet<>();
annotations.add(OneToMany.class);
annotations.add(OneToOne.class);
annotations.add(ManyToMany.class);
annotations.add(ManyToOne.class);

ASSOCIATION_ANNOTATIONS = Collections.unmodifiableSet(annotations);

annotations = new HashSet<>();
annotations.add(Id.class);
annotations.add(EmbeddedId.class);

ID_ANNOTATIONS = Collections.unmodifiableSet(annotations);

annotations = new HashSet<>();
annotations.add(Column.class);
annotations.add(OrderColumn.class);

UPDATEABLE_ANNOTATIONS = Collections.unmodifiableSet(annotations);
}

private static final Collection<Class<? extends Annotation>> ASSOCIATION_ANNOTATIONS = Set.of(OneToMany.class,
OneToOne.class, ManyToMany.class, ManyToOne.class);
private static final Collection<Class<? extends Annotation>> ID_ANNOTATIONS = Set.of(Id.class, EmbeddedId.class);
private static final Collection<Class<? extends Annotation>> UPDATEABLE_ANNOTATIONS = Set.of(Column.class,
OrderColumn.class);
private final @Nullable Boolean usePropertyAccess;
private final @Nullable TypeInformation<?> associationTargetType;
private final boolean updateable;
Expand Down Expand Up @@ -107,7 +85,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
Loading
Loading