From 401c0d220a8e6f89b7707530005ce802c67729e8 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 14 Oct 2021 16:45:08 +0200 Subject: [PATCH] Apply "instanceof pattern matching" Eclipse clean-up in spring-beans This commit also applies additional clean-up tasks such as the following. - final fields - diamond operator (<>) for anonymous inner classes - Comparator.comparing This has only been applied to `src/main/java`. --- .../beans/BeanMetadataAttribute.java | 5 +- .../beans/ExtendedBeanInfo.java | 10 ++- .../GenericTypeAwarePropertyDescriptor.java | 3 +- .../beans/MutablePropertyValues.java | 5 +- .../springframework/beans/PropertyValue.java | 5 +- .../beans/factory/BeanFactoryUtils.java | 23 +++---- .../BeanFactoryAnnotationUtils.java | 5 +- .../annotation/CustomAutowireConfigurer.java | 8 +-- ...nitDestroyAnnotationBeanPostProcessor.java | 3 +- .../factory/annotation/InjectionMetadata.java | 5 +- .../factory/config/BeanDefinitionHolder.java | 5 +- .../factory/config/BeanDefinitionVisitor.java | 11 ++- .../factory/config/BeanExpressionContext.java | 5 +- .../config/ConstructorArgumentValues.java | 3 +- .../config/RuntimeBeanNameReference.java | 5 +- .../factory/config/RuntimeBeanReference.java | 5 +- .../factory/config/TypedStringValue.java | 5 +- .../groovy/GroovyBeanDefinitionReader.java | 2 +- .../parsing/BeanComponentDefinition.java | 6 +- .../AbstractAutowireCapableBeanFactory.java | 7 +- .../support/AbstractBeanDefinition.java | 9 +-- .../factory/support/AbstractBeanFactory.java | 6 +- .../beans/factory/support/AutowireUtils.java | 11 ++- .../support/BeanDefinitionValueResolver.java | 68 +++++++++---------- .../factory/support/ChildBeanDefinition.java | 5 +- .../support/DefaultListableBeanFactory.java | 7 +- .../support/GenericBeanDefinition.java | 5 +- ...ricTypeAwareAutowireCandidateResolver.java | 5 +- .../beans/factory/support/LookupOverride.java | 3 +- .../beans/factory/support/MethodOverride.java | 5 +- .../factory/support/MethodOverrides.java | 5 +- .../PropertiesBeanDefinitionReader.java | 8 +-- .../factory/support/ReplaceOverride.java | 3 +- .../support/StaticListableBeanFactory.java | 4 +- .../xml/BeanDefinitionParserDelegate.java | 5 +- .../DefaultBeanDefinitionDocumentReader.java | 5 +- .../SimpleConstructorNamespaceHandler.java | 5 +- .../xml/SimplePropertyNamespaceHandler.java | 5 +- .../factory/xml/XmlBeanDefinitionReader.java | 4 +- .../beans/support/MutableSortDefinition.java | 5 +- 40 files changed, 120 insertions(+), 179 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/BeanMetadataAttribute.java b/spring-beans/src/main/java/org/springframework/beans/BeanMetadataAttribute.java index db6435d5c0e..57662a952b1 100644 --- a/spring-beans/src/main/java/org/springframework/beans/BeanMetadataAttribute.java +++ b/spring-beans/src/main/java/org/springframework/beans/BeanMetadataAttribute.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. @@ -85,10 +85,9 @@ public class BeanMetadataAttribute implements BeanMetadataElement { if (this == other) { return true; } - if (!(other instanceof BeanMetadataAttribute)) { + if (!(other instanceof BeanMetadataAttribute otherMa)) { return false; } - BeanMetadataAttribute otherMa = (BeanMetadataAttribute) other; return (this.name.equals(otherMa.name) && ObjectUtils.nullSafeEquals(this.value, otherMa.value) && ObjectUtils.nullSafeEquals(this.source, otherMa.source)); diff --git a/spring-beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java b/spring-beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java index 21ce57cf6ad..a338b7cde60 100644 --- a/spring-beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java +++ b/spring-beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.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. @@ -139,7 +139,7 @@ class ExtendedBeanInfo implements BeanInfo { // Sort non-void returning write methods to guard against the ill effects of // non-deterministic sorting of methods returned from Class#getDeclaredMethods // under JDK 7. See https://bugs.java.com/view_bug.do?bug_id=7023180 - matches.sort((m1, m2) -> m2.toString().compareTo(m1.toString())); + matches.sort(Comparator.comparing(Method::toString).reversed()); return matches; } @@ -188,8 +188,7 @@ class ExtendedBeanInfo implements BeanInfo { for (PropertyDescriptor pd : this.propertyDescriptors) { final Class candidateType; final String candidateName = pd.getName(); - if (pd instanceof IndexedPropertyDescriptor) { - IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd; + if (pd instanceof IndexedPropertyDescriptor ipd) { candidateType = ipd.getIndexedPropertyType(); if (candidateName.equals(propertyName) && (candidateType.equals(propertyType) || candidateType.equals(propertyType.getComponentType()))) { @@ -494,10 +493,9 @@ class ExtendedBeanInfo implements BeanInfo { if (this == other) { return true; } - if (!(other instanceof IndexedPropertyDescriptor)) { + if (!(other instanceof IndexedPropertyDescriptor otherPd)) { return false; } - IndexedPropertyDescriptor otherPd = (IndexedPropertyDescriptor) other; return (ObjectUtils.nullSafeEquals(getIndexedReadMethod(), otherPd.getIndexedReadMethod()) && ObjectUtils.nullSafeEquals(getIndexedWriteMethod(), otherPd.getIndexedWriteMethod()) && ObjectUtils.nullSafeEquals(getIndexedPropertyType(), otherPd.getIndexedPropertyType()) && diff --git a/spring-beans/src/main/java/org/springframework/beans/GenericTypeAwarePropertyDescriptor.java b/spring-beans/src/main/java/org/springframework/beans/GenericTypeAwarePropertyDescriptor.java index b4052b7b84e..fb5d4d15ae1 100644 --- a/spring-beans/src/main/java/org/springframework/beans/GenericTypeAwarePropertyDescriptor.java +++ b/spring-beans/src/main/java/org/springframework/beans/GenericTypeAwarePropertyDescriptor.java @@ -168,10 +168,9 @@ final class GenericTypeAwarePropertyDescriptor extends PropertyDescriptor { if (this == other) { return true; } - if (!(other instanceof GenericTypeAwarePropertyDescriptor)) { + if (!(other instanceof GenericTypeAwarePropertyDescriptor otherPd)) { return false; } - GenericTypeAwarePropertyDescriptor otherPd = (GenericTypeAwarePropertyDescriptor) other; return (getBeanClass().equals(otherPd.getBeanClass()) && PropertyDescriptorUtils.equals(this, otherPd)); } diff --git a/spring-beans/src/main/java/org/springframework/beans/MutablePropertyValues.java b/spring-beans/src/main/java/org/springframework/beans/MutablePropertyValues.java index 97c0a0ab05b..1c978059ef1 100644 --- a/spring-beans/src/main/java/org/springframework/beans/MutablePropertyValues.java +++ b/spring-beans/src/main/java/org/springframework/beans/MutablePropertyValues.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,8 +221,7 @@ public class MutablePropertyValues implements PropertyValues, Serializable { */ private PropertyValue mergeIfRequired(PropertyValue newPv, PropertyValue currentPv) { Object value = newPv.getValue(); - if (value instanceof Mergeable) { - Mergeable mergeable = (Mergeable) value; + if (value instanceof Mergeable mergeable) { if (mergeable.isMergeEnabled()) { Object merged = mergeable.merge(currentPv.getValue()); return new PropertyValue(newPv.getName(), merged); diff --git a/spring-beans/src/main/java/org/springframework/beans/PropertyValue.java b/spring-beans/src/main/java/org/springframework/beans/PropertyValue.java index 16c6bae560a..93b97042b01 100644 --- a/spring-beans/src/main/java/org/springframework/beans/PropertyValue.java +++ b/spring-beans/src/main/java/org/springframework/beans/PropertyValue.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. @@ -192,10 +192,9 @@ public class PropertyValue extends BeanMetadataAttributeAccessor implements Seri if (this == other) { return true; } - if (!(other instanceof PropertyValue)) { + if (!(other instanceof PropertyValue otherPv)) { return false; } - PropertyValue otherPv = (PropertyValue) other; return (this.name.equals(otherPv.name) && ObjectUtils.nullSafeEquals(this.value, otherPv.value) && ObjectUtils.nullSafeEquals(getSource(), otherPv.getSource())); diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java b/spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java index 17a5d70460d..fbc7cb4751d 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.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. @@ -161,8 +161,7 @@ public abstract class BeanFactoryUtils { public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, ResolvableType type) { Assert.notNull(lbf, "ListableBeanFactory must not be null"); String[] result = lbf.getBeanNamesForType(type); - if (lbf instanceof HierarchicalBeanFactory) { - HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf; + if (lbf instanceof HierarchicalBeanFactory hbf) { if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { String[] parentResult = beanNamesForTypeIncludingAncestors( (ListableBeanFactory) hbf.getParentBeanFactory(), type); @@ -199,8 +198,7 @@ public abstract class BeanFactoryUtils { Assert.notNull(lbf, "ListableBeanFactory must not be null"); String[] result = lbf.getBeanNamesForType(type, includeNonSingletons, allowEagerInit); - if (lbf instanceof HierarchicalBeanFactory) { - HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf; + if (lbf instanceof HierarchicalBeanFactory hbf) { if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { String[] parentResult = beanNamesForTypeIncludingAncestors( (ListableBeanFactory) hbf.getParentBeanFactory(), type, includeNonSingletons, allowEagerInit); @@ -226,8 +224,7 @@ public abstract class BeanFactoryUtils { public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, Class type) { Assert.notNull(lbf, "ListableBeanFactory must not be null"); String[] result = lbf.getBeanNamesForType(type); - if (lbf instanceof HierarchicalBeanFactory) { - HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf; + if (lbf instanceof HierarchicalBeanFactory hbf) { if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { String[] parentResult = beanNamesForTypeIncludingAncestors( (ListableBeanFactory) hbf.getParentBeanFactory(), type); @@ -263,8 +260,7 @@ public abstract class BeanFactoryUtils { Assert.notNull(lbf, "ListableBeanFactory must not be null"); String[] result = lbf.getBeanNamesForType(type, includeNonSingletons, allowEagerInit); - if (lbf instanceof HierarchicalBeanFactory) { - HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf; + if (lbf instanceof HierarchicalBeanFactory hbf) { if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { String[] parentResult = beanNamesForTypeIncludingAncestors( (ListableBeanFactory) hbf.getParentBeanFactory(), type, includeNonSingletons, allowEagerInit); @@ -289,8 +285,7 @@ public abstract class BeanFactoryUtils { Assert.notNull(lbf, "ListableBeanFactory must not be null"); String[] result = lbf.getBeanNamesForAnnotation(annotationType); - if (lbf instanceof HierarchicalBeanFactory) { - HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf; + if (lbf instanceof HierarchicalBeanFactory hbf) { if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { String[] parentResult = beanNamesForAnnotationIncludingAncestors( (ListableBeanFactory) hbf.getParentBeanFactory(), annotationType); @@ -327,8 +322,7 @@ public abstract class BeanFactoryUtils { Assert.notNull(lbf, "ListableBeanFactory must not be null"); Map result = new LinkedHashMap<>(4); result.putAll(lbf.getBeansOfType(type)); - if (lbf instanceof HierarchicalBeanFactory) { - HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf; + if (lbf instanceof HierarchicalBeanFactory hbf) { if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { Map parentResult = beansOfTypeIncludingAncestors( (ListableBeanFactory) hbf.getParentBeanFactory(), type); @@ -376,8 +370,7 @@ public abstract class BeanFactoryUtils { Assert.notNull(lbf, "ListableBeanFactory must not be null"); Map result = new LinkedHashMap<>(4); result.putAll(lbf.getBeansOfType(type, includeNonSingletons, allowEagerInit)); - if (lbf instanceof HierarchicalBeanFactory) { - HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf; + if (lbf instanceof HierarchicalBeanFactory hbf) { if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { Map parentResult = beansOfTypeIncludingAncestors( (ListableBeanFactory) hbf.getParentBeanFactory(), type, includeNonSingletons, allowEagerInit); diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/BeanFactoryAnnotationUtils.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/BeanFactoryAnnotationUtils.java index 58e04c4c032..722995aabec 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/BeanFactoryAnnotationUtils.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/BeanFactoryAnnotationUtils.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. @@ -166,8 +166,7 @@ public abstract class BeanFactoryAnnotationUtils { if (beanFactory instanceof ConfigurableBeanFactory) { BeanDefinition bd = ((ConfigurableBeanFactory) beanFactory).getMergedBeanDefinition(beanName); // Explicit qualifier metadata on bean definition? (typically in XML definition) - if (bd instanceof AbstractBeanDefinition) { - AbstractBeanDefinition abd = (AbstractBeanDefinition) bd; + if (bd instanceof AbstractBeanDefinition abd) { AutowireCandidateQualifier candidate = abd.getQualifier(Qualifier.class.getName()); if (candidate != null) { Object value = candidate.getAttribute(AutowireCandidateQualifier.VALUE_KEY); diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/CustomAutowireConfigurer.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/CustomAutowireConfigurer.java index d43329a0f62..86fe4482b2e 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/CustomAutowireConfigurer.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/CustomAutowireConfigurer.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. @@ -91,11 +91,10 @@ public class CustomAutowireConfigurer implements BeanFactoryPostProcessor, BeanC @SuppressWarnings("unchecked") public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { if (this.customQualifierTypes != null) { - if (!(beanFactory instanceof DefaultListableBeanFactory)) { + if (!(beanFactory instanceof DefaultListableBeanFactory dlbf)) { throw new IllegalStateException( "CustomAutowireConfigurer needs to operate on a DefaultListableBeanFactory"); } - DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory) beanFactory; if (!(dlbf.getAutowireCandidateResolver() instanceof QualifierAnnotationAutowireCandidateResolver)) { dlbf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); } @@ -106,8 +105,7 @@ public class CustomAutowireConfigurer implements BeanFactoryPostProcessor, BeanC if (value instanceof Class) { customType = (Class) value; } - else if (value instanceof String) { - String className = (String) value; + else if (value instanceof String className) { customType = (Class) ClassUtils.resolveClassName(className, this.beanClassLoader); } else { diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java index 2f1bd7763b2..804c89a4455 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java @@ -394,10 +394,9 @@ public class InitDestroyAnnotationBeanPostProcessor if (this == other) { return true; } - if (!(other instanceof LifecycleElement)) { + if (!(other instanceof LifecycleElement otherElement)) { return false; } - LifecycleElement otherElement = (LifecycleElement) other; return (this.identifier.equals(otherElement.identifier)); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java index f7dcb8d18cf..4e205558523 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.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. @@ -304,10 +304,9 @@ public class InjectionMetadata { if (this == other) { return true; } - if (!(other instanceof InjectedElement)) { + if (!(other instanceof InjectedElement otherElement)) { return false; } - InjectedElement otherElement = (InjectedElement) other; return this.member.equals(otherElement.member); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/BeanDefinitionHolder.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/BeanDefinitionHolder.java index d6767f7dcf7..0b25ad6144a 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/BeanDefinitionHolder.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/BeanDefinitionHolder.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. @@ -168,10 +168,9 @@ public class BeanDefinitionHolder implements BeanMetadataElement { if (this == other) { return true; } - if (!(other instanceof BeanDefinitionHolder)) { + if (!(other instanceof BeanDefinitionHolder otherHolder)) { return false; } - BeanDefinitionHolder otherHolder = (BeanDefinitionHolder) other; return this.beanDefinition.equals(otherHolder.beanDefinition) && this.beanName.equals(otherHolder.beanName) && ObjectUtils.nullSafeEquals(this.aliases, otherHolder.aliases); diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/BeanDefinitionVisitor.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/BeanDefinitionVisitor.java index 7b826c50d7c..d3d83c71ca9 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/BeanDefinitionVisitor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/BeanDefinitionVisitor.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. @@ -178,8 +178,7 @@ public class BeanDefinitionVisitor { else if (value instanceof BeanDefinitionHolder) { visitBeanDefinition(((BeanDefinitionHolder) value).getBeanDefinition()); } - else if (value instanceof RuntimeBeanReference) { - RuntimeBeanReference ref = (RuntimeBeanReference) value; + else if (value instanceof RuntimeBeanReference ref) { String newBeanName = resolveStringValue(ref.getBeanName()); if (newBeanName == null) { return null; @@ -188,8 +187,7 @@ public class BeanDefinitionVisitor { return new RuntimeBeanReference(newBeanName); } } - else if (value instanceof RuntimeBeanNameReference) { - RuntimeBeanNameReference ref = (RuntimeBeanNameReference) value; + else if (value instanceof RuntimeBeanNameReference ref) { String newBeanName = resolveStringValue(ref.getBeanName()); if (newBeanName == null) { return null; @@ -210,8 +208,7 @@ public class BeanDefinitionVisitor { else if (value instanceof Map) { visitMap((Map) value); } - else if (value instanceof TypedStringValue) { - TypedStringValue typedStringValue = (TypedStringValue) value; + else if (value instanceof TypedStringValue typedStringValue) { String stringValue = typedStringValue.getValue(); if (stringValue != null) { String visitedString = resolveStringValue(stringValue); diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/BeanExpressionContext.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/BeanExpressionContext.java index e6e383935e7..e50e9346587 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/BeanExpressionContext.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/BeanExpressionContext.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. @@ -73,10 +73,9 @@ public class BeanExpressionContext { if (this == other) { return true; } - if (!(other instanceof BeanExpressionContext)) { + if (!(other instanceof BeanExpressionContext otherContext)) { return false; } - BeanExpressionContext otherContext = (BeanExpressionContext) other; return (this.beanFactory == otherContext.beanFactory && this.scope == otherContext.scope); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java index c4d779e697b..a90e5709453 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java @@ -392,10 +392,9 @@ public class ConstructorArgumentValues { if (this == other) { return true; } - if (!(other instanceof ConstructorArgumentValues)) { + if (!(other instanceof ConstructorArgumentValues that)) { return false; } - ConstructorArgumentValues that = (ConstructorArgumentValues) other; if (this.genericArgumentValues.size() != that.genericArgumentValues.size() || this.indexedArgumentValues.size() != that.indexedArgumentValues.size()) { return false; diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/RuntimeBeanNameReference.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/RuntimeBeanNameReference.java index 805f6bbd61b..90a59c2ac7f 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/RuntimeBeanNameReference.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/RuntimeBeanNameReference.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. @@ -71,10 +71,9 @@ public class RuntimeBeanNameReference implements BeanReference { if (this == other) { return true; } - if (!(other instanceof RuntimeBeanNameReference)) { + if (!(other instanceof RuntimeBeanNameReference that)) { return false; } - RuntimeBeanNameReference that = (RuntimeBeanNameReference) other; return this.beanName.equals(that.beanName); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/RuntimeBeanReference.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/RuntimeBeanReference.java index 11b4f1ac79b..a414c3e7829 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/RuntimeBeanReference.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/RuntimeBeanReference.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. @@ -135,10 +135,9 @@ public class RuntimeBeanReference implements BeanReference { if (this == other) { return true; } - if (!(other instanceof RuntimeBeanReference)) { + if (!(other instanceof RuntimeBeanReference that)) { return false; } - RuntimeBeanReference that = (RuntimeBeanReference) other; return (this.beanName.equals(that.beanName) && this.beanType == that.beanType && this.toParent == that.toParent); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/TypedStringValue.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/TypedStringValue.java index 70d4ab2ff09..bd27173a3f8 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/TypedStringValue.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/TypedStringValue.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. @@ -219,10 +219,9 @@ public class TypedStringValue implements BeanMetadataElement { if (this == other) { return true; } - if (!(other instanceof TypedStringValue)) { + if (!(other instanceof TypedStringValue otherValue)) { return false; } - TypedStringValue otherValue = (TypedStringValue) other; return (ObjectUtils.nullSafeEquals(this.value, otherValue.value) && ObjectUtils.nullSafeEquals(this.targetType, otherValue.targetType)); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionReader.java b/spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionReader.java index 9ef7aa3799f..56dd8580e9f 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionReader.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionReader.java @@ -244,7 +244,7 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp } @SuppressWarnings("serial") - Closure beans = new Closure(this) { + Closure beans = new Closure<>(this) { @Override public Object call(Object... args) { invokeBeanDefiningClosure((Closure) args[0]); diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/parsing/BeanComponentDefinition.java b/spring-beans/src/main/java/org/springframework/beans/factory/parsing/BeanComponentDefinition.java index 32305abbacc..cb7720f85c9 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/parsing/BeanComponentDefinition.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/parsing/BeanComponentDefinition.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. @@ -36,9 +36,9 @@ import org.springframework.lang.Nullable; */ public class BeanComponentDefinition extends BeanDefinitionHolder implements ComponentDefinition { - private BeanDefinition[] innerBeanDefinitions; + private final BeanDefinition[] innerBeanDefinitions; - private BeanReference[] beanReferences; + private final BeanReference[] beanReferences; /** diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java index 77abc22fce6..5ab6fb085a2 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java @@ -303,9 +303,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac @Override public void copyConfigurationFrom(ConfigurableBeanFactory otherFactory) { super.copyConfigurationFrom(otherFactory); - if (otherFactory instanceof AbstractAutowireCapableBeanFactory) { - AbstractAutowireCapableBeanFactory otherAutowireFactory = - (AbstractAutowireCapableBeanFactory) otherFactory; + if (otherFactory instanceof AbstractAutowireCapableBeanFactory otherAutowireFactory) { this.instantiationStrategy = otherAutowireFactory.instantiationStrategy; this.allowCircularReferences = otherAutowireFactory.allowCircularReferences; this.ignoredDependencyTypes.addAll(otherAutowireFactory.ignoredDependencyTypes); @@ -344,8 +342,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac markBeanAsCreated(beanName); BeanDefinition mbd = getMergedBeanDefinition(beanName); RootBeanDefinition bd = null; - if (mbd instanceof RootBeanDefinition) { - RootBeanDefinition rbd = (RootBeanDefinition) mbd; + if (mbd instanceof RootBeanDefinition rbd) { bd = (rbd.isPrototype() ? rbd : rbd.cloneBeanDefinition()); } if (bd == null) { diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java index 2ceff658dbc..09bbf79d194 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java @@ -236,8 +236,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess setSource(original.getSource()); copyAttributesFrom(original); - if (original instanceof AbstractBeanDefinition) { - AbstractBeanDefinition originalAbd = (AbstractBeanDefinition) original; + if (original instanceof AbstractBeanDefinition originalAbd) { if (originalAbd.hasBeanClass()) { setBeanClass(originalAbd.getBeanClass()); } @@ -313,8 +312,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess setSource(other.getSource()); copyAttributesFrom(other); - if (other instanceof AbstractBeanDefinition) { - AbstractBeanDefinition otherAbd = (AbstractBeanDefinition) other; + if (other instanceof AbstractBeanDefinition otherAbd) { if (otherAbd.hasBeanClass()) { setBeanClass(otherAbd.getBeanClass()); } @@ -1178,10 +1176,9 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess if (this == other) { return true; } - if (!(other instanceof AbstractBeanDefinition)) { + if (!(other instanceof AbstractBeanDefinition that)) { return false; } - AbstractBeanDefinition that = (AbstractBeanDefinition) other; return (ObjectUtils.nullSafeEquals(getBeanClassName(), that.getBeanClassName()) && ObjectUtils.nullSafeEquals(this.scope, that.scope) && this.abstractFlag == that.abstractFlag && diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java index 921b46c3853..b33651f186d 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java @@ -1054,8 +1054,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp setCacheBeanMetadata(otherFactory.isCacheBeanMetadata()); setBeanExpressionResolver(otherFactory.getBeanExpressionResolver()); setConversionService(otherFactory.getConversionService()); - if (otherFactory instanceof AbstractBeanFactory) { - AbstractBeanFactory otherAbstractFactory = (AbstractBeanFactory) otherFactory; + if (otherFactory instanceof AbstractBeanFactory otherAbstractFactory) { this.propertyEditorRegistrars.addAll(otherAbstractFactory.propertyEditorRegistrars); this.customEditors.putAll(otherAbstractFactory.customEditors); this.typeConverter = otherAbstractFactory.typeConverter; @@ -1512,8 +1511,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp if (tempClassLoader != null) { dynamicLoader = tempClassLoader; freshResolve = true; - if (tempClassLoader instanceof DecoratingClassLoader) { - DecoratingClassLoader dcl = (DecoratingClassLoader) tempClassLoader; + if (tempClassLoader instanceof DecoratingClassLoader dcl) { for (Class typeToMatch : typesToMatch) { dcl.excludeClass(typeToMatch.getName()); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AutowireUtils.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AutowireUtils.java index d807cc4dc2f..c91508cb181 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AutowireUtils.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AutowireUtils.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. @@ -198,8 +198,7 @@ abstract class AutowireUtils { Type methodParameterType = methodParameterTypes[i]; Object arg = args[i]; if (methodParameterType.equals(genericReturnType)) { - if (arg instanceof TypedStringValue) { - TypedStringValue typedValue = ((TypedStringValue) arg); + if (arg instanceof TypedStringValue typedValue) { if (typedValue.hasTargetType()) { return typedValue.getTargetType(); } @@ -220,8 +219,7 @@ abstract class AutowireUtils { } return method.getReturnType(); } - else if (methodParameterType instanceof ParameterizedType) { - ParameterizedType parameterizedType = (ParameterizedType) methodParameterType; + else if (methodParameterType instanceof ParameterizedType parameterizedType) { Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); for (Type typeArg : actualTypeArguments) { if (typeArg.equals(genericReturnType)) { @@ -233,8 +231,7 @@ abstract class AutowireUtils { if (arg instanceof String) { className = (String) arg; } - else if (arg instanceof TypedStringValue) { - TypedStringValue typedValue = ((TypedStringValue) arg); + else if (arg instanceof TypedStringValue typedValue) { String targetTypeName = typedValue.getTargetTypeName(); if (targetTypeName == null || Class.class.getName().equals(targetTypeName)) { className = typedValue.getValue(); diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java index be9667b19a3..0468bc4aadd 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.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. @@ -55,6 +55,7 @@ import org.springframework.util.StringUtils; * Used by {@link AbstractAutowireCapableBeanFactory}. * * @author Juergen Hoeller + * @author Sam Brannen * @since 1.2 * @see AbstractAutowireCapableBeanFactory */ @@ -108,12 +109,11 @@ class BeanDefinitionValueResolver { public Object resolveValueIfNecessary(Object argName, @Nullable Object value) { // We must check each value to see whether it requires a runtime reference // to another bean to be resolved. - if (value instanceof RuntimeBeanReference) { - RuntimeBeanReference ref = (RuntimeBeanReference) value; + if (value instanceof RuntimeBeanReference ref) { return resolveReference(argName, ref); } - else if (value instanceof RuntimeBeanNameReference) { - String refName = ((RuntimeBeanNameReference) value).getBeanName(); + else if (value instanceof RuntimeBeanNameReference ref) { + String refName = ref.getBeanName(); refName = String.valueOf(doEvaluate(refName)); if (!this.beanFactory.containsBean(refName)) { throw new BeanDefinitionStoreException( @@ -121,22 +121,20 @@ class BeanDefinitionValueResolver { } return refName; } - else if (value instanceof BeanDefinitionHolder) { + else if (value instanceof BeanDefinitionHolder bdHolder) { // Resolve BeanDefinitionHolder: contains BeanDefinition with name and aliases. - BeanDefinitionHolder bdHolder = (BeanDefinitionHolder) value; return resolveInnerBean(argName, bdHolder.getBeanName(), bdHolder.getBeanDefinition()); } - else if (value instanceof BeanDefinition) { + else if (value instanceof BeanDefinition bd) { // Resolve plain BeanDefinition, without contained name: use dummy name. - BeanDefinition bd = (BeanDefinition) value; String innerBeanName = "(inner bean)" + BeanFactoryUtils.GENERATED_BEAN_NAME_SEPARATOR + ObjectUtils.getIdentityHexString(bd); return resolveInnerBean(argName, innerBeanName, bd); } - else if (value instanceof DependencyDescriptor) { + else if (value instanceof DependencyDescriptor dependencyDescriptor) { Set autowiredBeanNames = new LinkedHashSet<>(4); Object result = this.beanFactory.resolveDependency( - (DependencyDescriptor) value, this.beanName, autowiredBeanNames, this.typeConverter); + dependencyDescriptor, this.beanName, autowiredBeanNames, this.typeConverter); for (String autowiredBeanName : autowiredBeanNames) { if (this.beanFactory.containsBean(autowiredBeanName)) { this.beanFactory.registerDependentBean(autowiredBeanName, this.beanName); @@ -144,16 +142,15 @@ class BeanDefinitionValueResolver { } return result; } - else if (value instanceof ManagedArray) { + else if (value instanceof ManagedArray managedArray) { // May need to resolve contained runtime references. - ManagedArray array = (ManagedArray) value; - Class elementType = array.resolvedElementType; + Class elementType = managedArray.resolvedElementType; if (elementType == null) { - String elementTypeName = array.getElementTypeName(); + String elementTypeName = managedArray.getElementTypeName(); if (StringUtils.hasText(elementTypeName)) { try { elementType = ClassUtils.forName(elementTypeName, this.beanFactory.getBeanClassLoader()); - array.resolvedElementType = elementType; + managedArray.resolvedElementType = elementType; } catch (Throwable ex) { // Improve the message by showing the context. @@ -168,27 +165,27 @@ class BeanDefinitionValueResolver { } return resolveManagedArray(argName, (List) value, elementType); } - else if (value instanceof ManagedList) { + else if (value instanceof ManagedList managedList) { // May need to resolve contained runtime references. - return resolveManagedList(argName, (List) value); + return resolveManagedList(argName, managedList); } - else if (value instanceof ManagedSet) { + else if (value instanceof ManagedSet managedSet) { // May need to resolve contained runtime references. - return resolveManagedSet(argName, (Set) value); + return resolveManagedSet(argName, managedSet); } - else if (value instanceof ManagedMap) { + else if (value instanceof ManagedMap managedMap) { // May need to resolve contained runtime references. - return resolveManagedMap(argName, (Map) value); + return resolveManagedMap(argName, managedMap); } - else if (value instanceof ManagedProperties) { - Properties original = (Properties) value; + else if (value instanceof ManagedProperties original) { + // Properties original = managedProperties; Properties copy = new Properties(); original.forEach((propKey, propValue) -> { - if (propKey instanceof TypedStringValue) { - propKey = evaluate((TypedStringValue) propKey); + if (propKey instanceof TypedStringValue typedStringValue) { + propKey = evaluate(typedStringValue); } - if (propValue instanceof TypedStringValue) { - propValue = evaluate((TypedStringValue) propValue); + if (propValue instanceof TypedStringValue typedStringValue) { + propValue = evaluate(typedStringValue); } if (propKey == null || propValue == null) { throw new BeanCreationException( @@ -199,9 +196,8 @@ class BeanDefinitionValueResolver { }); return copy; } - else if (value instanceof TypedStringValue) { + else if (value instanceof TypedStringValue typedStringValue) { // Convert value to target type here. - TypedStringValue typedStringValue = (TypedStringValue) value; Object valueObject = evaluate(typedStringValue); try { Class resolvedTargetType = resolveTargetType(typedStringValue); @@ -248,11 +244,10 @@ class BeanDefinitionValueResolver { */ @Nullable protected Object evaluate(@Nullable Object value) { - if (value instanceof String) { - return doEvaluate((String) value); + if (value instanceof String str) { + return doEvaluate(str); } - else if (value instanceof String[]) { - String[] values = (String[]) value; + else if (value instanceof String[] values) { boolean actuallyResolved = false; Object[] resolvedValues = new Object[values.length]; for (int i = 0; i < values.length; i++) { @@ -372,10 +367,9 @@ class BeanDefinitionValueResolver { } // Actually create the inner bean instance now... Object innerBean = this.beanFactory.createBean(actualInnerBeanName, mbd, null); - if (innerBean instanceof FactoryBean) { + if (innerBean instanceof FactoryBean factoryBean) { boolean synthetic = mbd.isSynthetic(); - innerBean = this.beanFactory.getObjectFromFactoryBean( - (FactoryBean) innerBean, actualInnerBeanName, !synthetic); + innerBean = this.beanFactory.getObjectFromFactoryBean(factoryBean, actualInnerBeanName, !synthetic); } if (innerBean instanceof NullBean) { innerBean = null; diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/ChildBeanDefinition.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/ChildBeanDefinition.java index 51b981f6193..bab5f2118fa 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/ChildBeanDefinition.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/ChildBeanDefinition.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. @@ -160,10 +160,9 @@ public class ChildBeanDefinition extends AbstractBeanDefinition { if (this == other) { return true; } - if (!(other instanceof ChildBeanDefinition)) { + if (!(other instanceof ChildBeanDefinition that)) { return false; } - ChildBeanDefinition that = (ChildBeanDefinition) other; return (ObjectUtils.nullSafeEquals(this.parentName, that.parentName) && super.equals(other)); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index 7c16748e141..6848c734774 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -310,8 +310,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto @Override public void copyConfigurationFrom(ConfigurableBeanFactory otherFactory) { super.copyConfigurationFrom(otherFactory); - if (otherFactory instanceof DefaultListableBeanFactory) { - DefaultListableBeanFactory otherListableFactory = (DefaultListableBeanFactory) otherFactory; + if (otherFactory instanceof DefaultListableBeanFactory otherListableFactory) { this.allowBeanDefinitionOverriding = otherListableFactory.allowBeanDefinitionOverriding; this.allowEagerClassLoading = otherListableFactory.allowEagerClassLoading; this.dependencyComparator = otherListableFactory.dependencyComparator; @@ -389,7 +388,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto @Override public ObjectProvider getBeanProvider(ResolvableType requiredType, boolean allowEagerInit) { - return new BeanObjectProvider() { + return new BeanObjectProvider<>() { @Override public T getObject() throws BeansException { T resolved = resolveBean(requiredType, null, false); @@ -1248,7 +1247,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto if (bean instanceof NullBean) { return null; } - return new NamedBeanHolder(beanName, adaptBeanInstance(beanName, bean, requiredType.toClass())); + return new NamedBeanHolder<>(beanName, adaptBeanInstance(beanName, bean, requiredType.toClass())); } @Override diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/GenericBeanDefinition.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/GenericBeanDefinition.java index ca779d1e66e..b0a40b332ae 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/GenericBeanDefinition.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/GenericBeanDefinition.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. @@ -88,10 +88,9 @@ public class GenericBeanDefinition extends AbstractBeanDefinition { if (this == other) { return true; } - if (!(other instanceof GenericBeanDefinition)) { + if (!(other instanceof GenericBeanDefinition that)) { return false; } - GenericBeanDefinition that = (GenericBeanDefinition) other; return (ObjectUtils.nullSafeEquals(this.parentName, that.parentName) && super.equals(other)); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/GenericTypeAwareAutowireCandidateResolver.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/GenericTypeAwareAutowireCandidateResolver.java index 01b81450903..8fe5a854ffe 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/GenericTypeAwareAutowireCandidateResolver.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/GenericTypeAwareAutowireCandidateResolver.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. @@ -142,8 +142,7 @@ public class GenericTypeAwareAutowireCandidateResolver extends SimpleAutowireCan @Nullable protected RootBeanDefinition getResolvedDecoratedDefinition(RootBeanDefinition rbd) { BeanDefinitionHolder decDef = rbd.getDecoratedDefinition(); - if (decDef != null && this.beanFactory instanceof ConfigurableListableBeanFactory) { - ConfigurableListableBeanFactory clbf = (ConfigurableListableBeanFactory) this.beanFactory; + if (decDef != null && this.beanFactory instanceof ConfigurableListableBeanFactory clbf) { if (clbf.containsBeanDefinition(decDef.getBeanName())) { BeanDefinition dbd = clbf.getMergedBeanDefinition(decDef.getBeanName()); if (dbd instanceof RootBeanDefinition) { diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/LookupOverride.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/LookupOverride.java index c441bf7e9cc..faa7478a819 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/LookupOverride.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/LookupOverride.java @@ -102,10 +102,9 @@ public class LookupOverride extends MethodOverride { @Override public boolean equals(@Nullable Object other) { - if (!(other instanceof LookupOverride) || !super.equals(other)) { + if (!(other instanceof LookupOverride that) || !super.equals(other)) { return false; } - LookupOverride that = (LookupOverride) other; return (ObjectUtils.nullSafeEquals(this.method, that.method) && ObjectUtils.nullSafeEquals(this.beanName, that.beanName)); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/MethodOverride.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/MethodOverride.java index 24a2056cf19..0107e104c21 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/MethodOverride.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/MethodOverride.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. @@ -109,10 +109,9 @@ public abstract class MethodOverride implements BeanMetadataElement { if (this == other) { return true; } - if (!(other instanceof MethodOverride)) { + if (!(other instanceof MethodOverride that)) { return false; } - MethodOverride that = (MethodOverride) other; return (ObjectUtils.nullSafeEquals(this.methodName, that.methodName) && ObjectUtils.nullSafeEquals(this.source, that.source)); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/MethodOverrides.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/MethodOverrides.java index a84a15f8302..07dc3a6b0db 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/MethodOverrides.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/MethodOverrides.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. @@ -107,10 +107,9 @@ public class MethodOverrides { if (this == other) { return true; } - if (!(other instanceof MethodOverrides)) { + if (!(other instanceof MethodOverrides that)) { return false; } - MethodOverrides that = (MethodOverrides) other; return this.overrides.equals(that.overrides); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/PropertiesBeanDefinitionReader.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/PropertiesBeanDefinitionReader.java index 617a13be564..01d901048d0 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/PropertiesBeanDefinitionReader.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/PropertiesBeanDefinitionReader.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. @@ -363,10 +363,9 @@ public class PropertiesBeanDefinitionReader extends AbstractBeanDefinitionReader int beanCount = 0; for (Object key : map.keySet()) { - if (!(key instanceof String)) { + if (!(key instanceof String keyString)) { throw new IllegalArgumentException("Illegal key [" + key + "]: only Strings allowed"); } - String keyString = (String) key; if (keyString.startsWith(prefix)) { // Key is of form: prefix.property String nameAndProperty = keyString.substring(prefix.length()); @@ -519,8 +518,7 @@ public class PropertiesBeanDefinitionReader extends AbstractBeanDefinitionReader */ private Object readValue(Map.Entry entry) { Object val = entry.getValue(); - if (val instanceof String) { - String strVal = (String) val; + if (val instanceof String strVal) { // If it starts with a reference prefix... if (strVal.startsWith(REF_PREFIX)) { // Expand the reference. diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/ReplaceOverride.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/ReplaceOverride.java index 80f866d21cc..a702b7d7af1 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/ReplaceOverride.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/ReplaceOverride.java @@ -97,10 +97,9 @@ public class ReplaceOverride extends MethodOverride { @Override public boolean equals(@Nullable Object other) { - if (!(other instanceof ReplaceOverride) || !super.equals(other)) { + if (!(other instanceof ReplaceOverride that) || !super.equals(other)) { return false; } - ReplaceOverride that = (ReplaceOverride) other; return (ObjectUtils.nullSafeEquals(this.methodReplacerBeanName, that.methodReplacerBeanName) && ObjectUtils.nullSafeEquals(this.typeIdentifiers, that.typeIdentifiers)); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java index a5430120dfd..b9994f6f763 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.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. @@ -284,7 +284,7 @@ public class StaticListableBeanFactory implements ListableBeanFactory { @SuppressWarnings("unchecked") @Override public ObjectProvider getBeanProvider(ResolvableType requiredType, boolean allowEagerInit) { - return new ObjectProvider() { + return new ObjectProvider<>() { @Override public T getObject() throws BeansException { String[] beanNames = getBeanNamesForType(requiredType); diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java b/spring-beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java index 6be311eb1ef..fd5f4ad6452 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.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. @@ -1186,8 +1186,7 @@ public class BeanDefinitionParserDelegate { Element valueEle = null; for (int j = 0; j < entrySubNodes.getLength(); j++) { Node node = entrySubNodes.item(j); - if (node instanceof Element) { - Element candidateEle = (Element) node; + if (node instanceof Element candidateEle) { if (nodeNameEquals(candidateEle, KEY_ELEMENT)) { if (keyEle != null) { error(" element is only allowed to contain one sub-element", entryEle); diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/xml/DefaultBeanDefinitionDocumentReader.java b/spring-beans/src/main/java/org/springframework/beans/factory/xml/DefaultBeanDefinitionDocumentReader.java index af5026dce34..b8e2935a9c4 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/xml/DefaultBeanDefinitionDocumentReader.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/xml/DefaultBeanDefinitionDocumentReader.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. @@ -170,8 +170,7 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume NodeList nl = root.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); - if (node instanceof Element) { - Element ele = (Element) node; + if (node instanceof Element ele) { if (delegate.isDefaultNamespace(ele)) { parseDefaultElement(ele, delegate); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/xml/SimpleConstructorNamespaceHandler.java b/spring-beans/src/main/java/org/springframework/beans/factory/xml/SimpleConstructorNamespaceHandler.java index ddffb17d517..f30852f97b6 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/xml/SimpleConstructorNamespaceHandler.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/xml/SimpleConstructorNamespaceHandler.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. @@ -78,8 +78,7 @@ public class SimpleConstructorNamespaceHandler implements NamespaceHandler { @Override public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) { - if (node instanceof Attr) { - Attr attr = (Attr) node; + if (node instanceof Attr attr) { String argName = StringUtils.trimWhitespace(parserContext.getDelegate().getLocalName(attr)); String argValue = StringUtils.trimWhitespace(attr.getValue()); diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/xml/SimplePropertyNamespaceHandler.java b/spring-beans/src/main/java/org/springframework/beans/factory/xml/SimplePropertyNamespaceHandler.java index 9cecef4eed4..ec3c1512d8a 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/xml/SimplePropertyNamespaceHandler.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/xml/SimplePropertyNamespaceHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 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. @@ -67,8 +67,7 @@ public class SimplePropertyNamespaceHandler implements NamespaceHandler { @Override public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) { - if (node instanceof Attr) { - Attr attr = (Attr) node; + if (node instanceof Attr attr) { String propertyName = parserContext.getDelegate().getLocalName(attr); String propertyValue = attr.getValue(); MutablePropertyValues pvs = definition.getBeanDefinition().getPropertyValues(); diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReader.java b/spring-beans/src/main/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReader.java index 589208a4d3a..25cef22bd01 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReader.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReader.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. @@ -128,7 +128,7 @@ public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader { private final XmlValidationModeDetector validationModeDetector = new XmlValidationModeDetector(); private final ThreadLocal> resourcesCurrentlyBeingLoaded = - new NamedThreadLocal>("XML bean definition resources currently being loaded"){ + new NamedThreadLocal<>("XML bean definition resources currently being loaded"){ @Override protected Set initialValue() { return new HashSet<>(4); diff --git a/spring-beans/src/main/java/org/springframework/beans/support/MutableSortDefinition.java b/spring-beans/src/main/java/org/springframework/beans/support/MutableSortDefinition.java index 2a428fc9d07..096b0d6a2cd 100644 --- a/spring-beans/src/main/java/org/springframework/beans/support/MutableSortDefinition.java +++ b/spring-beans/src/main/java/org/springframework/beans/support/MutableSortDefinition.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 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. @@ -159,10 +159,9 @@ public class MutableSortDefinition implements SortDefinition, Serializable { if (this == other) { return true; } - if (!(other instanceof SortDefinition)) { + if (!(other instanceof SortDefinition otherSd)) { return false; } - SortDefinition otherSd = (SortDefinition) other; return (getProperty().equals(otherSd.getProperty()) && isAscending() == otherSd.isAscending() && isIgnoreCase() == otherSd.isIgnoreCase());