1.什么是注解(Annotation)?
注解就是代码里的标记,这些标记可以在编译,类加载,运行时被读取,并执行相应的处理。通过使用Annotation,程序员可以在不改变原有逻辑的情况下,在源文件中嵌入一些补充信息。注解可以向修饰符一样被使用,可用于修饰包,构造器,方法,成员变量,参数,局部变量的声明,这些信息被保存在Annotation的"name=value"对中。
2.如何自定义注解?
参照@SuppressWarnings定义
(1)注解声明为:@interface
(2)内部定义成员,通常使用value表示
(3)可以指定成员的默认值,使用default定义
(4)如果自定义注解没有成员,表明是一个标识作用
如果有成员,在使用注解时,需要指明成员的值;自定义注解必须配上注解的信息处理流程(使用反射)才有意义。
package com.yan.annotation;
import java.lang.annotation.*;
@Repeatable(MyAnnotations.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.FIELD,
ElementType.METHOD, ElementType.PARAMETER,
ElementType.CONSTRUCTOR,ElementType.LOCAL_VARIABLE,
ElementType.TYPE_PARAMETER,
ElementType.TYPE_USE})
public @interface MyAnnotation {
String value() default "hello";
}
3.JDK中提供的四种元注解:
元注解:对现有的注解进行解释说明的注解
Retention:指定所修饰的Annotation的生命周期:SOURCECLASS(默认)RUNTIME;只有声明为 RUNTIME生命周期的注解才能通过反射获取。
Target:用于指定被修饰的Annotation能被用于修饰哪些程序元素。
documented(使用频率低):用于指定被该元注解修饰的Annotation类将被javadoc工具提取为文档, 定义为documented的注解必须设置Retention的值为RUNTIME。
Inherited(频率低):被它修饰的Annotation将具有继承性。
4.可重复注解:
(1)在MyAnnotation上声明@Repeatable,成员值为MyAnnotations.class;
(2)MyAnnotation的Target和Retention和MyAnnotations相同。
package com.yan.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.FIELD,
ElementType.METHOD, ElementType.PARAMETER,
ElementType.CONSTRUCTOR,ElementType.LOCAL_VARIABLE})
public @interface MyAnnotations {
MyAnnotation[] value();
}
5.类型注解:
(1)ElementType.TYPE_PARAMETER 表示能写在类型变量的声明语句中(如:泛型声明);
(2)ElementType.TYPE_USE 表示该注解能写在使用类型的任何语句中。
6.类型注解的举例:
package com.yan.annotation;
import java.util.ArrayList;
interface Info{
void show();
}
public class Student implements Info{
@Override
public void show() {
System.out.println("哈哈哈");
}
}
class Generic<@MyAnnotation T>{
public void show() throws @MyAnnotation RuntimeException{
ArrayList<@MyAnnotation String> list = new ArrayList<>();
int num = (@MyAnnotation int) 10L;
}
}
7.使用反射来获取注解的举例(推荐掌握):
public void test9(){
Class clazz = Person.class;
Method[] declaredMethods = clazz.getDeclaredMethods();
for(Method m : declaredMethods){
//1.获取方法声明的注解
Annotation[] annotation = m.getAnnotations();
for(Annotation a : annotation){
System.out.println(a);
}
}
8.用于反射举例的Person类:
package com.yan.reflection; @MyAnnotation(value = "hi") public class Person extends Creatureimplements Comparable , MyInterface{ private String name; public int age; public int id; public Person() { } public Person(String name, int age) { this.name = name; this.age = age; } @MyAnnotation(value = "la") private Person(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void show(){ System.out.println("你好,我是一个人"); } @MyAnnotation private String showNation(String nation){ System.out.println("我的国籍是:"+nation); return nation; } @MyAnnotation(value = "oye") public String display(String interests)throws NullPointerException, ClassCastException{ return interests; } @Override public String toString() { return "Person{" + "name='" + name + ''' + ", age=" + age + '}'; } @Override public int compareTo(String o) { return 0; } @Override public void info() { System.out.println("我是人"); } private static void showDesc(){ System.out.println("我是一个帅气的男人"); } }
9.用于反射举例的注解:
package com.yan.reflection;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
@Target({TYPE, FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() default "hello";
}



