/**
* 重复注解和类型注解
*/
public class
TestAnnotation {
// 4、
@MyAnnotation
(
"hello"
)
@MyAnnotation
(
"world"
)
public void
show
(){
}
@Test
public void
test1
()
throws
Exception {
Class<TestAnnotation> clazz = TestAnnotation.
class;
Method m1 = clazz.getMethod(
"show"
)
;
MyAnnotation
[] mas = m1.getAnnotationsByType(
MyAnnotation
.
class
)
;
for
(
MyAnnotation
myAnnotation : mas) {
System.
out
.println(myAnnotation.value())
;
}
}
}
// 如果忘了怎么写,随便找一个进去,复制一下比如@SuppressWarnings()
// 3、
@Repeatable
(
MyAnnotations
.
class
)
// 想用重复注解,需要repeatable修饰
// 1、
@Target
({
TYPE
,
FIELD
,
METHOD
,
PARAMETER
,
CONSTRUCTOR
,
LOCAL_VARIABLE
})
@Retention
(RetentionPolicy.
RUNTIME
)
// 生命周期
@
interface
MyAnnotation
{
String
value
()
default
"coco"
;
}
// 2、
@Target
({
TYPE
,
FIELD
,
METHOD
,
PARAMETER
,
CONSTRUCTOR
,
LOCAL_VARIABLE
})
@Retention
(RetentionPolicy.
RUNTIME
)
// 生命周期
@
interface
MyAnnotations
{
MyAnnotation
[]
value
()
;
}