diff --git a/spring-core/src/test/java/org/springframework/core/type/AbstractAnnotationMetadataTests.java b/spring-core/src/test/java/org/springframework/core/type/AbstractAnnotationMetadataTests.java new file mode 100644 index 0000000000..c483013b61 --- /dev/null +++ b/spring-core/src/test/java/org/springframework/core/type/AbstractAnnotationMetadataTests.java @@ -0,0 +1,405 @@ +/* + * Copyright 2002-2019 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.core.type; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import org.junit.Test; + +import org.springframework.core.type.AbstractAnnotationMetadataTests.TestMemberClass.TestMemberClassInnerClass; +import org.springframework.core.type.AbstractAnnotationMetadataTests.TestMemberClass.TestMemberClassInnerInterface; +import org.springframework.util.MultiValueMap; + +import static org.assertj.core.api.Assertions.*; + +/** + * Base class for {@link AnnotationMetadata} tests. + * + * @author Phillip Webb + */ +public abstract class AbstractAnnotationMetadataTests { + + @Test + public void getClassNameReturnsClassName() { + assertThat(get(TestClass.class).getClassName()).isEqualTo( + TestClass.class.getName()); + } + + @Test + public void isInterfaceWhenInterfaceReturnsTrue() { + assertThat(get(TestInterface.class).isInterface()).isTrue(); + assertThat(get(TestAnnotation.class).isInterface()).isTrue(); + } + + @Test + public void isInterfaceWhenNotInterfaceReturnsFalse() { + assertThat(get(TestClass.class).isInterface()).isFalse(); + } + + @Test + public void isAnnotationWhenAnnotationReturnsTrue() { + assertThat(get(TestAnnotation.class).isAnnotation()).isTrue(); + } + + @Test + public void isAnnotationWhenNotAnnotationReturnsFalse() { + assertThat(get(TestClass.class).isAnnotation()).isFalse(); + assertThat(get(TestInterface.class).isAnnotation()).isFalse(); + } + + @Test + public void isFinalWhenFinalReturnsTrue() { + assertThat(get(TestFinalClass.class).isFinal()).isTrue(); + } + + @Test + public void isFinalWhenNonFinalReturnsFalse() { + assertThat(get(TestClass.class).isFinal()).isFalse(); + } + + @Test + public void isIndependentWhenIndependentReturnsTrue() { + assertThat(get(AbstractAnnotationMetadataTests.class).isIndependent()).isTrue(); + assertThat(get(TestClass.class).isIndependent()).isTrue(); + } + + @Test + public void isIndependentWhenNotIndependentReturnsFalse() { + assertThat(get(TestNonStaticInnerClass.class).isIndependent()).isFalse(); + } + + @Test + public void getEnclosingClassNameWhenHasEnclosingClassReturnsEnclosingClass() { + assertThat(get(TestClass.class).getEnclosingClassName()).isEqualTo( + AbstractAnnotationMetadataTests.class.getName()); + } + + @Test + public void getEnclosingClassNameWhenHasNoEnclosingClassReturnsNull() { + assertThat(get( + AbstractAnnotationMetadataTests.class).getEnclosingClassName()).isNull(); + } + + @Test + public void getSuperClassNameWhenHasSuperClassReturnsName() { + assertThat(get(TestSubclass.class).getSuperClassName()).isEqualTo( + TestClass.class.getName()); + assertThat(get(TestClass.class).getSuperClassName()).isEqualTo( + Object.class.getName()); + } + + @Test + public void getSuperClassNameWhenHasNoSuperClassReturnsNull() { + assertThat(get(Object.class).getSuperClassName()).isNull(); + assertThat(get(TestInterface.class).getSuperClassName()).isNull(); + assertThat(get(TestSubInterface.class).getSuperClassName()).isNull(); + } + + @Test + public void getInterfaceNamesWhenHasInterfacesReturnsNames() { + assertThat(get(TestSubclass.class).getInterfaceNames()).containsExactlyInAnyOrder( + TestInterface.class.getName()); + assertThat(get( + TestSubInterface.class).getInterfaceNames()).containsExactlyInAnyOrder( + TestInterface.class.getName()); + } + + @Test + public void getInterfaceNamesWhenHasNoInterfacesReturnsEmptyArray() { + assertThat(get(TestClass.class).getInterfaceNames()).isEmpty(); + } + + @Test + public void getMemberClassNamesWhenHasMemberClassesReturnsNames() { + assertThat(get( + TestMemberClass.class).getMemberClassNames()).containsExactlyInAnyOrder( + TestMemberClassInnerClass.class.getName(), + TestMemberClassInnerInterface.class.getName()); + } + + @Test + public void getMemberClassNamesWhenHasNoMemberClassesReturnsEmptyArray() { + assertThat(get(TestClass.class).getMemberClassNames()).isEmpty(); + } + + @Test + public void isAnnotatedWhenMatchesDirectAnnotationReturnsTrue() { + assertThat(get(WithDirectAnnotations.class).isAnnotated( + DirectAnnotation1.class.getName())).isTrue(); + } + + @Test + public void isAnnotatedWhenMatchesMetaAnnotationReturnsTrue() { + assertThat(get(WithMetaAnnotations.class).isAnnotated( + MetaAnnotation2.class.getName())).isTrue(); + } + + @Test + public void isAnnotatedWhenDoesNotMatchDirectOrMetaAnnotationReturnsFalse() { + assertThat(get(TestClass.class).isAnnotated( + DirectAnnotation1.class.getName())).isFalse(); + } + + @Test + public void getAnnotationAttributesReturnsAttributes() { + assertThat(get(WithAnnotationAttributes.class).getAnnotationAttributes( + AnnotationAttributes.class.getName())).containsOnly(entry("name", "test"), + entry("size", 1)); + } + + @Test + public void getAllAnnotationAttributesReturnsAllAttributes() { + MultiValueMap attributes = get( + WithMetaAnnotationAttributes.class).getAllAnnotationAttributes( + AnnotationAttributes.class.getName()); + assertThat(attributes).containsOnlyKeys("name", "size"); + assertThat(attributes.get("name")).containsExactlyInAnyOrder("m1", "m2"); + assertThat(attributes.get("size")).containsExactlyInAnyOrder(1, 2); + } + + @Test + public void getAnnotationTypesReturnsDirectAnnotations() { + AnnotationMetadata metadata = get(WithDirectAnnotations.class); + assertThat(metadata.getAnnotationTypes()).containsExactlyInAnyOrder( + DirectAnnotation1.class.getName(), DirectAnnotation2.class.getName()); + } + + @Test + public void getMetaAnnotationTypesReturnsMetaAnnotations() { + AnnotationMetadata metadata = get(WithMetaAnnotations.class); + assertThat(metadata.getMetaAnnotationTypes( + MetaAnnotationRoot.class.getName())).containsExactlyInAnyOrder( + MetaAnnotation1.class.getName(), MetaAnnotation2.class.getName()); + } + + @Test + public void hasAnnotationWhenMatchesDirectAnnotationReturnsTrue() { + assertThat(get(WithDirectAnnotations.class).hasAnnotation( + DirectAnnotation1.class.getName())).isTrue(); + } + + @Test + public void hasAnnotationWhenMatchesMetaAnnotationReturnsFalse() { + assertThat(get(WithMetaAnnotations.class).hasAnnotation( + MetaAnnotation1.class.getName())).isFalse(); + assertThat(get(WithMetaAnnotations.class).hasAnnotation( + MetaAnnotation2.class.getName())).isFalse(); + } + + @Test + public void hasAnnotationWhenDoesNotMatchDirectOrMetaAnnotationReturnsFalse() { + assertThat(get(TestClass.class).hasAnnotation( + DirectAnnotation1.class.getName())).isFalse(); + } + + @Test + public void hasMetaAnnotationWhenMatchesDirectReturnsFalse() { + assertThat(get(WithDirectAnnotations.class).hasMetaAnnotation( + DirectAnnotation1.class.getName())).isFalse(); + } + + @Test + public void hasMetaAnnotationWhenMatchesMetaAnnotationReturnsTrue() { + assertThat(get(WithMetaAnnotations.class).hasMetaAnnotation( + MetaAnnotation1.class.getName())).isTrue(); + assertThat(get(WithMetaAnnotations.class).hasMetaAnnotation( + MetaAnnotation2.class.getName())).isTrue(); + } + + @Test + public void hasMetaAnnotationWhenDoesNotMatchDirectOrMetaAnnotationReturnsFalse() { + assertThat(get(TestClass.class).hasMetaAnnotation( + MetaAnnotation1.class.getName())).isFalse(); + } + + @Test + public void hasAnnotatedMethodsWhenMatchesDirectAnnotationReturnsTrue() { + assertThat(get(WithAnnotatedMethod.class).hasAnnotatedMethods( + DirectAnnotation1.class.getName())).isTrue(); + } + + @Test + public void hasAnnotatedMethodsWhenMatchesMetaAnnotationReturnsTrue() { + assertThat(get(WithMetaAnnotatedMethod.class).hasAnnotatedMethods( + MetaAnnotation2.class.getName())).isTrue(); + } + + @Test + public void hasAnnotatedMethodsWhenDoesNotMatchAnyAnnotationReturnsFalse() { + assertThat(get(WithAnnotatedMethod.class).hasAnnotatedMethods( + MetaAnnotation2.class.getName())).isFalse(); + assertThat(get(WithNonAnnotatedMethod.class).hasAnnotatedMethods( + DirectAnnotation1.class.getName())).isFalse(); + } + + @Test + public void getAnnotatedMethodsReturnsMatchingAnnotatedAndMetaAnnotatedMethods() { + assertThat(get(WithDirectAndMetaAnnotatedMethods.class).getAnnotatedMethods( + MetaAnnotation2.class.getName()).stream().map( + MethodMetadata::getMethodName)).containsExactlyInAnyOrder( + "direct", "meta"); + } + + protected abstract AnnotationMetadata get(Class source); + + @Retention(RetentionPolicy.RUNTIME) + public static @interface DirectAnnotation1 { + + } + + @Retention(RetentionPolicy.RUNTIME) + public static @interface DirectAnnotation2 { + + } + + @Retention(RetentionPolicy.RUNTIME) + @MetaAnnotation1 + public static @interface MetaAnnotationRoot { + + } + + @Retention(RetentionPolicy.RUNTIME) + @MetaAnnotation2 + public static @interface MetaAnnotation1 { + + } + + @Retention(RetentionPolicy.RUNTIME) + public static @interface MetaAnnotation2 { + + } + + public static class TestClass { + + } + + public static interface TestInterface { + + } + + public static interface TestSubInterface extends TestInterface { + + } + + public @interface TestAnnotation { + + } + + public static final class TestFinalClass { + + } + + public class TestNonStaticInnerClass { + + } + + public static class TestSubclass extends TestClass implements TestInterface { + + } + + @DirectAnnotation1 + @DirectAnnotation2 + public static class WithDirectAnnotations { + + } + + @MetaAnnotationRoot + public static class WithMetaAnnotations { + + } + + public static class TestMemberClass { + + public static class TestMemberClassInnerClass { + + } + + interface TestMemberClassInnerInterface { + + } + + } + + public static class WithAnnotatedMethod { + + @DirectAnnotation1 + public void test() { + } + + } + + public static class WithMetaAnnotatedMethod { + + @MetaAnnotationRoot + public void test() { + } + + } + + public static class WithNonAnnotatedMethod { + + public void test() { + } + + } + + public static class WithDirectAndMetaAnnotatedMethods { + + @MetaAnnotation2 + public void direct() { + } + + @MetaAnnotationRoot + public void meta() { + } + + } + + @AnnotationAttributes(name = "test", size = 1) + public static class WithAnnotationAttributes { + + } + + @MetaAnnotationAttributes1 + @MetaAnnotationAttributes2 + public static class WithMetaAnnotationAttributes { + + } + + @Retention(RetentionPolicy.RUNTIME) + @AnnotationAttributes(name = "m1", size = 1) + public static @interface MetaAnnotationAttributes1 { + + } + + @Retention(RetentionPolicy.RUNTIME) + @AnnotationAttributes(name = "m2", size = 2) + public static @interface MetaAnnotationAttributes2 { + + } + + @Retention(RetentionPolicy.RUNTIME) + public static @interface AnnotationAttributes { + + String name(); + + int size(); + + } + +} diff --git a/spring-core/src/test/java/org/springframework/core/type/AbstractMethodMetadataTests.java b/spring-core/src/test/java/org/springframework/core/type/AbstractMethodMetadataTests.java new file mode 100644 index 0000000000..8e0a3fca6a --- /dev/null +++ b/spring-core/src/test/java/org/springframework/core/type/AbstractMethodMetadataTests.java @@ -0,0 +1,251 @@ +/* + * Copyright 2002-2019 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.core.type; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import org.junit.Test; + +import org.springframework.util.MultiValueMap; + +import static org.assertj.core.api.Assertions.*; + +/** + * Base class for {@link MethodMetadata} tests. + * + * @author Phillip Webb + */ +public abstract class AbstractMethodMetadataTests { + + @Test + public void getMethodNameReturnsMethodName() { + assertThat(getTagged(WithMethod.class).getMethodName()).isEqualTo("test"); + } + + @Test + public void getDeclaringClassReturnsDeclaringClass() { + assertThat(getTagged(WithMethod.class).getDeclaringClassName()).isEqualTo( + WithMethod.class.getName()); + } + + @Test + public void getReturnTypeReturnsReturnType() { + assertThat(getTagged(WithMethod.class).getReturnTypeName()).isEqualTo( + String.class.getName()); + } + + @Test + public void isAbstractWhenAbstractReturnsTrue() { + assertThat(getTagged(WithAbstractMethod.class).isAbstract()).isTrue(); + } + + @Test + public void isAbstractWhenNotAbstractReturnsFalse() { + assertThat(getTagged(WithMethod.class).isAbstract()).isFalse(); + } + + @Test + public void isStatusWhenStaticReturnsTrue() { + assertThat(getTagged(WithStaticMethod.class).isStatic()).isTrue(); + } + + @Test + public void isStaticWhenNotStaticReturnsFalse() { + assertThat(getTagged(WithMethod.class).isStatic()).isFalse(); + } + + @Test + public void isFinalWhenFinalReturnsTrue() { + assertThat(getTagged(WithFinalMethod.class).isFinal()).isTrue(); + } + + @Test + public void isFinalWhenNonFinalReturnsFalse() { + assertThat(getTagged(WithMethod.class).isFinal()).isFalse(); + } + + @Test + public void isOverridableWhenOverridableReturnsTrue() { + assertThat(getTagged(WithMethod.class).isOverridable()).isTrue(); + } + + @Test + public void isOverridableWhenNonOverridableReturnsFalse() { + assertThat(getTagged(WithStaticMethod.class).isOverridable()).isFalse(); + assertThat(getTagged(WithFinalMethod.class).isOverridable()).isFalse(); + assertThat(getTagged(WithPrivateMethod.class).isOverridable()).isFalse(); + } + + @Test + public void isAnnotatedWhenMatchesDirectAnnotationReturnsTrue() { + assertThat(getTagged(WithDirectAnnotation.class).isAnnotated( + DirectAnnotation.class.getName())).isTrue(); + } + + @Test + public void isAnnotatedWhenMatchesMetaAnnotationReturnsTrue() { + assertThat(getTagged(WithMetaAnnotation.class).isAnnotated( + DirectAnnotation.class.getName())).isTrue(); + } + + @Test + public void isAnnotatedWhenDoesNotMatchDirectOrMetaAnnotationReturnsFalse() { + assertThat(getTagged(WithMethod.class).isAnnotated( + DirectAnnotation.class.getName())).isFalse(); + } + + @Test + public void getAnnotationAttributesReturnsAttributes() { + assertThat(getTagged(WithAnnotationAttributes.class).getAnnotationAttributes( + AnnotationAttributes.class.getName())).containsOnly(entry("name", "test"), + entry("size", 1)); + } + + @Test + public void getAllAnnotationAttributesReturnsAllAttributes() { + MultiValueMap attributes = getTagged( + WithMetaAnnotationAttributes.class).getAllAnnotationAttributes( + AnnotationAttributes.class.getName()); + assertThat(attributes).containsOnlyKeys("name", "size"); + assertThat(attributes.get("name")).containsExactlyInAnyOrder("m1", "m2"); + assertThat(attributes.get("size")).containsExactlyInAnyOrder(1, 2); + } + + protected MethodMetadata getTagged(Class source) { + return get(source, Tag.class.getName()); + } + + protected MethodMetadata get(Class source, String annotationName) { + return get(source).getAnnotatedMethods(annotationName).iterator().next(); + } + + protected abstract AnnotationMetadata get(Class source); + + @Retention(RetentionPolicy.RUNTIME) + public static @interface Tag { + + } + + public static class WithMethod { + + @Tag + public String test() { + return ""; + } + + } + + public abstract static class WithAbstractMethod { + + @Tag + public abstract String test(); + + } + + public static class WithStaticMethod { + + @Tag + public static String test() { + return ""; + } + + } + + public static class WithFinalMethod { + + @Tag + public final String test() { + return ""; + } + + } + + public static class WithPrivateMethod { + + @Tag + private final String test() { + return ""; + } + + } + + public static abstract class WithDirectAnnotation { + + @Tag + @DirectAnnotation + public abstract String test(); + + } + + public static abstract class WithMetaAnnotation { + + @Tag + @MetaAnnotation + public abstract String test(); + + } + + @Retention(RetentionPolicy.RUNTIME) + public static @interface DirectAnnotation { + + } + + @DirectAnnotation + @Retention(RetentionPolicy.RUNTIME) + public static @interface MetaAnnotation { + + } + + public static abstract class WithAnnotationAttributes { + + @Tag + @AnnotationAttributes(name = "test", size = 1) + public abstract String test(); + + } + + public static abstract class WithMetaAnnotationAttributes { + + @Tag + @MetaAnnotationAttributes1 + @MetaAnnotationAttributes2 + public abstract String test(); + + } + + @Retention(RetentionPolicy.RUNTIME) + @AnnotationAttributes(name = "m1", size = 1) + public static @interface MetaAnnotationAttributes1 { + + } + + @Retention(RetentionPolicy.RUNTIME) + @AnnotationAttributes(name = "m2", size = 2) + public static @interface MetaAnnotationAttributes2 { + + } + + @Retention(RetentionPolicy.RUNTIME) + public static @interface AnnotationAttributes { + + String name(); + + int size(); + + } +} diff --git a/spring-core/src/test/java/org/springframework/core/type/StandardAnnotationMetadataTests.java b/spring-core/src/test/java/org/springframework/core/type/StandardAnnotationMetadataTests.java new file mode 100644 index 0000000000..328419e882 --- /dev/null +++ b/spring-core/src/test/java/org/springframework/core/type/StandardAnnotationMetadataTests.java @@ -0,0 +1,31 @@ +/* + * Copyright 2002-2019 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.core.type; + +/** + * Tests for {@link StandardAnnotationMetadata}. + * + * @author Phillip Webb + */ +public class StandardAnnotationMetadataTests extends AbstractAnnotationMetadataTests { + + @Override + protected AnnotationMetadata get(Class source) { + return new StandardAnnotationMetadata(source); + } + +} diff --git a/spring-core/src/test/java/org/springframework/core/type/StandardMethodMetadataTests.java b/spring-core/src/test/java/org/springframework/core/type/StandardMethodMetadataTests.java new file mode 100644 index 0000000000..ebfe4ba87c --- /dev/null +++ b/spring-core/src/test/java/org/springframework/core/type/StandardMethodMetadataTests.java @@ -0,0 +1,31 @@ +/* + * Copyright 2002-2019 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.core.type; + +/** + * Tests for {@link StandardMethodMetadata}. + * + * @author Phillip Webb + */ +public class StandardMethodMetadataTests extends AbstractMethodMetadataTests { + + @Override + protected AnnotationMetadata get(Class source) { + return new StandardAnnotationMetadata(source); + } + +} diff --git a/spring-core/src/test/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitorTests.java b/spring-core/src/test/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitorTests.java new file mode 100644 index 0000000000..711f931b06 --- /dev/null +++ b/spring-core/src/test/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitorTests.java @@ -0,0 +1,61 @@ +/* + * Copyright 2002-2019 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.core.type.classreading; + +import java.io.BufferedInputStream; +import java.io.InputStream; + +import org.springframework.asm.ClassReader; +import org.springframework.core.io.DefaultResourceLoader; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; +import org.springframework.core.type.AbstractAnnotationMetadataTests; +import org.springframework.core.type.AnnotationMetadata; +import org.springframework.util.ClassUtils; + +/** + * Tests for {@link AnnotationMetadataReadingVisitor}. + * + * @author Phillip Webb + */ +public class AnnotationMetadataReadingVisitorTests + extends AbstractAnnotationMetadataTests { + + @Override + protected AnnotationMetadata get(Class source) { + try { + ClassLoader classLoader = source.getClassLoader(); + String className = source.getName(); + String resourcePath = ResourceLoader.CLASSPATH_URL_PREFIX + + ClassUtils.convertClassNameToResourcePath(className) + + ClassUtils.CLASS_FILE_SUFFIX; + Resource resource = new DefaultResourceLoader().getResource(resourcePath); + try (InputStream inputStream = new BufferedInputStream( + resource.getInputStream())) { + ClassReader classReader = new ClassReader(inputStream); + AnnotationMetadataReadingVisitor metadata = new AnnotationMetadataReadingVisitor( + classLoader); + classReader.accept(metadata, ClassReader.SKIP_DEBUG); + return metadata; + } + } + catch (Exception ex) { + throw new IllegalStateException(ex); + } + } + +} diff --git a/spring-core/src/test/java/org/springframework/core/type/classreading/MethodMetadataReadingVisitorTests.java b/spring-core/src/test/java/org/springframework/core/type/classreading/MethodMetadataReadingVisitorTests.java new file mode 100644 index 0000000000..f111744219 --- /dev/null +++ b/spring-core/src/test/java/org/springframework/core/type/classreading/MethodMetadataReadingVisitorTests.java @@ -0,0 +1,60 @@ +/* + * Copyright 2002-2019 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.core.type.classreading; + +import java.io.BufferedInputStream; +import java.io.InputStream; + +import org.springframework.asm.ClassReader; +import org.springframework.core.io.DefaultResourceLoader; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; +import org.springframework.core.type.AbstractMethodMetadataTests; +import org.springframework.core.type.AnnotationMetadata; +import org.springframework.util.ClassUtils; + +/** + * Tests for {@link MethodMetadataReadingVisitor}. + * + * @author Phillip Webb + */ +public class MethodMetadataReadingVisitorTests extends AbstractMethodMetadataTests { + + @Override + protected AnnotationMetadata get(Class source) { + try { + ClassLoader classLoader = source.getClassLoader(); + String className = source.getName(); + String resourcePath = ResourceLoader.CLASSPATH_URL_PREFIX + + ClassUtils.convertClassNameToResourcePath(className) + + ClassUtils.CLASS_FILE_SUFFIX; + Resource resource = new DefaultResourceLoader().getResource(resourcePath); + try (InputStream inputStream = new BufferedInputStream( + resource.getInputStream())) { + ClassReader classReader = new ClassReader(inputStream); + AnnotationMetadataReadingVisitor metadata = new AnnotationMetadataReadingVisitor( + classLoader); + classReader.accept(metadata, ClassReader.SKIP_DEBUG); + return metadata; + } + } + catch (Exception ex) { + throw new IllegalStateException(ex); + } + } + +}