(1)一个人只要自己不放弃自己,整个世界也不会放弃你.
(2)天生我才必有大用
(3)不能忍受学习之苦就一定要忍受生活之苦,这是多么痛苦而深刻的领悟.
(4)做难事必有所得
(5)精神乃真正的刀锋
(6)战胜对手有两次,第一次在内心中.
(7)编写实属不易,若喜欢或者对你有帮助记得点赞+关注或者收藏哦~
Java SE 071 Target及ElementType详解
1.通过反射读取注解信息
(1)注解中的信息已经在Class中了,我们应该如何读取出来呢?
1.1java.lang.reflect.AnnotatedElement接口
public Annotation[] getAnnotation(Class annotationType);
public Annotation[] getDeclaredAnnotations();
public Boolean isAnnotationPresent(Class annotationType);
(1)Class 、Constructor、Field、Method、Package等类别,都实现了AnnotatedElement接口
(2)既然Class 、Constructor、Field、Method、Package等类别,都实现了AnnotatedElement接口,显然它们都可以去调用AnnotatedElement里面的方法。以上各方法是定义在这个接口中的。
1.2通过反射的方式读取作用在类、方法上面所修饰的注解信息
(1)定义Annotation时必须设定RententionPolicy为RUNTIME,也就是可以在VM中读取Annotation信息
1.2.1判断某个方法上面是否存在注解
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
annotationClass指定的注解,如果存在于方法的上面,就返回一个true,这个方法主要是用来判断某个方法上面是否存在注解。
1.2.2获取一个注解对象
public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
会返回一个Annotation这样一个对象,参数里边儿是一个具体的Annotation Class对象。
如果这样一个Annotation存在的话,才会返回这个Annotation,否则返回一个空值。
1.2.3返回注解所对应的Class对象
Class<? extends Annotation> annotationType()
2.使用@Target限定注解的使用对象
(1)使用java.lang.annotation.Target可以定义其使用之时机
(2)即限定可以在什么地方使用注解:如方法、类、成员属性、变量
(3)在定义时要指定java.lang.annotation.ElementType的枚举值之一,表示这个注解可以修饰哪些目标。
package java.lang.annotation
public Enum ElementType{
TYPE,//适用class,interface,enum
FIELD,//适用field
METHOD,//适用method
PARAMETER,//适用method之上的parater
CONSTTRUCTOR,//适用constructor
LOCAL_VARIABLE,//适用局部变量
ANNOTATION_TYPE,//适用annotation型态
PACKAGE//适用package
}
2.1定义注解限定该注解只能作用于类的方法上
package com.javareview.annotation.targetannotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
/**
* 定义注解:该注解限定自己可以使用在哪些目标上面
*/
@Target(ElementType.METHOD)
public @interface XianDingAnnotation {
String value();
}
2.2使用有限定的注解
package com.javareview.annotation.targetannotation.test;
import com.javareview.annotation.targetannotation.XianDingAnnotation;
/**
* 测试设置了可以使用在哪些目标(类或方法)上的注解
* @author xiongjie
*
*/
public class XianDingAnnotationTest {
@XianDingAnnotation(value = "zhangsan")
public void doSomething() {
System.out.println("hello world");
}
}
3.使用@Documented注解将注解信息加入到Java的API文档中
(1)想要在使用者制作JavaDoc文件的同时,也一并将Annotation的讯息加入到API文件中使用
java.lang.annotation.Documented注解
3.1自定义带属性的Java API文档注解
package com.javareview.annotation.documentannotation;
import java.lang.annotation.Documented;
/**
* 使用@Documented注解将Annotation的讯息加入到API文件中
*/
@Documented
public @interface DocumentedAnnotation {
String hello();
}
3.2使用带属性的Java API文档注解
package com.javareview.annotation.documentannotation.test;
import com.javareview.annotation.documentannotation.DocumentedAnnotation;
/**
* 使用API文档注解(在生成java doc的时候,将注解信息也生成到java doc中。)
*/
public class DocumentedAnnotationTest {
@DocumentedAnnotation(hello="welcome")
public void method(){
System.out.println("helloworld");
}
}
4.使用@Inherited注解限定子类是否继承父类
(1)预设上父类中的注解Annotation类并不会被继承至子类别中。
(2)可以在定义注解时加上java.lang.annotation.Inherited注解.
(3)Inherited注解表示一个注解信息是否可以被自动继承下来,如果在注解中加上Inherited注解,就会把父类中的注解信息继承子类中来。
5.小结
(1)要知道注解的本源是什么?
(2)如何去定义注解?
(3)如何使用反射的方式去解析注解的信息?
(4)如何去解析注解下面所修饰的类、方法?
(5)重点:根据注解是否存在,注解的值到底是什么,来去调用对应的方法,或者处理对应的类。