Migrate code to Java 17 style.

Use var instead of explicit local types where applicable. Use pattern variable instead instanceof and cast. Prefer loops and nullable types over Stream and Optional. Convert classes to records where applicable.

See #2465
This commit is contained in:
Mark Paluch
2021-10-05 15:21:12 +02:00
committed by Jens Schauder
parent d4036ec0a9
commit c735b58607
463 changed files with 3670 additions and 4014 deletions

View File

@@ -16,12 +16,12 @@
package org.springframework.data.config;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
@@ -60,8 +60,8 @@ public class BeanComponentDefinitionBuilder {
Assert.notNull(builder, "Builder must not be null!");
AbstractBeanDefinition definition = builder.getRawBeanDefinition();
String name = BeanDefinitionReaderUtils.generateBeanName(definition, context.getRegistry(), context.isNested());
var definition = builder.getRawBeanDefinition();
var name = BeanDefinitionReaderUtils.generateBeanName(definition, context.getRegistry(), context.isNested());
return getComponent(builder, name);
}
@@ -78,7 +78,7 @@ public class BeanComponentDefinitionBuilder {
Assert.hasText(fallback, "Fallback component id must not be null or empty!");
String id = defaultSource.getAttribute("id");
var id = defaultSource.getAttribute("id");
return getComponent(builder, StringUtils.hasText(id) ? id : fallback);
}
@@ -107,7 +107,7 @@ public class BeanComponentDefinitionBuilder {
Assert.notNull(builder, "Builder must not be null!");
Assert.hasText(name, "Name of bean must not be null or empty!");
AbstractBeanDefinition definition = builder.getRawBeanDefinition();
var definition = builder.getRawBeanDefinition();
definition.setSource(context.extractSource(rawSource));
return new BeanComponentDefinition(definition, name);

View File

@@ -41,7 +41,7 @@ public interface ConfigurationUtils {
Assert.notNull(context, "XmlReaderContext must not be null!");
ResourceLoader resourceLoader = context.getResourceLoader();
var resourceLoader = context.getResourceLoader();
if (resourceLoader == null) {
throw new IllegalArgumentException("Could not obtain ResourceLoader from XmlReaderContext!");
@@ -72,7 +72,7 @@ public interface ConfigurationUtils {
Assert.notNull(resourceLoader, "ResourceLoader must not be null!");
ClassLoader classLoader = resourceLoader.getClassLoader();
var classLoader = resourceLoader.getClassLoader();
if (classLoader == null) {
throw new IllegalArgumentException("Could not obtain ClassLoader from ResourceLoader!");
@@ -92,7 +92,7 @@ public interface ConfigurationUtils {
Assert.notNull(beanDefinition, "BeanDefinition must not be null!");
String result = beanDefinition.getBeanClassName();
var result = beanDefinition.getBeanClassName();
if (result == null) {
throw new IllegalArgumentException(

View File

@@ -55,7 +55,7 @@ public abstract class ParsingUtils {
Assert.hasText(attrName, "Attribute name must not be null!");
Assert.hasText(propertyName, "Property name must not be null!");
String attr = element.getAttribute(attrName);
var attr = element.getAttribute(attrName);
if (StringUtils.hasText(attr)) {
builder.addPropertyValue(propertyName, attr);
@@ -90,7 +90,7 @@ public abstract class ParsingUtils {
Assert.hasText(attribute, "Attribute name must not be null!");
Assert.hasText(property, "Property name must not be null!");
String value = element.getAttribute(attribute);
var value = element.getAttribute(attribute);
if (StringUtils.hasText(value)) {
builder.addPropertyReference(property, value);
@@ -126,7 +126,7 @@ public abstract class ParsingUtils {
Assert.notNull(builder, "Builder must not be null!");
AbstractBeanDefinition definition = builder.getRawBeanDefinition();
var definition = builder.getRawBeanDefinition();
definition.setSource(source);
return definition;
}
@@ -143,7 +143,7 @@ public abstract class ParsingUtils {
Assert.hasText(targetBeanName, "Target bean name must not be null or empty!");
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(ObjectFactoryCreatingFactoryBean.class);
var builder = BeanDefinitionBuilder.rootBeanDefinition(ObjectFactoryCreatingFactoryBean.class);
builder.addPropertyValue("targetBeanName", targetBeanName);
builder.setRole(AbstractBeanDefinition.ROLE_INFRASTRUCTURE);

View File

@@ -32,9 +32,9 @@ import org.springframework.core.type.filter.RegexPatternTypeFilter;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* Parser to populate the given {@link ClassPathScanningCandidateComponentProvider} with {@link TypeFilter}s parsed from
@@ -85,13 +85,13 @@ public class TypeFilterParser {
*/
public Collection<TypeFilter> parseTypeFilters(Element element, Type type) {
NodeList nodeList = element.getChildNodes();
var nodeList = element.getChildNodes();
Collection<TypeFilter> filters = new HashSet<>();
for (int i = 0; i < nodeList.getLength(); i++) {
for (var i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element childElement = type.getElement(node);
var node = nodeList.item(i);
var childElement = type.getElement(node);
if (childElement == null) {
continue;
@@ -116,12 +116,12 @@ public class TypeFilterParser {
*/
protected TypeFilter createTypeFilter(Element element, ClassLoader classLoader) {
String filterType = element.getAttribute(FILTER_TYPE_ATTRIBUTE);
String expression = element.getAttribute(FILTER_EXPRESSION_ATTRIBUTE);
var filterType = element.getAttribute(FILTER_TYPE_ATTRIBUTE);
var expression = element.getAttribute(FILTER_EXPRESSION_ATTRIBUTE);
try {
FilterType filter = FilterType.fromString(filterType);
var filter = FilterType.fromString(filterType);
return filter.getFilter(expression, classLoader);
} catch (ClassNotFoundException ex) {
@@ -136,7 +136,7 @@ public class TypeFilterParser {
* @author Oliver Gierke
* @see #getFilter(String, ClassLoader)
*/
private static enum FilterType {
private enum FilterType {
ANNOTATION {
@Override
@@ -171,7 +171,7 @@ public class TypeFilterParser {
@Override
public TypeFilter getFilter(String expression, ClassLoader classLoader) throws ClassNotFoundException {
Class<?> filterClass = classLoader.loadClass(expression);
var filterClass = classLoader.loadClass(expression);
if (!TypeFilter.class.isAssignableFrom(filterClass)) {
throw new IllegalArgumentException(
"Class is not assignable to [" + TypeFilter.class.getName() + "]: " + expression);
@@ -199,7 +199,7 @@ public class TypeFilterParser {
*/
static FilterType fromString(String typeString) {
for (FilterType filter : FilterType.values()) {
for (var filter : FilterType.values()) {
if (filter.name().equalsIgnoreCase(typeString)) {
return filter;
}
@@ -209,13 +209,13 @@ public class TypeFilterParser {
}
}
public static enum Type {
public enum Type {
INCLUDE("include-filter"), EXCLUDE("exclude-filter");
private String elementName;
private final String elementName;
private Type(String elementName) {
Type(String elementName) {
this.elementName = elementName;
}
@@ -230,7 +230,7 @@ public class TypeFilterParser {
Element getElement(Node node) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
String localName = node.getLocalName();
var localName = node.getLocalName();
if (elementName.equals(localName)) {
return (Element) node;
}