Skip to content

Commit 47a7475

Browse files
committed
Resolve remaining nullability warnings
Issue: SPR-15869
1 parent ac5e259 commit 47a7475

File tree

11 files changed

+54
-80
lines changed

11 files changed

+54
-80
lines changed

spring-aop/src/main/java/org/springframework/aop/target/CommonsPool2TargetSource.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
2525

2626
import org.springframework.lang.Nullable;
27+
import org.springframework.util.Assert;
2728

2829
/**
2930
* {@link org.springframework.aop.TargetSource} implementation that holds
@@ -230,6 +231,7 @@ protected ObjectPool createObjectPool() {
230231
*/
231232
@Override
232233
public Object getTarget() throws Exception {
234+
Assert.state(this.pool != null, "No Commons ObjectPool available");
233235
return this.pool.borrowObject();
234236
}
235237

@@ -238,17 +240,19 @@ public Object getTarget() throws Exception {
238240
*/
239241
@Override
240242
public void releaseTarget(Object target) throws Exception {
241-
this.pool.returnObject(target);
243+
if (this.pool != null) {
244+
this.pool.returnObject(target);
245+
}
242246
}
243247

244248
@Override
245249
public int getActiveCount() throws UnsupportedOperationException {
246-
return this.pool.getNumActive();
250+
return (this.pool != null ? this.pool.getNumActive() : 0);
247251
}
248252

249253
@Override
250254
public int getIdleCount() throws UnsupportedOperationException {
251-
return this.pool.getNumIdle();
255+
return (this.pool != null ? this.pool.getNumIdle() : 0);
252256
}
253257

254258

@@ -257,8 +261,10 @@ public int getIdleCount() throws UnsupportedOperationException {
257261
*/
258262
@Override
259263
public void destroy() throws Exception {
260-
logger.debug("Closing Commons ObjectPool");
261-
this.pool.close();
264+
if (this.pool != null) {
265+
logger.debug("Closing Commons ObjectPool");
266+
this.pool.close();
267+
}
262268
}
263269

264270

spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -446,14 +446,12 @@ public void query(String sql, RowCallbackHandler rch) throws DataAccessException
446446

447447
@Override
448448
public <T> List<T> query(String sql, RowMapper<T> rowMapper) throws DataAccessException {
449-
List<T> result = query(sql, new RowMapperResultSetExtractor<>(rowMapper));
450-
Assert.state(result != null, "No result list");
451-
return result;
449+
return nonNull(query(sql, new RowMapperResultSetExtractor<>(rowMapper)));
452450
}
453451

454452
@Override
455453
public Map<String, Object> queryForMap(String sql) throws DataAccessException {
456-
return queryForObject(sql, getColumnMapRowMapper());
454+
return nonNull(queryForObject(sql, getColumnMapRowMapper()));
457455
}
458456

459457
@Override
@@ -481,9 +479,7 @@ public List<Map<String, Object>> queryForList(String sql) throws DataAccessExcep
481479

482480
@Override
483481
public SqlRowSet queryForRowSet(String sql) throws DataAccessException {
484-
SqlRowSet result = query(sql, new SqlRowSetResultSetExtractor());
485-
Assert.state(result != null, "No SqlRowSet");
486-
return result;
482+
return nonNull(query(sql, new SqlRowSetResultSetExtractor()));
487483
}
488484

489485
@Override
@@ -798,12 +794,12 @@ public <T> T queryForObject(String sql, Class<T> requiredType, @Nullable Object.
798794

799795
@Override
800796
public Map<String, Object> queryForMap(String sql, Object[] args, int[] argTypes) throws DataAccessException {
801-
return queryForObject(sql, args, argTypes, getColumnMapRowMapper());
797+
return nonNull(queryForObject(sql, args, argTypes, getColumnMapRowMapper()));
802798
}
803799

804800
@Override
805801
public Map<String, Object> queryForMap(String sql, @Nullable Object... args) throws DataAccessException {
806-
return queryForObject(sql, args, getColumnMapRowMapper());
802+
return nonNull(queryForObject(sql, args, getColumnMapRowMapper()));
807803
}
808804

809805
@Override

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,16 @@ public <T> T queryForObject(String sql, Map<String, ?> paramMap, Class<T> requir
246246

247247
@Override
248248
public Map<String, Object> queryForMap(String sql, SqlParameterSource paramSource) throws DataAccessException {
249-
return queryForObject(sql, paramSource, new ColumnMapRowMapper());
249+
Map<String, Object> result = queryForObject(sql, paramSource, new ColumnMapRowMapper());
250+
Assert.state(result != null, "No result map");
251+
return result;
250252
}
251253

252254
@Override
253255
public Map<String, Object> queryForMap(String sql, Map<String, ?> paramMap) throws DataAccessException {
254-
return queryForObject(sql, paramMap, new ColumnMapRowMapper());
256+
Map<String, Object> result = queryForObject(sql, paramMap, new ColumnMapRowMapper());
257+
Assert.state(result != null, "No result map");
258+
return result;
255259
}
256260

257261
@Override

spring-jms/src/main/java/org/springframework/jms/listener/adapter/MessageListenerAdapter.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,6 @@ public void onMessage(Message message, @Nullable Session session) throws JMSExce
216216
// Regular case: find a handler method reflectively.
217217
Object convertedMessage = extractMessage(message);
218218
String methodName = getListenerMethodName(message, convertedMessage);
219-
if (methodName == null) {
220-
throw new javax.jms.IllegalStateException("No default listener method specified: " +
221-
"Either specify a non-null value for the 'defaultListenerMethod' property or " +
222-
"override the 'getListenerMethodName' method.");
223-
}
224219

225220
// Invoke the handler method with appropriate arguments.
226221
Object[] listenerArguments = buildListenerArguments(convertedMessage);

spring-jms/src/test/java/org/springframework/jms/listener/adapter/MessageListenerAdapterTests.java

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@
1919
import java.io.ByteArrayInputStream;
2020
import java.io.Serializable;
2121
import javax.jms.BytesMessage;
22-
import javax.jms.IllegalStateException;
2322
import javax.jms.InvalidDestinationException;
2423
import javax.jms.JMSException;
2524
import javax.jms.Message;
@@ -377,40 +376,6 @@ protected Object extractMessage(Message message) {
377376
catch (ListenerExecutionFailedException ex) { /* expected */ }
378377
}
379378

380-
@Test
381-
public void testFailsIfNoDefaultListenerMethodNameIsSupplied() throws Exception {
382-
final TextMessage message = mock(TextMessage.class);
383-
given(message.getText()).willReturn(TEXT);
384-
385-
final MessageListenerAdapter adapter = new MessageListenerAdapter() {
386-
@Override
387-
protected void handleListenerException(Throwable ex) {
388-
assertTrue(ex instanceof IllegalStateException);
389-
}
390-
};
391-
adapter.setDefaultListenerMethod(null);
392-
adapter.onMessage(message);
393-
}
394-
395-
@Test
396-
public void testFailsWhenOverriddenGetListenerMethodNameReturnsNull() throws Exception {
397-
final TextMessage message = mock(TextMessage.class);
398-
given(message.getText()).willReturn(TEXT);
399-
400-
final MessageListenerAdapter adapter = new MessageListenerAdapter() {
401-
@Override
402-
protected void handleListenerException(Throwable ex) {
403-
assertTrue(ex instanceof javax.jms.IllegalStateException);
404-
}
405-
@Override
406-
protected String getListenerMethodName(Message originalMessage, Object extractedMessage) {
407-
return null;
408-
}
409-
};
410-
adapter.setDefaultListenerMethod(null);
411-
adapter.onMessage(message);
412-
}
413-
414379
@Test
415380
public void testWithResponsiveMessageDelegateWhenReturnTypeIsNotAJMSMessageAndNoMessageConverterIsSupplied() throws Exception {
416381
final TextMessage sentTextMessage = mock(TextMessage.class);

spring-web/src/main/java/org/springframework/http/MediaType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ public static MediaType parseMediaType(String mediaType) {
525525
* @return the list of media types
526526
* @throws InvalidMediaTypeException if the media type value cannot be parsed
527527
*/
528-
public static List<MediaType> parseMediaTypes(String mediaTypes) {
528+
public static List<MediaType> parseMediaTypes(@Nullable String mediaTypes) {
529529
if (!StringUtils.hasLength(mediaTypes)) {
530530
return Collections.emptyList();
531531
}

spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/AbstractNameValueExpression.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.web.reactive.result.condition;
1818

1919
import org.springframework.lang.Nullable;
20+
import org.springframework.util.ObjectUtils;
2021
import org.springframework.web.server.ServerWebExchange;
2122

2223
/**
@@ -36,20 +37,22 @@ abstract class AbstractNameValueExpression<T> implements NameValueExpression<T>
3637

3738
protected final boolean isNegated;
3839

40+
3941
AbstractNameValueExpression(String expression) {
4042
int separator = expression.indexOf('=');
4143
if (separator == -1) {
4244
this.isNegated = expression.startsWith("!");
43-
this.name = isNegated ? expression.substring(1) : expression;
45+
this.name = (this.isNegated ? expression.substring(1) : expression);
4446
this.value = null;
4547
}
4648
else {
4749
this.isNegated = (separator > 0) && (expression.charAt(separator - 1) == '!');
48-
this.name = isNegated ? expression.substring(0, separator - 1) : expression.substring(0, separator);
50+
this.name = (this.isNegated ? expression.substring(0, separator - 1) : expression.substring(0, separator));
4951
this.value = parseValue(expression.substring(separator + 1));
5052
}
5153
}
5254

55+
5356
@Override
5457
public String getName() {
5558
return this.name;
@@ -66,10 +69,6 @@ public boolean isNegated() {
6669
return this.isNegated;
6770
}
6871

69-
protected abstract boolean isCaseSensitiveName();
70-
71-
protected abstract T parseValue(String valueExpression);
72-
7372
public final boolean match(ServerWebExchange exchange) {
7473
boolean isMatch;
7574
if (this.value != null) {
@@ -81,10 +80,16 @@ public final boolean match(ServerWebExchange exchange) {
8180
return this.isNegated != isMatch;
8281
}
8382

83+
84+
protected abstract boolean isCaseSensitiveName();
85+
86+
protected abstract T parseValue(String valueExpression);
87+
8488
protected abstract boolean matchName(ServerWebExchange exchange);
8589

8690
protected abstract boolean matchValue(ServerWebExchange exchange);
8791

92+
8893
@Override
8994
public boolean equals(Object obj) {
9095
if (this == obj) {
@@ -103,28 +108,28 @@ public boolean equals(Object obj) {
103108

104109
@Override
105110
public int hashCode() {
106-
int result = isCaseSensitiveName() ? name.hashCode() : name.toLowerCase().hashCode();
107-
result = 31 * result + (value != null ? value.hashCode() : 0);
108-
result = 31 * result + (isNegated ? 1 : 0);
111+
int result = (isCaseSensitiveName() ? this.name : this.name.toLowerCase()).hashCode();
112+
result = 31 * result + ObjectUtils.nullSafeHashCode(this.value);
113+
result = 31 * result + (this.isNegated ? 1 : 0);
109114
return result;
110115
}
111116

112117
@Override
113118
public String toString() {
114119
StringBuilder builder = new StringBuilder();
115-
if (value != null) {
116-
builder.append(name);
117-
if (isNegated) {
120+
if (this.value != null) {
121+
builder.append(this.name);
122+
if (this.isNegated) {
118123
builder.append('!');
119124
}
120125
builder.append('=');
121-
builder.append(value);
126+
builder.append(this.value);
122127
}
123128
else {
124-
if (isNegated) {
129+
if (this.isNegated) {
125130
builder.append('!');
126131
}
127-
builder.append(name);
132+
builder.append(this.name);
128133
}
129134
return builder.toString();
130135
}

spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/HeadersRequestCondition.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -159,12 +159,12 @@ protected String parseValue(String valueExpression) {
159159

160160
@Override
161161
protected boolean matchName(ServerWebExchange exchange) {
162-
return exchange.getRequest().getHeaders().get(name) != null;
162+
return (exchange.getRequest().getHeaders().get(this.name) != null);
163163
}
164164

165165
@Override
166166
protected boolean matchValue(ServerWebExchange exchange) {
167-
return value.equals(exchange.getRequest().getHeaders().getFirst(name));
167+
return (this.value != null && this.value.equals(exchange.getRequest().getHeaders().getFirst(this.name)));
168168
}
169169
}
170170

spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/ParamsRequestCondition.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -146,7 +146,8 @@ protected boolean matchName(ServerWebExchange exchange) {
146146

147147
@Override
148148
protected boolean matchValue(ServerWebExchange exchange) {
149-
return this.value.equals(exchange.getRequest().getQueryParams().getFirst(this.name));
149+
return (this.value != null &&
150+
this.value.equals(exchange.getRequest().getQueryParams().getFirst(this.name)));
150151
}
151152
}
152153

spring-webflux/src/main/java/org/springframework/web/reactive/result/view/DefaultRendering.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class DefaultRendering implements Rendering {
5454

5555

5656
@Override
57+
@Nullable
5758
public Object view() {
5859
return this.view;
5960
}

spring-webflux/src/main/java/org/springframework/web/reactive/result/view/Rendering.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public interface Rendering {
4646
/**
4747
* Return the selected {@link String} view name or {@link View} object.
4848
*/
49+
@Nullable
4950
Object view();
5051

5152
/**

0 commit comments

Comments
 (0)