Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
def7663e
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(gitcode.net)2024年7月9日维护升级公告
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
def7663e
编写于
5月 23, 2015
作者:
S
Sam Brannen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement hashCode() for synthesized annotations
Issue: SPR-13066
上级
ae5c8285
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
114 addition
and
0 deletion
+114
-0
spring-core/src/main/java/org/springframework/core/annotation/SynthesizedAnnotationInvocationHandler.java
...re/annotation/SynthesizedAnnotationInvocationHandler.java
+76
-0
spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java
...springframework/core/annotation/AnnotationUtilsTests.java
+38
-0
未找到文件。
spring-core/src/main/java/org/springframework/core/annotation/SynthesizedAnnotationInvocationHandler.java
浏览文件 @
def7663e
...
...
@@ -20,6 +20,7 @@ import java.lang.annotation.Annotation;
import
java.lang.reflect.AnnotatedElement
;
import
java.lang.reflect.InvocationHandler
;
import
java.lang.reflect.Method
;
import
java.util.Arrays
;
import
java.util.Iterator
;
import
java.util.Map
;
...
...
@@ -70,6 +71,9 @@ class SynthesizedAnnotationInvocationHandler implements InvocationHandler {
if
(
isEqualsMethod
(
method
))
{
return
equals
(
proxy
,
args
[
0
]);
}
if
(
isHashCodeMethod
(
method
))
{
return
hashCode
(
proxy
);
}
if
(
isToStringMethod
(
method
))
{
return
toString
(
proxy
);
}
...
...
@@ -137,6 +141,12 @@ class SynthesizedAnnotationInvocationHandler implements InvocationHandler {
return
value
;
}
/**
* See {@link Annotation#equals(Object)} for a definition of the required algorithm.
*
* @param proxy the synthesized annotation
* @param other the other object to compare against
*/
private
boolean
equals
(
Object
proxy
,
Object
other
)
{
if
(
this
==
other
)
{
return
true
;
...
...
@@ -156,6 +166,72 @@ class SynthesizedAnnotationInvocationHandler implements InvocationHandler {
return
true
;
}
/**
* See {@link Annotation#hashCode()} for a definition of the required algorithm.
*
* @param proxy the synthesized annotation
*/
private
int
hashCode
(
Object
proxy
)
{
int
result
=
0
;
for
(
Method
attributeMethod
:
getAttributeMethods
(
this
.
annotationType
))
{
Object
value
=
invokeMethod
(
attributeMethod
,
proxy
);
int
hashCode
;
if
(
value
.
getClass
().
isArray
())
{
hashCode
=
hashCodeForArray
(
value
);
}
else
{
hashCode
=
value
.
hashCode
();
}
result
+=
(
127
*
attributeMethod
.
getName
().
hashCode
())
^
hashCode
;
}
return
result
;
}
/**
* WARNING: we can NOT use any of the {@code nullSafeHashCode()} methods
* in Spring's {@link ObjectUtils} because those hash code generation
* algorithms do not comply with the requirements specified in
* {@link Annotation#hashCode()}.
*
* @param array the array to compute the hash code for
*/
private
int
hashCodeForArray
(
Object
array
)
{
if
(
array
instanceof
boolean
[])
{
return
Arrays
.
hashCode
((
boolean
[])
array
);
}
if
(
array
instanceof
byte
[])
{
return
Arrays
.
hashCode
((
byte
[])
array
);
}
if
(
array
instanceof
char
[])
{
return
Arrays
.
hashCode
((
char
[])
array
);
}
if
(
array
instanceof
double
[])
{
return
Arrays
.
hashCode
((
double
[])
array
);
}
if
(
array
instanceof
float
[])
{
return
Arrays
.
hashCode
((
float
[])
array
);
}
if
(
array
instanceof
int
[])
{
return
Arrays
.
hashCode
((
int
[])
array
);
}
if
(
array
instanceof
long
[])
{
return
Arrays
.
hashCode
((
long
[])
array
);
}
if
(
array
instanceof
short
[])
{
return
Arrays
.
hashCode
((
short
[])
array
);
}
// else
return
Arrays
.
hashCode
((
Object
[])
array
);
}
/**
* See {@link Annotation#toString()} for guidelines on the recommended format.
*
* @param proxy the synthesized annotation
*/
private
String
toString
(
Object
proxy
)
{
StringBuilder
sb
=
new
StringBuilder
(
"@"
).
append
(
annotationType
.
getName
()).
append
(
"("
);
...
...
spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java
浏览文件 @
def7663e
...
...
@@ -694,6 +694,44 @@ public class AnnotationUtilsTests {
assertThat
(
webMappingWithAliases
,
is
(
not
(
synthesizedWebMapping1
)));
}
@Test
public
void
hashCodeForSynthesizedAnnotations
()
throws
Exception
{
Method
methodWithPath
=
WebController
.
class
.
getMethod
(
"handleMappedWithPathAttribute"
);
WebMapping
webMappingWithAliases
=
methodWithPath
.
getAnnotation
(
WebMapping
.
class
);
assertNotNull
(
webMappingWithAliases
);
Method
methodWithPathAndValue
=
WebController
.
class
.
getMethod
(
"handleMappedWithSamePathAndValueAttributes"
);
WebMapping
webMappingWithPathAndValue
=
methodWithPathAndValue
.
getAnnotation
(
WebMapping
.
class
);
assertNotNull
(
webMappingWithPathAndValue
);
WebMapping
synthesizedWebMapping1
=
synthesizeAnnotation
(
webMappingWithAliases
);
assertNotNull
(
synthesizedWebMapping1
);
WebMapping
synthesizedWebMapping2
=
synthesizeAnnotation
(
webMappingWithAliases
);
assertNotNull
(
synthesizedWebMapping2
);
// Equality amongst standard annotations
assertThat
(
webMappingWithAliases
.
hashCode
(),
is
(
webMappingWithAliases
.
hashCode
()));
assertThat
(
webMappingWithPathAndValue
.
hashCode
(),
is
(
webMappingWithPathAndValue
.
hashCode
()));
// Inequality amongst standard annotations
assertThat
(
webMappingWithAliases
.
hashCode
(),
is
(
not
(
webMappingWithPathAndValue
.
hashCode
())));
assertThat
(
webMappingWithPathAndValue
.
hashCode
(),
is
(
not
(
webMappingWithAliases
.
hashCode
())));
// Equality amongst synthesized annotations
assertThat
(
synthesizedWebMapping1
.
hashCode
(),
is
(
synthesizedWebMapping1
.
hashCode
()));
assertThat
(
synthesizedWebMapping2
.
hashCode
(),
is
(
synthesizedWebMapping2
.
hashCode
()));
assertThat
(
synthesizedWebMapping1
.
hashCode
(),
is
(
synthesizedWebMapping2
.
hashCode
()));
assertThat
(
synthesizedWebMapping2
.
hashCode
(),
is
(
synthesizedWebMapping1
.
hashCode
()));
// Equality between standard and synthesized annotations
assertThat
(
synthesizedWebMapping1
.
hashCode
(),
is
(
webMappingWithPathAndValue
.
hashCode
()));
assertThat
(
webMappingWithPathAndValue
.
hashCode
(),
is
(
synthesizedWebMapping1
.
hashCode
()));
// Inequality between standard and synthesized annotations
assertThat
(
synthesizedWebMapping1
.
hashCode
(),
is
(
not
(
webMappingWithAliases
.
hashCode
())));
assertThat
(
webMappingWithAliases
.
hashCode
(),
is
(
not
(
synthesizedWebMapping1
.
hashCode
())));
}
/**
* Fully reflection-based test that verifies support for
* {@linkplain AnnotationUtils#synthesizeAnnotation synthesizing annotations}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录