DATAREST-957 - Polish most critical Sonar warnings.

Fixed broken equals(…) in ProjectionDefinition. Switched to iterating over Map's entry set instead of the keys. Made UriAwareHttpServletRequest static.
This commit is contained in:
Oliver Gierke
2016-12-08 10:12:01 +01:00
parent 29345d3e49
commit a254e1adae
6 changed files with 30 additions and 53 deletions

View File

@@ -15,6 +15,11 @@
*/
package org.springframework.data.rest.core.config;
import lombok.AccessLevel;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@@ -118,7 +123,7 @@ public class ProjectionDefinitionConfiguration implements ProjectionDefinitions
Assert.notEmpty(sourceTypes, "Source types must not be null!");
for (Class<?> sourceType : sourceTypes) {
this.projectionDefinitions.add(new ProjectionDefinition(sourceType, projectionType, name));
this.projectionDefinitions.add(ProjectionDefinition.of(sourceType, projectionType, name));
}
return this;
@@ -189,10 +194,12 @@ public class ProjectionDefinitionConfiguration implements ProjectionDefinitions
*
* @author Oliver Gierke
*/
@Value
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
static final class ProjectionDefinition {
private final Class<?> sourceType, targetType;
private final String name;
private final @NonNull Class<?> sourceType, targetType;
private final @NonNull String name;
/**
* Creates a new {@link ProjectionDefinitionKey} for the given source type and name;
@@ -201,47 +208,11 @@ public class ProjectionDefinitionConfiguration implements ProjectionDefinitions
* @param targetType must not be {@literal null}.
* @param name must not be {@literal null} or empty.
*/
public ProjectionDefinition(Class<?> sourceType, Class<?> targetType, String name) {
static ProjectionDefinition of(Class<?> sourceType, Class<?> targetType, String name) {
Assert.notNull(sourceType, "Source type must not be null!");
Assert.notNull(targetType, "Target type must not be null!");
Assert.hasText(name, "Name must not be null or empty!");
this.sourceType = sourceType;
this.targetType = targetType;
this.name = name;
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof ProjectionDefinition)) {
return false;
}
ProjectionDefinition that = (ProjectionDefinition) obj;
return this.name.equals(that.name) && this.sourceType.equals(that.sourceType)
&& this.sourceType.equals(that.sourceType);
}
/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
int result = 31;
result += name.hashCode();
result += sourceType.hashCode();
result += targetType.hashCode();
return result;
return new ProjectionDefinition(sourceType, targetType, name);
}
}
}

View File

@@ -121,10 +121,10 @@ public class ProjectionDefinitionConfigurationUnitTests {
@Test
public void definitionEquals() {
ProjectionDefinition objectName = new ProjectionDefinition(Object.class, Object.class, "name");
ProjectionDefinition sameObjectName = new ProjectionDefinition(Object.class, Object.class, "name");
ProjectionDefinition stringName = new ProjectionDefinition(String.class, Object.class, "name");
ProjectionDefinition objectOtherNameKey = new ProjectionDefinition(Object.class, Object.class, "otherName");
ProjectionDefinition objectName = ProjectionDefinition.of(Object.class, Object.class, "name");
ProjectionDefinition sameObjectName = ProjectionDefinition.of(Object.class, Object.class, "name");
ProjectionDefinition stringName = ProjectionDefinition.of(String.class, Object.class, "name");
ProjectionDefinition objectOtherNameKey = ProjectionDefinition.of(Object.class, Object.class, "otherName");
assertThat(objectName, is(objectName));
assertThat(objectName, is(sameObjectName));

View File

@@ -190,7 +190,7 @@ public class BasePathAwareHandlerMapping extends RequestMappingHandlerMapping {
super.afterPropertiesSet();
}
private class UriAwareHttpServletRequest implements HttpServletRequest {
private static class UriAwareHttpServletRequest implements HttpServletRequest {
private final ServletContext context;
private final String path;

View File

@@ -382,10 +382,13 @@ class RepositoryPropertyReferenceController extends AbstractRepositoryRestContro
}
}
} else if (prop.property.isMap()) {
Map<Object, Object> m = (Map<Object, Object>) prop.propertyValue;
Iterator<Object> itr = m.keySet().iterator();
Iterator<Entry<Object, Object>> itr = m.entrySet().iterator();
while (itr.hasNext()) {
Object key = itr.next();
Object key = itr.next().getKey();
IdentifierAccessor accessor = prop.entity.getIdentifierAccessor(m.get(key));
String s = accessor.getIdentifier().toString();
@@ -394,6 +397,7 @@ class RepositoryPropertyReferenceController extends AbstractRepositoryRestContro
itr.remove();
}
}
} else {
prop.accessor.setProperty(prop.property, null);
}

View File

@@ -17,6 +17,7 @@ package org.springframework.data.rest.webmvc.config;
import java.util.Arrays;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.core.MethodParameter;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
@@ -103,8 +104,8 @@ class QuerydslAwareRootResourceInformationHandlerMethodArgumentResolver
MultiValueMap<String, String> result = new LinkedMultiValueMap<String, String>();
for (String key : source.keySet()) {
result.put(key, Arrays.asList(source.get(key)));
for (Entry<String, String[]> entry : source.entrySet()) {
result.put(entry.getKey(), Arrays.asList(entry.getValue()));
}
return result;

View File

@@ -23,6 +23,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
@@ -166,14 +167,14 @@ class WrappedProperties {
Map<String, List<PersistentProperty<?>>> nestedProperties = findUnwrappedPropertyPaths(
annotatedMember.getRawType(), propertyNameTransformer, true);
for (String key : nestedProperties.keySet()) {
for (Entry<String, List<PersistentProperty<?>>> entry : nestedProperties.entrySet()) {
List<PersistentProperty<?>> persistentProperties = new ArrayList<PersistentProperty<?>>();
persistentProperties.add(persistentProperty);
persistentProperties.addAll(nestedProperties.get(key));
persistentProperties.addAll(entry.getValue());
mapping.put(key, persistentProperties);
mapping.put(entry.getKey(), persistentProperties);
}
return mapping;