Apply "instanceof pattern matching" in remainder of spring-core module

See gh-30067
This commit is contained in:
Sam Brannen
2023-03-05 17:21:32 +01:00
parent 162c09d036
commit 8ebd746d69
28 changed files with 74 additions and 83 deletions

View File

@@ -139,7 +139,7 @@ public class BeanGenerator extends AbstractClassGenerator
@Override
protected Object nextInstance(Object instance) {
Class protoclass = (instance instanceof Class) ? (Class)instance : instance.getClass();
Class protoclass = (instance instanceof Class<?> clazz) ? clazz : instance.getClass();
if (classOnly) {
return protoclass;
} else {

View File

@@ -367,15 +367,14 @@ public class EmitUtils {
if (obj == null) {
e.aconst_null();
} else {
Class type = obj.getClass();
if (type.isArray()) {
push_array(e, (Object[])obj);
} else if (obj instanceof String) {
e.push((String)obj);
} else if (obj instanceof Type) {
load_class(e, (Type)obj);
} else if (obj instanceof Class) {
load_class(e, Type.getType((Class)obj));
if (obj.getClass().isArray()) {
push_array(e, (Object[]) obj);
} else if (obj instanceof String text) {
e.push(text);
} else if (obj instanceof Type type) {
load_class(e, type);
} else if (obj instanceof Class<?> clazz) {
load_class(e, Type.getType(clazz));
} else if (obj instanceof BigInteger) {
e.new_instance(Constants.TYPE_BIG_INTEGER);
e.dup();

View File

@@ -69,8 +69,8 @@ public class LoadingCache<K, KK, V> {
throw new IllegalStateException("Interrupted while loading cache item", e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) {
throw ((RuntimeException) cause);
if (cause instanceof RuntimeException runtimeException) {
throw runtimeException;
}
throw new IllegalStateException("Unable to load cache item", cause);
}

View File

@@ -154,10 +154,10 @@ abstract public class FastClass
@Override
public boolean equals(Object o) {
if (o == null || !(o instanceof FastClass)) {
if (o == null || !(o instanceof FastClass that)) {
return false;
}
return type.equals(((FastClass)o).type);
return type.equals(that.type);
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -105,9 +105,9 @@ public abstract class AttributeAccessorSupport implements AttributeAccessor, Ser
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof AttributeAccessorSupport &&
this.attributes.equals(((AttributeAccessorSupport) other).attributes)));
public boolean equals(@Nullable Object obj) {
return (this == obj || (obj instanceof AttributeAccessorSupport that &&
this.attributes.equals(that.attributes)));
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -111,10 +111,10 @@ public class Constants {
*/
public Number asNumber(String code) throws ConstantException {
Object obj = asObject(code);
if (!(obj instanceof Number)) {
if (!(obj instanceof Number number)) {
throw new ConstantException(this.className, code, "not a Number");
}
return (Number) obj;
return number;
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -172,9 +172,8 @@ public final class GenericTypeResolver {
ResolvableType contextType = ResolvableType.forClass(contextClass);
for (int i = 0; i < typeArguments.length; i++) {
Type typeArgument = typeArguments[i];
if (typeArgument instanceof TypeVariable) {
ResolvableType resolvedTypeArgument = resolveVariable(
(TypeVariable<?>) typeArgument, contextType);
if (typeArgument instanceof TypeVariable<?> typeVariable) {
ResolvableType resolvedTypeArgument = resolveVariable(typeVariable, contextType);
if (resolvedTypeArgument != ResolvableType.NONE) {
generics[i] = resolvedTypeArgument.resolve();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -195,7 +195,7 @@ public class MethodParameter {
*/
@Nullable
public Method getMethod() {
return (this.executable instanceof Method ? (Method) this.executable : null);
return (this.executable instanceof Method method ? method : null);
}
/**
@@ -205,7 +205,7 @@ public class MethodParameter {
*/
@Nullable
public Constructor<?> getConstructor() {
return (this.executable instanceof Constructor ? (Constructor<?>) this.executable : null);
return (this.executable instanceof Constructor<?> constructor ? constructor : null);
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -142,7 +142,7 @@ public class OrderComparator implements Comparator<Object> {
*/
@Nullable
protected Integer findOrder(Object obj) {
return (obj instanceof Ordered ? ((Ordered) obj).getOrder() : null);
return (obj instanceof Ordered ordered ? ordered.getOrder() : null);
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -67,9 +67,8 @@ public abstract class ParameterizedTypeReference<T> {
}
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof ParameterizedTypeReference &&
this.type.equals(((ParameterizedTypeReference<?>) other).type)));
public boolean equals(@Nullable Object obj) {
return (this == obj || (obj instanceof ParameterizedTypeReference<?> that && this.type.equals(that.type)));
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -149,7 +149,7 @@ public class ReactiveAdapterRegistry {
return null;
}
Object sourceToUse = (source instanceof Optional ? ((Optional<?>) source).orElse(null) : source);
Object sourceToUse = (source instanceof Optional<?> optional ? optional.orElse(null) : source);
Class<?> clazz = (sourceToUse != null ? sourceToUse.getClass() : reactiveType);
if (clazz == null) {
return null;

View File

@@ -215,7 +215,7 @@ public class ResolvableType implements Serializable {
if (rawType instanceof ParameterizedType parameterizedType) {
rawType = parameterizedType.getRawType();
}
return (rawType instanceof Class ? (Class<?>) rawType : null);
return (rawType instanceof Class<?> rawClass ? rawClass : null);
}
/**
@@ -376,7 +376,7 @@ public class ResolvableType implements Serializable {
if (this == NONE) {
return false;
}
return ((this.type instanceof Class && ((Class<?>) this.type).isArray()) ||
return ((this.type instanceof Class<?> clazz && clazz.isArray()) ||
this.type instanceof GenericArrayType || resolveType().isArray());
}
@@ -1647,13 +1647,12 @@ public class ResolvableType implements Serializable {
@Nullable
public static WildcardBounds get(ResolvableType type) {
ResolvableType resolveToWildcard = type;
while (!(resolveToWildcard.getType() instanceof WildcardType)) {
while (!(resolveToWildcard.getType() instanceof WildcardType wildcardType)) {
if (resolveToWildcard == NONE) {
return null;
}
resolveToWildcard = resolveToWildcard.resolveType();
}
WildcardType wildcardType = (WildcardType) resolveToWildcard.type;
Kind boundsType = (wildcardType.getLowerBounds().length > 0 ? Kind.LOWER : Kind.UPPER);
Type[] bounds = (boundsType == Kind.UPPER ? wildcardType.getUpperBounds() : wildcardType.getLowerBounds());
ResolvableType[] resolvableBounds = new ResolvableType[bounds.length];

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -352,7 +352,7 @@ final class SerializableTypeWrapper {
// Cache the result for further calls to getType()
this.result = result;
}
return (result instanceof Type[] ? ((Type[]) result)[this.index] : (Type) result);
return (result instanceof Type[] results ? results[this.index] : (Type) result);
}
@Override

View File

@@ -70,7 +70,7 @@ public class AnnotationAwareOrderComparator extends OrderComparator {
@Nullable
private Integer findOrderFromAnnotation(Object obj) {
AnnotatedElement element = (obj instanceof AnnotatedElement ? (AnnotatedElement) obj : obj.getClass());
AnnotatedElement element = (obj instanceof AnnotatedElement ae ? ae : obj.getClass());
MergedAnnotations annotations = MergedAnnotations.from(element, SearchStrategy.TYPE_HIERARCHY);
Integer order = OrderUtils.getOrderFromAnnotations(element, annotations);
if (order == null && obj instanceof DecoratingProxy decoratingProxy) {

View File

@@ -107,7 +107,7 @@ public interface Decoder<T> {
catch (InterruptedException ex) {
failure = ex;
}
throw (failure instanceof CodecException ? (CodecException) failure :
throw (failure instanceof CodecException codecException ? codecException :
new DecodingException("Failed to decode: " + failure.getMessage(), failure));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -779,9 +779,9 @@ public class TypeDescriptor implements Serializable {
}
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof AnnotatedElementAdapter &&
Arrays.equals(this.annotations, ((AnnotatedElementAdapter) other).annotations)));
public boolean equals(@Nullable Object obj) {
return (this == obj || (obj instanceof AnnotatedElementAdapter that &&
Arrays.equals(this.annotations, that.annotations)));
}
@Override

View File

@@ -76,7 +76,7 @@ final class ObjectToOptionalConverter implements ConditionalGenericConverter {
else if (targetType.getResolvableType().hasGenerics()) {
Object target = this.conversionService.convert(source, sourceType, new GenericTypeDescriptor(targetType));
if (target == null || (target.getClass().isArray() && Array.getLength(target) == 0) ||
(target instanceof Collection && ((Collection<?>) target).isEmpty())) {
(target instanceof Collection<?> collection && collection.isEmpty())) {
return Optional.empty();
}
return Optional.of(target);

View File

@@ -337,9 +337,8 @@ public final class Netty5DataBuffer implements CloseableDataBuffer, TouchableDat
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof Netty5DataBuffer dataBuffer &&
this.buffer.equals(dataBuffer.buffer)));
public boolean equals(@Nullable Object obj) {
return (this == obj || (obj instanceof Netty5DataBuffer that && this.buffer.equals(that.buffer)));
}
@Override

View File

@@ -363,9 +363,8 @@ public class NettyDataBuffer implements PooledDataBuffer {
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof NettyDataBuffer &&
this.byteBuf.equals(((NettyDataBuffer) other).byteBuf)));
public boolean equals(@Nullable Object obj) {
return (this == obj || (obj instanceof NettyDataBuffer that && this.byteBuf.equals(that.byteBuf)));
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -122,8 +122,8 @@ public class StandardClassMetadata implements ClassMetadata {
@Override
public boolean equals(@Nullable Object obj) {
return ((this == obj) || ((obj instanceof StandardClassMetadata) &&
getIntrospectedClass().equals(((StandardClassMetadata) obj).getIntrospectedClass())));
return (this == obj || (obj instanceof StandardClassMetadata that &&
getIntrospectedClass().equals(that.getIntrospectedClass())));
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -151,8 +151,8 @@ public class StandardMethodMetadata implements MethodMetadata {
@Override
public boolean equals(@Nullable Object obj) {
return ((this == obj) || ((obj instanceof StandardMethodMetadata) &&
this.introspectedMethod.equals(((StandardMethodMetadata) obj).introspectedMethod)));
return (this == obj || (obj instanceof StandardMethodMetadata that &&
this.introspectedMethod.equals(that.introspectedMethod)));
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -164,8 +164,7 @@ final class SimpleAnnotationMetadata implements AnnotationMetadata {
@Override
public boolean equals(@Nullable Object obj) {
return ((this == obj) || ((obj instanceof SimpleAnnotationMetadata) &&
this.className.equals(((SimpleAnnotationMetadata) obj).className)));
return (this == obj || (obj instanceof SimpleAnnotationMetadata that && this.className.equals(that.className)));
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -102,8 +102,7 @@ final class SimpleMethodMetadata implements MethodMetadata {
@Override
public boolean equals(@Nullable Object obj) {
return ((this == obj) || ((obj instanceof SimpleMethodMetadata) &&
this.source.equals(((SimpleMethodMetadata) obj).source)));
return (this == obj || (obj instanceof SimpleMethodMetadata that && this.source.equals(that.source)));
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -361,8 +361,8 @@ public abstract class CollectionUtils {
if (isEmpty(set)) {
return null;
}
if (set instanceof SortedSet) {
return ((SortedSet<T>) set).first();
if (set instanceof SortedSet<T> sortedSet) {
return sortedSet.first();
}
Iterator<T> it = set.iterator();
@@ -402,8 +402,8 @@ public abstract class CollectionUtils {
if (isEmpty(set)) {
return null;
}
if (set instanceof SortedSet) {
return ((SortedSet<T>) set).last();
if (set instanceof SortedSet<T> sortedSet) {
return sortedSet.last();
}
// Full iteration necessary...

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -154,7 +154,7 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
@Override
public boolean containsKey(Object key) {
return (key instanceof String && this.caseInsensitiveKeys.containsKey(convertKey((String) key)));
return (key instanceof String string && this.caseInsensitiveKeys.containsKey(convertKey(string)));
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -246,8 +246,8 @@ public abstract class NumberUtils {
Assert.notNull(targetClass, "Target class must not be null");
DecimalFormat decimalFormat = null;
boolean resetBigDecimal = false;
if (numberFormat instanceof DecimalFormat) {
decimalFormat = (DecimalFormat) numberFormat;
if (numberFormat instanceof DecimalFormat dc) {
decimalFormat = dc;
if (BigDecimal.class == targetClass && !decimalFormat.isParseBigDecimal()) {
decimalFormat.setParseBigDecimal(true);
resetBigDecimal = true;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -510,7 +510,7 @@ public abstract class StringUtils {
*/
@Nullable
public static Object quoteIfString(@Nullable Object obj) {
return (obj instanceof String ? quote((String) obj) : obj);
return (obj instanceof String text ? quote(text) : obj);
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -69,9 +69,8 @@ public class BooleanComparator implements Comparator<Boolean>, Serializable {
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof BooleanComparator &&
this.trueLow == ((BooleanComparator) other).trueLow));
public boolean equals(@Nullable Object obj) {
return (this == obj || (obj instanceof BooleanComparator that && this.trueLow == that.trueLow));
}
@Override