Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
ae216fbb
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,发现更多精彩内容 >>
提交
ae216fbb
编写于
5月 16, 2012
作者:
C
Chris Beams
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #63 from dukehoops/SPR-9298
* SPR-9298: Cache MethodParameter annotation lookup results
上级
39f74b23
c10d63dc
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
78 addition
and
6 deletion
+78
-6
spring-core/src/main/java/org/springframework/core/MethodParameter.java
...c/main/java/org/springframework/core/MethodParameter.java
+49
-3
spring-core/src/test/java/org/springframework/core/MethodParameterTests.java
...t/java/org/springframework/core/MethodParameterTests.java
+29
-3
未找到文件。
spring-core/src/main/java/org/springframework/core/MethodParameter.java
浏览文件 @
ae216fbb
/*
* Copyright 2002-201
1
the original author or authors.
* Copyright 2002-201
2
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.
...
...
@@ -26,6 +26,8 @@ import java.lang.reflect.Type;
import
java.lang.reflect.TypeVariable
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ConcurrentMap
;
import
org.springframework.util.Assert
;
...
...
@@ -37,11 +39,21 @@ import org.springframework.util.Assert;
* @author Juergen Hoeller
* @author Rob Harrop
* @author Andy Clement
* @author Nikita Tovstoles
* @author Chris Beams
* @since 2.0
* @see GenericCollectionTypeResolver
*/
public
class
MethodParameter
{
private
static
final
Annotation
[][]
EMPTY_ANNOTATION_MATRIX
=
new
Annotation
[
0
][
0
];
private
static
final
Annotation
[]
EMPTY_ANNOTATION_ARRAY
=
new
Annotation
[
0
];
static
final
ConcurrentMap
<
Method
,
Annotation
[][]>
methodParamAnnotationsCache
=
new
ConcurrentHashMap
<
Method
,
Annotation
[][]>();
private
final
Method
method
;
private
final
Constructor
constructor
;
...
...
@@ -280,7 +292,7 @@ public class MethodParameter {
public
Annotation
[]
getParameterAnnotations
()
{
if
(
this
.
parameterAnnotations
==
null
)
{
Annotation
[][]
annotationArray
=
(
this
.
method
!=
null
?
this
.
method
.
getParameterAnnotations
(
)
:
this
.
constructor
.
getParameterAnnotations
());
getMethodParameterAnnotations
(
this
.
method
)
:
this
.
constructor
.
getParameterAnnotations
());
if
(
this
.
parameterIndex
>=
0
&&
this
.
parameterIndex
<
annotationArray
.
length
)
{
this
.
parameterAnnotations
=
annotationArray
[
this
.
parameterIndex
];
}
...
...
@@ -439,6 +451,41 @@ public class MethodParameter {
}
}
/**
* Return the parameter annotations for the given method, retrieving cached values
* if a lookup has already been performed for this method, otherwise perform a fresh
* lookup and populate the cache with the result before returning. <strong>For
* internal use only.</strong>
* @param method the method to introspect for parameter annotations
*/
static
Annotation
[][]
getMethodParameterAnnotations
(
Method
method
)
{
Assert
.
notNull
(
method
);
Annotation
[][]
result
=
methodParamAnnotationsCache
.
get
(
method
);
if
(
result
==
null
)
{
result
=
method
.
getParameterAnnotations
();
if
(
result
.
length
==
0
)
{
result
=
EMPTY_ANNOTATION_MATRIX
;
}
else
{
for
(
int
i
=
0
;
i
<
result
.
length
;
i
++)
{
if
(
result
[
i
].
length
==
0
)
{
result
[
i
]
=
EMPTY_ANNOTATION_ARRAY
;
}
}
}
methodParamAnnotationsCache
.
put
(
method
,
result
);
}
//always return deep copy to prevent caller from modifying cache state
Annotation
[][]
resultCopy
=
new
Annotation
[
result
.
length
][
0
];
for
(
int
i
=
0
;
i
<
result
.
length
;
i
++)
{
resultCopy
[
i
]
=
result
[
i
].
clone
();
}
return
resultCopy
;
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
{
...
...
@@ -460,7 +507,6 @@ public class MethodParameter {
return
false
;
}
@Override
public
int
hashCode
()
{
int
result
=
this
.
hash
;
...
...
spring-core/src/test/java/org/springframework/core/MethodParameterTests.java
浏览文件 @
ae216fbb
/*
* Copyright 2002-201
1
the original author or authors.
* Copyright 2002-201
2
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.
...
...
@@ -16,6 +16,11 @@
package
org.springframework.core
;
import
java.lang.annotation.Annotation
;
import
java.lang.annotation.ElementType
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.RetentionPolicy
;
import
java.lang.annotation.Target
;
import
java.lang.reflect.Method
;
import
org.junit.Before
;
...
...
@@ -25,6 +30,7 @@ import static org.junit.Assert.*;
/**
* @author Arjen Poutsma
* @author Nikita Tovstoles
*/
public
class
MethodParameterTests
{
...
...
@@ -43,7 +49,6 @@ public class MethodParameterTests {
}
@Test
public
void
testEquals
()
throws
NoSuchMethodException
{
assertEquals
(
stringParameter
,
stringParameter
);
...
...
@@ -77,9 +82,30 @@ public class MethodParameterTests {
assertTrue
(
longParameter
.
hashCode
()
!=
methodParameter
.
hashCode
());
}
@Test
public
void
testGetMethodParamaterAnnotations
()
{
Method
method
=
stringParameter
.
getMethod
();
Annotation
[][]
expectedAnnotations
=
method
.
getParameterAnnotations
();
assertEquals
(
2
,
expectedAnnotations
.
length
);
assertEquals
(
DummyAnnotation
.
class
,
expectedAnnotations
[
0
][
0
].
annotationType
());
//start with empty cache
MethodParameter
.
methodParamAnnotationsCache
.
clear
();
//check correctness
assertArrayEquals
(
expectedAnnotations
,
MethodParameter
.
getMethodParameterAnnotations
(
method
));
//check that return value's been cached
assertArrayEquals
(
expectedAnnotations
,
MethodParameter
.
methodParamAnnotationsCache
.
get
(
method
));
}
public
int
method
(
String
p1
,
long
p2
)
{
public
int
method
(
@DummyAnnotation
String
p1
,
long
p2
)
{
return
42
;
}
@Target
(
ElementType
.
PARAMETER
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
public
@interface
DummyAnnotation
{
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录