DATACMNS-801 - Make sure PropertyReferenceException does not expose duplicate potential matches.

We now use a Set to collect potential matches from fields and accessors.

Polished assertions and JavaDoc in PropertyPath along the way.
This commit is contained in:
Oliver Gierke
2016-01-12 08:39:44 +01:00
parent 80e44a3fc0
commit c916fcb1c1
3 changed files with 125 additions and 15 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.data.mapping;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
@@ -53,7 +54,7 @@ public class PropertyPath implements Iterable<PropertyPath> {
* @param owningType must not be {@literal null}.
*/
PropertyPath(String name, Class<?> owningType) {
this(name, ClassTypeInformation.from(owningType), null);
this(name, ClassTypeInformation.from(owningType), Collections.<PropertyPath> emptyList());
}
/**
@@ -65,8 +66,9 @@ public class PropertyPath implements Iterable<PropertyPath> {
*/
PropertyPath(String name, TypeInformation<?> owningType, List<PropertyPath> base) {
Assert.hasText(name);
Assert.notNull(owningType);
Assert.hasText(name, "Name must not be null or empty!");
Assert.notNull(owningType, "Owning type must not be null!");
Assert.notNull(base, "Perviously found properties must not be null!");
String propertyName = name.matches(ALL_UPPERCASE) ? name : StringUtils.uncapitalize(name);
TypeInformation<?> propertyType = owningType.getProperty(propertyName);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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.
@@ -15,9 +15,11 @@
*/
package org.springframework.data.mapping;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.springframework.beans.PropertyMatches;
import org.springframework.data.util.TypeInformation;
@@ -38,20 +40,21 @@ public class PropertyReferenceException extends RuntimeException {
private final String propertyName;
private final TypeInformation<?> type;
private final List<PropertyPath> alreadyResolvedPath;
private final List<String> propertyMatches;
private final Set<String> propertyMatches;
/**
* Creates a new {@link PropertyReferenceException}.
*
* @param propertyName the name of the property not found on the given type.
* @param type the type the property could not be found on.
* @param alreadyResolvedPah the previously calculated {@link PropertyPath}s.
* @param propertyName the name of the property not found on the given type, must not be {@literal null} or empty.
* @param type the type the property could not be found on, must not be {@literal null}.
* @param alreadyResolvedPah the previously calculated {@link PropertyPath}s, must not be {@literal null}.
*/
public PropertyReferenceException(String propertyName, TypeInformation<?> type,
List<PropertyPath> alreadyResolvedPah) {
Assert.hasText(propertyName);
Assert.notNull(type);
Assert.hasText(propertyName, "Property name must not be null!");
Assert.notNull(type, "Type must not be null!");
Assert.notNull(alreadyResolvedPah, "Already resolved paths must not be null!");
this.propertyName = propertyName;
this.type = type;
@@ -71,12 +74,21 @@ public class PropertyReferenceException extends RuntimeException {
/**
* Returns the type the property could not be found on.
*
* @return the type
* @return will never be {@literal null}.
*/
public TypeInformation<?> getType() {
return type;
}
/**
* Returns the properties that the invalid property might have been meant to be referred to.
*
* @return will never be {@literal null}.
*/
Collection<String> getPropertyMatches() {
return propertyMatches;
}
/*
* (non-Javadoc)
* @see java.lang.Throwable#getMessage()
@@ -114,7 +126,7 @@ public class PropertyReferenceException extends RuntimeException {
* Returns whether the given {@link PropertyReferenceException} has a deeper resolution depth (i.e. a longer path of
* already resolved properties) than the current exception.
*
* @param exception
* @param exception must not be {@literal null}.
* @return
*/
public boolean hasDeeperResolutionDepthThan(PropertyReferenceException exception) {
@@ -128,9 +140,9 @@ public class PropertyReferenceException extends RuntimeException {
* @param type must not be {@literal null}.
* @return
*/
private static List<String> detectPotentialMatches(String propertyName, Class<?> type) {
private static Set<String> detectPotentialMatches(String propertyName, Class<?> type) {
List<String> result = new ArrayList<String>();
Set<String> result = new HashSet<String>();
result.addAll(Arrays.asList(PropertyMatches.forField(propertyName, type).getPossibleMatches()));
result.addAll(Arrays.asList(PropertyMatches.forProperty(propertyName, type).getPossibleMatches()));