Skip to content

Commit 2a2d5aa

Browse files
committed
Class identity comparisons wherever possible
Issue: SPR-12926
1 parent 0b76b13 commit 2a2d5aa

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ private class AnnotationPrinterConverter implements ConditionalGenericConverter
226226

227227
public AnnotationPrinterConverter(Class<? extends Annotation> annotationType,
228228
AnnotationFormatterFactory<?> annotationFormatterFactory, Class<?> fieldType) {
229+
229230
this.annotationType = annotationType;
230231
this.annotationFormatterFactory = annotationFormatterFactory;
231232
this.fieldType = fieldType;
@@ -279,6 +280,7 @@ private class AnnotationParserConverter implements ConditionalGenericConverter {
279280

280281
public AnnotationParserConverter(Class<? extends Annotation> annotationType,
281282
AnnotationFormatterFactory<?> annotationFormatterFactory, Class<?> fieldType) {
283+
282284
this.annotationType = annotationType;
283285
this.annotationFormatterFactory = annotationFormatterFactory;
284286
this.fieldType = fieldType;
@@ -345,16 +347,13 @@ public boolean equals(Object other) {
345347
if (this == other) {
346348
return true;
347349
}
348-
if (!(other instanceof AnnotationConverterKey)) {
349-
return false;
350-
}
351350
AnnotationConverterKey otherKey = (AnnotationConverterKey) other;
352-
return (this.annotation.equals(otherKey.annotation) && this.fieldType.equals(otherKey.fieldType));
351+
return (this.fieldType == otherKey.fieldType && this.annotation.equals(otherKey.annotation));
353352
}
354353

355354
@Override
356355
public int hashCode() {
357-
return (this.annotation.hashCode() + 29 * this.fieldType.hashCode());
356+
return (this.fieldType.hashCode() * 29 + this.annotation.hashCode());
358357
}
359358
}
360359

spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ public class TypeDescriptor implements Serializable {
8080
* @param methodParameter the method parameter
8181
*/
8282
public TypeDescriptor(MethodParameter methodParameter) {
83-
Assert.notNull(methodParameter, "MethodParameter must not be null");
8483
this.resolvableType = ResolvableType.forMethodParameter(methodParameter);
8584
this.type = this.resolvableType.resolve(methodParameter.getParameterType());
8685
this.annotations = nullSafeAnnotations(methodParameter.getParameterIndex() == -1 ?
@@ -93,7 +92,6 @@ public TypeDescriptor(MethodParameter methodParameter) {
9392
* @param field the field
9493
*/
9594
public TypeDescriptor(Field field) {
96-
Assert.notNull(field, "Field must not be null");
9795
this.resolvableType = ResolvableType.forField(field);
9896
this.type = this.resolvableType.resolve(field.getType());
9997
this.annotations = nullSafeAnnotations(field.getAnnotations());
@@ -451,31 +449,31 @@ private TypeDescriptor narrow(Object value, TypeDescriptor typeDescriptor) {
451449
}
452450

453451
@Override
454-
public boolean equals(Object obj) {
455-
if (this == obj) {
452+
public boolean equals(Object other) {
453+
if (this == other) {
456454
return true;
457455
}
458-
if (!(obj instanceof TypeDescriptor)) {
456+
if (!(other instanceof TypeDescriptor)) {
459457
return false;
460458
}
461-
TypeDescriptor other = (TypeDescriptor) obj;
462-
if (!ObjectUtils.nullSafeEquals(this.type, other.type)) {
459+
TypeDescriptor otherDesc = (TypeDescriptor) other;
460+
if (getType() != otherDesc.getType()) {
463461
return false;
464462
}
465-
if (getAnnotations().length != other.getAnnotations().length) {
463+
if (getAnnotations().length != otherDesc.getAnnotations().length) {
466464
return false;
467465
}
468466
for (Annotation ann : getAnnotations()) {
469-
if (!other.hasAnnotation(ann.annotationType())) {
467+
if (!otherDesc.hasAnnotation(ann.annotationType())) {
470468
return false;
471469
}
472470
}
473471
if (isCollection() || isArray()) {
474-
return ObjectUtils.nullSafeEquals(getElementTypeDescriptor(), other.getElementTypeDescriptor());
472+
return ObjectUtils.nullSafeEquals(getElementTypeDescriptor(), otherDesc.getElementTypeDescriptor());
475473
}
476474
else if (isMap()) {
477-
return ObjectUtils.nullSafeEquals(getMapKeyTypeDescriptor(), other.getMapKeyTypeDescriptor()) &&
478-
ObjectUtils.nullSafeEquals(getMapValueTypeDescriptor(), other.getMapValueTypeDescriptor());
475+
return (ObjectUtils.nullSafeEquals(getMapKeyTypeDescriptor(), otherDesc.getMapKeyTypeDescriptor()) &&
476+
ObjectUtils.nullSafeEquals(getMapValueTypeDescriptor(), otherDesc.getMapValueTypeDescriptor()));
479477
}
480478
else {
481479
return true;

0 commit comments

Comments
 (0)