Apply "instanceof pattern matching" in core annotation support

This commit is contained in:
Sam Brannen 2022-03-18 17:24:01 +01:00
parent 2fb1dd177b
commit 5c540e5390
10 changed files with 72 additions and 79 deletions

View File

@ -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 {

View File

@ -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<String, Object> {
}
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<String, Object> {
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<String, Object> {
if (map == null) {
return null;
}
if (map instanceof AnnotationAttributes) {
return (AnnotationAttributes) map;
if (map instanceof AnnotationAttributes annotationAttributes) {
return annotationAttributes;
}
return new AnnotationAttributes(map);
}

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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<String, Object> 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;

View File

@ -82,11 +82,11 @@ abstract class AnnotationsScanner {
private static <C, R> R process(C context, AnnotatedElement source,
SearchStrategy searchStrategy, AnnotationsProcessor<C, R> 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 &&

View File

@ -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 <A extends Annotation> Predicate<MergedAnnotation<? extends A>> 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()));
}

View File

@ -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);

View File

@ -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);

View File

@ -297,13 +297,11 @@ final class TypeMappedAnnotation<A extends Annotation> extends AbstractMergedAnn
private <T extends Map<String, Object>> Object adaptValueForMapOptions(Method attribute, Object value,
Class<?> mapType, Function<MergedAnnotation<?>, 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<A extends Annotation> 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<A extends Annotation> 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<A extends Annotation> 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<A extends Annotation> extends AbstractMergedAnn
}
private MergedAnnotation<?> adaptToMergedAnnotation(Object value, Class<? extends Annotation> 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<A extends Annotation> 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<A extends Annotation> extends AbstractMergedAnn
static <A extends Annotation> TypeMappedAnnotation<A> 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,