Apply "instanceof pattern matching" in spring-jdbc

This commit also applies additional clean-up tasks such as the following.

- final fields

This has only been applied to `src/main/java`.
This commit is contained in:
Sam Brannen 2021-10-14 19:53:40 +02:00
parent 4e6ef82d8f
commit 971f665eb1
14 changed files with 36 additions and 52 deletions

View File

@ -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.
@ -61,8 +61,7 @@ public class ArgumentPreparedStatementSetter implements PreparedStatementSetter,
* @throws SQLException if thrown by PreparedStatement methods
*/
protected void doSetValue(PreparedStatement ps, int parameterPosition, Object argValue) throws SQLException {
if (argValue instanceof SqlParameterValue) {
SqlParameterValue paramValue = (SqlParameterValue) argValue;
if (argValue instanceof SqlParameterValue paramValue) {
StatementCreatorUtils.setParameterValue(ps, parameterPosition, paramValue, paramValue.getValue());
}
else {

View File

@ -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.
@ -64,8 +64,7 @@ public class ArgumentTypePreparedStatementSetter implements PreparedStatementSet
if (arg instanceof Collection && this.argTypes[i] != Types.ARRAY) {
Collection<?> entries = (Collection<?>) arg;
for (Object entry : entries) {
if (entry instanceof Object[]) {
Object[] valueArray = ((Object[]) entry);
if (entry instanceof Object[] valueArray) {
for (Object argValue : valueArray) {
doSetValue(ps, parameterPosition, this.argTypes[i], argValue);
parameterPosition++;

View File

@ -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.
@ -62,8 +62,7 @@ public abstract class BatchUpdateUtils {
int colIndex = 0;
for (Object value : values) {
colIndex++;
if (value instanceof SqlParameterValue) {
SqlParameterValue paramValue = (SqlParameterValue) value;
if (value instanceof SqlParameterValue paramValue) {
StatementCreatorUtils.setParameterValue(ps, colIndex, paramValue, paramValue.getValue());
}
else {

View File

@ -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.
@ -1094,8 +1094,7 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
int colIndex = 0;
for (Object value : values) {
colIndex++;
if (value instanceof SqlParameterValue) {
SqlParameterValue paramValue = (SqlParameterValue) value;
if (value instanceof SqlParameterValue paramValue) {
StatementCreatorUtils.setParameterValue(ps, colIndex, paramValue, paramValue.getValue());
}
else {
@ -1337,8 +1336,7 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
Map<String, Object> results = CollectionUtils.newLinkedHashMap(parameters.size());
int sqlColIndex = 1;
for (SqlParameter param : parameters) {
if (param instanceof SqlOutParameter) {
SqlOutParameter outParam = (SqlOutParameter) param;
if (param instanceof SqlOutParameter outParam) {
Assert.state(outParam.getName() != null, "Anonymous parameters not allowed");
SqlReturnType returnType = outParam.getSqlReturnType();
if (returnType != null) {

View File

@ -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.
@ -20,6 +20,7 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
@ -228,7 +229,7 @@ public class PreparedStatementCreatorFactory {
ps = con.prepareStatement(this.actualSql, generatedKeysColumnNames);
}
else {
ps = con.prepareStatement(this.actualSql, PreparedStatement.RETURN_GENERATED_KEYS);
ps = con.prepareStatement(this.actualSql, Statement.RETURN_GENERATED_KEYS);
}
}
else if (resultSetType == ResultSet.TYPE_FORWARD_ONLY && !updatableResults) {
@ -251,8 +252,7 @@ public class PreparedStatementCreatorFactory {
SqlParameter declaredParameter;
// SqlParameterValue overrides declared parameter meta-data, in particular for
// independence from the declared parameter position in case of named parameters.
if (in instanceof SqlParameterValue) {
SqlParameterValue paramValue = (SqlParameterValue) in;
if (in instanceof SqlParameterValue paramValue) {
in = paramValue.getValue();
declaredParameter = paramValue;
}
@ -268,8 +268,7 @@ public class PreparedStatementCreatorFactory {
if (in instanceof Iterable && declaredParameter.getSqlType() != Types.ARRAY) {
Iterable<?> entries = (Iterable<?>) in;
for (Object entry : entries) {
if (entry instanceof Object[]) {
Object[] valueArray = (Object[]) entry;
if (entry instanceof Object[] valueArray) {
for (Object argValue : valueArray) {
StatementCreatorUtils.setParameterValue(ps, sqlColIndx++, declaredParameter, argValue);
}

View File

@ -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.
@ -202,8 +202,7 @@ public abstract class StatementCreatorUtils {
Object inValueToUse = inValue;
// override type info?
if (inValue instanceof SqlParameterValue) {
SqlParameterValue parameterValue = (SqlParameterValue) inValue;
if (inValue instanceof SqlParameterValue parameterValue) {
if (logger.isDebugEnabled()) {
logger.debug("Overriding type info with runtime info from SqlParameterValue: column index " + paramIndex +
", SQL type " + parameterValue.getSqlType() + ", type name " + parameterValue.getTypeName());
@ -350,8 +349,7 @@ public abstract class StatementCreatorUtils {
ps.setDate(paramIndex, new java.sql.Date(((java.util.Date) inValue).getTime()));
}
}
else if (inValue instanceof Calendar) {
Calendar cal = (Calendar) inValue;
else if (inValue instanceof Calendar cal) {
ps.setDate(paramIndex, new java.sql.Date(cal.getTime().getTime()), cal);
}
else {
@ -367,8 +365,7 @@ public abstract class StatementCreatorUtils {
ps.setTime(paramIndex, new java.sql.Time(((java.util.Date) inValue).getTime()));
}
}
else if (inValue instanceof Calendar) {
Calendar cal = (Calendar) inValue;
else if (inValue instanceof Calendar cal) {
ps.setTime(paramIndex, new java.sql.Time(cal.getTime().getTime()), cal);
}
else {
@ -384,8 +381,7 @@ public abstract class StatementCreatorUtils {
ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime()));
}
}
else if (inValue instanceof Calendar) {
Calendar cal = (Calendar) inValue;
else if (inValue instanceof Calendar cal) {
ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal);
}
else {
@ -400,8 +396,7 @@ public abstract class StatementCreatorUtils {
else if (isDateValue(inValue.getClass())) {
ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime()));
}
else if (inValue instanceof Calendar) {
Calendar cal = (Calendar) inValue;
else if (inValue instanceof Calendar cal) {
ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal);
}
else {

View File

@ -56,7 +56,7 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
/** the name of the user currently connected. */
@Nullable
private String userName;
private final String userName;
/** indicates whether the identifiers are uppercased. */
private boolean storesUpperCaseIdentifiers = true;
@ -71,11 +71,11 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
private boolean generatedKeysColumnNameArraySupported = true;
/** database products we know not supporting the use of a String[] for generated keys. */
private List<String> productsNotSupportingGeneratedKeysColumnNameArray =
private final List<String> productsNotSupportingGeneratedKeysColumnNameArray =
Arrays.asList("Apache Derby", "HSQL Database Engine");
/** Collection of TableParameterMetaData objects. */
private List<TableParameterMetaData> tableParameterMetaData = new ArrayList<>();
private final List<TableParameterMetaData> tableParameterMetaData = new ArrayList<>();
/**

View File

@ -293,8 +293,7 @@ public abstract class NamedParameterUtils {
}
k++;
Object entryItem = entryIter.next();
if (entryItem instanceof Object[]) {
Object[] expressionList = (Object[]) entryItem;
if (entryItem instanceof Object[] expressionList) {
actualSql.append('(');
for (int m = 0; m < expressionList.length; m++) {
if (m > 0) {

View File

@ -474,7 +474,7 @@ public abstract class DataSourceUtils {
private final DataSource dataSource;
private int order;
private final int order;
private boolean holderActive = true;

View File

@ -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.
@ -185,8 +185,7 @@ public class EmbeddedDatabaseFactory {
this.dataSource = this.dataSourceFactory.getDataSource();
if (logger.isInfoEnabled()) {
if (this.dataSource instanceof SimpleDriverDataSource) {
SimpleDriverDataSource simpleDriverDataSource = (SimpleDriverDataSource) this.dataSource;
if (this.dataSource instanceof SimpleDriverDataSource simpleDriverDataSource) {
logger.info(String.format("Starting embedded database: url='%s', username='%s'",
simpleDriverDataSource.getUrl(), simpleDriverDataSource.getUsername()));
}

View File

@ -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.
@ -106,8 +106,7 @@ public class IsolationLevelDataSourceRouter extends AbstractRoutingDataSource {
if (lookupKey instanceof Integer) {
return lookupKey;
}
else if (lookupKey instanceof String) {
String constantName = (String) lookupKey;
else if (lookupKey instanceof String constantName) {
if (!constantName.startsWith(DefaultTransactionDefinition.PREFIX_ISOLATION)) {
throw new IllegalArgumentException("Only isolation constants allowed");
}

View File

@ -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.
@ -285,12 +285,10 @@ public abstract class JdbcUtils {
if (obj != null) {
className = obj.getClass().getName();
}
if (obj instanceof Blob) {
Blob blob = (Blob) obj;
if (obj instanceof Blob blob) {
obj = blob.getBytes(1, (int) blob.length());
}
else if (obj instanceof Clob) {
Clob clob = (Clob) obj;
else if (obj instanceof Clob clob) {
obj = clob.getSubString(1, (int) clob.length());
}
else if ("oracle.sql.TIMESTAMP".equals(className) || "oracle.sql.TIMESTAMPTZ".equals(className)) {

View File

@ -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.
@ -40,7 +40,7 @@ class PassThroughBlob implements Blob {
@Nullable
private InputStream binaryStream;
private long contentLength;
private final long contentLength;
public PassThroughBlob(byte[] content) {

View File

@ -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.
@ -50,7 +50,7 @@ class PassThroughClob implements Clob {
@Nullable
private InputStream asciiStream;
private long contentLength;
private final long contentLength;
public PassThroughClob(String content) {