From 314aad7caa5adcff5708d5c8f62359df94431827 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 28 May 2014 15:06:06 +0200 Subject: [PATCH] =?UTF-8?q?DATACMNS-511=20-=20Improve=20equals(=E2=80=A6)?= =?UTF-8?q?=20and=20hashCode()=20in=20TypeVariableTypeInformation.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The equals(…) and hashCode() methods of TypeVariableTypeInformation previously tried to evaluate the unresolved context of the type variable. This can cause issues in recursive type definitions as set up in the according test case. We now implement the methods based on the resolved type to makes sure we break the recursive lookup of PersistentEntity instances in AbstractMappingContext. The erroneous lookup was actually caused by unresolved base types being added to the mapping context within the JPA project as the JPA meta-model also returns those types as managed types. Related pull request: #84. --- .../util/TypeVariableTypeInformation.java | 35 +++++----- .../data/util/DataCmns511Tests.java | 67 +++++++++++++++++++ 2 files changed, 85 insertions(+), 17 deletions(-) create mode 100644 src/test/java/org/springframework/data/util/DataCmns511Tests.java diff --git a/src/main/java/org/springframework/data/util/TypeVariableTypeInformation.java b/src/main/java/org/springframework/data/util/TypeVariableTypeInformation.java index d210fd425..794c7ae31 100644 --- a/src/main/java/org/springframework/data/util/TypeVariableTypeInformation.java +++ b/src/main/java/org/springframework/data/util/TypeVariableTypeInformation.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 the original author or authors. + * Copyright 2011-2014 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. @@ -55,8 +55,7 @@ class TypeVariableTypeInformation extends ParentTypeAwareTypeInformation { /* * (non-Javadoc) - * - * @see org.springframework.data.document.mongodb.TypeDiscovererTest.TypeDiscoverer#getType() + * @see org.springframework.data.util.TypeDiscoverer#getType() */ @Override public Class getType() { @@ -92,38 +91,40 @@ class TypeVariableTypeInformation extends ParentTypeAwareTypeInformation { } /* - * (non-Javadoc) - * - * @see org.springframework.data.util.TypeDiscoverer#equals(java.lang.Object) - */ + * (non-Javadoc) + * @see org.springframework.data.util.ParentTypeAwareTypeInformation#equals(java.lang.Object) + */ @Override public boolean equals(Object obj) { - if (!super.equals(obj)) { + if (obj == this) { + return true; + } + + if (!(obj instanceof TypeVariableTypeInformation)) { return false; } TypeVariableTypeInformation that = (TypeVariableTypeInformation) obj; - return nullSafeEquals(this.owningType, that.owningType) && nullSafeEquals(this.variable, that.variable); + + return getType().equals(that.getType()); } /* - * (non-Javadoc) - * - * @see org.springframework.data.util.TypeDiscoverer#hashCode() - */ + * (non-Javadoc) + * @see org.springframework.data.util.ParentTypeAwareTypeInformation#hashCode() + */ @Override public int hashCode() { - int result = super.hashCode(); + int result = 17; - result += 31 * nullSafeHashCode(this.owningType); - result += 31 * nullSafeHashCode(this.variable); + result += 31 * nullSafeHashCode(getType()); return result; } - /* + /* * (non-Javadoc) * @see java.lang.Object#toString() */ diff --git a/src/test/java/org/springframework/data/util/DataCmns511Tests.java b/src/test/java/org/springframework/data/util/DataCmns511Tests.java new file mode 100644 index 000000000..006dcd9aa --- /dev/null +++ b/src/test/java/org/springframework/data/util/DataCmns511Tests.java @@ -0,0 +1,67 @@ +/* + * Copyright 2014 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.util; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.util.HashSet; +import java.util.Set; + +import org.junit.Test; + +/** + * Unit tests to reproduce issues reported in DATACMNS-511. + * + * @author Oliver Gierke + */ +public class DataCmns511Tests { + + /** + * @see DATACMNS-511 + */ + @Test + @SuppressWarnings("rawtypes") + public void detectsEqualTypeVariableTypeInformationInstances() { + + TypeInformation firstRoleType = ClassTypeInformation.from(AbstractRole.class); + TypeInformation firstCreatedBy = firstRoleType.getProperty("createdBy"); + TypeInformation secondRoleType = firstCreatedBy.getProperty("roles").getActualType(); + TypeInformation secondCreatedBy = secondRoleType.getProperty("createdBy"); + TypeInformation thirdRoleType = secondCreatedBy.getProperty("roles").getActualType(); + TypeInformation thirdCreatedBy = thirdRoleType.getProperty("createdBy"); + + assertThat(secondCreatedBy, is(thirdCreatedBy)); + assertThat(secondCreatedBy.hashCode(), is(thirdCreatedBy.hashCode())); + } + + static class AbstractRole, ROLE extends AbstractRole> extends + AuditingEntity { + + String name; + } + + static abstract class AbstractUser, ROLE extends AbstractRole> { + + Set roles = new HashSet(); + } + + static abstract class AuditingEntity> { + + USER createdBy; + USER lastModifiedBy; + } +}