From 971f665eb1e8cd9e8a0c788a8f5612f815894082 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 14 Oct 2021 19:53:40 +0200 Subject: [PATCH] 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`. --- .../core/ArgumentPreparedStatementSetter.java | 5 ++--- .../ArgumentTypePreparedStatementSetter.java | 5 ++--- .../jdbc/core/BatchUpdateUtils.java | 5 ++--- .../springframework/jdbc/core/JdbcTemplate.java | 8 +++----- .../core/PreparedStatementCreatorFactory.java | 11 +++++------ .../jdbc/core/StatementCreatorUtils.java | 17 ++++++----------- .../metadata/GenericTableMetaDataProvider.java | 6 +++--- .../core/namedparam/NamedParameterUtils.java | 3 +-- .../jdbc/datasource/DataSourceUtils.java | 2 +- .../embedded/EmbeddedDatabaseFactory.java | 5 ++--- .../lookup/IsolationLevelDataSourceRouter.java | 5 ++--- .../springframework/jdbc/support/JdbcUtils.java | 8 +++----- .../jdbc/support/lob/PassThroughBlob.java | 4 ++-- .../jdbc/support/lob/PassThroughClob.java | 4 ++-- 14 files changed, 36 insertions(+), 52 deletions(-) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/ArgumentPreparedStatementSetter.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/ArgumentPreparedStatementSetter.java index 4f92385be8..915c602e6b 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/ArgumentPreparedStatementSetter.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/ArgumentPreparedStatementSetter.java @@ -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 { diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/ArgumentTypePreparedStatementSetter.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/ArgumentTypePreparedStatementSetter.java index 7e102efe31..3d89d8abe7 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/ArgumentTypePreparedStatementSetter.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/ArgumentTypePreparedStatementSetter.java @@ -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++; diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/BatchUpdateUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/BatchUpdateUtils.java index 8f1fcef395..dcab3b2ca9 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/BatchUpdateUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/BatchUpdateUtils.java @@ -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 { diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java index f41390c2c4..179a6b79e1 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -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 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) { diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementCreatorFactory.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementCreatorFactory.java index e6083f8be0..ecf784e4c6 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementCreatorFactory.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementCreatorFactory.java @@ -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); } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/StatementCreatorUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/StatementCreatorUtils.java index 5c6c1d859d..3d7edf0e8e 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/StatementCreatorUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/StatementCreatorUtils.java @@ -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 { diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/GenericTableMetaDataProvider.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/GenericTableMetaDataProvider.java index 5228cb9b05..85c3bc65fc 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/GenericTableMetaDataProvider.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/GenericTableMetaDataProvider.java @@ -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 productsNotSupportingGeneratedKeysColumnNameArray = + private final List productsNotSupportingGeneratedKeysColumnNameArray = Arrays.asList("Apache Derby", "HSQL Database Engine"); /** Collection of TableParameterMetaData objects. */ - private List tableParameterMetaData = new ArrayList<>(); + private final List tableParameterMetaData = new ArrayList<>(); /** diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java index 1325cdedf5..981dfc22e7 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java @@ -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) { diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java index ae708030bc..6f14c748e0 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java @@ -474,7 +474,7 @@ public abstract class DataSourceUtils { private final DataSource dataSource; - private int order; + private final int order; private boolean holderActive = true; diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseFactory.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseFactory.java index 52b66888b9..afc49cadd8 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseFactory.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseFactory.java @@ -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())); } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/IsolationLevelDataSourceRouter.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/IsolationLevelDataSourceRouter.java index b008aabf3d..6e5cd3c67b 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/IsolationLevelDataSourceRouter.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/IsolationLevelDataSourceRouter.java @@ -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"); } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/support/JdbcUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/support/JdbcUtils.java index 6fe7e9ea12..1fbeb25370 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/support/JdbcUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/support/JdbcUtils.java @@ -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)) { diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/support/lob/PassThroughBlob.java b/spring-jdbc/src/main/java/org/springframework/jdbc/support/lob/PassThroughBlob.java index ad50eeb94e..099bd27391 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/support/lob/PassThroughBlob.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/support/lob/PassThroughBlob.java @@ -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) { diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/support/lob/PassThroughClob.java b/spring-jdbc/src/main/java/org/springframework/jdbc/support/lob/PassThroughClob.java index 0f4d25a12d..b9a27afe92 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/support/lob/PassThroughClob.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/support/lob/PassThroughClob.java @@ -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) {