From 5c540e5390383e0c17acd9b9acc4ee3649aee8fb Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 18 Mar 2022 17:24:01 +0100 Subject: [PATCH] Apply "instanceof pattern matching" in core annotation support --- .../annotation/AnnotatedElementUtils.java | 4 +- .../core/annotation/AnnotationAttributes.java | 14 +++---- .../AnnotationAwareOrderComparator.java | 22 +++++----- .../annotation/AnnotationTypeMapping.java | 16 ++++---- .../core/annotation/AnnotationUtils.java | 17 ++++---- .../core/annotation/AnnotationsScanner.java | 21 +++++----- .../MergedAnnotationPredicates.java | 4 +- .../core/annotation/OrderUtils.java | 4 +- .../SynthesizingMethodParameter.java | 8 ++-- .../core/annotation/TypeMappedAnnotation.java | 41 ++++++++----------- 10 files changed, 72 insertions(+), 79 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java index 850cee07d21..1033e2d10c8 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 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. @@ -794,7 +794,7 @@ public abstract class AnnotatedElementUtils { /** - * Adapted {@link AnnotatedElement} that hold specific annotations. + * Adapted {@link AnnotatedElement} that holds specific annotations. */ private static class AnnotatedElementForAnnotations implements AnnotatedElement { diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAttributes.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAttributes.java index d864a4cb1e1..febfc54a6f1 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAttributes.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAttributes.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 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. @@ -369,10 +369,10 @@ public class AnnotationAttributes extends LinkedHashMap { } private void assertNotException(String attributeName, Object attributeValue) { - if (attributeValue instanceof Throwable) { + if (attributeValue instanceof Throwable throwable) { throw new IllegalArgumentException(String.format( "Attribute '%s' for annotation [%s] was not resolvable due to exception [%s]", - attributeName, this.displayName, attributeValue), (Throwable) attributeValue); + attributeName, this.displayName, attributeValue), throwable); } } @@ -406,8 +406,8 @@ public class AnnotationAttributes extends LinkedHashMap { if (value == this) { return "(this Map)"; } - if (value instanceof Object[]) { - return "[" + StringUtils.arrayToDelimitedString((Object[]) value, ", ") + "]"; + if (value instanceof Object[] objects) { + return "[" + StringUtils.arrayToDelimitedString(objects, ", ") + "]"; } return String.valueOf(value); } @@ -426,8 +426,8 @@ public class AnnotationAttributes extends LinkedHashMap { if (map == null) { return null; } - if (map instanceof AnnotationAttributes) { - return (AnnotationAttributes) map; + if (map instanceof AnnotationAttributes annotationAttributes) { + return annotationAttributes; } return new AnnotationAttributes(map); } diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAwareOrderComparator.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAwareOrderComparator.java index a57a0179c3e..172275f8b46 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAwareOrderComparator.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationAwareOrderComparator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 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. @@ -73,8 +73,8 @@ public class AnnotationAwareOrderComparator extends OrderComparator { AnnotatedElement element = (obj instanceof AnnotatedElement ? (AnnotatedElement) obj : obj.getClass()); MergedAnnotations annotations = MergedAnnotations.from(element, SearchStrategy.TYPE_HIERARCHY); Integer order = OrderUtils.getOrderFromAnnotations(element, annotations); - if (order == null && obj instanceof DecoratingProxy) { - return findOrderFromAnnotation(((DecoratingProxy) obj).getDecoratedClass()); + if (order == null && obj instanceof DecoratingProxy decoratingProxy) { + return findOrderFromAnnotation(decoratingProxy.getDecoratedClass()); } return order; } @@ -88,12 +88,12 @@ public class AnnotationAwareOrderComparator extends OrderComparator { @Override @Nullable public Integer getPriority(Object obj) { - if (obj instanceof Class) { - return OrderUtils.getPriority((Class) obj); + if (obj instanceof Class clazz) { + return OrderUtils.getPriority(clazz); } Integer priority = OrderUtils.getPriority(obj.getClass()); - if (priority == null && obj instanceof DecoratingProxy) { - return getPriority(((DecoratingProxy) obj).getDecoratedClass()); + if (priority == null && obj instanceof DecoratingProxy decoratingProxy) { + return getPriority(decoratingProxy.getDecoratedClass()); } return priority; } @@ -134,11 +134,11 @@ public class AnnotationAwareOrderComparator extends OrderComparator { * @see java.util.Arrays#sort(Object[], java.util.Comparator) */ public static void sortIfNecessary(Object value) { - if (value instanceof Object[]) { - sort((Object[]) value); + if (value instanceof Object[] objects) { + sort(objects); } - else if (value instanceof List) { - sort((List) value); + else if (value instanceof List list) { + sort(list); } } diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMapping.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMapping.java index 77302db78ae..fd90775eb99 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMapping.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMapping.java @@ -561,14 +561,14 @@ final class AnnotationTypeMapping { if (ObjectUtils.nullSafeEquals(value, extractedValue)) { return true; } - if (value instanceof Class && extractedValue instanceof String) { - return areEquivalent((Class) value, (String) extractedValue); + if (value instanceof Class clazz && extractedValue instanceof String string) { + return areEquivalent(clazz, string); } - if (value instanceof Class[] && extractedValue instanceof String[]) { - return areEquivalent((Class[]) value, (String[]) extractedValue); + if (value instanceof Class[] classes && extractedValue instanceof String[] strings) { + return areEquivalent(classes, strings); } - if (value instanceof Annotation) { - return areEquivalent((Annotation) value, extractedValue, valueExtractor); + if (value instanceof Annotation annotation) { + return areEquivalent(annotation, extractedValue, valueExtractor); } return false; } @@ -597,8 +597,8 @@ final class AnnotationTypeMapping { Method attribute = attributes.get(i); Object value1 = ReflectionUtils.invokeMethod(attribute, annotation); Object value2; - if (extractedValue instanceof TypeMappedAnnotation) { - value2 = ((TypeMappedAnnotation) extractedValue).getValue(attribute.getName()).orElse(null); + if (extractedValue instanceof TypeMappedAnnotation typeMappedAnnotation) { + value2 = typeMappedAnnotation.getValue(attribute.getName()).orElse(null); } else { value2 = valueExtractor.extract(attribute, extractedValue); 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 2009d848b8c..6525aeb1983 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-2021 the original author or authors. + * Copyright 2002-2022 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. @@ -976,8 +976,8 @@ public abstract class AnnotationUtils { for (Map.Entry attributeEntry : attributes.entrySet()) { String attributeName = attributeEntry.getKey(); Object value = attributeEntry.getValue(); - if (value instanceof DefaultValueHolder) { - value = ((DefaultValueHolder) value).defaultValue; + if (value instanceof DefaultValueHolder defaultValueHolder) { + value = defaultValueHolder.defaultValue; attributes.put(attributeName, adaptValue(annotatedElement, value, classValuesAsString)); } @@ -986,7 +986,7 @@ public abstract class AnnotationUtils { private static Object getAttributeValueForMirrorResolution(Method attribute, Object attributes) { Object result = ((AnnotationAttributes) attributes).get(attribute.getName()); - return (result instanceof DefaultValueHolder ? ((DefaultValueHolder) result).defaultValue : result); + return (result instanceof DefaultValueHolder defaultValueHolder ? defaultValueHolder.defaultValue : result); } @Nullable @@ -994,11 +994,10 @@ public abstract class AnnotationUtils { @Nullable Object annotatedElement, @Nullable Object value, boolean classValuesAsString) { if (classValuesAsString) { - if (value instanceof Class) { - return ((Class) value).getName(); + if (value instanceof Class clazz) { + return clazz.getName(); } - if (value instanceof Class[]) { - Class[] classes = (Class[]) value; + if (value instanceof Class[] classes) { String[] names = new String[classes.length]; for (int i = 0; i < classes.length; i++) { names[i] = classes[i].getName(); @@ -1099,7 +1098,7 @@ public abstract class AnnotationUtils { rethrowAnnotationConfigurationException(ex); IntrospectionFailureLogger logger = IntrospectionFailureLogger.INFO; boolean meta = false; - if (element instanceof Class && Annotation.class.isAssignableFrom((Class) element)) { + if (element instanceof Class clazz && Annotation.class.isAssignableFrom(clazz)) { // Meta-annotation or (default) value lookup on an annotation type logger = IntrospectionFailureLogger.DEBUG; meta = true; 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 fa90a703b4e..27eae73cd55 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 @@ -82,11 +82,11 @@ abstract class AnnotationsScanner { private static R process(C context, AnnotatedElement source, SearchStrategy searchStrategy, AnnotationsProcessor processor) { - if (source instanceof Class) { - return processClass(context, (Class) source, searchStrategy, processor); + if (source instanceof Class clazz) { + return processClass(context, clazz, searchStrategy, processor); } - if (source instanceof Method) { - return processMethod(context, (Method) source, searchStrategy, processor); + if (source instanceof Method method) { + return processMethod(context, method, searchStrategy, processor); } return processElement(context, source, processor); } @@ -477,7 +477,7 @@ abstract class AnnotationsScanner { return true; } if (searchStrategy == SearchStrategy.DIRECT || isWithoutHierarchy(source, searchStrategy)) { - if (source instanceof Method && ((Method) source).isBridge()) { + if (source instanceof Method method && method.isBridge()) { return false; } return getDeclaredAnnotations(source, false).length == 0; @@ -486,11 +486,11 @@ abstract class AnnotationsScanner { } static boolean hasPlainJavaAnnotationsOnly(@Nullable Object annotatedElement) { - if (annotatedElement instanceof Class) { - return hasPlainJavaAnnotationsOnly((Class) annotatedElement); + if (annotatedElement instanceof Class clazz) { + return hasPlainJavaAnnotationsOnly(clazz); } - else if (annotatedElement instanceof Member) { - return hasPlainJavaAnnotationsOnly(((Member) annotatedElement).getDeclaringClass()); + else if (annotatedElement instanceof Member member) { + return hasPlainJavaAnnotationsOnly(member.getDeclaringClass()); } else { return false; @@ -506,8 +506,7 @@ abstract class AnnotationsScanner { if (source == Object.class) { return true; } - if (source instanceof Class) { - Class sourceClass = (Class) source; + if (source instanceof Class sourceClass) { boolean noSuperTypes = (sourceClass.getSuperclass() == Object.class && sourceClass.getInterfaces().length == 0); return (searchStrategy == SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES ? noSuperTypes && diff --git a/spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotationPredicates.java b/spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotationPredicates.java index 9fafbc0d7f5..95c2bd6a520 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotationPredicates.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/MergedAnnotationPredicates.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 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. @@ -74,7 +74,7 @@ public abstract class MergedAnnotationPredicates { */ public static Predicate> typeIn(Collection types) { return annotation -> types.stream() - .map(type -> type instanceof Class ? ((Class) type).getName() : type.toString()) + .map(type -> type instanceof Class clazz ? clazz.getName() : type.toString()) .anyMatch(typeName -> typeName.equals(annotation.getType().getName())); } diff --git a/spring-core/src/main/java/org/springframework/core/annotation/OrderUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/OrderUtils.java index 46d196e5d2d..bc38dcec195 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/OrderUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/OrderUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 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,7 +111,7 @@ public abstract class OrderUtils { } Object cached = orderCache.get(element); if (cached != null) { - return (cached instanceof Integer ? (Integer) cached : null); + return (cached instanceof Integer integer ? integer : null); } Integer result = findOrder(annotations); orderCache.put(element, result != null ? result : NOT_ANNOTATED); diff --git a/spring-core/src/main/java/org/springframework/core/annotation/SynthesizingMethodParameter.java b/spring-core/src/main/java/org/springframework/core/annotation/SynthesizingMethodParameter.java index efc95dcbcca..5b4885aeb63 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/SynthesizingMethodParameter.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/SynthesizingMethodParameter.java @@ -120,11 +120,11 @@ public class SynthesizingMethodParameter extends MethodParameter { * @since 5.0 */ public static SynthesizingMethodParameter forExecutable(Executable executable, int parameterIndex) { - if (executable instanceof Method) { - return new SynthesizingMethodParameter((Method) executable, parameterIndex); + if (executable instanceof Method method) { + return new SynthesizingMethodParameter(method, parameterIndex); } - else if (executable instanceof Constructor) { - return new SynthesizingMethodParameter((Constructor) executable, parameterIndex); + else if (executable instanceof Constructor constructor) { + return new SynthesizingMethodParameter(constructor, parameterIndex); } else { throw new IllegalArgumentException("Not a Method/Constructor: " + executable); 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 079eea85937..575214ab746 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 @@ -297,13 +297,11 @@ final class TypeMappedAnnotation extends AbstractMergedAnn private > Object adaptValueForMapOptions(Method attribute, Object value, Class mapType, Function, T> factory, Adapt[] adaptations) { - if (value instanceof MergedAnnotation) { - MergedAnnotation annotation = (MergedAnnotation) value; + if (value instanceof MergedAnnotation annotation) { return (Adapt.ANNOTATION_TO_MAP.isIn(adaptations) ? annotation.asMap(factory, adaptations) : annotation.synthesize()); } - if (value instanceof MergedAnnotation[]) { - MergedAnnotation[] annotations = (MergedAnnotation[]) value; + if (value instanceof MergedAnnotation[] annotations) { if (Adapt.ANNOTATION_TO_MAP.isIn(adaptations)) { Object result = Array.newInstance(mapType, annotations.length); for (int i = 0; i < annotations.length; i++) { @@ -420,14 +418,13 @@ final class TypeMappedAnnotation extends AbstractMergedAnn } value = adaptForAttribute(attribute, value); type = getAdaptType(attribute, type); - if (value instanceof Class && type == String.class) { - value = ((Class) value).getName(); + if (value instanceof Class clazz && type == String.class) { + value = clazz.getName(); } - else if (value instanceof String && type == Class.class) { - value = ClassUtils.resolveClassName((String) value, getClassLoader()); + else if (value instanceof String str && type == Class.class) { + value = ClassUtils.resolveClassName(str, getClassLoader()); } - else if (value instanceof Class[] && type == String[].class) { - Class[] classes = (Class[]) value; + else if (value instanceof Class[] classes && type == String[].class) { String[] names = new String[classes.length]; for (int i = 0; i < classes.length; i++) { names[i] = classes[i].getName(); @@ -441,12 +438,11 @@ final class TypeMappedAnnotation extends AbstractMergedAnn } value = classes; } - else if (value instanceof MergedAnnotation && type.isAnnotation()) { - MergedAnnotation annotation = (MergedAnnotation) value; + else if (value instanceof MergedAnnotation annotation && type.isAnnotation()) { value = annotation.synthesize(); } - else if (value instanceof MergedAnnotation[] && type.isArray() && type.getComponentType().isAnnotation()) { - MergedAnnotation[] annotations = (MergedAnnotation[]) value; + else if (value instanceof MergedAnnotation[] annotations && + type.isArray() && type.getComponentType().isAnnotation()) { Object array = Array.newInstance(type.getComponentType(), annotations.length); for (int i = 0; i < annotations.length; i++) { Array.set(array, i, annotations[i].synthesize()); @@ -498,7 +494,7 @@ final class TypeMappedAnnotation extends AbstractMergedAnn } private boolean isEmptyObjectArray(Object value) { - return (value instanceof Object[] && ((Object[]) value).length == 0); + return (value instanceof Object[] objects && objects.length == 0); } private Object emptyArray(Class componentType) { @@ -510,8 +506,8 @@ final class TypeMappedAnnotation extends AbstractMergedAnn } private MergedAnnotation adaptToMergedAnnotation(Object value, Class annotationType) { - if (value instanceof MergedAnnotation) { - return (MergedAnnotation) value; + if (value instanceof MergedAnnotation mergedAnnotation) { + return mergedAnnotation; } AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType(annotationType).get(0); return new TypeMappedAnnotation<>( @@ -566,11 +562,11 @@ final class TypeMappedAnnotation extends AbstractMergedAnn return this.classLoader; } if (this.source != null) { - if (this.source instanceof Class) { - return ((Class) this.source).getClassLoader(); + if (this.source instanceof Class clazz) { + return clazz.getClassLoader(); } - if (this.source instanceof Member) { - ((Member) this.source).getDeclaringClass().getClassLoader(); + if (this.source instanceof Member member) { + member.getDeclaringClass().getClassLoader(); } } return null; @@ -597,8 +593,7 @@ final class TypeMappedAnnotation extends AbstractMergedAnn static TypeMappedAnnotation createIfPossible( AnnotationTypeMapping mapping, MergedAnnotation annotation, IntrospectionFailureLogger logger) { - if (annotation instanceof TypeMappedAnnotation) { - TypeMappedAnnotation typeMappedAnnotation = (TypeMappedAnnotation) annotation; + if (annotation instanceof TypeMappedAnnotation typeMappedAnnotation) { return createIfPossible(mapping, typeMappedAnnotation.source, typeMappedAnnotation.rootAttributes, typeMappedAnnotation.valueExtractor,