Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
5a87cd05
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
5a87cd05
编写于
7月 31, 2019
作者:
S
Sam Brannen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[WIP] Use MergedAnnotations API
See gh-19930
上级
606028c1
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
18 addition
and
25 deletion
+18
-25
spring-test/src/main/java/org/springframework/test/context/support/ContextLoaderUtils.java
...ingframework/test/context/support/ContextLoaderUtils.java
+18
-25
未找到文件。
spring-test/src/main/java/org/springframework/test/context/support/ContextLoaderUtils.java
浏览文件 @
5a87cd05
...
...
@@ -16,6 +16,7 @@
package
org.springframework.test.context.support
;
import
java.lang.reflect.Modifier
;
import
java.util.ArrayList
;
import
java.util.HashSet
;
import
java.util.LinkedHashMap
;
...
...
@@ -27,19 +28,19 @@ import org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory
;
import
org.springframework.core.annotation.AnnotationUtils
;
import
org.springframework.core.annotation.MergedAnnotation
;
import
org.springframework.core.annotation.MergedAnnotations
;
import
org.springframework.core.annotation.MergedAnnotations.SearchStrategy
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.ContextConfigurationAttributes
;
import
org.springframework.test.context.ContextHierarchy
;
import
org.springframework.test.context.SmartContextLoader
;
import
org.springframework.test.util.MetaAnnotationUtils.AnnotationDescriptor
;
import
org.springframework.test.util.MetaAnnotationUtils.UntypedAnnotationDescriptor
;
import
org.springframework.util.Assert
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.util.StringUtils
;
import
static
org
.
springframework
.
core
.
annotation
.
AnnotationUtils
.
getAnnotation
;
import
static
org
.
springframework
.
core
.
annotation
.
AnnotationUtils
.
isAnnotationDeclaredLocally
;
import
static
org
.
springframework
.
test
.
util
.
MetaAnnotationUtils
.
findAnnotationDescriptor
;
import
static
org
.
springframework
.
test
.
util
.
MetaAnnotationUtils
.
findAnnotationDescriptorForTypes
;
/**
...
...
@@ -183,7 +184,7 @@ abstract class ContextLoaderUtils {
* @see #resolveContextHierarchyAttributes(Class)
*/
static
Map
<
String
,
List
<
ContextConfigurationAttributes
>>
buildContextHierarchyMap
(
Class
<?>
testClass
)
{
final
Map
<
String
,
List
<
ContextConfigurationAttributes
>>
map
=
new
LinkedHashMap
<>();
Map
<
String
,
List
<
ContextConfigurationAttributes
>>
map
=
new
LinkedHashMap
<>();
int
hierarchyLevel
=
1
;
for
(
List
<
ContextConfigurationAttributes
>
configAttributesList
:
resolveContextHierarchyAttributes
(
testClass
))
{
...
...
@@ -239,32 +240,24 @@ abstract class ContextLoaderUtils {
Assert
.
notNull
(
testClass
,
"Class must not be null"
);
Class
<
ContextConfiguration
>
annotationType
=
ContextConfiguration
.
class
;
AnnotationDescriptor
<
ContextConfiguration
>
descriptor
=
findAnnotationDescriptor
(
testClass
,
annotationType
);
Assert
.
notNull
(
descriptor
,
()
->
String
.
format
(
"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]"
,
annotationType
.
getName
(),
testClass
.
getName
()));
MergedAnnotations
mergedAnnotations
=
MergedAnnotations
.
from
(
testClass
,
SearchStrategy
.
TYPE_HIERARCHY_AND_ENCLOSING_CLASSES
);
Assert
.
isTrue
(
mergedAnnotations
.
isPresent
(
annotationType
),
()
->
String
.
format
(
"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]"
,
annotationType
.
getName
(),
testClass
.
getName
()));
List
<
ContextConfigurationAttributes
>
attributesList
=
new
ArrayList
<>();
resolveContextConfigurationAttributes
(
attributesList
,
descriptor
);
mergedAnnotations
.
stream
(
annotationType
).
forEach
(
mergedAnnotation
->
resolveContextConfigurationAttributes
(
attributesList
,
mergedAnnotation
));
return
attributesList
;
}
private
static
void
resolveContextConfigurationAttributes
(
List
<
ContextConfigurationAttributes
>
attributesList
,
AnnotationDescriptor
<
ContextConfiguration
>
descriptor
)
{
if
(
descriptor
!=
null
)
{
Class
<?>
rootDeclaringClass
=
descriptor
.
getRootDeclaringClass
();
convertContextConfigToConfigAttributesAndAddToList
(
descriptor
.
synthesizeAnnotation
(),
rootDeclaringClass
,
attributesList
);
// Search on superclass
resolveContextConfigurationAttributes
(
attributesList
,
findAnnotationDescriptor
(
rootDeclaringClass
.
getSuperclass
(),
ContextConfiguration
.
class
));
// Search on enclosing class for "nested test class"
if
(
ClassUtils
.
isInnerClass
(
rootDeclaringClass
))
{
resolveContextConfigurationAttributes
(
attributesList
,
findAnnotationDescriptor
(
rootDeclaringClass
.
getDeclaringClass
(),
ContextConfiguration
.
class
));
}
}
MergedAnnotation
<
ContextConfiguration
>
mergedAnnotation
)
{
Class
<?>
rootDeclaringClass
=
(
Class
<?>)
mergedAnnotation
.
getSource
();
convertContextConfigToConfigAttributesAndAddToList
(
mergedAnnotation
.
synthesize
(),
rootDeclaringClass
,
attributesList
);
}
/**
...
...
@@ -273,7 +266,7 @@ abstract class ContextLoaderUtils {
* declaring class and then adding the attributes to the supplied list.
*/
private
static
void
convertContextConfigToConfigAttributesAndAddToList
(
ContextConfiguration
contextConfiguration
,
Class
<?>
declaringClass
,
final
List
<
ContextConfigurationAttributes
>
attributesList
)
{
Class
<?>
declaringClass
,
List
<
ContextConfigurationAttributes
>
attributesList
)
{
if
(
logger
.
isTraceEnabled
())
{
logger
.
trace
(
String
.
format
(
"Retrieved @ContextConfiguration [%s] for declaring class [%s]."
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录