Implement equals, hashCode, & toString in BeanMethod and *Metadata types
Prior to this commit, ConfigurationClass implemented equals(), hashCode(), and toString(), but BeanMethod did not. This commit introduces equals(), hashCode(), and toString() implementations in BeanMethod for consistency with ConfigurationClass to make it possible to use BeanMethod instances to index additional metadata as well. In order to properly implement equals() in BeanMethod, the method argument types are required, but these are not directly available in BeanMethod. However, they are available via ASM when processing @Bean methods. This commit therefore implements equals(), hashCode(), and toString() in SimpleMethodMetadata which BeanMethod delegates to. For completeness, this commit also implements equals(), hashCode(), and toString() in StandardClassMetadata, StandardMethodMetadata, and SimpleAnnotationMetadata. Closes gh-27076
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2021 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.
|
||||
@@ -28,6 +28,7 @@ import org.springframework.util.StringUtils;
|
||||
* to introspect a given {@code Class}.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
public class StandardClassMetadata implements ClassMetadata {
|
||||
@@ -119,4 +120,20 @@ public class StandardClassMetadata implements ClassMetadata {
|
||||
return StringUtils.toStringArray(memberClassNames);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
return ((this == obj) || ((obj instanceof StandardClassMetadata) &&
|
||||
getIntrospectedClass().equals(((StandardClassMetadata) obj).getIntrospectedClass())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return getIntrospectedClass().hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClassName();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2021 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.
|
||||
@@ -36,6 +36,7 @@ import org.springframework.util.MultiValueMap;
|
||||
* @author Mark Pollack
|
||||
* @author Chris Beams
|
||||
* @author Phillip Webb
|
||||
* @author Sam Brannen
|
||||
* @since 3.0
|
||||
*/
|
||||
public class StandardMethodMetadata implements MethodMetadata {
|
||||
@@ -150,4 +151,20 @@ public class StandardMethodMetadata implements MethodMetadata {
|
||||
annotationName, classValuesAsString, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
return ((this == obj) || ((obj instanceof StandardMethodMetadata) &&
|
||||
this.introspectedMethod.equals(((StandardMethodMetadata) obj).introspectedMethod)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.introspectedMethod.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.introspectedMethod.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2021 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.
|
||||
@@ -31,6 +31,7 @@ import org.springframework.lang.Nullable;
|
||||
* {@link SimpleAnnotationMetadataReadingVisitor}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Sam Brannen
|
||||
* @since 5.2
|
||||
*/
|
||||
final class SimpleAnnotationMetadata implements AnnotationMetadata {
|
||||
@@ -156,4 +157,20 @@ final class SimpleAnnotationMetadata implements AnnotationMetadata {
|
||||
return this.annotations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
return ((this == obj) || ((obj instanceof SimpleAnnotationMetadata) &&
|
||||
this.className.equals(((SimpleAnnotationMetadata) obj).className)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.className.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.className;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,11 +19,13 @@ package org.springframework.core.type.classreading;
|
||||
import org.springframework.asm.Opcodes;
|
||||
import org.springframework.core.annotation.MergedAnnotations;
|
||||
import org.springframework.core.type.MethodMetadata;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* {@link MethodMetadata} created from a {@link SimpleMethodMetadataReadingVisitor}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Sam Brannen
|
||||
* @since 5.2
|
||||
*/
|
||||
final class SimpleMethodMetadata implements MethodMetadata {
|
||||
@@ -36,16 +38,20 @@ final class SimpleMethodMetadata implements MethodMetadata {
|
||||
|
||||
private final String returnTypeName;
|
||||
|
||||
// The source implements equals(), hashCode(), and toString() for the underlying method.
|
||||
private final Object source;
|
||||
|
||||
private final MergedAnnotations annotations;
|
||||
|
||||
|
||||
public SimpleMethodMetadata(String methodName, int access, String declaringClassName,
|
||||
String returnTypeName, MergedAnnotations annotations) {
|
||||
SimpleMethodMetadata(String methodName, int access, String declaringClassName,
|
||||
String returnTypeName, Object source, MergedAnnotations annotations) {
|
||||
|
||||
this.methodName = methodName;
|
||||
this.access = access;
|
||||
this.declaringClassName = declaringClassName;
|
||||
this.returnTypeName = returnTypeName;
|
||||
this.source = source;
|
||||
this.annotations = annotations;
|
||||
}
|
||||
|
||||
@@ -94,4 +100,20 @@ final class SimpleMethodMetadata implements MethodMetadata {
|
||||
return this.annotations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
return ((this == obj) || ((obj instanceof SimpleMethodMetadata) &&
|
||||
this.source.equals(((SimpleMethodMetadata) obj).source)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.source.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.source.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -81,8 +81,8 @@ final class SimpleMethodMetadataReadingVisitor extends MethodVisitor {
|
||||
if (!this.annotations.isEmpty()) {
|
||||
String returnTypeName = Type.getReturnType(this.descriptor).getClassName();
|
||||
MergedAnnotations annotations = MergedAnnotations.of(this.annotations);
|
||||
SimpleMethodMetadata metadata = new SimpleMethodMetadata(this.methodName,
|
||||
this.access, this.declaringClassName, returnTypeName, annotations);
|
||||
SimpleMethodMetadata metadata = new SimpleMethodMetadata(this.methodName, this.access,
|
||||
this.declaringClassName, returnTypeName, getSource(), annotations);
|
||||
this.consumer.accept(metadata);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2021 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.
|
||||
@@ -33,9 +33,51 @@ import static org.assertj.core.api.Assertions.entry;
|
||||
* Base class for {@link AnnotationMetadata} tests.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public abstract class AbstractAnnotationMetadataTests {
|
||||
|
||||
@Test
|
||||
public void verifyEquals() throws Exception {
|
||||
AnnotationMetadata testClass1 = get(TestClass.class);
|
||||
AnnotationMetadata testClass2 = get(TestClass.class);
|
||||
AnnotationMetadata testMemberClass1 = get(TestMemberClass.class);
|
||||
AnnotationMetadata testMemberClass2 = get(TestMemberClass.class);
|
||||
|
||||
assertThat(testClass1.equals(null)).isFalse();
|
||||
|
||||
assertThat(testClass1.equals(testClass1)).isTrue();
|
||||
assertThat(testClass2.equals(testClass2)).isTrue();
|
||||
assertThat(testClass1.equals(testClass2)).isTrue();
|
||||
assertThat(testClass2.equals(testClass1)).isTrue();
|
||||
|
||||
assertThat(testMemberClass1.equals(testMemberClass1)).isTrue();
|
||||
assertThat(testMemberClass2.equals(testMemberClass2)).isTrue();
|
||||
assertThat(testMemberClass1.equals(testMemberClass2)).isTrue();
|
||||
assertThat(testMemberClass2.equals(testMemberClass1)).isTrue();
|
||||
|
||||
assertThat(testClass1.equals(testMemberClass1)).isFalse();
|
||||
assertThat(testMemberClass1.equals(testClass1)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyHashCode() throws Exception {
|
||||
AnnotationMetadata testClass1 = get(TestClass.class);
|
||||
AnnotationMetadata testClass2 = get(TestClass.class);
|
||||
AnnotationMetadata testMemberClass1 = get(TestMemberClass.class);
|
||||
AnnotationMetadata testMemberClass2 = get(TestMemberClass.class);
|
||||
|
||||
assertThat(testClass1).hasSameHashCodeAs(testClass2);
|
||||
assertThat(testMemberClass1).hasSameHashCodeAs(testMemberClass2);
|
||||
|
||||
assertThat(testClass1).doesNotHaveSameHashCodeAs(testMemberClass1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyToString() throws Exception {
|
||||
assertThat(get(TestClass.class).toString()).isEqualTo(TestClass.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getClassNameReturnsClassName() {
|
||||
assertThat(get(TestClass.class).getClassName()).isEqualTo(TestClass.class.getName());
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2021 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.
|
||||
@@ -37,6 +37,54 @@ import static org.assertj.core.api.Assertions.entry;
|
||||
*/
|
||||
public abstract class AbstractMethodMetadataTests {
|
||||
|
||||
@Test
|
||||
public void verifyEquals() throws Exception {
|
||||
MethodMetadata withMethod1 = getTagged(WithMethod.class);
|
||||
MethodMetadata withMethod2 = getTagged(WithMethod.class);
|
||||
MethodMetadata withMethodWithTwoArguments1 = getTagged(WithMethodWithTwoArguments.class);
|
||||
MethodMetadata withMethodWithTwoArguments2 = getTagged(WithMethodWithTwoArguments.class);
|
||||
|
||||
assertThat(withMethod1.equals(null)).isFalse();
|
||||
|
||||
assertThat(withMethod1.equals(withMethod1)).isTrue();
|
||||
assertThat(withMethod2.equals(withMethod2)).isTrue();
|
||||
assertThat(withMethod1.equals(withMethod2)).isTrue();
|
||||
assertThat(withMethod2.equals(withMethod1)).isTrue();
|
||||
|
||||
assertThat(withMethodWithTwoArguments1.equals(withMethodWithTwoArguments1)).isTrue();
|
||||
assertThat(withMethodWithTwoArguments2.equals(withMethodWithTwoArguments2)).isTrue();
|
||||
assertThat(withMethodWithTwoArguments1.equals(withMethodWithTwoArguments2)).isTrue();
|
||||
assertThat(withMethodWithTwoArguments2.equals(withMethodWithTwoArguments1)).isTrue();
|
||||
|
||||
assertThat(withMethod1.equals(withMethodWithTwoArguments1)).isFalse();
|
||||
assertThat(withMethodWithTwoArguments1.equals(withMethod1)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyHashCode() throws Exception {
|
||||
MethodMetadata withMethod1 = getTagged(WithMethod.class);
|
||||
MethodMetadata withMethod2 = getTagged(WithMethod.class);
|
||||
MethodMetadata withMethodWithTwoArguments1 = getTagged(WithMethodWithTwoArguments.class);
|
||||
MethodMetadata withMethodWithTwoArguments2 = getTagged(WithMethodWithTwoArguments.class);
|
||||
|
||||
assertThat(withMethod1).hasSameHashCodeAs(withMethod2);
|
||||
assertThat(withMethodWithTwoArguments1).hasSameHashCodeAs(withMethodWithTwoArguments2);
|
||||
|
||||
assertThat(withMethod1).doesNotHaveSameHashCodeAs(withMethodWithTwoArguments1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyToString() throws Exception {
|
||||
assertThat(getTagged(WithMethod.class).toString())
|
||||
.endsWith(WithMethod.class.getName() + ".test()");
|
||||
|
||||
assertThat(getTagged(WithMethodWithOneArgument.class).toString())
|
||||
.endsWith(WithMethodWithOneArgument.class.getName() + ".test(java.lang.String)");
|
||||
|
||||
assertThat(getTagged(WithMethodWithTwoArguments.class).toString())
|
||||
.endsWith(WithMethodWithTwoArguments.class.getName() + ".test(java.lang.String,java.lang.Integer)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMethodNameReturnsMethodName() {
|
||||
assertThat(getTagged(WithMethod.class).getMethodName()).isEqualTo("test");
|
||||
@@ -171,6 +219,24 @@ public abstract class AbstractMethodMetadataTests {
|
||||
|
||||
}
|
||||
|
||||
public static class WithMethodWithOneArgument {
|
||||
|
||||
@Tag
|
||||
public String test(String text) {
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class WithMethodWithTwoArguments {
|
||||
|
||||
@Tag
|
||||
public String test(String text, Integer num) {
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public abstract static class WithAbstractMethod {
|
||||
|
||||
@Tag
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2021 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.
|
||||
@@ -19,6 +19,7 @@ package org.springframework.core.type.classreading;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.asm.ClassReader;
|
||||
@@ -35,6 +36,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
* Tests for {@link AnnotationMetadataReadingVisitor}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
class AnnotationMetadataReadingVisitorTests extends AbstractAnnotationMetadataTests {
|
||||
@@ -62,6 +64,24 @@ class AnnotationMetadataReadingVisitorTests extends AbstractAnnotationMetadataTe
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("equals() not implemented in deprecated AnnotationMetadataReadingVisitor")
|
||||
@Override
|
||||
public void verifyEquals() throws Exception {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("hashCode() not implemented in deprecated AnnotationMetadataReadingVisitor")
|
||||
@Override
|
||||
public void verifyHashCode() throws Exception {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("toString() not implemented in deprecated AnnotationMetadataReadingVisitor")
|
||||
@Override
|
||||
public void verifyToString() {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Test
|
||||
public void getAnnotationsReturnsDirectAnnotations() {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2021 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.
|
||||
@@ -19,6 +19,7 @@ package org.springframework.core.type.classreading;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.asm.ClassReader;
|
||||
@@ -35,6 +36,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
* Tests for {@link MethodMetadataReadingVisitor}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
class MethodMetadataReadingVisitorTests extends AbstractMethodMetadataTests {
|
||||
@@ -62,8 +64,26 @@ class MethodMetadataReadingVisitorTests extends AbstractMethodMetadataTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Test
|
||||
@Disabled("equals() not implemented in deprecated MethodMetadataReadingVisitor")
|
||||
@Override
|
||||
public void verifyEquals() throws Exception {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("hashCode() not implemented in deprecated MethodMetadataReadingVisitor")
|
||||
@Override
|
||||
public void verifyHashCode() throws Exception {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("toString() not implemented in deprecated MethodMetadataReadingVisitor")
|
||||
@Override
|
||||
public void verifyToString() {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void getAnnotationsReturnsDirectAnnotations() {
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(
|
||||
super::getAnnotationsReturnsDirectAnnotations);
|
||||
|
||||
Reference in New Issue
Block a user