Skip to content

Commit bf272b0

Browse files
committed
Nullability fine-tuning based on IntelliJ IDEA 2018.3 inspection
Issue: SPR-15540
1 parent b1b28d4 commit bf272b0

File tree

14 files changed

+66
-66
lines changed

14 files changed

+66
-66
lines changed

spring-beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ public void setWriteMethod(@Nullable Method writeMethod) {
314314
}
315315

316316
@Override
317+
@Nullable
317318
public Class<?> getPropertyType() {
318319
if (this.propertyType == null) {
319320
try {
@@ -425,6 +426,7 @@ public void setWriteMethod(@Nullable Method writeMethod) {
425426
}
426427

427428
@Override
429+
@Nullable
428430
public Class<?> getPropertyType() {
429431
if (this.propertyType == null) {
430432
try {
@@ -460,6 +462,7 @@ public void setIndexedWriteMethod(@Nullable Method indexedWriteMethod) throws In
460462
}
461463

462464
@Override
465+
@Nullable
463466
public Class<?> getIndexedPropertyType() {
464467
if (this.indexedPropertyType == null) {
465468
try {

spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public BeanWrapper autowireConstructor(String beanName, RootBeanDefinition mbd,
196196
for (Constructor<?> candidate : candidates) {
197197
Class<?>[] paramTypes = candidate.getParameterTypes();
198198

199-
if (constructorToUse != null && argsToUse.length > paramTypes.length) {
199+
if (constructorToUse != null && argsToUse != null && argsToUse.length > paramTypes.length) {
200200
// Already found greedy constructor that can be satisfied ->
201201
// do not look any further, there are only less greedy constructors left.
202202
break;
@@ -276,11 +276,12 @@ else if (ambiguousConstructors != null && !mbd.isLenientConstructorResolution())
276276
ambiguousConstructors);
277277
}
278278

279-
if (explicitArgs == null) {
279+
if (explicitArgs == null && argsHolderToUse != null) {
280280
argsHolderToUse.storeCache(mbd, constructorToUse);
281281
}
282282
}
283283

284+
Assert.state(argsToUse != null, "Unresolved constructor arguments");
284285
bw.setBeanInstance(instantiate(beanName, mbd, constructorToUse, argsToUse));
285286
return bw;
286287
}
@@ -602,12 +603,13 @@ else if (ambiguousFactoryMethods != null) {
602603
}
603604
}
604605

606+
Assert.state(argsToUse != null, "Unresolved factory method arguments");
605607
bw.setBeanInstance(instantiate(beanName, mbd, factoryBean, factoryMethodToUse, argsToUse));
606608
return bw;
607609
}
608610

609-
private Object instantiate(
610-
String beanName, RootBeanDefinition mbd, Object factoryBean, Method factoryMethod, Object[] args) {
611+
private Object instantiate(String beanName, RootBeanDefinition mbd,
612+
@Nullable Object factoryBean, Method factoryMethod, Object[] args) {
611613

612614
try {
613615
if (System.getSecurityManager() != null) {

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ private Object resolveBeanReference(Method beanMethod, Object[] beanMethodArgs,
393393
Object beanInstance = (useArgs ? beanFactory.getBean(beanName, beanMethodArgs) :
394394
beanFactory.getBean(beanName));
395395
if (!ClassUtils.isAssignableValue(beanMethod.getReturnType(), beanInstance)) {
396+
// Detect package-protected NullBean instance through equals(null) check
396397
if (beanInstance.equals(null)) {
397398
if (logger.isDebugEnabled()) {
398399
logger.debug(String.format("@Bean method %s.%s called as bean reference " +

spring-context/src/main/java/org/springframework/jmx/support/ConnectorServerFactoryBean.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,12 @@ public void afterPropertiesSet() throws JMException, IOException {
170170
try {
171171
if (this.threaded) {
172172
// Start the connector server asynchronously (in a separate thread).
173+
final JMXConnectorServer serverToStart = this.connectorServer;
173174
Thread connectorThread = new Thread() {
174175
@Override
175176
public void run() {
176177
try {
177-
connectorServer.start();
178+
serverToStart.start();
178179
}
179180
catch (IOException ex) {
180181
throw new JmxException("Could not start JMX connector server after delay", ex);

spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,25 +111,25 @@ public static ParsedSql parseSqlStatement(final String sql) {
111111
char c = statement[i];
112112
if (c == ':' || c == '&') {
113113
int j = i + 1;
114-
if (j < statement.length && statement[j] == ':' && c == ':') {
114+
if (c == ':' && j < statement.length && statement[j] == ':') {
115115
// Postgres-style "::" casting operator should be skipped
116116
i = i + 2;
117117
continue;
118118
}
119119
String parameter = null;
120-
if (j < statement.length && c == ':' && statement[j] == '{') {
120+
if (c == ':' && j < statement.length && statement[j] == '{') {
121121
// :{x} style parameter
122-
while (j < statement.length && statement[j] != '}') {
122+
while (statement[j] != '}') {
123123
j++;
124+
if (j >= statement.length) {
125+
throw new InvalidDataAccessApiUsageException("Non-terminated named parameter declaration " +
126+
"at position " + i + " in statement: " + sql);
127+
}
124128
if (statement[j] == ':' || statement[j] == '{') {
125129
throw new InvalidDataAccessApiUsageException("Parameter name contains invalid character '" +
126130
statement[j] + "' at position " + i + " in statement: " + sql);
127131
}
128132
}
129-
if (j >= statement.length) {
130-
throw new InvalidDataAccessApiUsageException(
131-
"Non-terminated named parameter declaration at position " + i + " in statement: " + sql);
132-
}
133133
if (j - i > 2) {
134134
parameter = sql.substring(i + 2, j);
135135
namedParameterCount = addNewNamedParameter(namedParameters, namedParameterCount, parameter);

spring-jdbc/src/main/java/org/springframework/jdbc/object/RdbmsOperation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public boolean isReturnGeneratedKeys() {
211211
* Set the column names of the auto-generated keys.
212212
* @see java.sql.Connection#prepareStatement(String, String[])
213213
*/
214-
public void setGeneratedKeysColumnNames(String... names) {
214+
public void setGeneratedKeysColumnNames(@Nullable String... names) {
215215
if (isCompiled()) {
216216
throw new InvalidDataAccessApiUsageException(
217217
"The column names for the generated keys must be set before the operation is compiled");
@@ -230,7 +230,7 @@ public String[] getGeneratedKeysColumnNames() {
230230
/**
231231
* Set the SQL executed by this operation.
232232
*/
233-
public void setSql(String sql) {
233+
public void setSql(@Nullable String sql) {
234234
this.sql = sql;
235235
}
236236

spring-jdbc/src/main/java/org/springframework/jdbc/object/SqlCall.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,33 @@
4040
*/
4141
public abstract class SqlCall extends RdbmsOperation {
4242

43-
/**
44-
* Object enabling us to create CallableStatementCreators
45-
* efficiently, based on this class's declared parameters.
46-
*/
47-
@Nullable
48-
private CallableStatementCreatorFactory callableStatementFactory;
49-
5043
/**
5144
* Flag used to indicate that this call is for a function and to
5245
* use the {? = call get_invoice_count(?)} syntax.
5346
*/
5447
private boolean function = false;
5548

5649
/**
57-
* Flag used to indicate that the sql for this call should be used exactly as it is
58-
* defined. No need to add the escape syntax and parameter place holders.
50+
* Flag used to indicate that the sql for this call should be used exactly as
51+
* it is defined. No need to add the escape syntax and parameter place holders.
5952
*/
6053
private boolean sqlReadyForUse = false;
6154

6255
/**
6356
* Call string as defined in java.sql.CallableStatement.
64-
* String of form {call add_invoice(?, ?, ?)}
65-
* or {? = call get_invoice_count(?)} if isFunction is set to true
66-
* Updated after each parameter is added.
57+
* String of form {call add_invoice(?, ?, ?)} or {? = call get_invoice_count(?)}
58+
* if isFunction is set to true. Updated after each parameter is added.
6759
*/
6860
@Nullable
6961
private String callString;
7062

63+
/**
64+
* Object enabling us to create CallableStatementCreators
65+
* efficiently, based on this class's declared parameters.
66+
*/
67+
@Nullable
68+
private CallableStatementCreatorFactory callableStatementFactory;
69+
7170

7271
/**
7372
* Constructor to allow use as a JavaBean.
@@ -129,30 +128,32 @@ public boolean isSqlReadyForUse() {
129128
@Override
130129
protected final void compileInternal() {
131130
if (isSqlReadyForUse()) {
132-
this.callString = getSql();
131+
this.callString = resolveSql();
133132
}
134133
else {
134+
StringBuilder callString = new StringBuilder(32);
135135
List<SqlParameter> parameters = getDeclaredParameters();
136136
int parameterCount = 0;
137137
if (isFunction()) {
138-
this.callString = "{? = call " + getSql() + "(";
138+
callString.append("{? = call ").append(resolveSql()).append('(');
139139
parameterCount = -1;
140140
}
141141
else {
142-
this.callString = "{call " + getSql() + "(";
142+
callString.append("{call ").append(resolveSql()).append('(');
143143
}
144144
for (SqlParameter parameter : parameters) {
145-
if (!(parameter.isResultsParameter())) {
145+
if (!parameter.isResultsParameter()) {
146146
if (parameterCount > 0) {
147-
this.callString += ", ";
147+
callString.append(", ");
148148
}
149149
if (parameterCount >= 0) {
150-
this.callString += "?";
150+
callString.append('?');
151151
}
152152
parameterCount++;
153153
}
154154
}
155-
this.callString += ")}";
155+
callString.append(")}");
156+
this.callString = callString.toString();
156157
}
157158
if (logger.isDebugEnabled()) {
158159
logger.debug("Compiled stored procedure. Call string is [" + this.callString + "]");

spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterUtilsTests.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,21 @@ public void convertTypeMapToSqlParameterList() {
117117
}
118118

119119
@Test(expected = InvalidDataAccessApiUsageException.class)
120-
public void buildValueArrayWithMissingParameterValue() throws Exception {
120+
public void buildValueArrayWithMissingParameterValue() {
121121
String sql = "select count(0) from foo where id = :id";
122122
NamedParameterUtils.buildValueArray(sql, Collections.<String, Object>emptyMap());
123123
}
124124

125125
@Test
126-
public void substituteNamedParametersWithStringContainingQuotes() throws Exception {
126+
public void substituteNamedParametersWithStringContainingQuotes() {
127127
String expectedSql = "select 'first name' from artists where id = ? and quote = 'exsqueeze me?'";
128128
String sql = "select 'first name' from artists where id = :id and quote = 'exsqueeze me?'";
129129
String newSql = NamedParameterUtils.substituteNamedParameters(sql, new MapSqlParameterSource());
130130
assertEquals(expectedSql, newSql);
131131
}
132132

133133
@Test
134-
public void testParseSqlStatementWithStringContainingQuotes() throws Exception {
134+
public void testParseSqlStatementWithStringContainingQuotes() {
135135
String expectedSql = "select 'first name' from artists where id = ? and quote = 'exsqueeze me?'";
136136
String sql = "select 'first name' from artists where id = :id and quote = 'exsqueeze me?'";
137137
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
@@ -173,15 +173,15 @@ public void parseSqlContainingComments() {
173173
}
174174

175175
@Test // SPR-4612
176-
public void parseSqlStatementWithPostgresCasting() throws Exception {
176+
public void parseSqlStatementWithPostgresCasting() {
177177
String expectedSql = "select 'first name' from artists where id = ? and birth_date=?::timestamp";
178178
String sql = "select 'first name' from artists where id = :id and birth_date=:birthDate::timestamp";
179179
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
180180
assertEquals(expectedSql, NamedParameterUtils.substituteNamedParameters(parsedSql, null));
181181
}
182182

183183
@Test // SPR-13582
184-
public void parseSqlStatementWithPostgresContainedOperator() throws Exception {
184+
public void parseSqlStatementWithPostgresContainedOperator() {
185185
String expectedSql = "select 'first name' from artists where info->'stat'->'albums' = ?? ? and '[\"1\",\"2\",\"3\"]'::jsonb ?? '4'";
186186
String sql = "select 'first name' from artists where info->'stat'->'albums' = ?? :album and '[\"1\",\"2\",\"3\"]'::jsonb ?? '4'";
187187
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
@@ -190,7 +190,7 @@ public void parseSqlStatementWithPostgresContainedOperator() throws Exception {
190190
}
191191

192192
@Test // SPR-15382
193-
public void parseSqlStatementWithPostgresAnyArrayStringsExistsOperator() throws Exception {
193+
public void parseSqlStatementWithPostgresAnyArrayStringsExistsOperator() {
194194
String expectedSql = "select '[\"3\", \"11\"]'::jsonb ?| '{1,3,11,12,17}'::text[]";
195195
String sql = "select '[\"3\", \"11\"]'::jsonb ?| '{1,3,11,12,17}'::text[]";
196196

@@ -200,7 +200,7 @@ public void parseSqlStatementWithPostgresAnyArrayStringsExistsOperator() throws
200200
}
201201

202202
@Test // SPR-15382
203-
public void parseSqlStatementWithPostgresAllArrayStringsExistsOperator() throws Exception {
203+
public void parseSqlStatementWithPostgresAllArrayStringsExistsOperator() {
204204
String expectedSql = "select '[\"3\", \"11\"]'::jsonb ?& '{1,3,11,12,17}'::text[] AND ? = 'Back in Black'";
205205
String sql = "select '[\"3\", \"11\"]'::jsonb ?& '{1,3,11,12,17}'::text[] AND :album = 'Back in Black'";
206206

@@ -210,7 +210,7 @@ public void parseSqlStatementWithPostgresAllArrayStringsExistsOperator() throws
210210
}
211211

212212
@Test // SPR-7476
213-
public void parseSqlStatementWithEscapedColon() throws Exception {
213+
public void parseSqlStatementWithEscapedColon() {
214214
String expectedSql = "select '0\\:0' as a, foo from bar where baz < DATE(? 23:59:59) and baz = ?";
215215
String sql = "select '0\\:0' as a, foo from bar where baz < DATE(:p1 23\\:59\\:59) and baz = :p2";
216216

@@ -223,7 +223,7 @@ public void parseSqlStatementWithEscapedColon() throws Exception {
223223
}
224224

225225
@Test // SPR-7476
226-
public void parseSqlStatementWithBracketDelimitedParameterNames() throws Exception {
226+
public void parseSqlStatementWithBracketDelimitedParameterNames() {
227227
String expectedSql = "select foo from bar where baz = b??z";
228228
String sql = "select foo from bar where baz = b:{p1}:{p2}z";
229229

@@ -236,7 +236,7 @@ public void parseSqlStatementWithBracketDelimitedParameterNames() throws Excepti
236236
}
237237

238238
@Test // SPR-7476
239-
public void parseSqlStatementWithEmptyBracketsOrBracketsInQuotes() throws Exception {
239+
public void parseSqlStatementWithEmptyBracketsOrBracketsInQuotes() {
240240
String expectedSql = "select foo from bar where baz = b:{}z";
241241
String sql = "select foo from bar where baz = b:{}z";
242242
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
@@ -273,14 +273,14 @@ public void parseSqlStatementWithLogicalAnd() {
273273
}
274274

275275
@Test // SPR-2544
276-
public void substituteNamedParametersWithLogicalAnd() throws Exception {
276+
public void substituteNamedParametersWithLogicalAnd() {
277277
String expectedSql = "xxx & yyyy";
278278
String newSql = NamedParameterUtils.substituteNamedParameters(expectedSql, new MapSqlParameterSource());
279279
assertEquals(expectedSql, newSql);
280280
}
281281

282282
@Test // SPR-3173
283-
public void variableAssignmentOperator() throws Exception {
283+
public void variableAssignmentOperator() {
284284
String expectedSql = "x := 1";
285285
String newSql = NamedParameterUtils.substituteNamedParameters(expectedSql, new MapSqlParameterSource());
286286
assertEquals(expectedSql, newSql);

spring-orm/src/main/java/org/springframework/orm/hibernate5/SessionHolder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class SessionHolder extends EntityManagerHolder {
5050

5151

5252
public SessionHolder(Session session) {
53+
// Check below is always true against Hibernate >= 5.2 but not against 5.0/5.1 at runtime
5354
super(EntityManager.class.isInstance(session) ? session : null);
5455
this.session = session;
5556
}

spring-tx/src/main/java/org/springframework/transaction/jta/JtaTransactionManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ protected UserTransaction findUserTransaction() {
709709
* @see #FALLBACK_TRANSACTION_MANAGER_NAMES
710710
*/
711711
@Nullable
712-
protected TransactionManager findTransactionManager(UserTransaction ut) {
712+
protected TransactionManager findTransactionManager(@Nullable UserTransaction ut) {
713713
if (ut instanceof TransactionManager) {
714714
if (logger.isDebugEnabled()) {
715715
logger.debug("JTA UserTransaction object [" + ut + "] implements TransactionManager");

0 commit comments

Comments
 (0)