Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_langtools
提交
a0704575
D
dragonwell8_langtools
项目概览
openanolis
/
dragonwell8_langtools
通知
0
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell8_langtools
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
a0704575
编写于
2月 03, 2010
作者:
J
jjg
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
6921979: add test program to verify annotations are attached to nodes as expected
Reviewed-by: darcy
上级
8b6b2c70
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
451 addition
and
0 deletion
+451
-0
test/tools/javac/treeannotests/AnnoTreeTests.java
test/tools/javac/treeannotests/AnnoTreeTests.java
+44
-0
test/tools/javac/treeannotests/DA.java
test/tools/javac/treeannotests/DA.java
+37
-0
test/tools/javac/treeannotests/TA.java
test/tools/javac/treeannotests/TA.java
+36
-0
test/tools/javac/treeannotests/Test.java
test/tools/javac/treeannotests/Test.java
+32
-0
test/tools/javac/treeannotests/TestProcessor.java
test/tools/javac/treeannotests/TestProcessor.java
+302
-0
未找到文件。
test/tools/javac/treeannotests/AnnoTreeTests.java
0 → 100644
浏览文件 @
a0704575
/*
* Copyright 2010 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/*
* @test
* @build DA TA Test TestProcessor
* @compile -proc:only -processor TestProcessor AnnoTreeTests.java
*/
@Test
(
6
)
class
AnnoTreeTests
{
// primitive types
@DA
(
"int"
)
int
i1
;
int
i2
=
(
@TA
(
"int"
)
int
)
0
;
// simple array types
@DA
(
"int[]"
)
int
[]
a1
;
int
@TA
(
"int"
)
[]
a2
;
int
[]
a3
=
(
@TA
(
"int[]"
)
int
[])
a1
;
int
[]
a4
=
(
int
@TA
(
"int"
)
[])
a1
;
// multi-dimensional array types
// (still to come)
}
test/tools/javac/treeannotests/DA.java
0 → 100644
浏览文件 @
a0704575
/*
* Copyright 2010 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
import
java.lang.annotation.*
;
import
static
java
.
lang
.
annotation
.
ElementType
.*;
/**
* Annotation used by TestProcessor to indicate the expected type
* of the declaration to which the annotation is attached.
* These annotations are expected to be found in the modifiers
* field on ClassDef, MethodDef and VarDef nodes.
*/
@Target
({
FIELD
,
LOCAL_VARIABLE
,
METHOD
,
PACKAGE
,
PARAMETER
,
TYPE
})
@interface
DA
{
String
value
();
}
test/tools/javac/treeannotests/TA.java
0 → 100644
浏览文件 @
a0704575
/*
* Copyright 2010 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
import
java.lang.annotation.*
;
import
static
java
.
lang
.
annotation
.
ElementType
.*;
/**
* Annotation used by TestProcessor to indicate the expected type
* to which the annotation is attached.
* These annotations are expected to be found in AnnotatedType nodes.
*/
@Target
({
TYPE_USE
,
TYPE_PARAMETER
})
@interface
TA
{
String
value
();
}
test/tools/javac/treeannotests/Test.java
0 → 100644
浏览文件 @
a0704575
/*
* Copyright 2010 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/**
* Annotation used by TestProcessor to indicate the expected number of
* @DA and @TA annotations in the source tree to which the Test annotation
* is attached.
*/
@interface
Test
{
int
value
();
}
test/tools/javac/treeannotests/TestProcessor.java
0 → 100644
浏览文件 @
a0704575
/*
* Copyright 2010 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
import
java.io.*
;
import
java.util.*
;
import
javax.annotation.processing.*
;
import
javax.lang.model.*
;
import
javax.lang.model.element.*
;
import
javax.lang.model.util.*
;
import
javax.tools.*
;
import
com.sun.source.util.*
;
import
com.sun.tools.javac.code.BoundKind
;
import
com.sun.tools.javac.tree.JCTree.*
;
import
com.sun.tools.javac.tree.TreeScanner
;
import
com.sun.tools.javac.tree.*
;
import
com.sun.tools.javac.util.List
;
/**
* Test processor used to check test programs using the @Test, @DA, and @TA
* annotations.
*
* The processor looks for elements annotated with @Test, and analyzes the
* syntax trees for those elements. Within such trees, the processor looks
* for the DA annotations on decls and TA annotations on types.
* The value of these annotations should be a simple string rendition of
* the tree node to which it is attached.
* The expected number of annotations is given by the parameter to the
* @Test annotation itself.
*/
@SupportedAnnotationTypes
({
"Test"
})
public
class
TestProcessor
extends
AbstractProcessor
{
public
SourceVersion
getSupportedSourceVersion
()
{
return
SourceVersion
.
latest
();
}
/** Process trees for elements annotated with the @Test(n) annotation. */
public
boolean
process
(
Set
<?
extends
TypeElement
>
annos
,
RoundEnvironment
renv
)
{
if
(
renv
.
processingOver
())
return
true
;
Elements
elements
=
processingEnv
.
getElementUtils
();
Trees
trees
=
Trees
.
instance
(
processingEnv
);
TypeElement
testAnno
=
elements
.
getTypeElement
(
"Test"
);
for
(
Element
elem:
renv
.
getElementsAnnotatedWith
(
testAnno
))
{
System
.
err
.
println
(
"ELEM: "
+
elem
);
int
count
=
getValue
(
getAnnoMirror
(
elem
,
testAnno
),
Integer
.
class
);
System
.
err
.
println
(
"count: "
+
count
);
TreePath
p
=
trees
.
getPath
(
elem
);
JavaFileObject
file
=
p
.
getCompilationUnit
().
getSourceFile
();
JCTree
tree
=
(
JCTree
)
p
.
getLeaf
();
System
.
err
.
println
(
"tree: "
+
tree
);
new
TestScanner
(
file
).
check
(
tree
,
count
);
}
return
true
;
}
/** Get the AnnotationMirror on an element for a given annotation. */
AnnotationMirror
getAnnoMirror
(
Element
e
,
TypeElement
anno
)
{
Types
types
=
processingEnv
.
getTypeUtils
();
for
(
AnnotationMirror
m:
e
.
getAnnotationMirrors
())
{
if
(
types
.
isSameType
(
m
.
getAnnotationType
(),
anno
.
asType
()))
return
m
;
}
return
null
;
}
/** Get the value of the value element of an annotation mirror. */
<
T
>
T
getValue
(
AnnotationMirror
m
,
Class
<
T
>
type
)
{
for
(
Map
.
Entry
<?
extends
ExecutableElement
,?
extends
AnnotationValue
>
e:
m
.
getElementValues
().
entrySet
())
{
ExecutableElement
ee
=
e
.
getKey
();
if
(
ee
.
getSimpleName
().
contentEquals
(
"value"
))
{
AnnotationValue
av
=
e
.
getValue
();
return
type
.
cast
(
av
.
getValue
());
}
}
return
null
;
}
/** Report an error to the annotation processing system. */
void
error
(
String
msg
)
{
Messager
messager
=
processingEnv
.
getMessager
();
messager
.
printMessage
(
Diagnostic
.
Kind
.
ERROR
,
msg
);
}
/** Report an error to the annotation processing system. */
void
error
(
JavaFileObject
file
,
JCTree
tree
,
String
msg
)
{
// need better API for reporting tree position errors to the messager
Messager
messager
=
processingEnv
.
getMessager
();
String
text
=
file
.
getName
()
+
":"
+
getLine
(
file
,
tree
)
+
": "
+
msg
;
messager
.
printMessage
(
Diagnostic
.
Kind
.
ERROR
,
text
);
}
/** Get the line number for the primary position for a tree.
* The code is intended to be simple, although not necessarily efficient.
* However, note that a file manager such as JavacFileManager is likely
* to cache the results of file.getCharContent, avoiding the need to read
* the bits from disk each time this method is called.
*/
int
getLine
(
JavaFileObject
file
,
JCTree
tree
)
{
try
{
CharSequence
cs
=
file
.
getCharContent
(
true
);
int
line
=
1
;
for
(
int
i
=
0
;
i
<
tree
.
pos
;
i
++)
{
if
(
cs
.
charAt
(
i
)
==
'\n'
)
// jtreg tests always use Unix line endings
line
++;
}
return
line
;
}
catch
(
IOException
e
)
{
return
-
1
;
}
}
/** Scan a tree, looking for @DA and @TA annotations, and verifying that such
* annotations are attached to the expected tree node matching the string
* parameter of the annotation.
*/
class
TestScanner
extends
TreeScanner
{
/** Create a scanner for a given file. */
TestScanner
(
JavaFileObject
file
)
{
this
.
file
=
file
;
}
/** Check the annotations in a given tree. */
void
check
(
JCTree
tree
,
int
expectCount
)
{
foundCount
=
0
;
scan
(
tree
);
if
(
foundCount
!=
expectCount
)
error
(
file
,
tree
,
"Wrong number of annotations found: "
+
foundCount
+
", expected: "
+
expectCount
);
}
/** Check @DA annotations on a class declaration. */
@Override
public
void
visitClassDef
(
JCClassDecl
tree
)
{
super
.
visitClassDef
(
tree
);
check
(
tree
.
mods
.
annotations
,
"DA"
,
tree
);
}
/** Check @DA annotations on a method declaration. */
@Override
public
void
visitMethodDef
(
JCMethodDecl
tree
)
{
super
.
visitMethodDef
(
tree
);
check
(
tree
.
mods
.
annotations
,
"DA"
,
tree
);
}
/** Check @DA annotations on a field, parameter or local variable declaration. */
@Override
public
void
visitVarDef
(
JCVariableDecl
tree
)
{
super
.
visitVarDef
(
tree
);
check
(
tree
.
mods
.
annotations
,
"DA"
,
tree
);
}
/** Check @TA annotations on a type. */
public
void
visitAnnotatedType
(
JCAnnotatedType
tree
)
{
super
.
visitAnnotatedType
(
tree
);
check
(
tree
.
annotations
,
"TA"
,
tree
);
}
/** Check to see if a list of annotations contains a named annotation, and
* if so, verify the annotation is expected by comparing the value of the
* annotation's argument against the string rendition of the reference tree
* node.
* @param annos the list of annotations to be checked
* @param name the name of the annotation to be checked
* @param tree the tree against which to compare the annotations's argument
*/
void
check
(
List
<?
extends
JCAnnotation
>
annos
,
String
name
,
JCTree
tree
)
{
for
(
List
<?
extends
JCAnnotation
>
l
=
annos
;
l
.
nonEmpty
();
l
=
l
.
tail
)
{
JCAnnotation
anno
=
l
.
head
;
if
(
anno
.
annotationType
.
toString
().
equals
(
name
)
&&
(
anno
.
args
.
size
()
==
1
))
{
String
expect
=
getStringValue
(
anno
.
args
.
head
);
foundCount
++;
System
.
err
.
println
(
"found: "
+
name
+
" "
+
expect
);
String
found
=
new
TypePrinter
().
print
(
tree
);
if
(!
found
.
equals
(
expect
))
error
(
file
,
anno
,
"Unexpected result: expected: \""
+
expect
+
"\", found: \""
+
found
+
"\""
);
}
}
}
/** Get the string value of an annotation argument, which is given by the
* expression <i>name</i>=<i>value</i>.
*/
String
getStringValue
(
JCExpression
e
)
{
if
(
e
.
getTag
()
==
JCTree
.
ASSIGN
)
{
JCAssign
a
=
(
JCAssign
)
e
;
JCExpression
rhs
=
a
.
rhs
;
if
(
rhs
.
getTag
()
==
JCTree
.
LITERAL
)
{
JCLiteral
l
=
(
JCLiteral
)
rhs
;
return
(
String
)
l
.
value
;
}
}
throw
new
IllegalArgumentException
(
e
.
toString
());
}
/** The file for the tree. Used to locate errors. */
JavaFileObject
file
;
/** The number of annotations that have been found. @see #check */
int
foundCount
;
}
/** Convert a type or decl tree to a reference string used by the @DA and @TA annotations. */
class
TypePrinter
extends
Visitor
{
/** Convert a type or decl tree to a string. */
String
print
(
JCTree
tree
)
{
if
(
tree
==
null
)
return
null
;
tree
.
accept
(
this
);
return
result
;
}
String
print
(
List
<?
extends
JCTree
>
list
)
{
return
print
(
list
,
", "
);
}
String
print
(
List
<?
extends
JCTree
>
list
,
String
sep
)
{
StringBuilder
sb
=
new
StringBuilder
();
if
(
list
.
nonEmpty
())
{
sb
.
append
(
print
(
list
.
head
));
for
(
List
<?
extends
JCTree
>
l
=
list
.
tail
;
l
.
nonEmpty
();
l
=
l
.
tail
)
{
sb
.
append
(
sep
);
sb
.
append
(
print
(
l
.
head
));
}
}
return
sb
.
toString
();
}
@Override
public
void
visitClassDef
(
JCClassDecl
tree
)
{
result
=
tree
.
name
.
toString
();
}
@Override
public
void
visitMethodDef
(
JCMethodDecl
tree
)
{
result
=
tree
.
name
.
toString
();
}
@Override
public
void
visitVarDef
(
JCVariableDecl
tree
)
{
tree
.
vartype
.
accept
(
this
);
}
@Override
public
void
visitAnnotatedType
(
JCAnnotatedType
tree
)
{
tree
.
underlyingType
.
accept
(
this
);
}
@Override
public
void
visitTypeIdent
(
JCPrimitiveTypeTree
tree
)
{
result
=
tree
.
toString
();
}
@Override
public
void
visitTypeArray
(
JCArrayTypeTree
tree
)
{
result
=
print
(
tree
.
elemtype
)
+
"[]"
;
}
@Override
public
void
visitTypeApply
(
JCTypeApply
tree
)
{
result
=
print
(
tree
.
clazz
)
+
"<"
+
print
(
tree
.
arguments
)
+
">"
;
}
@Override
public
void
visitTypeParameter
(
JCTypeParameter
tree
)
{
if
(
tree
.
bounds
.
isEmpty
())
result
=
tree
.
name
.
toString
();
else
result
=
tree
.
name
+
" extends "
+
print
(
tree
.
bounds
,
"&"
);
}
@Override
public
void
visitWildcard
(
JCWildcard
tree
)
{
if
(
tree
.
kind
.
kind
==
BoundKind
.
UNBOUND
)
result
=
tree
.
kind
.
toString
();
else
result
=
tree
.
kind
+
" "
+
print
(
tree
.
inner
);
}
private
String
result
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录