From ad46f0295c87982b3900c65fb4af3a211be0c4b5 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 30 May 2022 13:29:33 +0200 Subject: [PATCH] Apply "instanceof pattern matching" --- .../beans/AbstractPropertyAccessor.java | 6 +-- .../groovy/GroovyBeanDefinitionReader.java | 46 +++++++++---------- .../support/ArrayToArrayConverter.java | 9 ++-- .../core/env/CompositePropertySource.java | 6 +-- .../support/ResourceArrayPropertyEditor.java | 12 ++--- .../springframework/util/xml/DomUtils.java | 14 +++--- 6 files changed, 45 insertions(+), 48 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.java b/spring-beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.java index 1d6b5f48eab..01e67dbdf14 100644 --- a/spring-beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.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. @@ -89,8 +89,8 @@ public abstract class AbstractPropertyAccessor extends TypeConverterSupport impl throws BeansException { List propertyAccessExceptions = null; - List propertyValues = (pvs instanceof MutablePropertyValues ? - ((MutablePropertyValues) pvs).getPropertyValueList() : Arrays.asList(pvs.getPropertyValues())); + List propertyValues = (pvs instanceof MutablePropertyValues mpvs ? + mpvs.getPropertyValueList() : Arrays.asList(pvs.getPropertyValues())); if (ignoreUnknown) { this.suppressNotWritablePropertyException = true; 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 be6dac24a4b..2b201f8767c 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 @@ -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. @@ -377,23 +377,23 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp @Override public Object invokeMethod(String name, Object arg) { Object[] args = (Object[])arg; - if ("beans".equals(name) && args.length == 1 && args[0] instanceof Closure) { - return beans((Closure) args[0]); + if ("beans".equals(name) && args.length == 1 && args[0] instanceof Closure closure) { + return beans(closure); } else if ("ref".equals(name)) { String refName; if (args[0] == null) { throw new IllegalArgumentException("Argument to ref() is not a valid bean or was not found"); } - if (args[0] instanceof RuntimeBeanReference) { - refName = ((RuntimeBeanReference) args[0]).getBeanName(); + if (args[0] instanceof RuntimeBeanReference runtimeBeanReference) { + refName = runtimeBeanReference.getBeanName(); } else { refName = args[0].toString(); } boolean parentRef = false; - if (args.length > 1 && args[1] instanceof Boolean) { - parentRef = (Boolean) args[1]; + if (args.length > 1 && args[1] instanceof Boolean bool) { + parentRef = bool; } return new RuntimeBeanReference(refName, parentRef); } @@ -430,11 +430,11 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp private void finalizeDeferredProperties() { for (DeferredProperty dp : this.deferredProperties.values()) { - if (dp.value instanceof List) { - dp.value = manageListIfNecessary((List) dp.value); + if (dp.value instanceof List list) { + dp.value = manageListIfNecessary(list); } - else if (dp.value instanceof Map) { - dp.value = manageMapIfNecessary((Map) dp.value); + else if (dp.value instanceof Map map) { + dp.value = manageMapIfNecessary(map); } dp.apply(); } @@ -477,17 +477,16 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp beanName, beanClass, resolveConstructorArguments(args, 1, args.length)); } } - else if (args[0] instanceof RuntimeBeanReference) { + else if (args[0] instanceof RuntimeBeanReference runtimeBeanReference) { this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName); - this.currentBeanDefinition.getBeanDefinition().setFactoryBeanName(((RuntimeBeanReference) args[0]).getBeanName()); + this.currentBeanDefinition.getBeanDefinition().setFactoryBeanName(runtimeBeanReference.getBeanName()); } - else if (args[0] instanceof Map) { + else if (args[0] instanceof Map namedArgs) { // named constructor arguments - if (args.length > 1 && args[1] instanceof Class) { + if (args.length > 1 && args[1] instanceof Class clazz) { List constructorArgs = resolveConstructorArguments(args, 2, hasClosureArgument ? args.length - 1 : args.length); - this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, (Class) args[1], constructorArgs); - Map namedArgs = (Map) args[0]; + this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, clazz, constructorArgs); for (Map.Entry entity : namedArgs.entrySet()) { String propName = (String) entity.getKey(); setProperty(propName, entity.getValue()); @@ -497,7 +496,7 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp else { this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName); // First arg is the map containing factoryBean : factoryMethod - Map.Entry factoryBeanEntry = ((Map) args[0]).entrySet().iterator().next(); + Map.Entry factoryBeanEntry = namedArgs.entrySet().iterator().next(); // If we have a closure body, that will be the last argument. // In between are the constructor args int constructorArgsTest = (hasClosureArgument ? 2 : 1); @@ -546,11 +545,11 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp if (constructorArgs[i] instanceof GString) { constructorArgs[i] = constructorArgs[i].toString(); } - else if (constructorArgs[i] instanceof List) { - constructorArgs[i] = manageListIfNecessary((List) constructorArgs[i]); + else if (constructorArgs[i] instanceof List list) { + constructorArgs[i] = manageListIfNecessary(list); } - else if (constructorArgs[i] instanceof Map){ - constructorArgs[i] = manageMapIfNecessary((Map) constructorArgs[i]); + else if (constructorArgs[i] instanceof Map map){ + constructorArgs[i] = manageMapIfNecessary(map); } } return Arrays.asList(constructorArgs); @@ -618,10 +617,9 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp if (addDeferredProperty(name, value)) { return; } - else if (value instanceof Closure) { + else if (value instanceof Closure callable) { GroovyBeanDefinitionWrapper current = this.currentBeanDefinition; try { - Closure callable = (Closure) value; Class parameterType = callable.getParameterTypes()[0]; if (Object.class == parameterType) { this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(""); diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java index a51144741ca..7c345d1c76d 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 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. @@ -62,11 +62,10 @@ final class ArrayToArrayConverter implements ConditionalGenericConverter { @Override @Nullable public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { - if (this.conversionService instanceof GenericConversionService) { + if (this.conversionService instanceof GenericConversionService genericConversionService) { TypeDescriptor targetElement = targetType.getElementTypeDescriptor(); - if (targetElement != null && - ((GenericConversionService) this.conversionService).canBypassConvert( - sourceType.getElementTypeDescriptor(), targetElement)) { + if (targetElement != null && genericConversionService.canBypassConvert( + sourceType.getElementTypeDescriptor(), targetElement)) { return source; } } diff --git a/spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java b/spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java index ef6b764cf04..6224a95f339 100644 --- a/spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java +++ b/spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 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. @@ -80,11 +80,11 @@ public class CompositePropertySource extends EnumerablePropertySource { public String[] getPropertyNames() { Set names = new LinkedHashSet<>(); for (PropertySource propertySource : this.propertySources) { - if (!(propertySource instanceof EnumerablePropertySource)) { + if (!(propertySource instanceof EnumerablePropertySource enumerablePropertySource)) { throw new IllegalStateException( "Failed to enumerate property names due to non-enumerable property source: " + propertySource); } - names.addAll(Arrays.asList(((EnumerablePropertySource) propertySource).getPropertyNames())); + names.addAll(Arrays.asList(enumerablePropertySource.getPropertyNames())); } return StringUtils.toStringArray(names); } diff --git a/spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java b/spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java index ec1c00c599b..7d4aa971116 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java @@ -124,18 +124,18 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport { /** * Treat the given value as a collection or array and convert it to a Resource array. - * Considers String elements as location patterns and takes Resource elements as-is. + *

Considers String elements as location patterns and takes Resource elements as-is. */ @Override public void setValue(Object value) throws IllegalArgumentException { if (value instanceof Collection || (value instanceof Object[] && !(value instanceof Resource[]))) { - Collection input = (value instanceof Collection ? (Collection) value : Arrays.asList((Object[]) value)); + Collection input = (value instanceof Collection collection ? collection : Arrays.asList((Object[]) value)); Set merged = new LinkedHashSet<>(); for (Object element : input) { - if (element instanceof String) { + if (element instanceof String path) { // A location pattern: resolve it into a Resource array. // Might point to a single resource or to multiple resources. - String pattern = resolvePath((String) element).trim(); + String pattern = resolvePath(path.trim()); try { Resource[] resources = this.resourcePatternResolver.getResources(pattern); Collections.addAll(merged, resources); @@ -147,9 +147,9 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport { } } } - else if (element instanceof Resource) { + else if (element instanceof Resource resource) { // A Resource object: add it to the result. - merged.add((Resource) element); + merged.add(resource); } else { throw new IllegalArgumentException("Cannot convert element [" + element + "] to [" + diff --git a/spring-core/src/main/java/org/springframework/util/xml/DomUtils.java b/spring-core/src/main/java/org/springframework/util/xml/DomUtils.java index 3fd081a3afb..6481dbba309 100644 --- a/spring-core/src/main/java/org/springframework/util/xml/DomUtils.java +++ b/spring-core/src/main/java/org/springframework/util/xml/DomUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 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. @@ -65,8 +65,8 @@ public abstract class DomUtils { List childEles = new ArrayList<>(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); - if (node instanceof Element && nodeNameMatch(node, childEleNameList)) { - childEles.add((Element) node); + if (node instanceof Element element && nodeNameMatch(node, childEleNameList)) { + childEles.add(element); } } return childEles; @@ -99,8 +99,8 @@ public abstract class DomUtils { NodeList nl = ele.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); - if (node instanceof Element && nodeNameMatch(node, childEleName)) { - return (Element) node; + if (node instanceof Element element && nodeNameMatch(node, childEleName)) { + return element; } } return null; @@ -129,8 +129,8 @@ public abstract class DomUtils { List childEles = new ArrayList<>(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); - if (node instanceof Element) { - childEles.add((Element) node); + if (node instanceof Element element) { + childEles.add(element); } } return childEles;