一、概念
说明程序的。给计算机看的
注释:用文字描述程序的。给程序员看的
定义:注解(Annotation),也叫元数据。一种代码级别的说明。它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明,注释。
概念描述: JDK1.5之后的新特性
说明程序的
使用注解:@注解名称
JDK1.5之后的新特性
说明程序的
使用注解:@注解名称
作用分类:
1. 编写文档:通过代码里标识的注解生成文档【生成文档doc文档】
/**
* 注解JavaDoc描述
*
* @author bobo
* @version 1.0
* @since jdk1.5
*/
public class User {
private Integer id;
private String userName;
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
'}';
}
/**
* 两数相加
* @param a 第一个整数
* @param b 第二个整数
* @return 返回结果
*/
public int add(int a,int b){
return a + b;
}
}
2.代码分析:通过代码里标识的注解对代码进行分析【使用反射】
3.编译检查:通过代码里标识的注解让编译器能够实现基本的编译检查【Override】
二、 JDK中预定义的注解
@Override:检测被该注解标注的方法是否是继承自父类(接口)的
@Deprecated:该注解标注的内容,表示已过时
@SuppressWarnings:压制警告
一般传递参数all @SuppressWarnings(“all”)
• all to suppress all warnings (抑制所有警告)
• boxing to suppress warnings relative to boxing/unboxing operations(抑制装箱、拆箱操作时候的警告)
• cast to suppress warnings relative to cast operations (抑制映射相关的警告)
• dep-ann to suppress warnings relative to deprecated annotation(抑制启用注释的警告)
• deprecation to suppress warnings relative to deprecation(抑制过期方法警告)
• fallthrough to suppress warnings relative to missing breaks in switch statements(抑制确在switch中缺失breaks的警告)
• finally to suppress warnings relative to finally block that don’t return (抑制finally模块没有返回的警告)
• hiding to suppress warnings relative to locals that hide variable()
• incomplete-switch to suppress warnings relative to missing entries in a switch statement (enum case)(忽略没有完整的switch语句)
• nls to suppress warnings relative to non-nls string literals(忽略非nls格式的字符)
• null to suppress warnings relative to null analysis(忽略对null的操作)
• rawtypes to suppress warnings relative to un-specific types when using generics on class params(使用generics时忽略没有指定相应的类型)
• restriction to suppress warnings relative to usage of discouraged or forbidden references
• serial to suppress warnings relative to missing serialVersionUID field for a serializable class(忽略在serializable类中没有声明serialVersionUID变量)
• static-access to suppress warnings relative to incorrect static access(抑制不正确的静态访问方式警告)
• synthetic-access to suppress warnings relative to unoptimized access from inner classes(抑制子类没有按最优方法访问内部类的警告)
• unchecked to suppress warnings relative to unchecked operations(抑制没有进行类型检查操作的警告)
• unqualified-field-access to suppress warnings relative to field access unqualified (抑制没有权限访问的域的警告)
• unused to suppress warnings relative to unused code (抑制没被使用过的代码的警告)
三、自定义注解
格式:
元注解
public @interface 注解名称{
属性列表;
}
本质:注解本质上就是一个接口,该接口默认继承Annotation接口
public interface MyAnno extends java.lang.annotation.Annotation {}
属性:接口中的抽象方法
要求:
-
属性的返回值类型有下列取值
基本数据类型
String
枚举
注解
以上类型的数组 -
定义了属性,在使用时需要给属性赋值
2.1 如果定义属性时,使用default关键字给属性默认初始化值,则使用注解时,可以不进行属性的赋值。
2.2 如果只有一个属性需要赋值,并且属性的名称是value,则value可以省略,直接定义值即可。
2.3 数组赋值时,值使用{}包裹。如果数组中只有一个值,则{}可以省略
四、元注解:用于描述注解的注解
@Target:描述注解能够作用的位置
ElementType取值:
TYPE:可以作用于类上
METHOD:可以作用于方法上
FIELD:可以作用于成员变量上
@Retention:描述注解被保留的阶段
@Retention(RetentionPolicy.RUNTIME):当前被描述的注解,会保留到class字节码文件中,并被JVM读取到
@Documented:描述注解是否被抽取到api文档中
@Inherited:描述注解是否被子类继承
五、自定义注解案例实现
/**
* 自定义注解
* 该注解表面要执行哪个类中的哪个方法
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface InvokAnno {
String className();
String methodName();
}
public class Student1 {
public void show(){
System.out.println("student1 show ....");
}
}
public class Student2 {
public void show(){
System.out.println("student2 show ....");
}
}
package com.gupao.edu.anno2;
import java.lang.reflect.Method;
@InvokAnno(className = "com.gupao.edu.anno2.Student2",methodName = "show")
public class MyMain {
public static void main(String[] args) throws Exception {
// 获取类对象
Class<MyMain> clazz = MyMain.class;
// 获取类对象中的注解
InvokAnno an = clazz.getAnnotation(InvokAnno.class);
/**
* 注解本质是 接口 获取到的其实是接口的实现
* public class MyInvokAnno implements InvokAnno{
*
* String className(){
* return "com.gupao.edu.anno2.Student1";
* }
* String methodName(){
* return "show";
* }
* }
*/
// 获取注解中对应的属性
String className = an.className();
String methodName = an.methodName();
System.out.println(className);
System.out.println(methodName);
// 通过反射的方式实现接口的功能
Class<?> aClass = Class.forName(className);
Method show = aClass.getDeclaredMethod("show");
// 方法的执行
Object o = aClass.newInstance();
show.invoke(o); // 执行对应的方法
}
}
效果