From cb17441780eee8489a26b7772b70cdc77aecacff Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 14 Oct 2021 18:12:27 +0200 Subject: [PATCH] Apply "instanceof pattern matching" Eclipse clean-up in spring-core This commit also applies additional clean-up tasks such as the following. - final fields - diamond operator (<>) for anonymous inner classes This has only been applied to `src/main/java`. --- .../core/CollectionFactory.java | 32 +++++----- .../springframework/core/MethodClassKey.java | 5 +- .../springframework/core/MethodParameter.java | 5 +- .../core/ParameterizedTypeReference.java | 4 +- .../springframework/core/ResolvableType.java | 14 ++-- .../core/annotation/AnnotationUtils.java | 8 +-- .../core/annotation/AnnotationsScanner.java | 5 +- .../MergedAnnotationsCollection.java | 4 +- .../core/annotation/TypeMappedAnnotation.java | 5 +- .../core/convert/Property.java | 5 +- .../core/convert/TypeDescriptor.java | 3 +- .../convert/support/ByteBufferConverter.java | 3 +- .../support/GenericConversionService.java | 5 +- .../support/ObjectToObjectConverter.java | 8 +-- .../io/AbstractFileResolvingResource.java | 3 +- .../core/io/ClassPathResource.java | 3 +- .../core/io/buffer/DataBufferUtils.java | 3 +- .../core/io/buffer/DefaultDataBuffer.java | 3 +- .../core/io/support/EncodedResource.java | 5 +- .../PathMatchingResourcePatternResolver.java | 9 ++- .../springframework/core/log/LogMessage.java | 4 +- .../core/style/DefaultValueStyler.java | 5 +- .../AnnotationReadingVisitorUtils.java | 8 +-- .../ClassMetadataReadingVisitor.java | 4 +- ...impleAnnotationMetadataReadingVisitor.java | 8 +-- .../objenesis/SpringObjenesis.java | 4 +- .../util/ConcurrentReferenceHashMap.java | 9 +-- .../util/LinkedCaseInsensitiveMap.java | 2 +- .../org/springframework/util/MimeType.java | 5 +- .../org/springframework/util/TypeUtils.java | 64 +++++++++---------- .../util/xml/XMLEventStreamReader.java | 5 +- 31 files changed, 108 insertions(+), 142 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/CollectionFactory.java b/spring-core/src/main/java/org/springframework/core/CollectionFactory.java index 4f90a3bff1..a9321e0a34 100644 --- a/spring-core/src/main/java/org/springframework/core/CollectionFactory.java +++ b/spring-core/src/main/java/org/springframework/core/CollectionFactory.java @@ -119,7 +119,7 @@ public final class CollectionFactory { * @see java.util.TreeSet * @see java.util.LinkedHashSet */ - @SuppressWarnings({"rawtypes", "unchecked", "cast"}) + @SuppressWarnings({"rawtypes", "unchecked"}) public static Collection createApproximateCollection(@Nullable Object collection, int capacity) { if (collection instanceof LinkedList) { return new LinkedList<>(); @@ -127,14 +127,13 @@ public final class CollectionFactory { else if (collection instanceof List) { return new ArrayList<>(capacity); } - else if (collection instanceof EnumSet) { - // Cast is necessary for compilation in Eclipse 4.4.1. - Collection enumSet = (Collection) EnumSet.copyOf((EnumSet) collection); - enumSet.clear(); - return enumSet; + else if (collection instanceof EnumSet enumSet) { + Collection copy = EnumSet.copyOf(enumSet); + copy.clear(); + return copy; } - else if (collection instanceof SortedSet) { - return new TreeSet<>(((SortedSet) collection).comparator()); + else if (collection instanceof SortedSet sortedSet) { + return new TreeSet<>(sortedSet.comparator()); } else { return new LinkedHashSet<>(capacity); @@ -178,7 +177,7 @@ public final class CollectionFactory { * @see java.util.TreeSet * @see java.util.EnumSet */ - @SuppressWarnings({"unchecked", "cast"}) + @SuppressWarnings("unchecked") public static Collection createCollection(Class collectionType, @Nullable Class elementType, int capacity) { Assert.notNull(collectionType, "Collection type must not be null"); if (collectionType.isInterface()) { @@ -197,8 +196,7 @@ public final class CollectionFactory { } else if (EnumSet.class.isAssignableFrom(collectionType)) { Assert.notNull(elementType, "Cannot create EnumSet for unknown element type"); - // Cast is necessary for compilation in Eclipse 4.4.1. - return (Collection) EnumSet.noneOf(asEnumType(elementType)); + return EnumSet.noneOf(asEnumType(elementType)); } else { if (!Collection.class.isAssignableFrom(collectionType)) { @@ -243,13 +241,13 @@ public final class CollectionFactory { */ @SuppressWarnings({"rawtypes", "unchecked"}) public static Map createApproximateMap(@Nullable Object map, int capacity) { - if (map instanceof EnumMap) { - EnumMap enumMap = new EnumMap((EnumMap) map); - enumMap.clear(); - return enumMap; + if (map instanceof EnumMap enumMap) { + EnumMap copy = new EnumMap(enumMap); + copy.clear(); + return copy; } - else if (map instanceof SortedMap) { - return new TreeMap<>(((SortedMap) map).comparator()); + else if (map instanceof SortedMap sortedMap) { + return new TreeMap<>(sortedMap.comparator()); } else { return new LinkedHashMap<>(capacity); diff --git a/spring-core/src/main/java/org/springframework/core/MethodClassKey.java b/spring-core/src/main/java/org/springframework/core/MethodClassKey.java index 62ca1ea04f..60e28be1e0 100644 --- a/spring-core/src/main/java/org/springframework/core/MethodClassKey.java +++ b/spring-core/src/main/java/org/springframework/core/MethodClassKey.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2021 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. @@ -54,10 +54,9 @@ public final class MethodClassKey implements Comparable { if (this == other) { return true; } - if (!(other instanceof MethodClassKey)) { + if (!(other instanceof MethodClassKey otherKey)) { return false; } - MethodClassKey otherKey = (MethodClassKey) other; return (this.method.equals(otherKey.method) && ObjectUtils.nullSafeEquals(this.targetClass, otherKey.targetClass)); } diff --git a/spring-core/src/main/java/org/springframework/core/MethodParameter.java b/spring-core/src/main/java/org/springframework/core/MethodParameter.java index 3ec9549cb6..3bbe10d5d4 100644 --- a/spring-core/src/main/java/org/springframework/core/MethodParameter.java +++ b/spring-core/src/main/java/org/springframework/core/MethodParameter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -753,10 +753,9 @@ public class MethodParameter { if (this == other) { return true; } - if (!(other instanceof MethodParameter)) { + if (!(other instanceof MethodParameter otherParam)) { return false; } - MethodParameter otherParam = (MethodParameter) other; return (getContainingClass() == otherParam.getContainingClass() && ObjectUtils.nullSafeEquals(this.typeIndexesPerLevel, otherParam.typeIndexesPerLevel) && this.nestingLevel == otherParam.nestingLevel && diff --git a/spring-core/src/main/java/org/springframework/core/ParameterizedTypeReference.java b/spring-core/src/main/java/org/springframework/core/ParameterizedTypeReference.java index c10dd89aee..2cb589eb74 100644 --- a/spring-core/src/main/java/org/springframework/core/ParameterizedTypeReference.java +++ b/spring-core/src/main/java/org/springframework/core/ParameterizedTypeReference.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 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. @@ -92,7 +92,7 @@ public abstract class ParameterizedTypeReference { * @since 4.3.12 */ public static ParameterizedTypeReference forType(Type type) { - return new ParameterizedTypeReference(type) { + return new ParameterizedTypeReference<>(type) { }; } diff --git a/spring-core/src/main/java/org/springframework/core/ResolvableType.java b/spring-core/src/main/java/org/springframework/core/ResolvableType.java index ae6fe4f084..17a9572ccd 100644 --- a/spring-core/src/main/java/org/springframework/core/ResolvableType.java +++ b/spring-core/src/main/java/org/springframework/core/ResolvableType.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -594,8 +594,7 @@ public class ResolvableType implements Serializable { * without specific bounds (i.e., equal to {@code ? extends Object}). */ private boolean isWildcardWithoutBounds() { - if (this.type instanceof WildcardType) { - WildcardType wt = (WildcardType) this.type; + if (this.type instanceof WildcardType wt) { if (wt.getLowerBounds().length == 0) { Type[] upperBounds = wt.getUpperBounds(); if (upperBounds.length == 0 || (upperBounds.length == 1 && Object.class == upperBounds[0])) { @@ -870,8 +869,7 @@ public class ResolvableType implements Serializable { if (this.type instanceof TypeVariable) { return resolveType().resolveVariable(variable); } - if (this.type instanceof ParameterizedType) { - ParameterizedType parameterizedType = (ParameterizedType) this.type; + if (this.type instanceof ParameterizedType parameterizedType) { Class resolved = resolve(); if (resolved == null) { return null; @@ -906,11 +904,10 @@ public class ResolvableType implements Serializable { if (this == other) { return true; } - if (!(other instanceof ResolvableType)) { + if (!(other instanceof ResolvableType otherType)) { return false; } - ResolvableType otherType = (ResolvableType) other; if (!ObjectUtils.nullSafeEquals(this.type, otherType.type)) { return false; } @@ -1573,10 +1570,9 @@ public class ResolvableType implements Serializable { if (this == other) { return true; } - if (!(other instanceof ParameterizedType)) { + if (!(other instanceof ParameterizedType otherType)) { return false; } - ParameterizedType otherType = (ParameterizedType) other; return (otherType.getOwnerType() == null && this.rawType.equals(otherType.getRawType()) && Arrays.equals(this.typeArguments, otherType.getActualTypeArguments())); } diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java index edd0996241..2009d848b8 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -1006,12 +1006,10 @@ public abstract class AnnotationUtils { return names; } } - if (value instanceof Annotation) { - Annotation annotation = (Annotation) value; + if (value instanceof Annotation annotation) { return MergedAnnotation.from(annotatedElement, annotation).synthesize(); } - if (value instanceof Annotation[]) { - Annotation[] annotations = (Annotation[]) value; + if (value instanceof Annotation[] annotations) { Annotation[] synthesized = (Annotation[]) Array.newInstance( annotations.getClass().getComponentType(), annotations.length); for (int i = 0; i < annotations.length; i++) { diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationsScanner.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationsScanner.java index 626413838c..19d520c3c1 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationsScanner.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationsScanner.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -521,8 +521,7 @@ abstract class AnnotationsScanner { return (searchStrategy == SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES ? noSuperTypes && sourceClass.getEnclosingClass() == null : noSuperTypes); } - if (source instanceof Method) { - Method sourceMethod = (Method) source; + if (source instanceof Method sourceMethod) { return (Modifier.isPrivate(sourceMethod.getModifiers()) || isWithoutHierarchy(sourceMethod.getDeclaringClass(), searchStrategy)); } diff --git a/spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotationsCollection.java b/spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotationsCollection.java index 9fc6cb5367..0d2e8372d0 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotationsCollection.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotationsCollection.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -221,7 +221,7 @@ final class MergedAnnotationsCollection implements MergedAnnotations { private class AnnotationsSpliterator implements Spliterator> { @Nullable - private Object requiredType; + private final Object requiredType; private final int[] mappingCursors; diff --git a/spring-core/src/main/java/org/springframework/core/annotation/TypeMappedAnnotation.java b/spring-core/src/main/java/org/springframework/core/annotation/TypeMappedAnnotation.java index 06aa93d7d5..660e43b869 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/TypeMappedAnnotation.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/TypeMappedAnnotation.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -439,8 +439,7 @@ final class TypeMappedAnnotation extends AbstractMergedAnn } value = names; } - else if (value instanceof String[] && type == Class[].class) { - String[] names = (String[]) value; + else if (value instanceof String[] names && type == Class[].class) { Class[] classes = new Class[names.length]; for (int i = 0; i < names.length; i++) { classes[i] = ClassUtils.resolveClassName(names[i], getClassLoader()); diff --git a/spring-core/src/main/java/org/springframework/core/convert/Property.java b/spring-core/src/main/java/org/springframework/core/convert/Property.java index 5cddaea876..afabd528fb 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/Property.java +++ b/spring-core/src/main/java/org/springframework/core/convert/Property.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -263,10 +263,9 @@ public final class Property { if (this == other) { return true; } - if (!(other instanceof Property)) { + if (!(other instanceof Property otherProperty)) { return false; } - Property otherProperty = (Property) other; return (ObjectUtils.nullSafeEquals(this.objectType, otherProperty.objectType) && ObjectUtils.nullSafeEquals(this.name, otherProperty.name) && ObjectUtils.nullSafeEquals(this.readMethod, otherProperty.readMethod) && diff --git a/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java b/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java index 1bc8cb281b..064579c82f 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java +++ b/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java @@ -458,10 +458,9 @@ public class TypeDescriptor implements Serializable { if (this == other) { return true; } - if (!(other instanceof TypeDescriptor)) { + if (!(other instanceof TypeDescriptor otherDesc)) { return false; } - TypeDescriptor otherDesc = (TypeDescriptor) other; if (getType() != otherDesc.getType()) { return false; } diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/ByteBufferConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/ByteBufferConverter.java index 0a230ef076..64e4971797 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/ByteBufferConverter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/ByteBufferConverter.java @@ -88,8 +88,7 @@ final class ByteBufferConverter implements ConditionalGenericConverter { @Nullable public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { boolean byteBufferTarget = targetType.isAssignableTo(BYTE_BUFFER_TYPE); - if (source instanceof ByteBuffer) { - ByteBuffer buffer = (ByteBuffer) source; + if (source instanceof ByteBuffer buffer) { return (byteBufferTarget ? buffer.duplicate() : convertFromByteBuffer(buffer, targetType)); } if (byteBufferTarget) { diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java b/spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java index 1134483e88..0c0e9c0364 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -463,10 +463,9 @@ public class GenericConversionService implements ConfigurableConversionService { if (this == other) { return true; } - if (!(other instanceof ConverterCacheKey)) { + if (!(other instanceof ConverterCacheKey otherKey)) { return false; } - ConverterCacheKey otherKey = (ConverterCacheKey) other; return (this.sourceType.equals(otherKey.sourceType)) && this.targetType.equals(otherKey.targetType); } diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/ObjectToObjectConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/ObjectToObjectConverter.java index 802e097008..145e28806f 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/ObjectToObjectConverter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/ObjectToObjectConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2021 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. @@ -92,8 +92,7 @@ final class ObjectToObjectConverter implements ConditionalGenericConverter { Member member = getValidatedMember(targetClass, sourceClass); try { - if (member instanceof Method) { - Method method = (Method) member; + if (member instanceof Method method) { ReflectionUtils.makeAccessible(method); if (!Modifier.isStatic(method.getModifiers())) { return method.invoke(source); @@ -152,8 +151,7 @@ final class ObjectToObjectConverter implements ConditionalGenericConverter { } private static boolean isApplicable(Member member, Class sourceClass) { - if (member instanceof Method) { - Method method = (Method) member; + if (member instanceof Method method) { return (!Modifier.isStatic(method.getModifiers()) ? ClassUtils.isAssignable(method.getDeclaringClass(), sourceClass) : method.getParameterTypes()[0] == sourceClass); diff --git a/spring-core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java b/spring-core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java index ac9ea29a5d..5369b2fdea 100644 --- a/spring-core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java @@ -106,8 +106,7 @@ public abstract class AbstractFileResolvingResource extends AbstractResource { // Try InputStream resolution for jar resources URLConnection con = url.openConnection(); customizeConnection(con); - if (con instanceof HttpURLConnection) { - HttpURLConnection httpCon = (HttpURLConnection) con; + if (con instanceof HttpURLConnection httpCon) { int code = httpCon.getResponseCode(); if (code != HttpURLConnection.HTTP_OK) { httpCon.disconnect(); diff --git a/spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java b/spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java index f99adce06c..cbaad7001b 100644 --- a/spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java @@ -267,10 +267,9 @@ public class ClassPathResource extends AbstractFileResolvingResource { if (this == other) { return true; } - if (!(other instanceof ClassPathResource)) { + if (!(other instanceof ClassPathResource otherRes)) { return false; } - ClassPathResource otherRes = (ClassPathResource) other; return (this.path.equals(otherRes.path) && ObjectUtils.nullSafeEquals(this.classLoader, otherRes.classLoader) && ObjectUtils.nullSafeEquals(this.clazz, otherRes.clazz)); diff --git a/spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferUtils.java b/spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferUtils.java index ad0419ee4b..d97a59fe45 100644 --- a/spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferUtils.java +++ b/spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferUtils.java @@ -513,8 +513,7 @@ public abstract class DataBufferUtils { * @return {@code true} if the buffer was released; {@code false} otherwise. */ public static boolean release(@Nullable DataBuffer dataBuffer) { - if (dataBuffer instanceof PooledDataBuffer) { - PooledDataBuffer pooledDataBuffer = (PooledDataBuffer) dataBuffer; + if (dataBuffer instanceof PooledDataBuffer pooledDataBuffer) { if (pooledDataBuffer.isAllocated()) { try { return pooledDataBuffer.release(); diff --git a/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java b/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java index ab6c80a25f..2efc753ab6 100644 --- a/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java +++ b/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java @@ -431,10 +431,9 @@ public class DefaultDataBuffer implements DataBuffer { if (this == other) { return true; } - if (!(other instanceof DefaultDataBuffer)) { + if (!(other instanceof DefaultDataBuffer otherBuffer)) { return false; } - DefaultDataBuffer otherBuffer = (DefaultDataBuffer) other; return (this.readPosition == otherBuffer.readPosition && this.writePosition == otherBuffer.writePosition && this.byteBuffer.equals(otherBuffer.byteBuffer)); diff --git a/spring-core/src/main/java/org/springframework/core/io/support/EncodedResource.java b/spring-core/src/main/java/org/springframework/core/io/support/EncodedResource.java index f718fe73cf..ed4eb169da 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/EncodedResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/EncodedResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2021 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. @@ -165,10 +165,9 @@ public class EncodedResource implements InputStreamSource { if (this == other) { return true; } - if (!(other instanceof EncodedResource)) { + if (!(other instanceof EncodedResource otherResource)) { return false; } - EncodedResource otherResource = (EncodedResource) other; return (this.resource.equals(otherResource.resource) && ObjectUtils.nullSafeEquals(this.charset, otherResource.charset) && ObjectUtils.nullSafeEquals(this.encoding, otherResource.encoding)); diff --git a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java index 69cd661348..aef2476b0a 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -368,9 +368,9 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol * @since 4.1.1 */ protected void addAllClassLoaderJarRoots(@Nullable ClassLoader classLoader, Set result) { - if (classLoader instanceof URLClassLoader) { + if (classLoader instanceof URLClassLoader urlClassLoader) { try { - for (URL url : ((URLClassLoader) classLoader).getURLs()) { + for (URL url : urlClassLoader.getURLs()) { try { UrlResource jarResource = (ResourceUtils.URL_PROTOCOL_JAR.equals(url.getProtocol()) ? new UrlResource(url) : @@ -597,9 +597,8 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol String rootEntryPath; boolean closeJarFile; - if (con instanceof JarURLConnection) { + if (con instanceof JarURLConnection jarCon) { // Should usually be the case for traditional JAR files. - JarURLConnection jarCon = (JarURLConnection) con; ResourceUtils.useCachesIfNecessary(jarCon); jarFile = jarCon.getJarFile(); jarFileUrl = jarCon.getJarFileURL().toExternalForm(); diff --git a/spring-core/src/main/java/org/springframework/core/log/LogMessage.java b/spring-core/src/main/java/org/springframework/core/log/LogMessage.java index ee7dd65f28..4546f7a76a 100644 --- a/spring-core/src/main/java/org/springframework/core/log/LogMessage.java +++ b/spring-core/src/main/java/org/springframework/core/log/LogMessage.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 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. @@ -143,7 +143,7 @@ public abstract class LogMessage implements CharSequence { private static final class SupplierMessage extends LogMessage { - private Supplier supplier; + private final Supplier supplier; SupplierMessage(Supplier supplier) { Assert.notNull(supplier, "Supplier must not be null"); diff --git a/spring-core/src/main/java/org/springframework/core/style/DefaultValueStyler.java b/spring-core/src/main/java/org/springframework/core/style/DefaultValueStyler.java index f56ac82af9..2b968d1130 100644 --- a/spring-core/src/main/java/org/springframework/core/style/DefaultValueStyler.java +++ b/spring-core/src/main/java/org/springframework/core/style/DefaultValueStyler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 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. @@ -61,8 +61,7 @@ public class DefaultValueStyler implements ValueStyler { else if (value instanceof Class) { return ClassUtils.getShortName((Class) value); } - else if (value instanceof Method) { - Method method = (Method) value; + else if (value instanceof Method method) { return method.getName() + "@" + ClassUtils.getShortName(method.getDeclaringClass()); } else if (value instanceof Map) { diff --git a/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationReadingVisitorUtils.java b/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationReadingVisitorUtils.java index d7c47ea266..98bb301ba4 100644 --- a/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationReadingVisitorUtils.java +++ b/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationReadingVisitorUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 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. @@ -61,8 +61,7 @@ abstract class AnnotationReadingVisitorUtils { value = convertClassValues( annotatedElement, classLoader, (AnnotationAttributes) value, classValuesAsString); } - else if (value instanceof AnnotationAttributes[]) { - AnnotationAttributes[] values = (AnnotationAttributes[]) value; + else if (value instanceof AnnotationAttributes[] values) { for (int i = 0; i < values.length; i++) { values[i] = convertClassValues(annotatedElement, classLoader, values[i], classValuesAsString); } @@ -72,8 +71,7 @@ abstract class AnnotationReadingVisitorUtils { value = (classValuesAsString ? ((Type) value).getClassName() : ClassUtils.forName(((Type) value).getClassName(), classLoader)); } - else if (value instanceof Type[]) { - Type[] array = (Type[]) value; + else if (value instanceof Type[] array) { Object[] convArray = (classValuesAsString ? new String[array.length] : new Class[array.length]); for (int i = 0; i < array.length; i++) { diff --git a/spring-core/src/main/java/org/springframework/core/type/classreading/ClassMetadataReadingVisitor.java b/spring-core/src/main/java/org/springframework/core/type/classreading/ClassMetadataReadingVisitor.java index aca2ecd8b7..c80520cb5e 100644 --- a/spring-core/src/main/java/org/springframework/core/type/classreading/ClassMetadataReadingVisitor.java +++ b/spring-core/src/main/java/org/springframework/core/type/classreading/ClassMetadataReadingVisitor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 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,7 +69,7 @@ class ClassMetadataReadingVisitor extends ClassVisitor implements ClassMetadata private String[] interfaces = new String[0]; - private Set memberClassNames = new LinkedHashSet<>(4); + private final Set memberClassNames = new LinkedHashSet<>(4); public ClassMetadataReadingVisitor() { diff --git a/spring-core/src/main/java/org/springframework/core/type/classreading/SimpleAnnotationMetadataReadingVisitor.java b/spring-core/src/main/java/org/springframework/core/type/classreading/SimpleAnnotationMetadataReadingVisitor.java index 6422cda991..2875b1e263 100644 --- a/spring-core/src/main/java/org/springframework/core/type/classreading/SimpleAnnotationMetadataReadingVisitor.java +++ b/spring-core/src/main/java/org/springframework/core/type/classreading/SimpleAnnotationMetadataReadingVisitor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 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. @@ -59,11 +59,11 @@ final class SimpleAnnotationMetadataReadingVisitor extends ClassVisitor { private boolean independentInnerClass; - private Set memberClassNames = new LinkedHashSet<>(4); + private final Set memberClassNames = new LinkedHashSet<>(4); - private List> annotations = new ArrayList<>(); + private final List> annotations = new ArrayList<>(); - private List annotatedMethods = new ArrayList<>(); + private final List annotatedMethods = new ArrayList<>(); @Nullable private SimpleAnnotationMetadata metadata; diff --git a/spring-core/src/main/java/org/springframework/objenesis/SpringObjenesis.java b/spring-core/src/main/java/org/springframework/objenesis/SpringObjenesis.java index 9984211efc..031ef027f0 100644 --- a/spring-core/src/main/java/org/springframework/objenesis/SpringObjenesis.java +++ b/spring-core/src/main/java/org/springframework/objenesis/SpringObjenesis.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2021 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,10 +102,12 @@ public class SpringObjenesis implements Objenesis { return getInstantiatorOf(clazz).newInstance(); } + @Override public T newInstance(Class clazz) { return getInstantiatorOf(clazz).newInstance(); } + @Override @SuppressWarnings("unchecked") public ObjectInstantiator getInstantiatorOf(Class clazz) { ObjectInstantiator instantiator = this.cache.get(clazz); diff --git a/spring-core/src/main/java/org/springframework/util/ConcurrentReferenceHashMap.java b/spring-core/src/main/java/org/springframework/util/ConcurrentReferenceHashMap.java index 3291ca6e07..47f44b188c 100644 --- a/spring-core/src/main/java/org/springframework/util/ConcurrentReferenceHashMap.java +++ b/spring-core/src/main/java/org/springframework/util/ConcurrentReferenceHashMap.java @@ -762,10 +762,9 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen if (this == other) { return true; } - if (!(other instanceof Map.Entry)) { + if (!(other instanceof Map.Entry otherEntry)) { return false; } - Map.Entry otherEntry = (Map.Entry) other; return (ObjectUtils.nullSafeEquals(getKey(), otherEntry.getKey()) && ObjectUtils.nullSafeEquals(getValue(), otherEntry.getValue())); } @@ -853,8 +852,7 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen @Override public boolean contains(@Nullable Object o) { - if (o instanceof Map.Entry) { - Map.Entry entry = (Map.Entry) o; + if (o instanceof Map.Entry entry) { Reference ref = ConcurrentReferenceHashMap.this.getReference(entry.getKey(), Restructure.NEVER); Entry otherEntry = (ref != null ? ref.get() : null); if (otherEntry != null) { @@ -866,8 +864,7 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen @Override public boolean remove(Object o) { - if (o instanceof Map.Entry) { - Map.Entry entry = (Map.Entry) o; + if (o instanceof Map.Entry entry) { return ConcurrentReferenceHashMap.this.remove(entry.getKey(), entry.getValue()); } return false; diff --git a/spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java b/spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java index 4689d53ee1..8deb9bf9c0 100644 --- a/spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java +++ b/spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java @@ -110,7 +110,7 @@ public class LinkedCaseInsensitiveMap implements Map, Serializable * @see #convertKey(String) */ public LinkedCaseInsensitiveMap(int expectedSize, @Nullable Locale locale) { - this.targetMap = new LinkedHashMap( + this.targetMap = new LinkedHashMap<>( (int) (expectedSize / CollectionUtils.DEFAULT_LOAD_FACTOR), CollectionUtils.DEFAULT_LOAD_FACTOR) { @Override public boolean containsKey(Object key) { diff --git a/spring-core/src/main/java/org/springframework/util/MimeType.java b/spring-core/src/main/java/org/springframework/util/MimeType.java index aa4ef6f53a..d77b2f5711 100644 --- a/spring-core/src/main/java/org/springframework/util/MimeType.java +++ b/spring-core/src/main/java/org/springframework/util/MimeType.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -453,10 +453,9 @@ public class MimeType implements Comparable, Serializable { if (this == other) { return true; } - if (!(other instanceof MimeType)) { + if (!(other instanceof MimeType otherType)) { return false; } - MimeType otherType = (MimeType) other; return (this.type.equalsIgnoreCase(otherType.type) && this.subtype.equalsIgnoreCase(otherType.subtype) && parametersAreEqual(otherType)); diff --git a/spring-core/src/main/java/org/springframework/util/TypeUtils.java b/spring-core/src/main/java/org/springframework/util/TypeUtils.java index 4a959aef52..0b77077955 100644 --- a/spring-core/src/main/java/org/springframework/util/TypeUtils.java +++ b/spring-core/src/main/java/org/springframework/util/TypeUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2021 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. @@ -25,11 +25,12 @@ import org.springframework.lang.Nullable; /** * Utility to work with Java 5 generic type parameters. - * Mainly for internal use within the framework. + *

Mainly for internal use within the framework. * * @author Ramnivas Laddad * @author Juergen Hoeller * @author Chris Beams + * @author Sam Brannen * @since 2.0.7 */ public abstract class TypeUtils { @@ -50,62 +51,56 @@ public abstract class TypeUtils { return true; } - if (lhsType instanceof Class) { - Class lhsClass = (Class) lhsType; - + if (lhsType instanceof Class lhsClass) { // just comparing two classes - if (rhsType instanceof Class) { - return ClassUtils.isAssignable(lhsClass, (Class) rhsType); + if (rhsType instanceof Class rhsClass) { + return ClassUtils.isAssignable(lhsClass, rhsClass); } - if (rhsType instanceof ParameterizedType) { - Type rhsRaw = ((ParameterizedType) rhsType).getRawType(); + if (rhsType instanceof ParameterizedType rhsParameterizedType) { + Type rhsRaw = rhsParameterizedType.getRawType(); // a parameterized type is always assignable to its raw class type - if (rhsRaw instanceof Class) { - return ClassUtils.isAssignable(lhsClass, (Class) rhsRaw); + if (rhsRaw instanceof Class rhRawClass) { + return ClassUtils.isAssignable(lhsClass, rhRawClass); } } - else if (lhsClass.isArray() && rhsType instanceof GenericArrayType) { - Type rhsComponent = ((GenericArrayType) rhsType).getGenericComponentType(); + else if (lhsClass.isArray() && rhsType instanceof GenericArrayType rhsGenericArrayType) { + Type rhsComponent = rhsGenericArrayType.getGenericComponentType(); return isAssignable(lhsClass.getComponentType(), rhsComponent); } } // parameterized types are only assignable to other parameterized types and class types - if (lhsType instanceof ParameterizedType) { - if (rhsType instanceof Class) { - Type lhsRaw = ((ParameterizedType) lhsType).getRawType(); + if (lhsType instanceof ParameterizedType lhsParameterizedType) { + if (rhsType instanceof Class rhsClass) { + Type lhsRaw = lhsParameterizedType.getRawType(); - if (lhsRaw instanceof Class) { - return ClassUtils.isAssignable((Class) lhsRaw, (Class) rhsType); + if (lhsRaw instanceof Class lhsClass) { + return ClassUtils.isAssignable(lhsClass, rhsClass); } } - else if (rhsType instanceof ParameterizedType) { - return isAssignable((ParameterizedType) lhsType, (ParameterizedType) rhsType); + else if (rhsType instanceof ParameterizedType rhsParameterizedType) { + return isAssignable(lhsParameterizedType, rhsParameterizedType); } } - if (lhsType instanceof GenericArrayType) { - Type lhsComponent = ((GenericArrayType) lhsType).getGenericComponentType(); + if (lhsType instanceof GenericArrayType lhsGenericArrayType) { + Type lhsComponent = lhsGenericArrayType.getGenericComponentType(); - if (rhsType instanceof Class) { - Class rhsClass = (Class) rhsType; - - if (rhsClass.isArray()) { - return isAssignable(lhsComponent, rhsClass.getComponentType()); - } + if (rhsType instanceof Class rhsClass && rhsClass.isArray()) { + return isAssignable(lhsComponent, rhsClass.getComponentType()); } - else if (rhsType instanceof GenericArrayType) { - Type rhsComponent = ((GenericArrayType) rhsType).getGenericComponentType(); + else if (rhsType instanceof GenericArrayType rhsGenericArrayType) { + Type rhsComponent = rhsGenericArrayType.getGenericComponentType(); return isAssignable(lhsComponent, rhsComponent); } } - if (lhsType instanceof WildcardType) { - return isAssignable((WildcardType) lhsType, rhsType); + if (lhsType instanceof WildcardType lhsWildcardType) { + return isAssignable(lhsWildcardType, rhsType); } return false; @@ -128,7 +123,7 @@ public abstract class TypeUtils { Type rhsArg = rhsTypeArguments[i]; if (!lhsArg.equals(rhsArg) && - !(lhsArg instanceof WildcardType && isAssignable((WildcardType) lhsArg, rhsArg))) { + !(lhsArg instanceof WildcardType wildcardType && isAssignable(wildcardType, rhsArg))) { return false; } } @@ -151,11 +146,10 @@ public abstract class TypeUtils { lLowerBounds = new Type[] { null }; } - if (rhsType instanceof WildcardType) { + if (rhsType instanceof WildcardType rhsWcType) { // both the upper and lower bounds of the right-hand side must be // completely enclosed in the upper and lower bounds of the left- // hand side. - WildcardType rhsWcType = (WildcardType) rhsType; Type[] rUpperBounds = rhsWcType.getUpperBounds(); if (rUpperBounds.length == 0) { diff --git a/spring-core/src/main/java/org/springframework/util/xml/XMLEventStreamReader.java b/spring-core/src/main/java/org/springframework/util/xml/XMLEventStreamReader.java index 7a27d683c2..7b1ba92bfb 100644 --- a/spring-core/src/main/java/org/springframework/util/xml/XMLEventStreamReader.java +++ b/spring-core/src/main/java/org/springframework/util/xml/XMLEventStreamReader.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 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. @@ -22,6 +22,7 @@ import javax.xml.namespace.NamespaceContext; import javax.xml.namespace.QName; import javax.xml.stream.Location; import javax.xml.stream.XMLEventReader; +import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.Attribute; import javax.xml.stream.events.Comment; @@ -155,7 +156,7 @@ class XMLEventStreamReader extends AbstractXMLStreamReader { if (this.event.isCharacters()) { return this.event.asCharacters().getData(); } - else if (this.event.getEventType() == XMLEvent.COMMENT) { + else if (this.event.getEventType() == XMLStreamConstants.COMMENT) { return ((Comment) this.event).getText(); } else {