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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 * @throws SQLException if thrown by PreparedStatement methods
*/ */
protected void doSetValue(PreparedStatement ps, int parameterPosition, Object argValue) throws SQLException { protected void doSetValue(PreparedStatement ps, int parameterPosition, Object argValue) throws SQLException {
if (argValue instanceof SqlParameterValue) { if (argValue instanceof SqlParameterValue paramValue) {
SqlParameterValue paramValue = (SqlParameterValue) argValue;
StatementCreatorUtils.setParameterValue(ps, parameterPosition, paramValue, paramValue.getValue()); StatementCreatorUtils.setParameterValue(ps, parameterPosition, paramValue, paramValue.getValue());
} }
else { 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { if (arg instanceof Collection && this.argTypes[i] != Types.ARRAY) {
Collection<?> entries = (Collection<?>) arg; Collection<?> entries = (Collection<?>) arg;
for (Object entry : entries) { for (Object entry : entries) {
if (entry instanceof Object[]) { if (entry instanceof Object[] valueArray) {
Object[] valueArray = ((Object[]) entry);
for (Object argValue : valueArray) { for (Object argValue : valueArray) {
doSetValue(ps, parameterPosition, this.argTypes[i], argValue); doSetValue(ps, parameterPosition, this.argTypes[i], argValue);
parameterPosition++; 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; int colIndex = 0;
for (Object value : values) { for (Object value : values) {
colIndex++; colIndex++;
if (value instanceof SqlParameterValue) { if (value instanceof SqlParameterValue paramValue) {
SqlParameterValue paramValue = (SqlParameterValue) value;
StatementCreatorUtils.setParameterValue(ps, colIndex, paramValue, paramValue.getValue()); StatementCreatorUtils.setParameterValue(ps, colIndex, paramValue, paramValue.getValue());
} }
else { 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; int colIndex = 0;
for (Object value : values) { for (Object value : values) {
colIndex++; colIndex++;
if (value instanceof SqlParameterValue) { if (value instanceof SqlParameterValue paramValue) {
SqlParameterValue paramValue = (SqlParameterValue) value;
StatementCreatorUtils.setParameterValue(ps, colIndex, paramValue, paramValue.getValue()); StatementCreatorUtils.setParameterValue(ps, colIndex, paramValue, paramValue.getValue());
} }
else { else {
@ -1337,8 +1336,7 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
Map<String, Object> results = CollectionUtils.newLinkedHashMap(parameters.size()); Map<String, Object> results = CollectionUtils.newLinkedHashMap(parameters.size());
int sqlColIndex = 1; int sqlColIndex = 1;
for (SqlParameter param : parameters) { for (SqlParameter param : parameters) {
if (param instanceof SqlOutParameter) { if (param instanceof SqlOutParameter outParam) {
SqlOutParameter outParam = (SqlOutParameter) param;
Assert.state(outParam.getName() != null, "Anonymous parameters not allowed"); Assert.state(outParam.getName() != null, "Anonymous parameters not allowed");
SqlReturnType returnType = outParam.getSqlReturnType(); SqlReturnType returnType = outParam.getSqlReturnType();
if (returnType != null) { 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types; import java.sql.Types;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -228,7 +229,7 @@ public class PreparedStatementCreatorFactory {
ps = con.prepareStatement(this.actualSql, generatedKeysColumnNames); ps = con.prepareStatement(this.actualSql, generatedKeysColumnNames);
} }
else { 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) { else if (resultSetType == ResultSet.TYPE_FORWARD_ONLY && !updatableResults) {
@ -251,8 +252,7 @@ public class PreparedStatementCreatorFactory {
SqlParameter declaredParameter; SqlParameter declaredParameter;
// SqlParameterValue overrides declared parameter meta-data, in particular for // SqlParameterValue overrides declared parameter meta-data, in particular for
// independence from the declared parameter position in case of named parameters. // independence from the declared parameter position in case of named parameters.
if (in instanceof SqlParameterValue) { if (in instanceof SqlParameterValue paramValue) {
SqlParameterValue paramValue = (SqlParameterValue) in;
in = paramValue.getValue(); in = paramValue.getValue();
declaredParameter = paramValue; declaredParameter = paramValue;
} }
@ -268,8 +268,7 @@ public class PreparedStatementCreatorFactory {
if (in instanceof Iterable && declaredParameter.getSqlType() != Types.ARRAY) { if (in instanceof Iterable && declaredParameter.getSqlType() != Types.ARRAY) {
Iterable<?> entries = (Iterable<?>) in; Iterable<?> entries = (Iterable<?>) in;
for (Object entry : entries) { for (Object entry : entries) {
if (entry instanceof Object[]) { if (entry instanceof Object[] valueArray) {
Object[] valueArray = (Object[]) entry;
for (Object argValue : valueArray) { for (Object argValue : valueArray) {
StatementCreatorUtils.setParameterValue(ps, sqlColIndx++, declaredParameter, argValue); 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; Object inValueToUse = inValue;
// override type info? // override type info?
if (inValue instanceof SqlParameterValue) { if (inValue instanceof SqlParameterValue parameterValue) {
SqlParameterValue parameterValue = (SqlParameterValue) inValue;
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Overriding type info with runtime info from SqlParameterValue: column index " + paramIndex + logger.debug("Overriding type info with runtime info from SqlParameterValue: column index " + paramIndex +
", SQL type " + parameterValue.getSqlType() + ", type name " + parameterValue.getTypeName()); ", 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())); ps.setDate(paramIndex, new java.sql.Date(((java.util.Date) inValue).getTime()));
} }
} }
else if (inValue instanceof Calendar) { else if (inValue instanceof Calendar cal) {
Calendar cal = (Calendar) inValue;
ps.setDate(paramIndex, new java.sql.Date(cal.getTime().getTime()), cal); ps.setDate(paramIndex, new java.sql.Date(cal.getTime().getTime()), cal);
} }
else { else {
@ -367,8 +365,7 @@ public abstract class StatementCreatorUtils {
ps.setTime(paramIndex, new java.sql.Time(((java.util.Date) inValue).getTime())); ps.setTime(paramIndex, new java.sql.Time(((java.util.Date) inValue).getTime()));
} }
} }
else if (inValue instanceof Calendar) { else if (inValue instanceof Calendar cal) {
Calendar cal = (Calendar) inValue;
ps.setTime(paramIndex, new java.sql.Time(cal.getTime().getTime()), cal); ps.setTime(paramIndex, new java.sql.Time(cal.getTime().getTime()), cal);
} }
else { else {
@ -384,8 +381,7 @@ public abstract class StatementCreatorUtils {
ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime())); ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime()));
} }
} }
else if (inValue instanceof Calendar) { else if (inValue instanceof Calendar cal) {
Calendar cal = (Calendar) inValue;
ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal); ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal);
} }
else { else {
@ -400,8 +396,7 @@ public abstract class StatementCreatorUtils {
else if (isDateValue(inValue.getClass())) { else if (isDateValue(inValue.getClass())) {
ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime())); ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime()));
} }
else if (inValue instanceof Calendar) { else if (inValue instanceof Calendar cal) {
Calendar cal = (Calendar) inValue;
ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal); ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal);
} }
else { else {

View File

@ -56,7 +56,7 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
/** the name of the user currently connected. */ /** the name of the user currently connected. */
@Nullable @Nullable
private String userName; private final String userName;
/** indicates whether the identifiers are uppercased. */ /** indicates whether the identifiers are uppercased. */
private boolean storesUpperCaseIdentifiers = true; private boolean storesUpperCaseIdentifiers = true;
@ -71,11 +71,11 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
private boolean generatedKeysColumnNameArraySupported = true; private boolean generatedKeysColumnNameArraySupported = true;
/** database products we know not supporting the use of a String[] for generated keys. */ /** 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"); Arrays.asList("Apache Derby", "HSQL Database Engine");
/** Collection of TableParameterMetaData objects. */ /** 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++; k++;
Object entryItem = entryIter.next(); Object entryItem = entryIter.next();
if (entryItem instanceof Object[]) { if (entryItem instanceof Object[] expressionList) {
Object[] expressionList = (Object[]) entryItem;
actualSql.append('('); actualSql.append('(');
for (int m = 0; m < expressionList.length; m++) { for (int m = 0; m < expressionList.length; m++) {
if (m > 0) { if (m > 0) {

View File

@ -474,7 +474,7 @@ public abstract class DataSourceUtils {
private final DataSource dataSource; private final DataSource dataSource;
private int order; private final int order;
private boolean holderActive = true; 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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(); this.dataSource = this.dataSourceFactory.getDataSource();
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
if (this.dataSource instanceof SimpleDriverDataSource) { if (this.dataSource instanceof SimpleDriverDataSource simpleDriverDataSource) {
SimpleDriverDataSource simpleDriverDataSource = (SimpleDriverDataSource) this.dataSource;
logger.info(String.format("Starting embedded database: url='%s', username='%s'", logger.info(String.format("Starting embedded database: url='%s', username='%s'",
simpleDriverDataSource.getUrl(), simpleDriverDataSource.getUsername())); 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { if (lookupKey instanceof Integer) {
return lookupKey; return lookupKey;
} }
else if (lookupKey instanceof String) { else if (lookupKey instanceof String constantName) {
String constantName = (String) lookupKey;
if (!constantName.startsWith(DefaultTransactionDefinition.PREFIX_ISOLATION)) { if (!constantName.startsWith(DefaultTransactionDefinition.PREFIX_ISOLATION)) {
throw new IllegalArgumentException("Only isolation constants allowed"); 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { if (obj != null) {
className = obj.getClass().getName(); className = obj.getClass().getName();
} }
if (obj instanceof Blob) { if (obj instanceof Blob blob) {
Blob blob = (Blob) obj;
obj = blob.getBytes(1, (int) blob.length()); obj = blob.getBytes(1, (int) blob.length());
} }
else if (obj instanceof Clob) { else if (obj instanceof Clob clob) {
Clob clob = (Clob) obj;
obj = clob.getSubString(1, (int) clob.length()); obj = clob.getSubString(1, (int) clob.length());
} }
else if ("oracle.sql.TIMESTAMP".equals(className) || "oracle.sql.TIMESTAMPTZ".equals(className)) { 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -40,7 +40,7 @@ class PassThroughBlob implements Blob {
@Nullable @Nullable
private InputStream binaryStream; private InputStream binaryStream;
private long contentLength; private final long contentLength;
public PassThroughBlob(byte[] content) { 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -50,7 +50,7 @@ class PassThroughClob implements Clob {
@Nullable @Nullable
private InputStream asciiStream; private InputStream asciiStream;
private long contentLength; private final long contentLength;
public PassThroughClob(String content) { public PassThroughClob(String content) {