Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
06144182
D
dragonwell8_jdk
项目概览
openanolis
/
dragonwell8_jdk
通知
4
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell8_jdk
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
06144182
编写于
4月 22, 2014
作者:
D
darcy
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
8035781: Improve equality for annotations
Reviewed-by: jfranck, abuckley, ahgross, dmeetry
上级
e87b4d05
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
104 addition
and
9 deletion
+104
-9
src/share/classes/sun/reflect/annotation/AnnotationInvocationHandler.java
...s/sun/reflect/annotation/AnnotationInvocationHandler.java
+104
-9
未找到文件。
src/share/classes/sun/reflect/annotation/AnnotationInvocationHandler.java
浏览文件 @
06144182
/*
/*
* Copyright (c) 2003, 201
3
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 201
4
, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
*
* This code is free software; you can redistribute it and/or modify it
* This code is free software; you can redistribute it and/or modify it
...
@@ -29,7 +29,6 @@ import java.lang.annotation.*;
...
@@ -29,7 +29,6 @@ import java.lang.annotation.*;
import
java.lang.reflect.*
;
import
java.lang.reflect.*
;
import
java.io.Serializable
;
import
java.io.Serializable
;
import
java.util.*
;
import
java.util.*
;
import
java.lang.annotation.*
;
import
java.security.AccessController
;
import
java.security.AccessController
;
import
java.security.PrivilegedAction
;
import
java.security.PrivilegedAction
;
...
@@ -45,6 +44,11 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
...
@@ -45,6 +44,11 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
private
final
Map
<
String
,
Object
>
memberValues
;
private
final
Map
<
String
,
Object
>
memberValues
;
AnnotationInvocationHandler
(
Class
<?
extends
Annotation
>
type
,
Map
<
String
,
Object
>
memberValues
)
{
AnnotationInvocationHandler
(
Class
<?
extends
Annotation
>
type
,
Map
<
String
,
Object
>
memberValues
)
{
Class
<?>[]
superInterfaces
=
type
.
getInterfaces
();
if
(!
type
.
isAnnotation
()
||
superInterfaces
.
length
!=
1
||
superInterfaces
[
0
]
!=
java
.
lang
.
annotation
.
Annotation
.
class
)
throw
new
AnnotationFormatError
(
"Attempt to create proxy for a non-annotation type."
);
this
.
type
=
type
;
this
.
type
=
type
;
this
.
memberValues
=
memberValues
;
this
.
memberValues
=
memberValues
;
}
}
...
@@ -57,13 +61,17 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
...
@@ -57,13 +61,17 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
if
(
member
.
equals
(
"equals"
)
&&
paramTypes
.
length
==
1
&&
if
(
member
.
equals
(
"equals"
)
&&
paramTypes
.
length
==
1
&&
paramTypes
[
0
]
==
Object
.
class
)
paramTypes
[
0
]
==
Object
.
class
)
return
equalsImpl
(
args
[
0
]);
return
equalsImpl
(
args
[
0
]);
assert
paramTypes
.
length
==
0
;
if
(
paramTypes
.
length
!=
0
)
if
(
member
.
equals
(
"toString"
))
throw
new
AssertionError
(
"Too many parameters for an annotation method"
);
switch
(
member
)
{
case
"toString"
:
return
toStringImpl
();
return
toStringImpl
();
if
(
member
.
equals
(
"hashCode"
))
case
"hashCode"
:
return
hashCodeImpl
();
return
hashCodeImpl
();
if
(
member
.
equals
(
"annotationType"
))
case
"annotationType"
:
return
type
;
return
type
;
}
// Handle annotation member accessors
// Handle annotation member accessors
Object
result
=
memberValues
.
get
(
member
);
Object
result
=
memberValues
.
get
(
member
);
...
@@ -129,7 +137,7 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
...
@@ -129,7 +137,7 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
* Implementation of dynamicProxy.toString()
* Implementation of dynamicProxy.toString()
*/
*/
private
String
toStringImpl
()
{
private
String
toStringImpl
()
{
StringBu
ffer
result
=
new
StringBuff
er
(
128
);
StringBu
ilder
result
=
new
StringBuild
er
(
128
);
result
.
append
(
'@'
);
result
.
append
(
'@'
);
result
.
append
(
type
.
getName
());
result
.
append
(
type
.
getName
());
result
.
append
(
'('
);
result
.
append
(
'('
);
...
@@ -277,6 +285,7 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
...
@@ -277,6 +285,7 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
new
PrivilegedAction
<
Method
[]>()
{
new
PrivilegedAction
<
Method
[]>()
{
public
Method
[]
run
()
{
public
Method
[]
run
()
{
final
Method
[]
mm
=
type
.
getDeclaredMethods
();
final
Method
[]
mm
=
type
.
getDeclaredMethods
();
validateAnnotationMethods
(
mm
);
AccessibleObject
.
setAccessible
(
mm
,
true
);
AccessibleObject
.
setAccessible
(
mm
,
true
);
return
mm
;
return
mm
;
}
}
...
@@ -286,6 +295,94 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
...
@@ -286,6 +295,94 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
}
}
private
transient
volatile
Method
[]
memberMethods
=
null
;
private
transient
volatile
Method
[]
memberMethods
=
null
;
/**
* Validates that a method is structurally appropriate for an
* annotation type. As of Java SE 8, annotation types cannot
* contain static methods and the declared methods of an
* annotation type must take zero arguments and there are
* restrictions on the return type.
*/
private
void
validateAnnotationMethods
(
Method
[]
memberMethods
)
{
/*
* Specification citations below are from JLS
* 9.6.1. Annotation Type Elements
*/
boolean
valid
=
true
;
for
(
Method
method
:
memberMethods
)
{
/*
* "By virtue of the AnnotationTypeElementDeclaration
* production, a method declaration in an annotation type
* declaration cannot have formal parameters, type
* parameters, or a throws clause.
*
* "By virtue of the AnnotationTypeElementModifier
* production, a method declaration in an annotation type
* declaration cannot be default or static."
*/
if
(
method
.
getModifiers
()
!=
(
Modifier
.
PUBLIC
|
Modifier
.
ABSTRACT
)
||
method
.
isDefault
()
||
method
.
getParameterCount
()
!=
0
||
method
.
getExceptionTypes
().
length
!=
0
)
{
valid
=
false
;
break
;
}
/*
* "It is a compile-time error if the return type of a
* method declared in an annotation type is not one of the
* following: a primitive type, String, Class, any
* parameterized invocation of Class, an enum type
* (section 8.9), an annotation type, or an array type
* (chapter 10) whose element type is one of the preceding
* types."
*/
Class
<?>
returnType
=
method
.
getReturnType
();
if
(
returnType
.
isArray
())
{
returnType
=
returnType
.
getComponentType
();
if
(
returnType
.
isArray
())
{
// Only single dimensional arrays
valid
=
false
;
break
;
}
}
if
(!((
returnType
.
isPrimitive
()
&&
returnType
!=
void
.
class
)
||
returnType
==
java
.
lang
.
String
.
class
||
returnType
==
java
.
lang
.
Class
.
class
||
returnType
.
isEnum
()
||
returnType
.
isAnnotation
()))
{
valid
=
false
;
break
;
}
/*
* "It is a compile-time error if any method declared in an
* annotation type has a signature that is
* override-equivalent to that of any public or protected
* method declared in class Object or in the interface
* java.lang.annotation.Annotation."
*
* The methods in Object or Annotation meeting the other
* criteria (no arguments, contrained return type, etc.)
* above are:
*
* String toString()
* int hashCode()
* Class<? extends Annotation> annotationType()
*/
String
methodName
=
method
.
getName
();
if
((
methodName
.
equals
(
"toString"
)
&&
returnType
==
java
.
lang
.
String
.
class
)
||
(
methodName
.
equals
(
"hashCode"
)
&&
returnType
==
int
.
class
)
||
(
methodName
.
equals
(
"annotationType"
)
&&
returnType
==
java
.
lang
.
Class
.
class
))
{
valid
=
false
;
break
;
}
}
if
(
valid
)
return
;
else
throw
new
AnnotationFormatError
(
"Malformed method on an annotation type"
);
}
/**
/**
* Implementation of dynamicProxy.hashCode()
* Implementation of dynamicProxy.hashCode()
*/
*/
...
@@ -330,7 +427,6 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
...
@@ -330,7 +427,6 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
throws
java
.
io
.
IOException
,
ClassNotFoundException
{
throws
java
.
io
.
IOException
,
ClassNotFoundException
{
s
.
defaultReadObject
();
s
.
defaultReadObject
();
// Check to make sure that types have not evolved incompatibly
// Check to make sure that types have not evolved incompatibly
AnnotationType
annotationType
=
null
;
AnnotationType
annotationType
=
null
;
...
@@ -343,7 +439,6 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
...
@@ -343,7 +439,6 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
Map
<
String
,
Class
<?>>
memberTypes
=
annotationType
.
memberTypes
();
Map
<
String
,
Class
<?>>
memberTypes
=
annotationType
.
memberTypes
();
// If there are annotation members without values, that
// If there are annotation members without values, that
// situation is handled by the invoke method.
// situation is handled by the invoke method.
for
(
Map
.
Entry
<
String
,
Object
>
memberValue
:
memberValues
.
entrySet
())
{
for
(
Map
.
Entry
<
String
,
Object
>
memberValue
:
memberValues
.
entrySet
())
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录