Browse Source

Apply "instanceof pattern matching"

pull/28542/head
Sam Brannen 4 years ago
parent
commit
ad46f0295c
  1. 6
      spring-beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.java
  2. 46
      spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionReader.java
  3. 9
      spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java
  4. 6
      spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java
  5. 12
      spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java
  6. 14
      spring-core/src/main/java/org/springframework/util/xml/DomUtils.java

6
spring-beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.java

@ -1,5 +1,5 @@ @@ -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 @@ -89,8 +89,8 @@ public abstract class AbstractPropertyAccessor extends TypeConverterSupport impl
throws BeansException {
List<PropertyAccessException> propertyAccessExceptions = null;
List<PropertyValue> propertyValues = (pvs instanceof MutablePropertyValues ?
((MutablePropertyValues) pvs).getPropertyValueList() : Arrays.asList(pvs.getPropertyValues()));
List<PropertyValue> propertyValues = (pvs instanceof MutablePropertyValues mpvs ?
mpvs.getPropertyValueList() : Arrays.asList(pvs.getPropertyValues()));
if (ignoreUnknown) {
this.suppressNotWritablePropertyException = true;

46
spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionReader.java

@ -1,5 +1,5 @@ @@ -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 @@ -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 @@ -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 @@ -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<Object> 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 @@ -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 @@ -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 @@ -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("");

9
spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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;
}
}

6
spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java vendored

@ -1,5 +1,5 @@ @@ -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<Object> { @@ -80,11 +80,11 @@ public class CompositePropertySource extends EnumerablePropertySource<Object> {
public String[] getPropertyNames() {
Set<String> 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);
}

12
spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java

@ -124,18 +124,18 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport { @@ -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.
* <p>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<Resource> 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 { @@ -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 [" +

14
spring-core/src/main/java/org/springframework/util/xml/DomUtils.java

@ -1,5 +1,5 @@ @@ -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 { @@ -65,8 +65,8 @@ public abstract class DomUtils {
List<Element> 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 { @@ -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 { @@ -129,8 +129,8 @@ public abstract class DomUtils {
List<Element> 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;

Loading…
Cancel
Save