DATACMNS-485 - Fixed bug in ParameterizedTypeInformation.hashCode().
Previously, ParameterizedTypeInformation.hashCode() had been inconsistent to equals in case of a fully resolved parameterized type. This is now fixed by not including the parent reference in case a parameterized type is resolved completely. Also, TypeDiscoverer.getActualType() does not return the component type for non-collection/non-maps anymore to make sure we don't accidentally unwrap parameterized types.
This commit is contained in:
@@ -181,7 +181,7 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return super.hashCode() + (isResolvedCompletely() ? this.type.hashCode() : 0);
|
||||
return isResolvedCompletely() ? this.type.hashCode() : super.hashCode();
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -284,13 +284,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
|
||||
return getComponentType();
|
||||
}
|
||||
|
||||
List<TypeInformation<?>> arguments = getTypeArguments();
|
||||
|
||||
if (arguments.isEmpty()) {
|
||||
return this;
|
||||
}
|
||||
|
||||
return arguments.get(arguments.size() - 1);
|
||||
return this;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -95,6 +95,31 @@ public class ParameterizedTypeUnitTests {
|
||||
is("org.springframework.data.util.ParameterizedTypeUnitTests$Localized<java.lang.String>"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-485
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void hashCodeShouldBeConsistentWithEqualsForResolvedTypes() {
|
||||
|
||||
TypeInformation first = from(First.class).getProperty("property");
|
||||
TypeInformation second = from(Second.class).getProperty("property");
|
||||
|
||||
assertThat(first, is(second));
|
||||
assertThat(first.hashCode(), is(second.hashCode()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-485
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void getActualTypeShouldNotUnwrapParameterizedTypes() {
|
||||
|
||||
TypeInformation type = from(First.class).getProperty("property");
|
||||
assertThat(type.getActualType(), is(type));
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
class Localized<S> extends HashMap<Locale, S> {
|
||||
S value;
|
||||
@@ -109,4 +134,16 @@ public class ParameterizedTypeUnitTests {
|
||||
Localized<String> param;
|
||||
Localized2<String> param2;
|
||||
}
|
||||
|
||||
class Parameterized<T> {
|
||||
T property;
|
||||
}
|
||||
|
||||
class First {
|
||||
Parameterized<String> property;
|
||||
}
|
||||
|
||||
class Second {
|
||||
Parameterized<String> property;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user