From dc14ea86eb2e2b7fca4f0882a3717f8ecdfaf197 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 9 Apr 2019 09:40:53 +0200 Subject: [PATCH] Fix regression for nested AnnotationAttributes.annotationType() result See gh-22738 --- .../context/annotation/ImportAwareTests.java | 49 +++++++++++++++++-- .../annotation/AbstractMergedAnnotation.java | 2 +- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ImportAwareTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ImportAwareTests.java index 50f2eabdcd..559e8e4008 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ImportAwareTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ImportAwareTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * 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. @@ -20,6 +20,7 @@ import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import java.util.Arrays; import org.junit.Test; @@ -81,7 +82,7 @@ public class ImportAwareTests { } @Test - public void importRegistrar() throws Exception { + public void importRegistrar() { ImportedRegistrar.called = false; AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ImportingRegistrarConfig.class); @@ -91,7 +92,7 @@ public class ImportAwareTests { } @Test - public void importRegistrarWithImport() throws Exception { + public void importRegistrarWithImport() { ImportedRegistrar.called = false; AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ImportingRegistrarConfigWithImport.class); @@ -120,6 +121,11 @@ public class ImportAwareTests { ((StandardAnnotationMetadata) importMetadata).getIntrospectedClass()); } + @Test + public void importAwareWithAnnotationAttributes() { + new AnnotationConfigApplicationContext(ApplicationConfiguration.class); + } + @Configuration @Import(ImportedConfig.class) @@ -290,4 +296,41 @@ public class ImportAwareTests { } } + + @Configuration + @EnableFeature(policies = { + @EnableFeature.FeaturePolicy(name = "one"), + @EnableFeature.FeaturePolicy(name = "two") + }) + public static class ApplicationConfiguration { + } + + + @Target(ElementType.TYPE) + @Retention(RetentionPolicy.RUNTIME) + @Import(FeatureConfiguration.class) + public @interface EnableFeature { + + FeaturePolicy[] policies() default {}; + + @interface FeaturePolicy { + + String name(); + } + } + + + @Configuration + public static class FeatureConfiguration implements ImportAware { + + @Override + public void setImportMetadata(AnnotationMetadata annotationMetadata) { + AnnotationAttributes enableFeatureAttributes = + AnnotationAttributes.fromMap(annotationMetadata.getAnnotationAttributes(EnableFeature.class.getName())); + assertEquals(EnableFeature.class, enableFeatureAttributes.annotationType()); + Arrays.stream(enableFeatureAttributes.getAnnotationArray("policies")).forEach(featurePolicyAttributes -> + assertEquals(EnableFeature.FeaturePolicy.class, featurePolicyAttributes.annotationType())); + } + } + } diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AbstractMergedAnnotation.java b/spring-core/src/main/java/org/springframework/core/annotation/AbstractMergedAnnotation.java index 62ae09a73f..fb96515f33 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AbstractMergedAnnotation.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AbstractMergedAnnotation.java @@ -168,7 +168,7 @@ abstract class AbstractMergedAnnotation implements MergedA @Override public AnnotationAttributes asAnnotationAttributes(Adapt... adaptations) { - return asMap(mergedAnnotation -> new AnnotationAttributes(getType()), adaptations); + return asMap(mergedAnnotation -> new AnnotationAttributes(mergedAnnotation.getType()), adaptations); } @Override