Nullability fine-tuning based on IntelliJ IDEA 2018.3 inspection

Issue: SPR-15540

(cherry picked from commit bf272b0b21)
This commit is contained in:
Juergen Hoeller
2018-11-22 16:12:38 +01:00
parent 23d1049363
commit e6c979606c
12 changed files with 87 additions and 85 deletions

View File

@@ -111,25 +111,25 @@ public abstract class NamedParameterUtils {
char c = statement[i];
if (c == ':' || c == '&') {
int j = i + 1;
if (j < statement.length && statement[j] == ':' && c == ':') {
if (c == ':' && j < statement.length && statement[j] == ':') {
// Postgres-style "::" casting operator should be skipped
i = i + 2;
continue;
}
String parameter = null;
if (j < statement.length && c == ':' && statement[j] == '{') {
if (c == ':' && j < statement.length && statement[j] == '{') {
// :{x} style parameter
while (j < statement.length && statement[j] != '}') {
while (statement[j] != '}') {
j++;
if (j >= statement.length) {
throw new InvalidDataAccessApiUsageException("Non-terminated named parameter declaration " +
"at position " + i + " in statement: " + sql);
}
if (statement[j] == ':' || statement[j] == '{') {
throw new InvalidDataAccessApiUsageException("Parameter name contains invalid character '" +
statement[j] + "' at position " + i + " in statement: " + sql);
}
}
if (j >= statement.length) {
throw new InvalidDataAccessApiUsageException(
"Non-terminated named parameter declaration at position " + i + " in statement: " + sql);
}
if (j - i > 2) {
parameter = sql.substring(i + 2, j);
namedParameterCount = addNewNamedParameter(namedParameters, namedParameterCount, parameter);
@@ -202,7 +202,7 @@ public abstract class NamedParameterUtils {
}
/**
* Skip over comments and quoted names present in an SQL statement
* Skip over comments and quoted names present in an SQL statement.
* @param statement character array containing SQL statement
* @param position current position of statement
* @return next position to process after any comments or quotes are skipped

View File

@@ -211,7 +211,7 @@ public abstract class RdbmsOperation implements InitializingBean {
* Set the column names of the auto-generated keys.
* @see java.sql.Connection#prepareStatement(String, String[])
*/
public void setGeneratedKeysColumnNames(String... names) {
public void setGeneratedKeysColumnNames(@Nullable String... names) {
if (isCompiled()) {
throw new InvalidDataAccessApiUsageException(
"The column names for the generated keys must be set before the operation is compiled");
@@ -230,7 +230,7 @@ public abstract class RdbmsOperation implements InitializingBean {
/**
* Set the SQL executed by this operation.
*/
public void setSql(String sql) {
public void setSql(@Nullable String sql) {
this.sql = sql;
}
@@ -297,7 +297,7 @@ public abstract class RdbmsOperation implements InitializingBean {
* Add one or more declared parameters. Used for configuring this operation
* when used in a bean factory. Each parameter will specify SQL type and (optionally)
* the parameter's name.
* @param parameters Array containing the declared {@link SqlParameter} objects
* @param parameters an array containing the declared {@link SqlParameter} objects
* @see #declaredParameters
*/
public void setParameters(SqlParameter... parameters) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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,13 +40,6 @@ import org.springframework.util.Assert;
*/
public abstract class SqlCall extends RdbmsOperation {
/**
* Object enabling us to create CallableStatementCreators
* efficiently, based on this class's declared parameters.
*/
@Nullable
private CallableStatementCreatorFactory callableStatementFactory;
/**
* Flag used to indicate that this call is for a function and to
* use the {? = call get_invoice_count(?)} syntax.
@@ -54,20 +47,26 @@ public abstract class SqlCall extends RdbmsOperation {
private boolean function = false;
/**
* Flag used to indicate that the sql for this call should be used exactly as it is
* defined. No need to add the escape syntax and parameter place holders.
* Flag used to indicate that the sql for this call should be used exactly as
* it is defined. No need to add the escape syntax and parameter place holders.
*/
private boolean sqlReadyForUse = false;
/**
* Call string as defined in java.sql.CallableStatement.
* String of form {call add_invoice(?, ?, ?)}
* or {? = call get_invoice_count(?)} if isFunction is set to true
* Updated after each parameter is added.
* String of form {call add_invoice(?, ?, ?)} or {? = call get_invoice_count(?)}
* if isFunction is set to true. Updated after each parameter is added.
*/
@Nullable
private String callString;
/**
* Object enabling us to create CallableStatementCreators
* efficiently, based on this class's declared parameters.
*/
@Nullable
private CallableStatementCreatorFactory callableStatementFactory;
/**
* Constructor to allow use as a JavaBean.
@@ -83,8 +82,8 @@ public abstract class SqlCall extends RdbmsOperation {
/**
* Create a new SqlCall object with SQL, but without parameters.
* Must add parameters or settle with none.
* @param ds DataSource to obtain connections from
* @param sql SQL to execute
* @param ds the DataSource to obtain connections from
* @param sql the SQL to execute
*/
public SqlCall(DataSource ds, String sql) {
setDataSource(ds);
@@ -103,7 +102,7 @@ public abstract class SqlCall extends RdbmsOperation {
* Return whether this call is for a function.
*/
public boolean isFunction() {
return function;
return this.function;
}
/**
@@ -117,7 +116,7 @@ public abstract class SqlCall extends RdbmsOperation {
* Return whether the SQL can be used as is.
*/
public boolean isSqlReadyForUse() {
return sqlReadyForUse;
return this.sqlReadyForUse;
}
@@ -129,30 +128,32 @@ public abstract class SqlCall extends RdbmsOperation {
@Override
protected final void compileInternal() {
if (isSqlReadyForUse()) {
this.callString = getSql();
this.callString = resolveSql();
}
else {
StringBuilder callString = new StringBuilder(32);
List<SqlParameter> parameters = getDeclaredParameters();
int parameterCount = 0;
if (isFunction()) {
this.callString = "{? = call " + getSql() + "(";
callString.append("{? = call ").append(resolveSql()).append('(');
parameterCount = -1;
}
else {
this.callString = "{call " + getSql() + "(";
callString.append("{call ").append(resolveSql()).append('(');
}
for (SqlParameter parameter : parameters) {
if (!(parameter.isResultsParameter())) {
if (!parameter.isResultsParameter()) {
if (parameterCount > 0) {
this.callString += ", ";
callString.append(", ");
}
if (parameterCount >= 0) {
this.callString += "?";
callString.append('?');
}
parameterCount++;
}
}
this.callString += ")}";
callString.append(")}");
this.callString = callString.toString();
}
if (logger.isDebugEnabled()) {
logger.debug("Compiled stored procedure. Call string is [" + this.callString + "]");