Migrate code to Java 17 style.
Use var instead of explicit local types where applicable. Use pattern variable instead instanceof and cast. Prefer loops and nullable types over Stream and Optional. Convert classes to records where applicable. See #2465
This commit is contained in:
committed by
Jens Schauder
parent
d4036ec0a9
commit
c735b58607
@@ -165,7 +165,7 @@ public class QSort extends Sort implements Serializable {
|
||||
*/
|
||||
private static String preparePropertyPath(Path<?> path) {
|
||||
|
||||
Path<?> root = path.getRoot();
|
||||
var root = path.getRoot();
|
||||
|
||||
return root == null || path.equals(root) ? path.toString()
|
||||
: path.toString().substring(root.toString().length() + 1);
|
||||
|
||||
@@ -19,7 +19,6 @@ import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.querydsl.core.types.Path;
|
||||
import com.querydsl.core.types.PathMetadata;
|
||||
import com.querydsl.core.types.PathType;
|
||||
|
||||
/**
|
||||
@@ -58,8 +57,8 @@ public abstract class QuerydslUtils {
|
||||
return tail;
|
||||
}
|
||||
|
||||
PathMetadata metadata = path.getMetadata();
|
||||
Path<?> parent = metadata.getParent();
|
||||
var metadata = path.getMetadata();
|
||||
var parent = metadata.getParent();
|
||||
|
||||
if (parent == null) {
|
||||
return tail;
|
||||
@@ -69,7 +68,7 @@ public abstract class QuerydslUtils {
|
||||
return toDotPath(parent, tail);
|
||||
}
|
||||
|
||||
Object element = metadata.getElement();
|
||||
var element = metadata.getElement();
|
||||
|
||||
if (element == null || !StringUtils.hasText(element.toString())) {
|
||||
return toDotPath(parent, tail);
|
||||
|
||||
@@ -64,11 +64,11 @@ public class SimpleEntityPathResolver implements EntityPathResolver {
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> EntityPath<T> createPath(Class<T> domainClass) {
|
||||
|
||||
String pathClassName = getQueryClassName(domainClass);
|
||||
var pathClassName = getQueryClassName(domainClass);
|
||||
|
||||
try {
|
||||
|
||||
Class<?> pathClass = ClassUtils.forName(pathClassName, domainClass.getClassLoader());
|
||||
var pathClass = ClassUtils.forName(pathClassName, domainClass.getClassLoader());
|
||||
|
||||
return getStaticFieldOfType(pathClass)//
|
||||
.map(it -> (EntityPath<T>) ReflectionUtils.getField(it, null))//
|
||||
@@ -88,17 +88,17 @@ public class SimpleEntityPathResolver implements EntityPathResolver {
|
||||
*/
|
||||
private Optional<Field> getStaticFieldOfType(Class<?> type) {
|
||||
|
||||
for (Field field : type.getDeclaredFields()) {
|
||||
for (var field : type.getDeclaredFields()) {
|
||||
|
||||
boolean isStatic = Modifier.isStatic(field.getModifiers());
|
||||
boolean hasSameType = type.equals(field.getType());
|
||||
var isStatic = Modifier.isStatic(field.getModifiers());
|
||||
var hasSameType = type.equals(field.getType());
|
||||
|
||||
if (isStatic && hasSameType) {
|
||||
return Optional.of(field);
|
||||
}
|
||||
}
|
||||
|
||||
Class<?> superclass = type.getSuperclass();
|
||||
var superclass = type.getSuperclass();
|
||||
return Object.class.equals(superclass) ? Optional.empty() : getStaticFieldOfType(superclass);
|
||||
}
|
||||
|
||||
@@ -110,8 +110,8 @@ public class SimpleEntityPathResolver implements EntityPathResolver {
|
||||
*/
|
||||
private String getQueryClassName(Class<?> domainClass) {
|
||||
|
||||
String simpleClassName = ClassUtils.getShortName(domainClass);
|
||||
String packageName = domainClass.getPackage().getName();
|
||||
var simpleClassName = ClassUtils.getShortName(domainClass);
|
||||
var packageName = domainClass.getPackage().getName();
|
||||
|
||||
return String.format("%s%s.Q%s%s", packageName, querySuffix, getClassBase(simpleClassName),
|
||||
domainClass.getSimpleName());
|
||||
@@ -125,7 +125,7 @@ public class SimpleEntityPathResolver implements EntityPathResolver {
|
||||
*/
|
||||
private String getClassBase(String shortName) {
|
||||
|
||||
String[] parts = shortName.split("\\.");
|
||||
var parts = shortName.split("\\.");
|
||||
|
||||
return parts.length < 2 ? "" : parts[0] + "_";
|
||||
}
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
package org.springframework.data.querydsl.binding;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.data.mapping.PropertyPath;
|
||||
@@ -38,13 +36,7 @@ import com.querydsl.core.types.dsl.CollectionPathBase;
|
||||
* @author Mark Paluch
|
||||
* @since 1.13
|
||||
*/
|
||||
class PropertyPathInformation implements PathInformation {
|
||||
|
||||
private final PropertyPath path;
|
||||
|
||||
private PropertyPathInformation(PropertyPath path) {
|
||||
this.path = path;
|
||||
}
|
||||
record PropertyPathInformation(PropertyPath path) implements PathInformation {
|
||||
|
||||
/**
|
||||
* Creates a new {@link PropertyPathInformation} for the given path and type.
|
||||
@@ -133,32 +125,26 @@ class PropertyPathInformation implements PathInformation {
|
||||
*/
|
||||
@Override
|
||||
public Path<?> reifyPath(EntityPathResolver resolver) {
|
||||
return reifyPath(resolver, path, Optional.empty());
|
||||
return reifyPath(resolver, path, null);
|
||||
}
|
||||
|
||||
private static Path<?> reifyPath(EntityPathResolver resolver, PropertyPath path, Optional<Path<?>> base) {
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
private static Path<?> reifyPath(EntityPathResolver resolver, PropertyPath path, @Nullable Path<?> base) {
|
||||
|
||||
Optional<Path<?>> map = base.filter(it -> it instanceof CollectionPathBase).map(CollectionPathBase.class::cast)//
|
||||
.map(CollectionPathBase::any)//
|
||||
.map(Path.class::cast)//
|
||||
.map(it -> reifyPath(resolver, path, Optional.of(it)));
|
||||
if (base instanceof CollectionPathBase) {
|
||||
return reifyPath(resolver, path, (Path<?>) ((CollectionPathBase<?, ?, ?>) base).any());
|
||||
}
|
||||
|
||||
return map.orElseGet(() -> {
|
||||
var entityPath = base != null ? base : resolver.createPath(path.getOwningType().getType());
|
||||
|
||||
Path<?> entityPath = base.orElseGet(() -> resolver.createPath(path.getOwningType().getType()));
|
||||
var field = ReflectionUtils.findField(entityPath.getClass(), path.getSegment());
|
||||
var value = ReflectionUtils.getField(field, entityPath);
|
||||
|
||||
Field field = org.springframework.data.util.ReflectionUtils.findRequiredField(entityPath.getClass(),
|
||||
path.getSegment());
|
||||
Object value = ReflectionUtils.getField(field, entityPath);
|
||||
if (path.hasNext()) {
|
||||
return reifyPath(resolver, path.next(), (Path<?>) value);
|
||||
}
|
||||
|
||||
PropertyPath next = path.next();
|
||||
|
||||
if (next != null) {
|
||||
return reifyPath(resolver, next, Optional.of((Path<?>) value));
|
||||
}
|
||||
|
||||
return (Path<?>) value;
|
||||
});
|
||||
return (Path<?>) value;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -172,11 +158,10 @@ class PropertyPathInformation implements PathInformation {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(o instanceof PathInformation)) {
|
||||
if (!(o instanceof PathInformation that)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PathInformation that = (PathInformation) o;
|
||||
return ObjectUtils.nullSafeEquals(getRootParentType(), that.getRootParentType())
|
||||
&& ObjectUtils.nullSafeEquals(toDotPath(), that.toDotPath());
|
||||
}
|
||||
@@ -187,7 +172,7 @@ class PropertyPathInformation implements PathInformation {
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = ObjectUtils.nullSafeHashCode(getRootParentType());
|
||||
var result = ObjectUtils.nullSafeHashCode(getRootParentType());
|
||||
result = 31 * result + ObjectUtils.nullSafeHashCode(toDotPath());
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ public class QuerydslBindings {
|
||||
|
||||
Assert.notEmpty(paths, "At least one path has to be provided!");
|
||||
|
||||
for (Path<?> path : paths) {
|
||||
for (var path : paths) {
|
||||
this.denyList.add(toDotPath(Optional.of(path)));
|
||||
}
|
||||
}
|
||||
@@ -142,7 +142,7 @@ public class QuerydslBindings {
|
||||
|
||||
Assert.notEmpty(paths, "At least one path has to be provided!");
|
||||
|
||||
for (Path<?> path : paths) {
|
||||
for (var path : paths) {
|
||||
this.allowList.add(toDotPath(Optional.of(path)));
|
||||
}
|
||||
}
|
||||
@@ -204,11 +204,11 @@ public class QuerydslBindings {
|
||||
|
||||
Assert.notNull(path, "PropertyPath must not be null!");
|
||||
|
||||
PathAndBinding<S, T> pathAndBinding = (PathAndBinding<S, T>) pathSpecs.get(createKey(path));
|
||||
var pathAndBinding = (PathAndBinding<S, T>) pathSpecs.get(createKey(path));
|
||||
|
||||
if (pathAndBinding != null) {
|
||||
|
||||
Optional<MultiValueBinding<S, T>> binding = pathAndBinding.getBinding();
|
||||
var binding = pathAndBinding.getBinding();
|
||||
|
||||
if (binding.isPresent()) {
|
||||
return binding;
|
||||
@@ -251,7 +251,7 @@ public class QuerydslBindings {
|
||||
}
|
||||
|
||||
// fully-qualified path lookup
|
||||
String key = createKey(type, path);
|
||||
var key = createKey(type, path);
|
||||
if (pathSpecs.containsKey(key)) {
|
||||
return pathSpecs.get(key).getPath()//
|
||||
.map(QuerydslPathInformation::of)//
|
||||
@@ -303,9 +303,9 @@ public class QuerydslBindings {
|
||||
*/
|
||||
private boolean isPathVisible(PathInformation path) {
|
||||
|
||||
List<String> segments = Arrays.asList(path.toDotPath().split("\\."));
|
||||
var segments = Arrays.asList(path.toDotPath().split("\\."));
|
||||
|
||||
for (int i = 1; i <= segments.size(); i++) {
|
||||
for (var i = 1; i <= segments.size(); i++) {
|
||||
|
||||
if (!isPathVisible(StringUtils.collectionToDelimitedString(segments.subList(0, i), "."))) {
|
||||
|
||||
@@ -354,7 +354,7 @@ public class QuerydslBindings {
|
||||
|
||||
private static String fromRootPath(Path<?> path) {
|
||||
|
||||
Path<?> rootPath = path.getMetadata().getRootPath();
|
||||
var rootPath = path.getMetadata().getRootPath();
|
||||
|
||||
if (rootPath == null) {
|
||||
throw new IllegalStateException(String.format("Couldn't find root path on path %s!", path));
|
||||
@@ -485,7 +485,7 @@ public class QuerydslBindings {
|
||||
|
||||
super.registerBinding(binding);
|
||||
|
||||
String dotPath = toDotPath(binding.getPath());
|
||||
var dotPath = toDotPath(binding.getPath());
|
||||
|
||||
if (alias != null) {
|
||||
QuerydslBindings.this.pathSpecs.put(alias, binding);
|
||||
@@ -584,10 +584,9 @@ public class QuerydslBindings {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof PathAndBinding)) {
|
||||
if (!(o instanceof PathAndBinding<?, ?> that)) {
|
||||
return false;
|
||||
}
|
||||
PathAndBinding<?, ?> that = (PathAndBinding<?, ?>) o;
|
||||
if (!ObjectUtils.nullSafeEquals(path, that.path)) {
|
||||
return false;
|
||||
}
|
||||
@@ -600,7 +599,7 @@ public class QuerydslBindings {
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = ObjectUtils.nullSafeHashCode(path);
|
||||
var result = ObjectUtils.nullSafeHashCode(path);
|
||||
result = 31 * result + ObjectUtils.nullSafeHashCode(binding);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package org.springframework.data.querydsl.binding;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -129,9 +128,9 @@ public class QuerydslBindingsFactory implements ApplicationContextAware {
|
||||
Assert.notNull(customizer, "Customizer must not be null!");
|
||||
Assert.notNull(domainType, "Domain type must not be null!");
|
||||
|
||||
EntityPath<?> path = verifyEntityPathPresent(domainType);
|
||||
var path = verifyEntityPathPresent(domainType);
|
||||
|
||||
QuerydslBindings bindings = new QuerydslBindings();
|
||||
var bindings = new QuerydslBindings();
|
||||
defaultCustomizer.customize(bindings, path);
|
||||
findCustomizerForDomainType(customizer, domainType.getType()).customize(bindings, path);
|
||||
|
||||
@@ -171,11 +170,11 @@ public class QuerydslBindingsFactory implements ApplicationContextAware {
|
||||
private QuerydslBinderCustomizer<EntityPath<?>> getDefaultQuerydslBinderCustomizer(
|
||||
AutowireCapableBeanFactory beanFactory) {
|
||||
|
||||
List<QuerydslBinderCustomizerDefaults> customizers = beanFactory
|
||||
var customizers = beanFactory
|
||||
.getBeanProvider(QuerydslBinderCustomizerDefaults.class).stream().collect(Collectors.toList());
|
||||
|
||||
return (bindings, root) -> {
|
||||
for (QuerydslBinderCustomizerDefaults querydslBinderCustomizerDefaults : customizers) {
|
||||
for (var querydslBinderCustomizerDefaults : customizers) {
|
||||
querydslBinderCustomizerDefaults.customize(bindings, root);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -59,7 +59,7 @@ class QuerydslDefaultBinding implements MultiValueBinding<Path<? extends Object>
|
||||
|
||||
if (path instanceof CollectionPathBase) {
|
||||
|
||||
BooleanBuilder builder = new BooleanBuilder();
|
||||
var builder = new BooleanBuilder();
|
||||
|
||||
for (Object element : value) {
|
||||
builder.and(((CollectionPathBase) path).contains(element));
|
||||
@@ -68,15 +68,13 @@ class QuerydslDefaultBinding implements MultiValueBinding<Path<? extends Object>
|
||||
return Optional.of(builder.getValue());
|
||||
}
|
||||
|
||||
if (path instanceof SimpleExpression) {
|
||||
|
||||
SimpleExpression expression = (SimpleExpression) path;
|
||||
if (path instanceof SimpleExpression expression) {
|
||||
|
||||
if (value.size() > 1) {
|
||||
return Optional.of(expression.in(value));
|
||||
}
|
||||
|
||||
Object object = value.iterator().next();
|
||||
var object = value.iterator().next();
|
||||
|
||||
return Optional.of(object == null //
|
||||
? expression.isNull() //
|
||||
|
||||
@@ -69,7 +69,7 @@ class QuerydslPathInformation implements PathInformation {
|
||||
@Override
|
||||
public Class<?> getLeafParentType() {
|
||||
|
||||
Path<?> parent = path.getMetadata().getParent();
|
||||
var parent = path.getMetadata().getParent();
|
||||
|
||||
if (parent == null) {
|
||||
throw new IllegalStateException(String.format("Could not obtain metadata for parent node of %s!", path));
|
||||
@@ -125,11 +125,10 @@ class QuerydslPathInformation implements PathInformation {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(o instanceof PathInformation)) {
|
||||
if (!(o instanceof PathInformation that)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PathInformation that = (PathInformation) o;
|
||||
return ObjectUtils.nullSafeEquals(getRootParentType(), that.getRootParentType())
|
||||
&& ObjectUtils.nullSafeEquals(toDotPath(), that.toDotPath());
|
||||
}
|
||||
@@ -140,7 +139,7 @@ class QuerydslPathInformation implements PathInformation {
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = ObjectUtils.nullSafeHashCode(getRootParentType());
|
||||
var result = ObjectUtils.nullSafeHashCode(getRootParentType());
|
||||
result = 31 * result + ObjectUtils.nullSafeHashCode(toDotPath());
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -15,13 +15,11 @@
|
||||
*/
|
||||
package org.springframework.data.querydsl.binding;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@@ -86,32 +84,32 @@ public class QuerydslPredicateBuilder {
|
||||
|
||||
Assert.notNull(bindings, "Context must not be null!");
|
||||
|
||||
BooleanBuilder builder = new BooleanBuilder();
|
||||
var builder = new BooleanBuilder();
|
||||
|
||||
if (values.isEmpty()) {
|
||||
return getPredicate(builder);
|
||||
}
|
||||
|
||||
for (Entry<String, List<String>> entry : values.entrySet()) {
|
||||
for (var entry : values.entrySet()) {
|
||||
|
||||
if (isSingleElementCollectionWithoutText(entry.getValue())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String path = entry.getKey();
|
||||
var path = entry.getKey();
|
||||
|
||||
if (!bindings.isPathAvailable(path, type)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
PathInformation propertyPath = bindings.getPropertyPath(path, type);
|
||||
var propertyPath = bindings.getPropertyPath(path, type);
|
||||
|
||||
if (propertyPath == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Collection<Object> value = convertToPropertyPathSpecificType(entry.getValue(), propertyPath);
|
||||
Optional<Predicate> predicate = invokeBinding(propertyPath, bindings, value);
|
||||
var value = convertToPropertyPathSpecificType(entry.getValue(), propertyPath);
|
||||
var predicate = invokeBinding(propertyPath, bindings, value);
|
||||
|
||||
predicate.ifPresent(builder::and);
|
||||
}
|
||||
@@ -142,7 +140,7 @@ public class QuerydslPredicateBuilder {
|
||||
private Optional<Predicate> invokeBinding(PathInformation dotPath, QuerydslBindings bindings,
|
||||
Collection<Object> values) {
|
||||
|
||||
Path<?> path = getPath(dotPath, bindings);
|
||||
var path = getPath(dotPath, bindings);
|
||||
|
||||
return bindings.getBindingForPath(dotPath).orElse(defaultBinding).bind(path, values);
|
||||
}
|
||||
@@ -158,7 +156,7 @@ public class QuerydslPredicateBuilder {
|
||||
*/
|
||||
private Path<?> getPath(PathInformation path, QuerydslBindings bindings) {
|
||||
|
||||
Optional<Path<?>> resolvedPath = bindings.getExistingPath(path);
|
||||
var resolvedPath = bindings.getExistingPath(path);
|
||||
|
||||
return resolvedPath.orElseGet(() -> paths.computeIfAbsent(path, it -> it.reifyPath(resolver)));
|
||||
}
|
||||
@@ -174,7 +172,7 @@ public class QuerydslPredicateBuilder {
|
||||
*/
|
||||
private Collection<Object> convertToPropertyPathSpecificType(List<String> source, PathInformation path) {
|
||||
|
||||
Class<?> targetType = path.getLeafType();
|
||||
var targetType = path.getLeafType();
|
||||
|
||||
if (source.isEmpty() || isSingleElementCollectionWithoutText(source)) {
|
||||
return Collections.emptyList();
|
||||
@@ -182,7 +180,7 @@ public class QuerydslPredicateBuilder {
|
||||
|
||||
Collection<Object> target = new ArrayList<>(source.size());
|
||||
|
||||
for (String value : source) {
|
||||
for (var value : source) {
|
||||
|
||||
target.add(conversionService.canConvert(String.class, targetType)
|
||||
? conversionService.convert(value, TypeDescriptor.forObject(value), getTargetTypeDescriptor(path))
|
||||
@@ -201,12 +199,12 @@ public class QuerydslPredicateBuilder {
|
||||
*/
|
||||
private static TypeDescriptor getTargetTypeDescriptor(PathInformation path) {
|
||||
|
||||
PropertyDescriptor descriptor = path.getLeafPropertyDescriptor();
|
||||
var descriptor = path.getLeafPropertyDescriptor();
|
||||
|
||||
Class<?> owningType = path.getLeafParentType();
|
||||
String leafProperty = path.getLeafProperty();
|
||||
var owningType = path.getLeafParentType();
|
||||
var leafProperty = path.getLeafProperty();
|
||||
|
||||
TypeDescriptor result = descriptor == null //
|
||||
var result = descriptor == null //
|
||||
? TypeDescriptor
|
||||
.nested(org.springframework.data.util.ReflectionUtils.findRequiredField(owningType, leafProperty), 0)
|
||||
: TypeDescriptor
|
||||
@@ -238,7 +236,7 @@ public class QuerydslPredicateBuilder {
|
||||
*/
|
||||
private static Predicate getPredicate(BooleanBuilder builder) {
|
||||
|
||||
Predicate predicate = builder.getValue();
|
||||
var predicate = builder.getValue();
|
||||
return predicate == null ? new BooleanBuilder() : predicate;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user