一、自定义注解格式

分析 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:
d:\002_project\004_java_learn\annotation\out\production\annotation>javap annotation.class
compiled from "annotation.java"
public interface annotation extends java.lang.annotation.annotation {
}
2_project
d:\002_project\004_java_learn\annotation\out\production\annotation>javap annotation.class
compiled from "annotation.java"
public interface annotation extends java.lang.annotation.annotation {
}
4_java_learn\annotation\out\production\annotation>javap annotation.class compiled 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自定义注解内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!