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:
Mark Paluch
2021-10-05 15:21:12 +02:00
committed by Jens Schauder
parent d4036ec0a9
commit c735b58607
463 changed files with 3670 additions and 4014 deletions

View File

@@ -17,7 +17,6 @@ package org.springframework.data.spel;
import static org.springframework.data.util.StreamUtils.*;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@@ -74,7 +73,7 @@ class EvaluationContextExtensionInformation {
Assert.notNull(type, "Extension type must not be null!");
Class<?> rootObjectType = org.springframework.data.util.ReflectionUtils.findRequiredMethod(type, "getRootObject")
var rootObjectType = org.springframework.data.util.ReflectionUtils.findRequiredMethod(type, "getRootObject")
.getReturnType();
this.rootObjectInformation = Optional
@@ -221,8 +220,8 @@ class EvaluationContextExtensionInformation {
return false;
}
boolean methodStatic = Modifier.isStatic(method.getModifiers());
boolean staticMatch = staticOnly ? methodStatic : !methodStatic;
var methodStatic = Modifier.isStatic(method.getModifiers());
var staticMatch = staticOnly ? methodStatic : !methodStatic;
return Modifier.isPublic(method.getModifiers()) && staticMatch;
}
@@ -234,8 +233,8 @@ class EvaluationContextExtensionInformation {
@Override
public boolean matches(Field field) {
boolean fieldStatic = Modifier.isStatic(field.getModifiers());
boolean staticMatch = staticOnly ? fieldStatic : !fieldStatic;
var fieldStatic = Modifier.isStatic(field.getModifiers());
var staticMatch = staticOnly ? fieldStatic : !fieldStatic;
return Modifier.isPublic(field.getModifiers()) && staticMatch;
}
@@ -274,7 +273,7 @@ class EvaluationContextExtensionInformation {
return;
}
Streamable<PropertyDescriptor> descriptors = Streamable.of(BeanUtils.getPropertyDescriptors(type));
var descriptors = Streamable.of(BeanUtils.getPropertyDescriptors(type));
ReflectionUtils.doWithMethods(type, method -> {
@@ -343,7 +342,7 @@ class EvaluationContextExtensionInformation {
return true;
}
for (Field field : fields) {
for (var field : fields) {
if (field.getName().equals(dependency.getSymbol())) {
return true;
}

View File

@@ -138,7 +138,7 @@ public class ExpressionDependencies implements Streamable<ExpressionDependencies
ExpressionDependency.forPropertyOrField(((PropertyOrFieldReference) node).getName()).nest(compoundPosition));
}
for (int i = 0; i < node.getChildCount(); i++) {
for (var i = 0; i < node.getChildCount(); i++) {
collectDependencies(node.getChild(i), node instanceof CompoundExpression ? i : 0, dependencies);
}
}
@@ -179,10 +179,9 @@ public class ExpressionDependencies implements Streamable<ExpressionDependencies
if (this == o) {
return true;
}
if (!(o instanceof ExpressionDependencies)) {
if (!(o instanceof ExpressionDependencies that)) {
return false;
}
ExpressionDependencies that = (ExpressionDependencies) o;
return ObjectUtils.nullSafeEquals(dependencies, that.dependencies);
}
@@ -273,10 +272,9 @@ public class ExpressionDependencies implements Streamable<ExpressionDependencies
if (this == o) {
return true;
}
if (!(o instanceof ExpressionDependency)) {
if (!(o instanceof ExpressionDependency that)) {
return false;
}
ExpressionDependency that = (ExpressionDependency) o;
if (nestLevel != that.nestLevel) {
return false;
}
@@ -292,7 +290,7 @@ public class ExpressionDependencies implements Streamable<ExpressionDependencies
*/
@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(type);
var result = ObjectUtils.nullSafeHashCode(type);
result = 31 * result + ObjectUtils.nullSafeHashCode(symbol);
result = 31 * result + nestLevel;
return result;

View File

@@ -31,8 +31,6 @@ import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.data.spel.EvaluationContextExtensionInformation.ExtensionTypeInformation;
import org.springframework.data.spel.EvaluationContextExtensionInformation.RootObjectInformation;
import org.springframework.data.spel.spi.EvaluationContextExtension;
import org.springframework.data.spel.spi.ExtensionIdAware;
import org.springframework.data.spel.spi.Function;
@@ -122,13 +120,13 @@ public class ExtensionAwareEvaluationContextProvider implements EvaluationContex
StandardEvaluationContext doGetEvaluationContext(Object rootObject,
Collection<? extends EvaluationContextExtension> extensions) {
StandardEvaluationContext context = new StandardEvaluationContext();
var context = new StandardEvaluationContext();
if (beanFactory != null) {
context.setBeanResolver(new BeanFactoryResolver(beanFactory));
}
ExtensionAwarePropertyAccessor accessor = new ExtensionAwarePropertyAccessor(extensions);
var accessor = new ExtensionAwarePropertyAccessor(extensions);
context.addPropertyAccessor(accessor);
context.addPropertyAccessor(new ReflectivePropertyAccessor());
@@ -152,9 +150,8 @@ public class ExtensionAwareEvaluationContextProvider implements EvaluationContex
for (ExtensionIdAware candidate : getExtensions()) {
if (candidate instanceof EvaluationContextExtension) {
if (candidate instanceof EvaluationContextExtension extension) {
EvaluationContextExtension extension = (EvaluationContextExtension) candidate;
if (extensionFilter.test(getOrCreateInformation(extension))) {
extensionsToUse.add(extension);
}
@@ -337,14 +334,12 @@ public class ExtensionAwareEvaluationContextProvider implements EvaluationContex
*/
private TypedValue lookupPropertyFrom(EvaluationContextExtensionAdapter extension, String name) {
Object value = extension.getProperties().get(name);
var value = extension.getProperties().get(name);
if (!(value instanceof Function)) {
if (!(value instanceof Function function)) {
return new TypedValue(value);
}
Function function = (Function) value;
try {
return new TypedValue(function.invoke(new Object[0]));
} catch (Exception e) {
@@ -412,9 +407,9 @@ public class ExtensionAwareEvaluationContextProvider implements EvaluationContex
Assert.notNull(extension, "Extension must not be null!");
Assert.notNull(information, "Extension information must not be null!");
Optional<Object> target = Optional.ofNullable(extension.getRootObject());
ExtensionTypeInformation extensionTypeInformation = information.getExtensionTypeInformation();
RootObjectInformation rootObjectInformation = information.getRootObjectInformation(target);
var target = Optional.ofNullable(extension.getRootObject());
var extensionTypeInformation = information.getExtensionTypeInformation();
var rootObjectInformation = information.getRootObjectInformation(target);
functions.addAll(extension.getFunctions());
functions.addAll(rootObjectInformation.getFunctions(target));

View File

@@ -20,7 +20,6 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.data.spel.spi.Function;
@@ -47,7 +46,7 @@ class Functions {
newFunctions.forEach((n, f) -> {
List<Function> currentElements = get(n);
var currentElements = get(n);
if (!contains(currentElements, f)) {
functions.add(n, f);
@@ -59,7 +58,7 @@ class Functions {
newFunctions.forEach((k, list) -> {
List<Function> currentElements = get(k);
var currentElements = get(k);
list.stream() //
.filter(f -> !contains(currentElements, f)) //
@@ -83,10 +82,10 @@ class Functions {
*/
Optional<Function> get(String name, List<TypeDescriptor> argumentTypes) {
Stream<Function> candidates = get(name).stream() //
var candidates = get(name).stream() //
.filter(f -> f.supports(argumentTypes));
List<Function> collect = candidates.collect(Collectors.toList());
var collect = candidates.collect(Collectors.toList());
return bestMatch(collect, argumentTypes);
}
@@ -105,7 +104,7 @@ class Functions {
return Optional.of(candidates.get(0));
}
Optional<Function> exactMatch = candidates.stream().filter(f -> f.supportsExact(argumentTypes)).findFirst();
var exactMatch = candidates.stream().filter(f -> f.supportsExact(argumentTypes)).findFirst();
if (!exactMatch.isPresent()) {
throw new IllegalStateException(createErrorMessage(candidates, argumentTypes));
@@ -116,7 +115,7 @@ class Functions {
private static String createErrorMessage(List<Function> candidates, List<TypeDescriptor> argumentTypes) {
String argumentTypeString = argumentTypes.stream()//
var argumentTypeString = argumentTypes.stream()//
.map(TypeDescriptor::getName)//
.collect(Collectors.joining(","));

View File

@@ -114,14 +114,13 @@ public class ReactiveExtensionAwareEvaluationContextProvider implements Reactive
private Mono<List<EvaluationContextExtension>> getExtensions(
Predicate<EvaluationContextExtensionInformation> extensionFilter) {
Collection<? extends ExtensionIdAware> extensions = evaluationContextProvider.getExtensions();
var extensions = evaluationContextProvider.getExtensions();
return Flux.fromIterable(extensions).concatMap(it -> {
if (it instanceof EvaluationContextExtension) {
if (it instanceof EvaluationContextExtension extension) {
EvaluationContextExtension extension = (EvaluationContextExtension) it;
EvaluationContextExtensionInformation information = evaluationContextProvider.getOrCreateInformation(extension);
var information = evaluationContextProvider.getOrCreateInformation(extension);
if (extensionFilter.test(information)) {
return Mono.just(extension);
@@ -130,16 +129,15 @@ public class ReactiveExtensionAwareEvaluationContextProvider implements Reactive
return Mono.empty();
}
if (it instanceof ReactiveEvaluationContextExtension) {
if (it instanceof ReactiveEvaluationContextExtension extension) {
ReactiveEvaluationContextExtension extension = (ReactiveEvaluationContextExtension) it;
ResolvableType actualType = getExtensionType(it);
var actualType = getExtensionType(it);
if (actualType.equals(ResolvableType.NONE) || actualType.isAssignableFrom(GENERIC_EXTENSION_TYPE)) {
return extension.getExtension();
}
EvaluationContextExtensionInformation information = evaluationContextProvider
var information = evaluationContextProvider
.getOrCreateInformation((Class) actualType.getRawClass());
if (extensionFilter.test(information)) {

View File

@@ -82,23 +82,23 @@ public class Function {
return method.invoke(target, arguments);
}
Class<?>[] types = method.getParameterTypes();
Class<?> tailType = types[types.length - 1];
var types = method.getParameterTypes();
var tailType = types[types.length - 1];
if (tailType.isArray()) {
List<Object> argumentsToUse = new ArrayList<>(types.length);
// Add all arguments up until the last one
for (int i = 0; i < types.length - 1; i++) {
for (var i = 0; i < types.length - 1; i++) {
argumentsToUse.add(arguments[i]);
}
// Gather all other arguments into an array of the tail type
Object[] varargs = (Object[]) Array.newInstance(tailType.getComponentType(), arguments.length - types.length + 1);
int count = 0;
var varargs = (Object[]) Array.newInstance(tailType.getComponentType(), arguments.length - types.length + 1);
var count = 0;
for (int i = types.length - 1; i < arguments.length; i++) {
for (var i = types.length - 1; i < arguments.length; i++) {
varargs[count++] = arguments[i];
}