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.
|
||||
@@ -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