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
@@ -21,7 +21,6 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.context.ResourceLoaderAware;
|
||||
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
|
||||
@@ -102,20 +101,20 @@ public class AnnotatedTypeScanner implements ResourceLoaderAware, EnvironmentAwa
|
||||
provider.setEnvironment(environment);
|
||||
}
|
||||
|
||||
for (Class<? extends Annotation> annotationType : annotationTypess) {
|
||||
for (var annotationType : annotationTypess) {
|
||||
provider.addIncludeFilter(new AnnotationTypeFilter(annotationType, true, considerInterfaces));
|
||||
}
|
||||
|
||||
Set<Class<?>> types = new HashSet<>();
|
||||
|
||||
ResourceLoader loader = resourceLoader;
|
||||
ClassLoader classLoader = loader == null ? null : loader.getClassLoader();
|
||||
var loader = resourceLoader;
|
||||
var classLoader = loader == null ? null : loader.getClassLoader();
|
||||
|
||||
for (String basePackage : basePackages) {
|
||||
for (var basePackage : basePackages) {
|
||||
|
||||
for (BeanDefinition definition : provider.findCandidateComponents(basePackage)) {
|
||||
for (var definition : provider.findCandidateComponents(basePackage)) {
|
||||
|
||||
String beanClassName = definition.getBeanClassName();
|
||||
var beanClassName = definition.getBeanClassName();
|
||||
|
||||
if (beanClassName == null) {
|
||||
throw new IllegalStateException(
|
||||
|
||||
@@ -83,7 +83,7 @@ public class AnnotationDetectionFieldCallback implements FieldCallback {
|
||||
*/
|
||||
public Field getRequiredField() {
|
||||
|
||||
Field field = this.field;
|
||||
var field = this.field;
|
||||
|
||||
if (field == null) {
|
||||
throw new IllegalStateException(String.format("No field found for annotation %s!", annotationType));
|
||||
@@ -100,7 +100,7 @@ public class AnnotationDetectionFieldCallback implements FieldCallback {
|
||||
@Nullable
|
||||
public Class<?> getType() {
|
||||
|
||||
Field field = this.field;
|
||||
var field = this.field;
|
||||
|
||||
return field == null ? null : field.getType();
|
||||
}
|
||||
@@ -127,7 +127,7 @@ public class AnnotationDetectionFieldCallback implements FieldCallback {
|
||||
|
||||
Assert.notNull(source, "Source object must not be null!");
|
||||
|
||||
Field field = this.field;
|
||||
var field = this.field;
|
||||
|
||||
if (field == null) {
|
||||
return null;
|
||||
|
||||
@@ -78,7 +78,7 @@ public class AnnotationDetectionMethodCallback<A extends Annotation> implements
|
||||
*/
|
||||
public Method getRequiredMethod() {
|
||||
|
||||
Method method = this.foundMethod;
|
||||
var method = this.foundMethod;
|
||||
|
||||
if (method == null) {
|
||||
throw new IllegalStateException(String.format("No method with annotation %s found!", annotationType));
|
||||
@@ -115,7 +115,7 @@ public class AnnotationDetectionMethodCallback<A extends Annotation> implements
|
||||
return;
|
||||
}
|
||||
|
||||
A foundAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, annotationType);
|
||||
var foundAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, annotationType);
|
||||
|
||||
if (foundAnnotation != null) {
|
||||
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package org.springframework.data.util;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
|
||||
@@ -63,7 +61,7 @@ public abstract class BeanLookup {
|
||||
@Nullable
|
||||
private static <T> T lookupBean(Class<T> type, ListableBeanFactory beanFactory) {
|
||||
|
||||
Map<String, T> names = beanFactory.getBeansOfType(type, false, false);
|
||||
var names = beanFactory.getBeansOfType(type, false, false);
|
||||
|
||||
switch (names.size()) {
|
||||
|
||||
|
||||
@@ -25,7 +25,6 @@ import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.core.GenericTypeResolver;
|
||||
@@ -112,17 +111,17 @@ public class ClassTypeInformation<S> extends TypeDiscoverer<S> {
|
||||
visited.add(type);
|
||||
}
|
||||
|
||||
Map<TypeVariable, Type> source = GenericTypeResolver.getTypeVariableMap(type);
|
||||
var source = GenericTypeResolver.getTypeVariableMap(type);
|
||||
Map<TypeVariable<?>, Type> map = new HashMap<>(source.size());
|
||||
|
||||
for (Entry<TypeVariable, Type> entry : source.entrySet()) {
|
||||
for (var entry : source.entrySet()) {
|
||||
|
||||
Type value = entry.getValue();
|
||||
var value = entry.getValue();
|
||||
map.put(entry.getKey(), entry.getValue());
|
||||
|
||||
if (value instanceof Class) {
|
||||
|
||||
for (Entry<TypeVariable<?>, Type> nestedEntry : getTypeVariableMap((Class<?>) value, visited).entrySet()) {
|
||||
for (var nestedEntry : getTypeVariableMap((Class<?>) value, visited).entrySet()) {
|
||||
if (!map.containsKey(nestedEntry.getKey())) {
|
||||
map.put(nestedEntry.getKey(), nestedEntry.getValue());
|
||||
}
|
||||
|
||||
@@ -17,8 +17,6 @@ package org.springframework.data.util;
|
||||
|
||||
import static org.springframework.util.ReflectionUtils.*;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.springframework.beans.BeanWrapperImpl;
|
||||
import org.springframework.beans.NotReadablePropertyException;
|
||||
import org.springframework.beans.NotWritablePropertyException;
|
||||
@@ -52,7 +50,7 @@ public class DirectFieldAccessFallbackBeanWrapper extends BeanWrapperImpl {
|
||||
return super.getPropertyValue(propertyName);
|
||||
} catch (NotReadablePropertyException e) {
|
||||
|
||||
Field field = findField(getWrappedClass(), propertyName);
|
||||
var field = findField(getWrappedClass(), propertyName);
|
||||
|
||||
if (field == null) {
|
||||
throw new NotReadablePropertyException(getWrappedClass(), propertyName,
|
||||
@@ -75,7 +73,7 @@ public class DirectFieldAccessFallbackBeanWrapper extends BeanWrapperImpl {
|
||||
super.setPropertyValue(propertyName, value);
|
||||
} catch (NotWritablePropertyException e) {
|
||||
|
||||
Field field = findField(getWrappedClass(), propertyName);
|
||||
var field = findField(getWrappedClass(), propertyName);
|
||||
|
||||
if (field == null) {
|
||||
throw new NotWritablePropertyException(getWrappedClass(), propertyName,
|
||||
|
||||
@@ -17,7 +17,6 @@ package org.springframework.data.util;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.GenericArrayType;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import org.springframework.lang.NonNull;
|
||||
|
||||
@@ -61,7 +60,7 @@ class GenericArrayTypeInformation<S> extends ParentTypeAwareTypeInformation<S> {
|
||||
@NonNull
|
||||
protected TypeInformation<?> doGetComponentType() {
|
||||
|
||||
Type componentType = type.getGenericComponentType();
|
||||
var componentType = type.getGenericComponentType();
|
||||
return createInfo(componentType);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,11 +17,9 @@ package org.springframework.data.util;
|
||||
|
||||
import kotlin.jvm.JvmClassMappingKt;
|
||||
import kotlin.reflect.KCallable;
|
||||
import kotlin.reflect.KClass;
|
||||
import kotlin.reflect.KFunction;
|
||||
import kotlin.reflect.KMutableProperty;
|
||||
import kotlin.reflect.KProperty;
|
||||
import kotlin.reflect.KType;
|
||||
import kotlin.reflect.jvm.KTypesJvm;
|
||||
import kotlin.reflect.jvm.ReflectJvmMapping;
|
||||
|
||||
@@ -78,7 +76,7 @@ public final class KotlinReflectionUtils {
|
||||
return false;
|
||||
}
|
||||
|
||||
KClass<?> kotlinClass = JvmClassMappingKt.getKotlinClass(type);
|
||||
var kotlinClass = JvmClassMappingKt.getKotlinClass(type);
|
||||
return kotlinClass.isData();
|
||||
}
|
||||
|
||||
@@ -92,7 +90,7 @@ public final class KotlinReflectionUtils {
|
||||
@Nullable
|
||||
public static KFunction<?> findKotlinFunction(Method method) {
|
||||
|
||||
KFunction<?> kotlinFunction = ReflectJvmMapping.getKotlinFunction(method);
|
||||
var kotlinFunction = ReflectJvmMapping.getKotlinFunction(method);
|
||||
|
||||
// Fallback to own lookup because there's no public Kotlin API for that kind of lookup until
|
||||
// https://youtrack.jetbrains.com/issue/KT-20768 gets resolved.
|
||||
@@ -108,7 +106,7 @@ public final class KotlinReflectionUtils {
|
||||
*/
|
||||
public static boolean isSuspend(Method method) {
|
||||
|
||||
KFunction<?> invokedFunction = KotlinDetector.isKotlinType(method.getDeclaringClass()) ? findKotlinFunction(method)
|
||||
var invokedFunction = KotlinDetector.isKotlinType(method.getDeclaringClass()) ? findKotlinFunction(method)
|
||||
: null;
|
||||
|
||||
return invokedFunction != null && invokedFunction.isSuspend();
|
||||
@@ -122,7 +120,7 @@ public final class KotlinReflectionUtils {
|
||||
*/
|
||||
public static Class<?> getReturnType(Method method) {
|
||||
|
||||
KFunction<?> kotlinFunction = KotlinReflectionUtils.findKotlinFunction(method);
|
||||
var kotlinFunction = KotlinReflectionUtils.findKotlinFunction(method);
|
||||
|
||||
if (kotlinFunction == null) {
|
||||
throw new IllegalArgumentException(String.format("Cannot resolve %s to a KFunction!", method));
|
||||
@@ -140,13 +138,13 @@ public final class KotlinReflectionUtils {
|
||||
*/
|
||||
static boolean isNullable(MethodParameter parameter) {
|
||||
|
||||
Method method = parameter.getMethod();
|
||||
var method = parameter.getMethod();
|
||||
|
||||
if (method == null) {
|
||||
throw new IllegalStateException(String.format("Cannot obtain method from parameter %s!", parameter));
|
||||
}
|
||||
|
||||
KFunction<?> kotlinFunction = findKotlinFunction(method);
|
||||
var kotlinFunction = findKotlinFunction(method);
|
||||
|
||||
if (kotlinFunction == null) {
|
||||
throw new IllegalArgumentException(String.format("Cannot resolve %s to a Kotlin function!", parameter));
|
||||
@@ -160,7 +158,7 @@ public final class KotlinReflectionUtils {
|
||||
// see https://github.com/spring-projects/spring-framework/issues/23991
|
||||
if (kotlinFunction.getParameters().size() > parameter.getParameterIndex() + 1) {
|
||||
|
||||
KType type = parameter.getParameterIndex() == -1 //
|
||||
var type = parameter.getParameterIndex() == -1 //
|
||||
? kotlinFunction.getReturnType() //
|
||||
: kotlinFunction.getParameters().get(parameter.getParameterIndex() + 1).getType();
|
||||
|
||||
@@ -172,7 +170,7 @@ public final class KotlinReflectionUtils {
|
||||
|
||||
private static boolean isLast(MethodParameter parameter) {
|
||||
|
||||
Method method = parameter.getMethod();
|
||||
var method = parameter.getMethod();
|
||||
|
||||
return method != null && parameter.getParameterIndex() == method.getParameterCount() - 1;
|
||||
}
|
||||
@@ -185,7 +183,7 @@ public final class KotlinReflectionUtils {
|
||||
*/
|
||||
private static Optional<? extends KFunction<?>> findKFunction(Method method) {
|
||||
|
||||
KClass<?> kotlinClass = JvmClassMappingKt.getKotlinClass(method.getDeclaringClass());
|
||||
var kotlinClass = JvmClassMappingKt.getKotlinClass(method.getDeclaringClass());
|
||||
|
||||
return kotlinClass.getMembers() //
|
||||
.stream() //
|
||||
@@ -196,15 +194,13 @@ public final class KotlinReflectionUtils {
|
||||
|
||||
private static Stream<? extends KFunction<?>> toKFunctionStream(KCallable<?> it) {
|
||||
|
||||
if (it instanceof KMutableProperty<?>) {
|
||||
if (it instanceof KMutableProperty<?> property) {
|
||||
|
||||
KMutableProperty<?> property = (KMutableProperty<?>) it;
|
||||
return Stream.of(property.getGetter(), property.getSetter());
|
||||
}
|
||||
|
||||
if (it instanceof KProperty<?>) {
|
||||
if (it instanceof KProperty<?> property) {
|
||||
|
||||
KProperty<?> property = (KProperty<?>) it;
|
||||
return Stream.of(property.getGetter());
|
||||
}
|
||||
|
||||
@@ -217,7 +213,7 @@ public final class KotlinReflectionUtils {
|
||||
|
||||
private static boolean isSame(KFunction<?> function, Method method) {
|
||||
|
||||
Method javaMethod = ReflectJvmMapping.getJavaMethod(function);
|
||||
var javaMethod = ReflectJvmMapping.getJavaMethod(function);
|
||||
return javaMethod != null && javaMethod.equals(method);
|
||||
}
|
||||
|
||||
|
||||
@@ -109,7 +109,7 @@ public class Lazy<T> implements Supplier<T> {
|
||||
*/
|
||||
public T get() {
|
||||
|
||||
T value = getNullable();
|
||||
var value = getNullable();
|
||||
|
||||
if (value == null) {
|
||||
throw new IllegalStateException("Expected lazy evaluation to yield a non-null value but got null!");
|
||||
@@ -164,7 +164,7 @@ public class Lazy<T> implements Supplier<T> {
|
||||
@Nullable
|
||||
public T orElse(@Nullable T value) {
|
||||
|
||||
T nullable = getNullable();
|
||||
var nullable = getNullable();
|
||||
|
||||
return nullable == null ? value : nullable;
|
||||
}
|
||||
@@ -181,7 +181,7 @@ public class Lazy<T> implements Supplier<T> {
|
||||
|
||||
Assert.notNull(supplier, "Default value supplier must not be null!");
|
||||
|
||||
T value = getNullable();
|
||||
var value = getNullable();
|
||||
|
||||
return value == null ? supplier.get() : value;
|
||||
}
|
||||
@@ -242,12 +242,10 @@ public class Lazy<T> implements Supplier<T> {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(o instanceof Lazy)) {
|
||||
if (!(o instanceof Lazy<?> lazy)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Lazy<?> lazy = (Lazy<?>) o;
|
||||
|
||||
if (resolved != lazy.resolved) {
|
||||
return false;
|
||||
}
|
||||
@@ -266,7 +264,7 @@ public class Lazy<T> implements Supplier<T> {
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
int result = ObjectUtils.nullSafeHashCode(supplier);
|
||||
var result = ObjectUtils.nullSafeHashCode(supplier);
|
||||
|
||||
result = 31 * result + ObjectUtils.nullSafeHashCode(value);
|
||||
result = 31 * result + (resolved ? 1 : 0);
|
||||
|
||||
@@ -83,9 +83,9 @@ public class MethodInvocationRecorder {
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> Recorded<T> create(Class<T> type) {
|
||||
|
||||
RecordingMethodInterceptor interceptor = new RecordingMethodInterceptor();
|
||||
var interceptor = new RecordingMethodInterceptor();
|
||||
|
||||
ProxyFactory proxyFactory = new ProxyFactory();
|
||||
var proxyFactory = new ProxyFactory();
|
||||
proxyFactory.addAdvice(interceptor);
|
||||
|
||||
if (!type.isInterface()) {
|
||||
@@ -95,7 +95,7 @@ public class MethodInvocationRecorder {
|
||||
proxyFactory.addInterface(type);
|
||||
}
|
||||
|
||||
T proxy = (T) proxyFactory.getProxy(type.getClassLoader());
|
||||
var proxy = (T) proxyFactory.getProxy(type.getClassLoader());
|
||||
|
||||
return new Recorded<T>(proxy, new MethodInvocationRecorder(Optional.ofNullable(interceptor)));
|
||||
}
|
||||
@@ -116,23 +116,23 @@ public class MethodInvocationRecorder {
|
||||
@SuppressWarnings("null")
|
||||
public Object invoke(MethodInvocation invocation) throws Throwable {
|
||||
|
||||
Method method = invocation.getMethod();
|
||||
Object[] arguments = invocation.getArguments();
|
||||
var method = invocation.getMethod();
|
||||
var arguments = invocation.getArguments();
|
||||
|
||||
if (ReflectionUtils.isObjectMethod(method)) {
|
||||
return method.invoke(this, arguments);
|
||||
}
|
||||
|
||||
ResolvableType type = ResolvableType.forMethodReturnType(method);
|
||||
Class<?> rawType = type.resolve(Object.class);
|
||||
var type = ResolvableType.forMethodReturnType(method);
|
||||
var rawType = type.resolve(Object.class);
|
||||
|
||||
if (Collection.class.isAssignableFrom(rawType)) {
|
||||
|
||||
Class<?> clazz = type.getGeneric(0).resolve(Object.class);
|
||||
var clazz = type.getGeneric(0).resolve(Object.class);
|
||||
|
||||
InvocationInformation information = registerInvocation(method, clazz);
|
||||
var information = registerInvocation(method, clazz);
|
||||
|
||||
Collection<Object> collection = CollectionFactory.createCollection(rawType, 1);
|
||||
var collection = CollectionFactory.createCollection(rawType, 1);
|
||||
collection.add(information.getCurrentInstance());
|
||||
|
||||
return collection;
|
||||
@@ -140,10 +140,10 @@ public class MethodInvocationRecorder {
|
||||
|
||||
if (Map.class.isAssignableFrom(rawType)) {
|
||||
|
||||
Class<?> clazz = type.getGeneric(1).resolve(Object.class);
|
||||
InvocationInformation information = registerInvocation(method, clazz);
|
||||
var clazz = type.getGeneric(1).resolve(Object.class);
|
||||
var information = registerInvocation(method, clazz);
|
||||
|
||||
Map<Object, Object> map = CollectionFactory.createMap(rawType, 1);
|
||||
var map = CollectionFactory.createMap(rawType, 1);
|
||||
map.put("_key_", information.getCurrentInstance());
|
||||
|
||||
return map;
|
||||
@@ -158,8 +158,8 @@ public class MethodInvocationRecorder {
|
||||
|
||||
private InvocationInformation registerInvocation(Method method, Class<?> proxyType) {
|
||||
|
||||
Recorded<?> create = Modifier.isFinal(proxyType.getModifiers()) ? new Unrecorded() : create(proxyType);
|
||||
InvocationInformation information = new InvocationInformation(create, method);
|
||||
var create = Modifier.isFinal(proxyType.getModifiers()) ? new Unrecorded() : create(proxyType);
|
||||
var information = new InvocationInformation(create, method);
|
||||
|
||||
return this.information = information;
|
||||
}
|
||||
@@ -187,14 +187,14 @@ public class MethodInvocationRecorder {
|
||||
|
||||
Optional<String> getPropertyPath(List<PropertyNameDetectionStrategy> strategies) {
|
||||
|
||||
Method invokedMethod = this.invokedMethod;
|
||||
var invokedMethod = this.invokedMethod;
|
||||
|
||||
if (invokedMethod == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
String propertyName = getPropertyName(invokedMethod, strategies);
|
||||
Optional<String> next = recorded.getPropertyPath(strategies);
|
||||
var propertyName = getPropertyName(invokedMethod, strategies);
|
||||
var next = recorded.getPropertyPath(strategies);
|
||||
|
||||
return Optionals.firstNonEmpty(() -> next.map(it -> propertyName.concat(".").concat(it)), //
|
||||
() -> Optional.of(propertyName));
|
||||
@@ -229,12 +229,10 @@ public class MethodInvocationRecorder {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(o instanceof InvocationInformation)) {
|
||||
if (!(o instanceof InvocationInformation that)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
InvocationInformation that = (InvocationInformation) o;
|
||||
|
||||
if (!ObjectUtils.nullSafeEquals(recorded, that.recorded)) {
|
||||
return false;
|
||||
}
|
||||
@@ -249,7 +247,7 @@ public class MethodInvocationRecorder {
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
int result = ObjectUtils.nullSafeHashCode(recorded);
|
||||
var result = ObjectUtils.nullSafeHashCode(recorded);
|
||||
|
||||
result = 31 * result + ObjectUtils.nullSafeHashCode(invokedMethod);
|
||||
|
||||
@@ -289,8 +287,8 @@ public class MethodInvocationRecorder {
|
||||
|
||||
private static String getPropertyName(Class<?> type, String methodName) {
|
||||
|
||||
String pattern = getPatternFor(type);
|
||||
String replaced = methodName.replaceFirst(pattern, "");
|
||||
var pattern = getPatternFor(type);
|
||||
var replaced = methodName.replaceFirst(pattern, "");
|
||||
|
||||
return StringUtils.uncapitalize(replaced);
|
||||
}
|
||||
@@ -317,14 +315,14 @@ public class MethodInvocationRecorder {
|
||||
|
||||
public Optional<String> getPropertyPath(PropertyNameDetectionStrategy strategy) {
|
||||
|
||||
MethodInvocationRecorder recorder = this.recorder;
|
||||
var recorder = this.recorder;
|
||||
|
||||
return recorder == null ? Optional.empty() : recorder.getPropertyPath(Arrays.asList(strategy));
|
||||
}
|
||||
|
||||
public Optional<String> getPropertyPath(List<PropertyNameDetectionStrategy> strategies) {
|
||||
|
||||
MethodInvocationRecorder recorder = this.recorder;
|
||||
var recorder = this.recorder;
|
||||
|
||||
return recorder == null ? Optional.empty() : recorder.getPropertyPath(strategies);
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ class MultiValueMapCollector<T, K, V> implements Collector<T, MultiValueMap<K, V
|
||||
|
||||
return (map1, map2) -> {
|
||||
|
||||
for (K key : map2.keySet()) {
|
||||
for (var key : map2.keySet()) {
|
||||
map1.addAll(key, map2.get(key));
|
||||
}
|
||||
|
||||
|
||||
@@ -22,8 +22,6 @@ import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
@@ -36,7 +34,6 @@ import org.springframework.core.annotation.MergedAnnotations;
|
||||
import org.springframework.lang.NonNullApi;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
/**
|
||||
* Utility methods to introspect nullability rules declared in packages, classes and methods.
|
||||
@@ -132,9 +129,9 @@ public abstract class NullableUtils {
|
||||
*/
|
||||
public static boolean isNonNull(AnnotatedElement element, ElementType elementType) {
|
||||
|
||||
for (Annotation annotation : element.getAnnotations()) {
|
||||
for (var annotation : element.getAnnotations()) {
|
||||
|
||||
boolean isNonNull = NON_NULL_ANNOTATION_CLASS.isPresent() ? isNonNull(annotation, elementType)
|
||||
var isNonNull = NON_NULL_ANNOTATION_CLASS.isPresent() ? isNonNull(annotation, elementType)
|
||||
: NON_NULLABLE_ANNOTATIONS.contains(annotation.annotationType());
|
||||
|
||||
if (isNonNull) {
|
||||
@@ -151,7 +148,7 @@ public abstract class NullableUtils {
|
||||
return false;
|
||||
}
|
||||
|
||||
Class<Annotation> annotationClass = NON_NULL_ANNOTATION_CLASS.get();
|
||||
var annotationClass = NON_NULL_ANNOTATION_CLASS.get();
|
||||
|
||||
if (annotation.annotationType().equals(annotationClass)) {
|
||||
return true;
|
||||
@@ -186,9 +183,9 @@ public abstract class NullableUtils {
|
||||
|
||||
private static boolean isExplicitNullable(Annotation[] annotations) {
|
||||
|
||||
for (Annotation annotation : annotations) {
|
||||
for (var annotation : annotations) {
|
||||
|
||||
boolean isNullable = NON_NULL_ANNOTATION_CLASS.isPresent() ? isNullable(annotation)
|
||||
var isNullable = NON_NULL_ANNOTATION_CLASS.isPresent() ? isNullable(annotation)
|
||||
: NULLABLE_ANNOTATIONS.contains(annotation.annotationType());
|
||||
|
||||
if (isNullable) {
|
||||
@@ -227,21 +224,21 @@ public abstract class NullableUtils {
|
||||
|
||||
if (annotation.annotationType().getName().equals(metaAnnotationName)) {
|
||||
|
||||
Map<String, Object> attributes = AnnotationUtils.getAnnotationAttributes(annotation);
|
||||
var attributes = AnnotationUtils.getAnnotationAttributes(annotation);
|
||||
|
||||
return !attributes.isEmpty() && filter.test((T) attributes.get(attribute));
|
||||
}
|
||||
|
||||
MultiValueMap<String, Object> attributes = AnnotatedElementUtils
|
||||
var attributes = AnnotatedElementUtils
|
||||
.getAllAnnotationAttributes(annotation.annotationType(), metaAnnotationName);
|
||||
|
||||
if (attributes == null || attributes.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
List<Object> elementTypes = attributes.get(attribute);
|
||||
var elementTypes = attributes.get(attribute);
|
||||
|
||||
for (Object value : elementTypes) {
|
||||
for (var value : elementTypes) {
|
||||
|
||||
if (filter.test((T) value)) {
|
||||
return true;
|
||||
|
||||
@@ -47,7 +47,7 @@ public class NullableWrapper {
|
||||
*/
|
||||
public Class<?> getValueType() {
|
||||
|
||||
Object value = this.value;
|
||||
var value = this.value;
|
||||
|
||||
return value == null ? Object.class : value.getClass();
|
||||
}
|
||||
|
||||
@@ -107,7 +107,7 @@ public abstract class NullableWrapperConverters {
|
||||
|
||||
return supportsCache.computeIfAbsent(type, key -> {
|
||||
|
||||
for (WrapperType candidate : WRAPPER_TYPES) {
|
||||
for (var candidate : WRAPPER_TYPES) {
|
||||
if (candidate.getType().isAssignableFrom(key)) {
|
||||
return true;
|
||||
}
|
||||
@@ -127,7 +127,7 @@ public abstract class NullableWrapperConverters {
|
||||
|
||||
Assert.notNull(type, "Type must not be null!");
|
||||
|
||||
for (WrapperType candidate : UNWRAPPER_TYPES) {
|
||||
for (var candidate : UNWRAPPER_TYPES) {
|
||||
if (candidate.getType().isAssignableFrom(type)) {
|
||||
return true;
|
||||
}
|
||||
@@ -138,7 +138,7 @@ public abstract class NullableWrapperConverters {
|
||||
|
||||
public static boolean isSingleValue(Class<?> type) {
|
||||
|
||||
for (WrapperType candidate : WRAPPER_TYPES) {
|
||||
for (var candidate : WRAPPER_TYPES) {
|
||||
if (candidate.getType().isAssignableFrom(type)) {
|
||||
return candidate.isSingleValue();
|
||||
}
|
||||
@@ -184,9 +184,9 @@ public abstract class NullableWrapperConverters {
|
||||
return source;
|
||||
}
|
||||
|
||||
for (Converter<Object, Object> converter : UNWRAPPERS) {
|
||||
for (var converter : UNWRAPPERS) {
|
||||
|
||||
Object result = converter.convert(source);
|
||||
var result = converter.convert(source);
|
||||
|
||||
if (result != source) {
|
||||
return result;
|
||||
@@ -206,9 +206,9 @@ public abstract class NullableWrapperConverters {
|
||||
|
||||
Assert.notNull(type, "type must not be null");
|
||||
|
||||
Class<?> rawType = type.getType();
|
||||
var rawType = type.getType();
|
||||
|
||||
boolean needToUnwrap = supports(rawType) //
|
||||
var needToUnwrap = supports(rawType) //
|
||||
|| Stream.class.isAssignableFrom(rawType);
|
||||
|
||||
return needToUnwrap ? unwrapActualType(type.getRequiredComponentType()) : type;
|
||||
@@ -267,8 +267,8 @@ public abstract class NullableWrapperConverters {
|
||||
return null;
|
||||
}
|
||||
|
||||
NullableWrapper wrapper = (NullableWrapper) source;
|
||||
Object value = wrapper.getValue();
|
||||
var wrapper = (NullableWrapper) source;
|
||||
var value = wrapper.getValue();
|
||||
|
||||
return value == null ? nullValue : wrap(value);
|
||||
}
|
||||
@@ -519,12 +519,10 @@ public abstract class NullableWrapperConverters {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(o instanceof WrapperType)) {
|
||||
if (!(o instanceof WrapperType that)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
WrapperType that = (WrapperType) o;
|
||||
|
||||
if (!ObjectUtils.nullSafeEquals(type, that.type)) {
|
||||
return false;
|
||||
}
|
||||
@@ -538,7 +536,7 @@ public abstract class NullableWrapperConverters {
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = ObjectUtils.nullSafeHashCode(type);
|
||||
var result = ObjectUtils.nullSafeHashCode(type);
|
||||
result = 31 * result + ObjectUtils.nullSafeHashCode(cardinality);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -97,12 +97,10 @@ public final class Pair<S, T> {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(o instanceof Pair)) {
|
||||
if (!(o instanceof Pair<?, ?> pair)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Pair<?, ?> pair = (Pair<?, ?>) o;
|
||||
|
||||
if (!ObjectUtils.nullSafeEquals(first, pair.first)) {
|
||||
return false;
|
||||
}
|
||||
@@ -116,7 +114,7 @@ public final class Pair<S, T> {
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = ObjectUtils.nullSafeHashCode(first);
|
||||
var result = ObjectUtils.nullSafeHashCode(first);
|
||||
result = 31 * result + ObjectUtils.nullSafeHashCode(second);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -171,9 +171,9 @@ public class ParameterTypes {
|
||||
return false;
|
||||
}
|
||||
|
||||
Class<?>[] parameterTypes = method.getParameterTypes();
|
||||
var parameterTypes = method.getParameterTypes();
|
||||
|
||||
for (int i = 0; i < parameterTypes.length; i++) {
|
||||
for (var i = 0; i < parameterTypes.length; i++) {
|
||||
if (parameterTypes[i] != types.get(i).getType()) {
|
||||
return false;
|
||||
}
|
||||
@@ -208,7 +208,7 @@ public class ParameterTypes {
|
||||
|
||||
protected Optional<ParameterTypes> withLastVarArgs() {
|
||||
|
||||
TypeDescriptor lastDescriptor = types.get(types.size() - 1);
|
||||
var lastDescriptor = types.get(types.size() - 1);
|
||||
|
||||
return lastDescriptor.isArray() //
|
||||
? Optional.empty() //
|
||||
@@ -218,7 +218,7 @@ public class ParameterTypes {
|
||||
@SuppressWarnings("null")
|
||||
private ParameterTypes withVarArgs(TypeDescriptor descriptor) {
|
||||
|
||||
TypeDescriptor lastDescriptor = types.get(types.size() - 1);
|
||||
var lastDescriptor = types.get(types.size() - 1);
|
||||
|
||||
if (lastDescriptor.isArray() && lastDescriptor.getElementTypeDescriptor().equals(descriptor)) {
|
||||
return this;
|
||||
@@ -240,7 +240,7 @@ public class ParameterTypes {
|
||||
|
||||
withLastVarArgs().ifPresent(alternatives::add);
|
||||
|
||||
ParameterTypes objectVarArgs = withVarArgs(OBJECT_DESCRIPTOR);
|
||||
var objectVarArgs = withVarArgs(OBJECT_DESCRIPTOR);
|
||||
|
||||
if (!alternatives.contains(objectVarArgs)) {
|
||||
alternatives.add(objectVarArgs);
|
||||
@@ -263,9 +263,9 @@ public class ParameterTypes {
|
||||
return false;
|
||||
}
|
||||
|
||||
Class<?>[] parameterTypes = method.getParameterTypes();
|
||||
var parameterTypes = method.getParameterTypes();
|
||||
|
||||
for (int i = 0; i < parameterTypes.length; i++) {
|
||||
for (var i = 0; i < parameterTypes.length; i++) {
|
||||
if (!TypeUtils.isAssignable(parameterTypes[i], types.get(i).getType())) {
|
||||
return false;
|
||||
}
|
||||
@@ -289,12 +289,10 @@ public class ParameterTypes {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(o instanceof ParameterTypes)) {
|
||||
if (!(o instanceof ParameterTypes that)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ParameterTypes that = (ParameterTypes) o;
|
||||
|
||||
return ObjectUtils.nullSafeEquals(types, that.types);
|
||||
}
|
||||
|
||||
@@ -359,7 +357,7 @@ public class ParameterTypes {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(o instanceof ParentParameterTypes)) {
|
||||
if (!(o instanceof ParentParameterTypes that)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -367,8 +365,6 @@ public class ParameterTypes {
|
||||
return false;
|
||||
}
|
||||
|
||||
ParentParameterTypes that = (ParentParameterTypes) o;
|
||||
|
||||
return ObjectUtils.nullSafeEquals(tail, that.tail);
|
||||
}
|
||||
|
||||
@@ -379,7 +375,7 @@ public class ParameterTypes {
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
int result = super.hashCode();
|
||||
var result = super.hashCode();
|
||||
|
||||
result = 31 * result + ObjectUtils.nullSafeHashCode(tail);
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
|
||||
|
||||
if (Map.class.isAssignableFrom(getType())) {
|
||||
|
||||
Type[] arguments = type.getActualTypeArguments();
|
||||
var arguments = type.getActualTypeArguments();
|
||||
|
||||
if (arguments.length > 1) {
|
||||
return createInfo(arguments[1]);
|
||||
@@ -82,13 +82,13 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
|
||||
Optional.ofNullable(rawType.getGenericSuperclass()).ifPresent(supertypes::add);
|
||||
supertypes.addAll(Arrays.asList(rawType.getGenericInterfaces()));
|
||||
|
||||
Optional<TypeInformation<?>> result = supertypes.stream()//
|
||||
var result = supertypes.stream()//
|
||||
.map(it -> Pair.of(it, resolveType(it)))//
|
||||
.filter(it -> Map.class.isAssignableFrom(it.getSecond()))//
|
||||
.<TypeInformation<?>> map(it -> {
|
||||
|
||||
ParameterizedType parameterizedSupertype = (ParameterizedType) it.getFirst();
|
||||
Type[] arguments = parameterizedSupertype.getActualTypeArguments();
|
||||
var parameterizedSupertype = (ParameterizedType) it.getFirst();
|
||||
var arguments = parameterizedSupertype.getActualTypeArguments();
|
||||
return createInfo(arguments[1]);
|
||||
}).findFirst();
|
||||
|
||||
@@ -104,7 +104,7 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
|
||||
|
||||
List<TypeInformation<?>> result = new ArrayList<>();
|
||||
|
||||
for (Type argument : type.getActualTypeArguments()) {
|
||||
for (var argument : type.getActualTypeArguments()) {
|
||||
result.add(createInfo(argument));
|
||||
}
|
||||
|
||||
@@ -122,17 +122,17 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
|
||||
return true;
|
||||
}
|
||||
|
||||
Class<T> rawType = getType();
|
||||
Class<?> rawTargetType = target.getType();
|
||||
var rawType = getType();
|
||||
var rawTargetType = target.getType();
|
||||
|
||||
if (!rawType.isAssignableFrom(rawTargetType)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
TypeInformation<?> otherTypeInformation = rawType.equals(rawTargetType) ? target
|
||||
var otherTypeInformation = rawType.equals(rawTargetType) ? target
|
||||
: target.getSuperTypeInformation(rawType);
|
||||
|
||||
List<TypeInformation<?>> myParameters = getTypeArguments();
|
||||
var myParameters = getTypeArguments();
|
||||
List<TypeInformation<?>> typeParameters = otherTypeInformation == null ? Collections.emptyList()
|
||||
: otherTypeInformation.getTypeArguments();
|
||||
|
||||
@@ -140,7 +140,7 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < myParameters.size(); i++) {
|
||||
for (var i = 0; i < myParameters.size(); i++) {
|
||||
if (!myParameters.get(i).isAssignableFrom(typeParameters.get(i))) {
|
||||
return false;
|
||||
}
|
||||
@@ -157,7 +157,7 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
|
||||
@Nullable
|
||||
protected TypeInformation<?> doGetComponentType() {
|
||||
|
||||
boolean isCustomMapImplementation = isMap() && !getType().equals(Map.class);
|
||||
var isCustomMapImplementation = isMap() && !getType().equals(Map.class);
|
||||
|
||||
if (isCustomMapImplementation) {
|
||||
return getRequiredSuperTypeInformation(Map.class).getComponentType();
|
||||
@@ -178,7 +178,7 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
|
||||
return (TypeInformation<? extends T>) type;
|
||||
}
|
||||
|
||||
TypeInformation<?> asSupertype = type.getSuperTypeInformation(getType());
|
||||
var asSupertype = type.getSuperTypeInformation(getType());
|
||||
|
||||
if (asSupertype == null || !ParameterizedTypeInformation.class.isInstance(asSupertype)) {
|
||||
return super.specialize(type);
|
||||
@@ -200,12 +200,10 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(obj instanceof ParameterizedTypeInformation)) {
|
||||
if (!(obj instanceof ParameterizedTypeInformation<?> that)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ParameterizedTypeInformation<?> that = (ParameterizedTypeInformation<?>) obj;
|
||||
|
||||
if (this.isResolved() && that.isResolved()) {
|
||||
return this.type.equals(that.type);
|
||||
}
|
||||
@@ -239,15 +237,15 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
|
||||
|
||||
private boolean isResolvedCompletely() {
|
||||
|
||||
Type[] typeArguments = type.getActualTypeArguments();
|
||||
var typeArguments = type.getActualTypeArguments();
|
||||
|
||||
if (typeArguments.length == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (Type typeArgument : typeArguments) {
|
||||
for (var typeArgument : typeArguments) {
|
||||
|
||||
TypeInformation<?> info = createInfo(typeArgument);
|
||||
var info = createInfo(typeArgument);
|
||||
|
||||
if (info instanceof ParameterizedTypeInformation) {
|
||||
if (!((ParameterizedTypeInformation<?>) info).isResolvedCompletely()) {
|
||||
@@ -273,9 +271,9 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
|
||||
*/
|
||||
private static Map<TypeVariable<?>, Type> calculateTypeVariables(ParameterizedType type, TypeDiscoverer<?> parent) {
|
||||
|
||||
Class<?> resolvedType = parent.resolveType(type);
|
||||
var resolvedType = parent.resolveType(type);
|
||||
TypeVariable<?>[] typeParameters = resolvedType.getTypeParameters();
|
||||
Type[] arguments = type.getActualTypeArguments();
|
||||
var arguments = type.getActualTypeArguments();
|
||||
|
||||
Map<TypeVariable<?>, Type> localTypeVariables = new HashMap<>(parent.getTypeVariableMap());
|
||||
|
||||
@@ -300,7 +298,7 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
|
||||
return source;
|
||||
}
|
||||
|
||||
Type value = variables.get(source);
|
||||
var value = variables.get(source);
|
||||
|
||||
return value == null ? source : flattenTypeVariable(value, variables);
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ public abstract class ParentTypeAwareTypeInformation<S> extends TypeDiscoverer<S
|
||||
return false;
|
||||
}
|
||||
|
||||
ParentTypeAwareTypeInformation<?> that = (ParentTypeAwareTypeInformation<?>) obj;
|
||||
var that = (ParentTypeAwareTypeInformation<?>) obj;
|
||||
return this.parent == null ? that.parent == null : this.parent.equals(that.parent);
|
||||
}
|
||||
|
||||
|
||||
@@ -80,10 +80,10 @@ public abstract class ParsingUtils {
|
||||
|
||||
Assert.notNull(source, "Source string must not be null!");
|
||||
|
||||
String[] parts = CAMEL_CASE.split(source);
|
||||
var parts = CAMEL_CASE.split(source);
|
||||
List<String> result = new ArrayList<>(parts.length);
|
||||
|
||||
for (String part : parts) {
|
||||
for (var part : parts) {
|
||||
result.add(toLower ? part.toLowerCase() : part);
|
||||
}
|
||||
|
||||
|
||||
@@ -55,9 +55,9 @@ public abstract class ProxyUtils {
|
||||
|
||||
return USER_TYPES.computeIfAbsent(type, it -> {
|
||||
|
||||
Class<?> result = it;
|
||||
var result = it;
|
||||
|
||||
for (ProxyDetector proxyDetector : DETECTORS) {
|
||||
for (var proxyDetector : DETECTORS) {
|
||||
result = proxyDetector.getUserType(result);
|
||||
}
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@ public final class ReflectionUtils {
|
||||
public static <T> T createInstanceIfPresent(String classname, T defaultInstance) {
|
||||
|
||||
try {
|
||||
Class<?> type = ClassUtils.forName(classname, ClassUtils.getDefaultClassLoader());
|
||||
var type = ClassUtils.forName(classname, ClassUtils.getDefaultClassLoader());
|
||||
return (T) BeanUtils.instantiateClass(type);
|
||||
} catch (Exception e) {
|
||||
return defaultInstance;
|
||||
@@ -190,12 +190,12 @@ public final class ReflectionUtils {
|
||||
Assert.notNull(type, "Type must not be null!");
|
||||
Assert.notNull(filter, "Filter must not be null!");
|
||||
|
||||
Class<?> targetClass = type;
|
||||
var targetClass = type;
|
||||
Field foundField = null;
|
||||
|
||||
while (targetClass != Object.class) {
|
||||
|
||||
for (Field field : targetClass.getDeclaredFields()) {
|
||||
for (var field : targetClass.getDeclaredFields()) {
|
||||
|
||||
if (!filter.matches(field)) {
|
||||
continue;
|
||||
@@ -228,7 +228,7 @@ public final class ReflectionUtils {
|
||||
*/
|
||||
public static Field findRequiredField(Class<?> type, String name) {
|
||||
|
||||
Field result = org.springframework.util.ReflectionUtils.findField(type, name);
|
||||
var result = org.springframework.util.ReflectionUtils.findField(type, name);
|
||||
|
||||
if (result == null) {
|
||||
throw new IllegalArgumentException(String.format("Unable to find field %s on %s!", name, type));
|
||||
@@ -283,11 +283,11 @@ public final class ReflectionUtils {
|
||||
Assert.notNull(name, "Method name must not be null");
|
||||
|
||||
Method result = null;
|
||||
Class<?> searchType = type;
|
||||
var searchType = type;
|
||||
while (searchType != null) {
|
||||
Method[] methods = (searchType.isInterface() ? searchType.getMethods()
|
||||
var methods = (searchType.isInterface() ? searchType.getMethods()
|
||||
: org.springframework.util.ReflectionUtils.getDeclaredMethods(searchType));
|
||||
for (Method method : methods) {
|
||||
for (var method : methods) {
|
||||
if (name.equals(method.getName()) && hasSameParams(method, parameterTypes)) {
|
||||
if (result == null || result.isSynthetic() || result.isBridge()) {
|
||||
result = method;
|
||||
@@ -299,7 +299,7 @@ public final class ReflectionUtils {
|
||||
|
||||
if (result == null) {
|
||||
|
||||
String parameterTypeNames = Arrays.stream(parameterTypes) //
|
||||
var parameterTypeNames = Arrays.stream(parameterTypes) //
|
||||
.map(Object::toString) //
|
||||
.collect(Collectors.joining(", "));
|
||||
|
||||
@@ -326,7 +326,7 @@ public final class ReflectionUtils {
|
||||
Assert.notNull(method, "Method must not be null!");
|
||||
|
||||
Stream<Class<?>> returnType = Stream.of(method.getReturnType());
|
||||
Stream<Class<?>> parameterTypes = Arrays.stream(method.getParameterTypes());
|
||||
var parameterTypes = Arrays.stream(method.getParameterTypes());
|
||||
|
||||
return Stream.concat(returnType, parameterTypes);
|
||||
}
|
||||
@@ -350,7 +350,7 @@ public final class ReflectionUtils {
|
||||
.map(ResolvableType::getRawClass)//
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Method method = org.springframework.util.ReflectionUtils.findMethod(type, name,
|
||||
var method = org.springframework.util.ReflectionUtils.findMethod(type, name,
|
||||
collect.toArray(new Class<?>[collect.size()]));
|
||||
|
||||
return Optional.ofNullable(method)//
|
||||
@@ -364,11 +364,11 @@ public final class ReflectionUtils {
|
||||
return false;
|
||||
}
|
||||
|
||||
int index = 0;
|
||||
var index = 0;
|
||||
|
||||
for (Class<?> argumentType : parameterTypes) {
|
||||
for (var argumentType : parameterTypes) {
|
||||
|
||||
Object argument = arguments[index];
|
||||
var argument = arguments[index];
|
||||
|
||||
// Reject nulls for primitives
|
||||
if (argumentType.isPrimitive() && argument == null) {
|
||||
|
||||
@@ -50,9 +50,9 @@ public interface StreamUtils {
|
||||
* @param iterator must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public static <T> Stream<T> createStreamFromIterator(Iterator<T> iterator) {
|
||||
static <T> Stream<T> createStreamFromIterator(Iterator<T> iterator) {
|
||||
|
||||
Spliterator<T> spliterator = Spliterators.spliteratorUnknownSize(iterator, Spliterator.NONNULL);
|
||||
var spliterator = Spliterators.spliteratorUnknownSize(iterator, Spliterator.NONNULL);
|
||||
return StreamSupport.stream(spliterator, false);
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ public interface StreamUtils {
|
||||
* @return
|
||||
* @since 2.0
|
||||
*/
|
||||
public static <T> Stream<T> createStreamFromIterator(CloseableIterator<T> iterator) {
|
||||
static <T> Stream<T> createStreamFromIterator(CloseableIterator<T> iterator) {
|
||||
|
||||
Assert.notNull(iterator, "Iterator must not be null!");
|
||||
|
||||
@@ -76,7 +76,7 @@ public interface StreamUtils {
|
||||
*
|
||||
* @return will never be {@literal null}.
|
||||
*/
|
||||
public static <T> Collector<T, ?, List<T>> toUnmodifiableList() {
|
||||
static <T> Collector<T, ?, List<T>> toUnmodifiableList() {
|
||||
return collectingAndThen(toList(), Collections::unmodifiableList);
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ public interface StreamUtils {
|
||||
*
|
||||
* @return will never be {@literal null}.
|
||||
*/
|
||||
public static <T> Collector<T, ?, Set<T>> toUnmodifiableSet() {
|
||||
static <T> Collector<T, ?, Set<T>> toUnmodifiableSet() {
|
||||
return collectingAndThen(toSet(), Collections::unmodifiableSet);
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ public interface StreamUtils {
|
||||
* @param keyFunction {@link Function} to create a key from an element of the {@link java.util.stream.Stream}
|
||||
* @param valueFunction {@link Function} to create a value from an element of the {@link java.util.stream.Stream}
|
||||
*/
|
||||
public static <T, K, V> Collector<T, MultiValueMap<K, V>, MultiValueMap<K, V>> toMultiMap(Function<T, K> keyFunction,
|
||||
static <T, K, V> Collector<T, MultiValueMap<K, V>, MultiValueMap<K, V>> toMultiMap(Function<T, K> keyFunction,
|
||||
Function<T, V> valueFunction) {
|
||||
return MultiValueMapCollector.of(keyFunction, valueFunction);
|
||||
}
|
||||
@@ -107,7 +107,7 @@ public interface StreamUtils {
|
||||
* @return a new {@link Stream} for the given value returning an empty {@link Stream} if the value is {@literal null}.
|
||||
* @since 2.0.6
|
||||
*/
|
||||
public static <T> Stream<T> fromNullable(@Nullable T source) {
|
||||
static <T> Stream<T> fromNullable(@Nullable T source) {
|
||||
return source == null ? Stream.empty() : Stream.of(source);
|
||||
}
|
||||
|
||||
@@ -122,18 +122,18 @@ public interface StreamUtils {
|
||||
* @return
|
||||
* @since 2.1
|
||||
*/
|
||||
public static <L, R, T> Stream<T> zip(Stream<L> left, Stream<R> right, BiFunction<L, R, T> combiner) {
|
||||
static <L, R, T> Stream<T> zip(Stream<L> left, Stream<R> right, BiFunction<L, R, T> combiner) {
|
||||
|
||||
Assert.notNull(left, "Left stream must not be null!");
|
||||
Assert.notNull(right, "Right must not be null!");
|
||||
Assert.notNull(combiner, "Combiner must not be null!");
|
||||
|
||||
Spliterator<L> lefts = left.spliterator();
|
||||
Spliterator<R> rights = right.spliterator();
|
||||
var lefts = left.spliterator();
|
||||
var rights = right.spliterator();
|
||||
|
||||
long size = Long.min(lefts.estimateSize(), rights.estimateSize());
|
||||
int characteristics = lefts.characteristics() & rights.characteristics();
|
||||
boolean parallel = left.isParallel() || right.isParallel();
|
||||
var size = Long.min(lefts.estimateSize(), rights.estimateSize());
|
||||
var characteristics = lefts.characteristics() & rights.characteristics();
|
||||
var parallel = left.isParallel() || right.isParallel();
|
||||
|
||||
return StreamSupport.stream(new AbstractSpliterator<T>(size, characteristics) {
|
||||
|
||||
@@ -141,16 +141,16 @@ public interface StreamUtils {
|
||||
@SuppressWarnings("null")
|
||||
public boolean tryAdvance(Consumer<? super T> action) {
|
||||
|
||||
Sink<L> leftSink = new Sink<L>();
|
||||
Sink<R> rightSink = new Sink<R>();
|
||||
var leftSink = new Sink<L>();
|
||||
var rightSink = new Sink<R>();
|
||||
|
||||
boolean leftAdvance = lefts.tryAdvance(leftSink);
|
||||
var leftAdvance = lefts.tryAdvance(leftSink);
|
||||
|
||||
if (!leftAdvance) {
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean rightAdvance = rights.tryAdvance(rightSink);
|
||||
var rightAdvance = rights.tryAdvance(rightSink);
|
||||
|
||||
if (!rightAdvance) {
|
||||
return false;
|
||||
|
||||
@@ -17,10 +17,8 @@ package org.springframework.data.util;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.GenericArrayType;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.TypeVariable;
|
||||
@@ -59,7 +57,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
|
||||
static {
|
||||
|
||||
ClassLoader classLoader = TypeDiscoverer.class.getClassLoader();
|
||||
var classLoader = TypeDiscoverer.class.getClassLoader();
|
||||
|
||||
Set<Class<?>> mapTypes = new HashSet<>();
|
||||
mapTypes.add(Map.class);
|
||||
@@ -127,15 +125,13 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
return ClassTypeInformation.from((Class<?>) fieldType);
|
||||
}
|
||||
|
||||
if (fieldType instanceof ParameterizedType) {
|
||||
if (fieldType instanceof ParameterizedType parameterizedType) {
|
||||
|
||||
ParameterizedType parameterizedType = (ParameterizedType) fieldType;
|
||||
return new ParameterizedTypeInformation(parameterizedType, this);
|
||||
}
|
||||
|
||||
if (fieldType instanceof TypeVariable) {
|
||||
if (fieldType instanceof TypeVariable<?> variable) {
|
||||
|
||||
TypeVariable<?> variable = (TypeVariable<?>) fieldType;
|
||||
return new TypeVariableTypeInformation(variable, this);
|
||||
}
|
||||
|
||||
@@ -143,10 +139,9 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
return new GenericArrayTypeInformation((GenericArrayType) fieldType, this);
|
||||
}
|
||||
|
||||
if (fieldType instanceof WildcardType) {
|
||||
if (fieldType instanceof WildcardType wildcardType) {
|
||||
|
||||
WildcardType wildcardType = (WildcardType) fieldType;
|
||||
Type[] bounds = wildcardType.getLowerBounds();
|
||||
var bounds = wildcardType.getLowerBounds();
|
||||
|
||||
if (bounds.length > 0) {
|
||||
return createInfo(bounds[0]);
|
||||
@@ -186,7 +181,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
Assert.notNull(constructor, "Constructor must not be null!");
|
||||
|
||||
List<TypeInformation<?>> parameterTypes = new ArrayList<>(constructor.getParameterCount());
|
||||
for (Parameter parameter : constructor.getParameters()) {
|
||||
for (var parameter : constructor.getParameters()) {
|
||||
parameterTypes.add(createInfo(parameter.getParameterizedType()));
|
||||
}
|
||||
return parameterTypes;
|
||||
@@ -199,14 +194,14 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
@Nullable
|
||||
public TypeInformation<?> getProperty(String fieldname) {
|
||||
|
||||
int separatorIndex = fieldname.indexOf('.');
|
||||
var separatorIndex = fieldname.indexOf('.');
|
||||
|
||||
if (separatorIndex == -1) {
|
||||
return fieldTypes.computeIfAbsent(fieldname, this::getPropertyInformation).orElse(null);
|
||||
}
|
||||
|
||||
String head = fieldname.substring(0, separatorIndex);
|
||||
TypeInformation<?> info = getProperty(head);
|
||||
var head = fieldname.substring(0, separatorIndex);
|
||||
var info = getProperty(head);
|
||||
|
||||
if (info == null) {
|
||||
return null;
|
||||
@@ -227,7 +222,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
private Optional<TypeInformation<?>> getPropertyInformation(String fieldname) {
|
||||
|
||||
Class<?> rawType = getType();
|
||||
Field field = ReflectionUtils.findField(rawType, fieldname);
|
||||
var field = ReflectionUtils.findField(rawType, fieldname);
|
||||
|
||||
if (field != null) {
|
||||
return Optional.of(createInfo(field.getGenericType()));
|
||||
@@ -245,7 +240,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
*/
|
||||
private static Optional<PropertyDescriptor> findPropertyDescriptor(Class<?> type, String fieldname) {
|
||||
|
||||
PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(type, fieldname);
|
||||
var descriptor = BeanUtils.getPropertyDescriptor(type, fieldname);
|
||||
|
||||
if (descriptor != null) {
|
||||
return Optional.of(descriptor);
|
||||
@@ -270,7 +265,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
@Nullable
|
||||
private static Type getGenericType(PropertyDescriptor descriptor) {
|
||||
|
||||
Method method = descriptor.getReadMethod();
|
||||
var method = descriptor.getReadMethod();
|
||||
|
||||
if (method != null) {
|
||||
return method.getGenericReturnType();
|
||||
@@ -282,7 +277,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
return null;
|
||||
}
|
||||
|
||||
Type[] parameterTypes = method.getGenericParameterTypes();
|
||||
var parameterTypes = method.getGenericParameterTypes();
|
||||
return parameterTypes.length == 0 ? null : parameterTypes[0];
|
||||
}
|
||||
|
||||
@@ -333,9 +328,9 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
*/
|
||||
public boolean isMap() {
|
||||
|
||||
Class<S> type = getType();
|
||||
var type = getType();
|
||||
|
||||
for (Class<?> mapType : MAP_TYPES) {
|
||||
for (var mapType : MAP_TYPES) {
|
||||
if (mapType.isAssignableFrom(type)) {
|
||||
return true;
|
||||
}
|
||||
@@ -385,7 +380,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
@Nullable
|
||||
protected TypeInformation<?> doGetComponentType() {
|
||||
|
||||
Class<S> rawType = getType();
|
||||
var rawType = getType();
|
||||
|
||||
if (rawType.isArray()) {
|
||||
return createInfo(rawType.getComponentType());
|
||||
@@ -403,7 +398,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
return getTypeArgument(rawType, 0);
|
||||
}
|
||||
|
||||
List<TypeInformation<?>> arguments = getTypeArguments();
|
||||
var arguments = getTypeArguments();
|
||||
|
||||
return arguments.size() > 0 ? arguments.get(0) : null;
|
||||
}
|
||||
@@ -449,7 +444,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
}
|
||||
|
||||
List<Type> candidates = new ArrayList<>();
|
||||
Type genericSuperclass = rawType.getGenericSuperclass();
|
||||
var genericSuperclass = rawType.getGenericSuperclass();
|
||||
|
||||
if (genericSuperclass != null) {
|
||||
candidates.add(genericSuperclass);
|
||||
@@ -457,15 +452,15 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
|
||||
candidates.addAll(Arrays.asList(rawType.getGenericInterfaces()));
|
||||
|
||||
for (Type candidate : candidates) {
|
||||
for (var candidate : candidates) {
|
||||
|
||||
TypeInformation<?> candidateInfo = createInfo(candidate);
|
||||
var candidateInfo = createInfo(candidate);
|
||||
|
||||
if (superType.equals(candidateInfo.getType())) {
|
||||
return candidateInfo;
|
||||
} else {
|
||||
|
||||
TypeInformation<?> nestedSuperType = candidateInfo.getSuperTypeInformation(superType);
|
||||
var nestedSuperType = candidateInfo.getSuperTypeInformation(superType);
|
||||
|
||||
if (nestedSuperType != null) {
|
||||
return nestedSuperType;
|
||||
@@ -489,7 +484,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
*/
|
||||
public boolean isAssignableFrom(TypeInformation<?> target) {
|
||||
|
||||
TypeInformation<?> superTypeInformation = target.getSuperTypeInformation(getType());
|
||||
var superTypeInformation = target.getSuperTypeInformation(getType());
|
||||
|
||||
return superTypeInformation == null ? false : superTypeInformation.equals(this);
|
||||
}
|
||||
@@ -506,7 +501,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
Assert.isTrue(getType().isAssignableFrom(type.getType()),
|
||||
() -> String.format("%s must be assignable from %s", getType(), type.getType()));
|
||||
|
||||
List<TypeInformation<?>> typeArguments = getTypeArguments();
|
||||
var typeArguments = getTypeArguments();
|
||||
|
||||
return (TypeInformation<? extends S>) (typeArguments.isEmpty() //
|
||||
? type //
|
||||
@@ -516,7 +511,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
@Nullable
|
||||
private TypeInformation<?> getTypeArgument(Class<?> bound, int index) {
|
||||
|
||||
Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(getType(), bound);
|
||||
var arguments = GenericTypeResolver.resolveTypeArguments(getType(), bound);
|
||||
|
||||
if (arguments != null) {
|
||||
return createInfo(arguments[index]);
|
||||
@@ -529,9 +524,9 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
|
||||
private Class<?> getBaseType(Class<?>[] candidates) {
|
||||
|
||||
Class<S> type = getType();
|
||||
var type = getType();
|
||||
|
||||
for (Class<?> candidate : candidates) {
|
||||
for (var candidate : candidates) {
|
||||
if (candidate.isAssignableFrom(type)) {
|
||||
return candidate;
|
||||
}
|
||||
@@ -563,7 +558,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
return false;
|
||||
}
|
||||
|
||||
TypeDiscoverer<?> that = (TypeDiscoverer<?>) obj;
|
||||
var that = (TypeDiscoverer<?>) obj;
|
||||
|
||||
if (!this.type.equals(that.type)) {
|
||||
return false;
|
||||
@@ -627,9 +622,9 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
@Override
|
||||
public Type[] getActualTypeArguments() {
|
||||
|
||||
Type[] result = new Type[typeParameters.size()];
|
||||
var result = new Type[typeParameters.size()];
|
||||
|
||||
for (int i = 0; i < typeParameters.size(); i++) {
|
||||
for (var i = 0; i < typeParameters.size(); i++) {
|
||||
result[i] = typeParameters.get(i).getType();
|
||||
}
|
||||
|
||||
@@ -647,12 +642,10 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(o instanceof SyntheticParamterizedType)) {
|
||||
if (!(o instanceof SyntheticParamterizedType that)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SyntheticParamterizedType that = (SyntheticParamterizedType) o;
|
||||
|
||||
if (!ObjectUtils.nullSafeEquals(typeInformation, that.typeInformation)) {
|
||||
return false;
|
||||
}
|
||||
@@ -666,7 +659,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = ObjectUtils.nullSafeHashCode(typeInformation);
|
||||
var result = ObjectUtils.nullSafeHashCode(typeInformation);
|
||||
result = 31 * result + ObjectUtils.nullSafeHashCode(typeParameters);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ public interface TypeInformation<S> {
|
||||
*/
|
||||
default TypeInformation<?> getRequiredProperty(String property) {
|
||||
|
||||
TypeInformation<?> typeInformation = getProperty(property);
|
||||
var typeInformation = getProperty(property);
|
||||
|
||||
if (typeInformation != null) {
|
||||
return typeInformation;
|
||||
@@ -97,7 +97,7 @@ public interface TypeInformation<S> {
|
||||
*/
|
||||
default TypeInformation<?> getRequiredComponentType() {
|
||||
|
||||
TypeInformation<?> componentType = getComponentType();
|
||||
var componentType = getComponentType();
|
||||
|
||||
if (componentType != null) {
|
||||
return componentType;
|
||||
@@ -133,7 +133,7 @@ public interface TypeInformation<S> {
|
||||
*/
|
||||
default TypeInformation<?> getRequiredMapValueType() {
|
||||
|
||||
TypeInformation<?> mapValueType = getMapValueType();
|
||||
var mapValueType = getMapValueType();
|
||||
|
||||
if (mapValueType != null) {
|
||||
return mapValueType;
|
||||
@@ -191,7 +191,7 @@ public interface TypeInformation<S> {
|
||||
*/
|
||||
default TypeInformation<?> getRequiredActualType() {
|
||||
|
||||
TypeInformation<?> result = getActualType();
|
||||
var result = getActualType();
|
||||
|
||||
if (result == null) {
|
||||
throw new IllegalStateException(
|
||||
@@ -238,7 +238,7 @@ public interface TypeInformation<S> {
|
||||
*/
|
||||
default TypeInformation<?> getRequiredSuperTypeInformation(Class<?> superType) {
|
||||
|
||||
TypeInformation<?> result = getSuperTypeInformation(superType);
|
||||
var result = getSuperTypeInformation(superType);
|
||||
|
||||
if (result == null) {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
|
||||
@@ -61,12 +61,10 @@ class TypeVariableTypeInformation<T> extends ParentTypeAwareTypeInformation<T> {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(obj instanceof TypeVariableTypeInformation)) {
|
||||
if (!(obj instanceof TypeVariableTypeInformation<?> that)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
TypeVariableTypeInformation<?> that = (TypeVariableTypeInformation<?>) obj;
|
||||
|
||||
return getType().equals(that.getType());
|
||||
}
|
||||
|
||||
@@ -77,7 +75,7 @@ class TypeVariableTypeInformation<T> extends ParentTypeAwareTypeInformation<T> {
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
int result = 17;
|
||||
var result = 17;
|
||||
|
||||
result += 31 * nullSafeHashCode(getType());
|
||||
|
||||
|
||||
@@ -68,12 +68,12 @@ public class Version implements Comparable<Version> {
|
||||
|
||||
Assert.hasText(version, "Version must not be null o empty!");
|
||||
|
||||
String[] parts = version.trim().split("\\.");
|
||||
int[] intParts = new int[parts.length];
|
||||
var parts = version.trim().split("\\.");
|
||||
var intParts = new int[parts.length];
|
||||
|
||||
for (int i = 0; i < parts.length; i++) {
|
||||
for (var i = 0; i < parts.length; i++) {
|
||||
|
||||
String input = i == parts.length - 1 ? parts[i].replaceAll("\\D.*", "") : parts[i];
|
||||
var input = i == parts.length - 1 ? parts[i].replaceAll("\\D.*", "") : parts[i];
|
||||
|
||||
if (StringUtils.hasText(input)) {
|
||||
try {
|
||||
@@ -182,12 +182,10 @@ public class Version implements Comparable<Version> {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(obj instanceof Version)) {
|
||||
if (!(obj instanceof Version that)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Version that = (Version) obj;
|
||||
|
||||
return this.major == that.major && this.minor == that.minor && this.bugfix == that.bugfix
|
||||
&& this.build == that.build;
|
||||
}
|
||||
@@ -199,7 +197,7 @@ public class Version implements Comparable<Version> {
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
int result = 17;
|
||||
var result = 17;
|
||||
result += 31 * major;
|
||||
result += 31 * minor;
|
||||
result += 31 * bugfix;
|
||||
|
||||
Reference in New Issue
Block a user