【Java】Annotation_学习笔记

时间:2023-03-09 02:23:05
【Java】Annotation_学习笔记

Annotation

1APT

访问和处理Annotation的工具统称,即Annotation Process Tool。

2java.lang下提供的五种基本Annotation

@Override、@Deprecated、@Suppress Warning、@Safe Varargs、@FunctionalInterface

3、限定重写父类方法:@Override

举例:

public class Fruit

{

public void info()

{

System.out.println(“水果的info方法”);

}

}

class Apple extends Fruit

{

@Override

public void info()

{

System.out.println(“苹果的info方法”);

}

}

说明:@Override的作用主要是告诉编译器,检查其下的方法是否有正确重写父类的方法,以避免不必要的低级错误。

4、标记已过时:@Deprecated

class Apple

{

@Deprecated

public void info()

{

System.out.println(“Apple的info方法”);

}

}

public class DeprecatedTest

{

public static vodi main(String[] args)

{

new.Apple.info();

}

}

说明:@Deprecated用于表示某个程序元素(类、方法等)已过时,当其他程序使用这个标记的元素时,编译器将会给出警告。

5、抑制编译器警告:@SuppressWarnings

举例:

//关闭整个类的编译器警告

@SuppressWarning(value=“unchecked”)

public class SuppressWarningsTest

{

public static void main(String[] args)

{

List<String>myList=new ArrayList();

}

}

说明:上述例子中整个类都不会看到没有使用泛型的警告,使用@SuppressWarning时,需要在()里使用name=value的形式为该Annotation的成员变量设置值。

6Java7的“堆污染”警告与@SafeVarargs

7Java8的函数式接口与@FunctionalInterface

举例:

@FunctionalInterface

public Interface FunInterface

{

static void foo()

{

System.out.println(“foo类方法”);

}

default void bar()

{

System.out.println(“bar默认方法”);

}

void test();//只定义一个抽象方法

}

说明:

a、函数式接口:接口中只有一个抽象方法(可以包含多个static方法或者default方法)。

b、@FunctionalInterface的作用是确保其下的接口是函数式接口,避免低级错误。

8JDK的元Annotation

说明:java.lang.annotation包下提供了6个Meta Annotation(元Annotation),其中5个元Annotation用于修饰其他的Annotation定义。

9、使用@Retention

格式1:

//保留到运行时

@Retention(value=RetentionPolicy.RUNTIME)

public @Interface Testable()

格式2:

//直接被编译器丢弃

@Retention(RetentionPolicy.SOURCE)//只为value赋值时,可以省略value=

public @interface Testable()

说明:

A、@Retention用于修饰Annotation,决定其保留时间。

B、@Retention类型里有一个value变量,须为其指定值。

C、value变量的值只能是如下三个:

a、RetentionPolicy.CLASS:Annotation被编译器记录在class文件中,运行时,JVM不能获得Annotation的信息,这是默认值。

b、RetentionPolicy.RUNTIME:Annotation被编译器记录在class文件中,运行时,JVM可以获得Annotation的信息,通过反射获得。

c、RetentionPolicy.SOURCE:Annotation只保留在源代码中,编译器直接丢弃这种Annotation。

10、使用@Target

格式:

//指定如下代码片段只能修饰成员变量

@Target(ElementType.FIELD)//与@Retention一致,value=可以省略

public @Interface ActionListenerFor()

说明:

A、@Target用于修饰Annotation可以修饰哪些程序单元。

B、@Target类型的value成员变量只能是如下几个:

a、ElementType.ANNOTATION_TYPE:指定该Annotation只能修饰Annotation。

b、ElementType.CONSTRUCTOR:指定该Annotation只能修饰构造器。

c、ElementType.FIELD:指定该策略的Annotation只能修饰成员变量。

e、ElementType.LOCAL_VARIABLE:指定该策略的Annotation只能修饰局部变量。

f、ElementType.METHOD:指定该策略的Annotation只能修饰方法定义。

g、ElementType.PACKAGE:指定该策略的Annotation只能修饰包定义。

h、ElementType.PARAMETER:指定该策略的Annotation只能修饰参数。

j、ElementType.TYPE:指定该策略的Annotation只能修饰类、接口(包括注解类)和枚举类。

11、使用@Documented

举例:

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.METHOD)

@Documented

public @interface Testable

{

}

public class MyTest

{

@Testable

public void info()

{

System.out.println(“info method”)

}

}

使用javadoc工具生成API文档如下:

方法详细资料

info

@Testable

public void info()

说明:@Document的作用就是生成API文档是多出如上红色标记的语句。

12、使用@Inherited

说明:使用@Inherited修饰的将具有继承性。

13、自定义Annotation

格式:

public @Interface Test

{

//成员变量以无形参的方法形式来声明

String name();

}