浅谈Java自定义注解相关知识

目录一、自定义注解格式二、注解本质分析三、注解属性及类型四、注解属性类型五、注解属性赋值简化操作

一、自定义注解格式

分析 Java 中自带的 @Override 注解 , 源码如下 :

@Target(ElementType.METHOD)@Retention(RetentionPolicy.SOURCE)public @interface Override {}

注解分为两部分 :

① 元注解 ;

② public @interface 注解名称 ;

二、注解本质分析

按照 public @interface 注解名称 格式 , 写出一个注解 , 编译该注解代码生成 Annotation.class 字节码文件 ;

public @interface Annotation {}

使用 javap 命令反编译Annotation.class 字节码文件 , 查看该注解的实际代码 ;

反编译命令如下 :

javap Annotation.class

输出内容 :

D:\002_Project\004_Java_Learn\Annotation\out\production\Annotation>javap Annotation.classCompiled from "Annotation.java"public interface Annotation extends java.lang.annotation.Annotation {}

注解的本质是一个 interface 接口 , 注解接口默认继承了 java.lang.annotation.Annotation 接口 ;

public interface Annotation extends java.lang.annotation.Annotation {}

三、注解属性及类型

注解的本质是接口 , 接口中可以定义 常量 和 方法 ;

在注解中定义 接口方法 , 就是 注解的属性 ;

为注解添加属性 : 接口中的方法都是抽象方法 , 其中 public abstract 可以省略 ;

public @interface Annotation {    public abstract String path();}

注解属性使用格式 :

@注解名称(属性名称 = 属性值)

注解属性使用 : 在相关的代码上使用

 @Annotation(path = "")    Student(String name, int age){    }

四、注解属性类型

注解属性 ( 接口方法 ) 返回值类型要求 :

① 基本数据类型 : byte , short , int , long , float , double , char , boolean ;

② 字符串类型 : String ;

③ 枚举类型 : enum ;

④ 注解类型 ;

⑤ 以上类型的数组形式 ;

注解属性返回值必须是以上的类型 , 不能设置其它类型返回值 , 否则会报错 ;

注解中定义了属性 , 在使用注解时 , 需要 给 注解属性 赋值 ;

定义 注解属性 时 , 可以 使用 default 关键字 指定属性默认值 , 下面代码中 , 制定 注解属性 intValue 值类型为 int 整型 , 默认值 88 ;

int intValue() default 88;

如果 注解属性 指定了默认值 , 在使用注解时 , 可以选择 不为该属性赋值 ( 此时使用默认属性值 ) , 也可以进行赋值 ( 指定一个新的属性值 ) ;

如果 注解属性 没有指定默认值 , 则使用 注解 时 , 必须为其指定一个默认值 , 否则编译时报错 ;

数组类型 的 注解属性 赋值 时 , 使用大括号进行赋值 , 大括号内是数组元素 , 如果只有一个属性 , 可以省略大括号 ,

注解 声明示例 :

public @interface Annotation {    /**     * 字符串类型     * @return     */    String stringValue();    /**     * int 基本类型     * @return     */    int intValue() default 88;    /**     * 枚举类型     * @return     */    Number enumValue();    /**     * 注解类型     * @return     */    Annotation2 annotationValue();    /**     * 字符串数组类型     * @return     */    String[] stringArrayValue();}

枚举类 :

public enum Number {    ONE, TWO, THREE}

Annotation2 注解类 :

public @interface Annotation2 {}

注解使用示例 :

/** * 注解生成文档 * * @author hsl * @version  0.1 * @since 1.5 */public class Student {    /**     * 构造函数     * @param name 参数一     * @param age 参数二     */    @Annotation(            stringValue = "tom",            enumValue = Number.ONE,            annotationValue = @Annotation2,            stringArrayValue = {"tom", "jerry"})    Student(String name, int age){    }    @SuppressWarnings("all")    @Override    public String toString() {        return super.toString();    }}

代码分析 : 重点关注注解的使用 , 使用注解时 , 需要给 没有默认值 的 注解属性 赋值 , 格式为 注解属性名称 = 对应类型属性值 , 如果 注解属性 有默认值 , 则

@Annotation(stringValue = "tom", enumValue = Number.ONE, stringArrayValue = {"tom", "jerry"})

五、注解属性赋值简化操作

如果 注解属性 名称是 value , 并且 注解中只有 1 1 1 个属性 , 那么在使用 注解 为 注解属性 赋值时 , 可以省略注解名称 , 直接传入 注解属性值 ;

示例 : JDK 自带的 SuppressWarnings 注解 ,

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})@Retention(RetentionPolicy.SOURCE)public @interface SuppressWarnings {    String[] value();}

注解使用 : 使用 SuppressWarnings 注解时 , 直接传入 “all” 参数 , 省略了注解属性名称 ;

 @SuppressWarnings("all")    @Override    public String toString() {        return super.toString();    }

满足两个条件 , 才能使用上述简化方式 ;

到此这篇关于浅谈Java自定义注解相关知识的文章就介绍到这了,更多相关Java自定义注解内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

人之所以能,是相信能。

浅谈Java自定义注解相关知识

相关文章:

你感兴趣的文章:

标签云: