Skip to content

Commit

Permalink
Apply "instanceof pattern matching" Eclipse clean-up in spring-core
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
sbrannen committed Oct 14, 2021
1 parent 9b9906c commit cb17441
Show file tree
Hide file tree
Showing 31 changed files with 108 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,21 @@ public static boolean isApproximableCollectionType(@Nullable Class<?> collection
* @see java.util.TreeSet
* @see java.util.LinkedHashSet
*/
@SuppressWarnings({"rawtypes", "unchecked", "cast"})
@SuppressWarnings({"rawtypes", "unchecked"})
public static <E> Collection<E> createApproximateCollection(@Nullable Object collection, int capacity) {
if (collection instanceof LinkedList) {
return new LinkedList<>();
}
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<E> enumSet = (Collection<E>) EnumSet.copyOf((EnumSet) collection);
enumSet.clear();
return enumSet;
else if (collection instanceof EnumSet enumSet) {
Collection<E> copy = EnumSet.copyOf(enumSet);
copy.clear();
return copy;
}
else if (collection instanceof SortedSet) {
return new TreeSet<>(((SortedSet<E>) collection).comparator());
else if (collection instanceof SortedSet sortedSet) {
return new TreeSet<>(sortedSet.comparator());
}
else {
return new LinkedHashSet<>(capacity);
Expand Down Expand Up @@ -178,7 +177,7 @@ public static <E> Collection<E> createCollection(Class<?> collectionType, int ca
* @see java.util.TreeSet
* @see java.util.EnumSet
*/
@SuppressWarnings({"unchecked", "cast"})
@SuppressWarnings("unchecked")
public static <E> Collection<E> createCollection(Class<?> collectionType, @Nullable Class<?> elementType, int capacity) {
Assert.notNull(collectionType, "Collection type must not be null");
if (collectionType.isInterface()) {
Expand All @@ -197,8 +196,7 @@ else if (SortedSet.class == collectionType || NavigableSet.class == collectionTy
}
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<E>) EnumSet.noneOf(asEnumType(elementType));
return EnumSet.noneOf(asEnumType(elementType));
}
else {
if (!Collection.class.isAssignableFrom(collectionType)) {
Expand Down Expand Up @@ -243,13 +241,13 @@ public static boolean isApproximableMapType(@Nullable Class<?> mapType) {
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public static <K, V> Map<K, V> 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<K, V>) map).comparator());
else if (map instanceof SortedMap sortedMap) {
return new TreeMap<>(sortedMap.comparator());
}
else {
return new LinkedHashMap<>(capacity);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -54,10 +54,9 @@ public boolean equals(@Nullable Object other) {
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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -753,10 +753,9 @@ public boolean equals(@Nullable Object other) {
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 &&
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -92,7 +92,7 @@ public String toString() {
* @since 4.3.12
*/
public static <T> ParameterizedTypeReference<T> forType(Type type) {
return new ParameterizedTypeReference<T>(type) {
return new ParameterizedTypeReference<>(type) {
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -594,8 +594,7 @@ private boolean isUnresolvableTypeVariable() {
* 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])) {
Expand Down Expand Up @@ -870,8 +869,7 @@ private ResolvableType resolveVariable(TypeVariable<?> variable) {
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;
Expand Down Expand Up @@ -906,11 +904,10 @@ public boolean equals(@Nullable Object other) {
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;
}
Expand Down Expand Up @@ -1573,10 +1570,9 @@ public boolean equals(@Nullable Object other) {
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()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -1006,12 +1006,10 @@ private static Object adaptValue(
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++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -521,8 +521,7 @@ private static boolean isWithoutHierarchy(AnnotatedElement source, SearchStrateg
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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -221,7 +221,7 @@ static MergedAnnotations of(Collection<MergedAnnotation<?>> annotations) {
private class AnnotationsSpliterator<A extends Annotation> implements Spliterator<MergedAnnotation<A>> {

@Nullable
private Object requiredType;
private final Object requiredType;

private final int[] mappingCursors;

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -439,8 +439,7 @@ else if (value instanceof Class[] && type == String[].class) {
}
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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -263,10 +263,9 @@ public boolean equals(@Nullable Object other) {
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) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,9 @@ public boolean equals(@Nullable Object other) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ private boolean matchesToByteBuffer(TypeDescriptor sourceType) {
@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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -463,10 +463,9 @@ public boolean equals(@Nullable Object other) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -92,8 +92,7 @@ public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDe
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);
Expand Down Expand Up @@ -152,8 +151,7 @@ private static Member getValidatedMember(Class<?> targetClass, Class<?> sourceCl
}

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ boolean checkReadable(URL url) {
// 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,9 @@ public boolean equals(@Nullable Object other) {
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,7 @@ public static <T extends DataBuffer> T touch(T dataBuffer, Object hint) {
* @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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,9 @@ public boolean equals(@Nullable Object other) {
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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -165,10 +165,9 @@ public boolean equals(@Nullable Object other) {
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));
Expand Down

0 comments on commit cb17441

Please sign in to comment.