Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
50e980c0
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,发现更多精彩内容 >>
提交
50e980c0
编写于
3月 12, 2018
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Validate declared annotations before deciding between reflection and ASM
Issue: SPR-16564
上级
1b1a69a1
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
51 addition
and
23 deletion
+51
-23
spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java
...ramework/context/annotation/ConfigurationClassParser.java
+7
-2
spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java
...pringframework/core/annotation/AnnotatedElementUtils.java
+17
-19
spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java
.../org/springframework/core/annotation/AnnotationUtils.java
+27
-2
未找到文件。
spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java
浏览文件 @
50e980c0
...
...
@@ -18,6 +18,7 @@ package org.springframework.context.annotation;
import
java.io.FileNotFoundException
;
import
java.io.IOException
;
import
java.lang.annotation.Annotation
;
import
java.net.UnknownHostException
;
import
java.util.ArrayDeque
;
import
java.util.ArrayList
;
...
...
@@ -55,6 +56,7 @@ import org.springframework.core.OrderComparator;
import
org.springframework.core.Ordered
;
import
org.springframework.core.annotation.AnnotationAttributes
;
import
org.springframework.core.annotation.AnnotationAwareOrderComparator
;
import
org.springframework.core.annotation.AnnotationUtils
;
import
org.springframework.core.env.CompositePropertySource
;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
org.springframework.core.env.Environment
;
...
...
@@ -659,8 +661,11 @@ class ConfigurationClassParser {
return
new
SourceClass
(
Object
.
class
);
}
try
{
// Sanity test that we can read annotations, if not fall back to ASM
classType
.
getAnnotations
();
// Sanity test that we can reflectively read annotations,
// including Class attributes; if not -> fall back to ASM
for
(
Annotation
ann
:
classType
.
getAnnotations
())
{
AnnotationUtils
.
validateAnnotation
(
ann
);
}
return
new
SourceClass
(
classType
);
}
catch
(
Throwable
ex
)
{
...
...
spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java
浏览文件 @
50e980c0
...
...
@@ -60,10 +60,9 @@ import org.springframework.util.MultiValueMap;
* individual method for details on which search algorithm is used.
*
* <p><strong>Get semantics</strong> are limited to searching for annotations
* that are either <em>present</em> on an {@code AnnotatedElement} (i.e.,
* declared locally or {@linkplain java.lang.annotation.Inherited inherited})
* or declared within the annotation hierarchy <em>above</em> the
* {@code AnnotatedElement}.
* that are either <em>present</em> on an {@code AnnotatedElement} (i.e. declared
* locally or {@linkplain java.lang.annotation.Inherited inherited}) or declared
* within the annotation hierarchy <em>above</em> the {@code AnnotatedElement}.
*
* <p><strong>Find semantics</strong> are much more exhaustive, providing
* <em>get semantics</em> plus support for the following:
...
...
@@ -77,14 +76,13 @@ import org.springframework.util.MultiValueMap;
* </ul>
*
* <h3>Support for {@code @Inherited}</h3>
* <p>Methods following <em>get semantics</em> will honor the contract of
* Java's {@link java.lang.annotation.Inherited @Inherited} annotation except
* that locally declared annotations (including custom composed annotations)
* will be favored over inherited annotations. In contrast, methods following
* <em>find semantics</em> will completely ignore the presence of
* {@code @Inherited} since the <em>find</em> search algorithm manually
* traverses type and method hierarchies and thereby implicitly supports
* annotation inheritance without the need for {@code @Inherited}.
* <p>Methods following <em>get semantics</em> will honor the contract of Java's
* {@link java.lang.annotation.Inherited @Inherited} annotation except that locally
* declared annotations (including custom composed annotations) will be favored over
* inherited annotations. In contrast, methods following <em>find semantics</em>
* will completely ignore the presence of {@code @Inherited} since the <em>find</em>
* search algorithm manually traverses type and method hierarchies and thereby
* implicitly supports annotation inheritance without a need for {@code @Inherited}.
*
* @author Phillip Webb
* @author Juergen Hoeller
...
...
@@ -794,7 +792,7 @@ public class AnnotatedElementUtils {
* @param annotationName the fully qualified class name of the annotation
* type to find (as an alternative to {@code annotationType})
* @param processor the processor to delegate to
* @return the result of the processor
, potentially {@code null}
* @return the result of the processor
(potentially {@code null})
*/
@Nullable
private
static
<
T
>
T
searchWithGetSemantics
(
AnnotatedElement
element
,
...
...
@@ -815,7 +813,7 @@ public class AnnotatedElementUtils {
* @param containerType the type of the container that holds repeatable
* annotations, or {@code null} if the annotation is not repeatable
* @param processor the processor to delegate to
* @return the result of the processor
, potentially {@code null}
* @return the result of the processor
(potentially {@code null})
* @since 4.3
*/
@Nullable
...
...
@@ -848,7 +846,7 @@ public class AnnotatedElementUtils {
* @param processor the processor to delegate to
* @param visited the set of annotated elements that have already been visited
* @param metaDepth the meta-depth of the annotation
* @return the result of the processor
, potentially {@code null}
* @return the result of the processor
(potentially {@code null})
*/
@Nullable
private
static
<
T
>
T
searchWithGetSemantics
(
AnnotatedElement
element
,
...
...
@@ -909,7 +907,7 @@ public class AnnotatedElementUtils {
* @param processor the processor to delegate to
* @param visited the set of annotated elements that have already been visited
* @param metaDepth the meta-depth of the annotation
* @return the result of the processor
, potentially {@code null}
* @return the result of the processor
(potentially {@code null})
* @since 4.2
*/
@Nullable
...
...
@@ -979,7 +977,7 @@ public class AnnotatedElementUtils {
* @param annotationName the fully qualified class name of the annotation
* type to find (as an alternative to {@code annotationType})
* @param processor the processor to delegate to
* @return the result of the processor
, potentially {@code null}
* @return the result of the processor
(potentially {@code null})
* @since 4.2
*/
@Nullable
...
...
@@ -1001,7 +999,7 @@ public class AnnotatedElementUtils {
* @param containerType the type of the container that holds repeatable
* annotations, or {@code null} if the annotation is not repeatable
* @param processor the processor to delegate to
* @return the result of the processor
, potentially {@code null}
* @return the result of the processor
(potentially {@code null})
* @since 4.3
*/
@Nullable
...
...
@@ -1039,7 +1037,7 @@ public class AnnotatedElementUtils {
* @param processor the processor to delegate to
* @param visited the set of annotated elements that have already been visited
* @param metaDepth the meta-depth of the annotation
* @return the result of the processor
, potentially {@code null}
* @return the result of the processor
(potentially {@code null})
* @since 4.2
*/
@Nullable
...
...
spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java
浏览文件 @
50e980c0
...
...
@@ -898,6 +898,31 @@ public abstract class AnnotationUtils {
return
(
annotationType
!=
null
&&
annotationType
.
startsWith
(
"java.lang.annotation"
));
}
/**
* Check the declared attributes of the given annotation, in particular covering
* Google App Engine's late arrival of {@code TypeNotPresentExceptionProxy} for
* {@code Class} values (instead of early {@code Class.getAnnotations() failure}.
* <p>This method not failing indicates that {@link #getAnnotationAttributes(Annotation)}
* won't failure either (when attempted later on).
* @param annotation the annotation to validate
* @throws IllegalStateException if a declared {@code Class} attribute could not be read
* @since 4.3.15
* @see Class#getAnnotations()
* @see #getAnnotationAttributes(Annotation)
*/
public
static
void
validateAnnotation
(
Annotation
annotation
)
{
for
(
Method
method
:
getAttributeMethods
(
annotation
.
annotationType
()))
{
if
(
method
.
getReturnType
()
==
Class
.
class
)
{
try
{
method
.
invoke
(
annotation
);
}
catch
(
Throwable
ex
)
{
throw
new
IllegalStateException
(
"Could not obtain annotation attribute value for "
+
method
,
ex
);
}
}
}
}
/**
* Retrieve the given annotation's attributes as a {@link Map}, preserving all
* attribute types.
...
...
@@ -1835,13 +1860,13 @@ public abstract class AnnotationUtils {
if
(
element
instanceof
Class
&&
Annotation
.
class
.
isAssignableFrom
((
Class
<?>)
element
))
{
// Meta-annotation or (default) value lookup on an annotation type
if
(
loggerToUse
.
isDebugEnabled
())
{
loggerToUse
.
debug
(
"Failed to meta-introspect annotation
["
+
element
+
"]
: "
+
ex
);
loggerToUse
.
debug
(
"Failed to meta-introspect annotation
"
+
element
+
"
: "
+
ex
);
}
}
else
{
// Direct annotation lookup on regular Class, Method, Field
if
(
loggerToUse
.
isInfoEnabled
())
{
loggerToUse
.
info
(
"Failed to introspect annotations on
["
+
element
+
"]
: "
+
ex
);
loggerToUse
.
info
(
"Failed to introspect annotations on
"
+
element
+
"
: "
+
ex
);
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录