當一個註解類型被定義爲運行時註解後,該註解纔是運行時能夠見,當class文件被裝載時被保存在class文件中的註解纔會被Java虛擬機所讀取。
java
要把@Retention註解的value成員變量的值設爲RetentionPolicy.RUNTIMEapp
咱們已知全部的註解都是繼承的java.lang.Annotation接口,也就是說Annotation是全部接口的父接口。除此以外,在java.lang.reflect包下的新增了AnnotatedElement接口,該接口表明程序中能夠接受註解的程序元素,ide
Class:類定義測試
Constructor:構造方法定義ui
Field:類的成員變量定義spa
Method:類的方法定義code
Package:類的包定義對象
getAnnotation()方法:用於返回該程序元素上存在的、指定類型的註解,若是該類型的註解不存在,則返回nullblog
getAnnotations():用來返回該程序元素上存在的全部註解。繼承
isAnnotationPresent():用來判斷該程序元素上是否包含指定類型的註解,若是存在返回true,不然返回false.
1
2
3
4
5
6
7
8
|
@Target
({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD,
ElementType.PARAMETER, ElementType.CONSTRUCTOR,
ElementType.LOCAL_VARIABLE })
@Retention
(RetentionPolicy.RUNTIME)
public
@interface
MyAnnotation {
String name()
default
"Jack"
;
int
age()
default
20
;
}
|
1
2
3
4
5
6
7
8
|
public
class
Person {
private
String name;
private
int
age;
@MyAnnotation
public
void
setInfo(){
}
}
|
1
2
3
4
5
6
7
8
9
10
|
public
class
AnnotationTest {
public
static
void
main(String[] args)
throws
ClassNotFoundException, NoSuchMethodException, SecurityException {
Class<Person> personClass = (Class<Person>) Class.forName(
"chapter10_04.Person"
);
Method method = personClass.getMethod(
"setInfo"
);
Annotation[] annotations = method.getAnnotations();
for
(Annotation annotation : annotations) {
System.out.println(annotation);
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public
class
AnnotationTest {
public
static
void
main(String[] args)
throws
ClassNotFoundException, NoSuchMethodException, SecurityException {
Class<Person> personClass = (Class<Person>) Class.forName(
"chapter10_04.Person"
);
Method method = personClass.getMethod(
"setInfo"
);
Annotation[] annotations = method.getAnnotations();
for
(Annotation annotation : annotations) {
//若是類型是MyAnnotation
if
(annotation
instanceof
MyAnnotation) {
System.out.println(annotation);
//強制類型轉換
MyAnnotation myAnnotation = (MyAnnotation) annotation;
//輸出值
System.out.println(
"myAnnotation.name:"
+myAnnotation.name());
System.out.println(
"myAnnotation.age:"
+myAnnotation.age());
}
}
}
}
|
@Inherited : 在您定義註解後並使用於程序代碼上時,預設上父類別中的註解並不會被繼承至子類別中,您能夠在定義註解時加上java.lang.annotation.Inherited 限定的Annotation,這讓您定義的Annotation型別被繼承下來。注意註解繼承只針對class 級別註解有效。
1
2
3
4
5
6
7
8
9
|
@Target
({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD,
ElementType.PARAMETER, ElementType.CONSTRUCTOR,
ElementType.LOCAL_VARIABLE })
@Retention
(RetentionPolicy.RUNTIME)
@Inherited
public
@interface
MyAnnotation {
String name()
default
"Jack"
;
int
age()
default
20
;
}
|
1
2
3
4
|
@MyAnnotation
public
class
Fruit {
}
|
1
2
3
|
public
class
Apple
extends
Fruit{
}
|
1
2
3
4
5
6
7
8
9
|
public
class
AnnotationTest2 {
public
static
void
main(String[] args)
throws
ClassNotFoundException, NoSuchMethodException, SecurityException {
Class<Apple> appleClass = (Class<Apple>) Class.forName(
"chapter10_04.Apple"
);
Annotation[] annotations = appleClass.getAnnotations();
for
(Annotation annotation : annotations) {
System.out.println(annotation);
}
}
}
|