INT-3632: Sonar Improvements

JIRA: https://jira.spring.io/browse/INT-3632

- Use Map.entrySet()
- Double check locking only works when the field(s) are volatile
- Remove unnecessary instanceof tests
This commit is contained in:
Gary Russell
2015-02-10 15:42:31 -05:00
parent 49b4957270
commit 195ee70568
34 changed files with 168 additions and 162 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
@@ -42,6 +43,7 @@ import org.springframework.web.multipart.MultipartFile;
* <code>multipart/form-data</code> content in an HTTP request.
*
* @author Mark Fisher
* @author Gary Russell
* @since 2.0
*/
public class MultipartAwareFormHttpMessageConverter implements HttpMessageConverter<MultiValueMap<String, ?>> {
@@ -112,11 +114,10 @@ public class MultipartAwareFormHttpMessageConverter implements HttpMessageConver
private MultiValueMap<String, ?> readMultipart(MultipartHttpInputMessage multipartRequest) throws IOException {
MultiValueMap<String, Object> resultMap = new LinkedMultiValueMap<String, Object>();
Map<?, ?> parameterMap = multipartRequest.getParameterMap();
for (Object key : parameterMap.keySet()) {
resultMap.add((String) key, parameterMap.get(key));
for (Entry<?, ?> entry : parameterMap.entrySet()) {
resultMap.add((String) entry.getKey(), entry.getValue());
}
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
for (Map.Entry<String, MultipartFile> entry : multipartRequest.getFileMap().entrySet()) {
MultipartFile multipartFile = entry.getValue();
if (multipartFile.isEmpty()) {
continue;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 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,7 +61,7 @@ public class SerializingHttpMessageConverter extends AbstractHttpMessageConverte
@SuppressWarnings("rawtypes")
public Serializable readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException {
try {
return (Serializable) new ObjectInputStream(inputMessage.getBody()).readObject();
return (Serializable) new ObjectInputStream(inputMessage.getBody()).readObject();//NOSONAR
}
catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -24,6 +24,7 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.xml.transform.Source;
@@ -556,11 +557,11 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
private MultiValueMap<Object, Object> convertToMultiValueMap(Map<?, ?> simpleMap) {
LinkedMultiValueMap<Object, Object> multipartValueMap = new LinkedMultiValueMap<Object, Object>();
for (Object key : simpleMap.keySet()) {
Object value = simpleMap.get(key);
for (Entry<?, ?> entry : simpleMap.entrySet()) {
Object key = entry.getKey();
Object value = entry.getValue();
if (value instanceof Object[]) {
Object[] valueArray = (Object[]) value;
value = Arrays.asList(valueArray);
value = Arrays.asList((Object[]) value);
}
if (value instanceof Collection) {
multipartValueMap.put(key, new ArrayList<Object>((Collection<?>) value));
@@ -577,8 +578,7 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
* the Map to be multipart/form-data
*/
private boolean isMultipart(Map<String, ?> map) {
for (String key : map.keySet()) {
Object value = map.get(key);
for (Object value : map.values()) {
if (value != null) {
if (value.getClass().isArray()) {
value = CollectionUtils.arrayToList(value);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -33,6 +33,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TimeZone;
@@ -308,7 +309,7 @@ public class DefaultHttpHeaderMapper implements HeaderMapper<HttpHeaders>, BeanF
public void setOutboundHeaderNames(String[] outboundHeaderNames) {
this.outboundHeaderNames = (outboundHeaderNames != null) ? outboundHeaderNames : new String[0];
this.outboundHeaderNamesLower = new String[this.outboundHeaderNames.length];
for (int i = 0; i < outboundHeaderNames.length; i++) {
for (int i = 0; i < this.outboundHeaderNames.length; i++) {
if (HTTP_REQUEST_HEADER_NAME_PATTERN.equals(this.outboundHeaderNames[i])
|| HTTP_RESPONSE_HEADER_NAME_PATTERN.equals(this.outboundHeaderNames[i])) {
this.outboundHeaderNamesLower[i] = this.outboundHeaderNames[i];
@@ -335,7 +336,7 @@ public class DefaultHttpHeaderMapper implements HeaderMapper<HttpHeaders>, BeanF
public void setInboundHeaderNames(String[] inboundHeaderNames) {
this.inboundHeaderNames = (inboundHeaderNames != null) ? inboundHeaderNames : new String[0];
this.inboundHeaderNamesLower = new String[this.inboundHeaderNames.length];
for (int i = 0; i < inboundHeaderNames.length; i++) {
for (int i = 0; i < this.inboundHeaderNames.length; i++) {
if (HTTP_REQUEST_HEADER_NAME_PATTERN.equals(this.inboundHeaderNames[i])
|| HTTP_RESPONSE_HEADER_NAME_PATTERN.equals(this.inboundHeaderNames[i])) {
this.inboundHeaderNamesLower[i] = this.inboundHeaderNames[i];
@@ -387,11 +388,11 @@ public class DefaultHttpHeaderMapper implements HeaderMapper<HttpHeaders>, BeanF
logger.debug(MessageFormat.format("outboundHeaderNames={0}",
CollectionUtils.arrayToList(outboundHeaderNames)));
}
Set<String> headerNames = headers.keySet();
for (String name : headerNames) {
for (Entry<String, Object> entry : headers.entrySet()) {
String name = entry.getKey();
String lowerName = name.toLowerCase();
if (this.shouldMapOutboundHeader(lowerName)) {
Object value = headers.get(name);
Object value = entry.getValue();
if (value != null) {
if (!HTTP_REQUEST_HEADER_NAMES_LOWER.contains(lowerName) &&
!HTTP_RESPONSE_HEADER_NAMES_LOWER.contains(lowerName) &&