当前位置: 移动技术网 > IT编程>开发语言>Java > 自定义注解的基本使用

自定义注解的基本使用

2020年01月03日  | 移动技术网IT编程  | 我要评论

波拉科夫斯,德云社周年庆典完整版,风山云海

0.使用自定义注解前,先了解相关元注解

java.lang.annotation 提供了四种元注解,专门注解其他的注解(在自定义注解的时候,需要使用到元注解):
   @documented – 注解是否将包含在javadoc中
   @retention – 什么时候使用该注解
   @target – 注解用于什么地方
   @inherited – 是否允许子类继承该注解

  1.)@retention – 定义该注解的生命周期
  ●   retentionpolicy.source : 在编译阶段丢弃。这些注解在编译结束之后就不再有任何意义,所以它们不会写入字节码。@override, @suppresswarnings都属于这类注解。
  ●   retentionpolicy.class : 在类加载的时候丢弃。在字节码文件的处理中有用。注解默认使用这种方式
  ●   retentionpolicy.runtime : 始终不会丢弃,运行期也保留该注解,因此可以使用反射机制读取该注解的信息。我们自定义的注解通常使用这种方式。

  2.)target – 表示该注解用于什么地方。默认值为任何元素,表示该注解用于什么地方。可用的elementtype 参数包括
  ● elementtype.constructor: 用于描述构造器
  ● elementtype.field: 成员变量、对象、属性(包括enum实例)
  ● elementtype.local_variable: 用于描述局部变量
  ● elementtype.method: 用于描述方法
  ● elementtype.package: 用于描述包
  ● elementtype.parameter: 用于描述参数
  ● elementtype.type: 用于描述类、接口(包括注解类型) 或enum声明

 3.)@documented – 一个简单的annotations 标记注解,表示是否将注解信息添加在java 文档中。

 4.)@inherited – 定义该注释和子类的关系
     @inherited 元注解是一个标记注解,@inherited 阐述了某个被标注的类型是被继承的。如果一个使用了@inherited 修饰的annotation 类型被用于一个class,则这个annotation 将被用于该class 的子类。

1.自定义注解类编写的一些规则

1. annotation 型定义为@interface, 所有的annotation 会自动继承java.lang.annotation这一接口,并且不能再去继承别的类或是接口.
  2. 参数成员只能用public 或默认(default) 这两个访问权修饰
  3. 参数成员只能用基本类型byte、short、char、int、long、float、double、boolean八种基本数据类型和string、enum、class、annotations等数据类型,以及这一些类型的数组.
  4. 要获取类方法和字段的注解信息,必须通过java的反射技术来获取 annotation 对象,因为你除此之外没有别的获取注解对象的方法
  5. 注解也可以没有定义成员,,不过这样注解就没啥用了
ps:自定义注解需要使用到元注解

2.实例

animalname.java

package com.xiaojian.basics.annotation;
/**
 * 动物种类
 */
import java.lang.annotation.documented;
import java.lang.annotation.retention;
import java.lang.annotation.target;

import static java.lang.annotation.elementtype.field;
import static java.lang.annotation.retentionpolicy.runtime;

@target(field)
@retention(runtime)
@documented
public @interface animalname {
    string value() default "";
}

animalcolor.java

package com.xiaojian.basics.annotation;

import java.lang.annotation.documented;
import java.lang.annotation.retention;
import java.lang.annotation.target;

import static java.lang.annotation.elementtype.field;
import static java.lang.annotation.retentionpolicy.runtime;
/**
 * 动物颜色
 */
@target(field)
@retention(runtime)
@documented
public @interface animalcolor {
    /**
     * 颜色枚举
     */
    public enum color{黑色,白色,灰色,杂色};

    color animalcolor() default color.白色;
}

animalmaster.java

package com.xiaojian.basics.annotation;

import java.lang.annotation.documented;
import java.lang.annotation.retention;
import java.lang.annotation.target;

import static java.lang.annotation.elementtype.field;
import static java.lang.annotation.retentionpolicy.runtime;

/**
 * 动物主人
 */

@target(field)
@retention(runtime)
@documented
public @interface animalmaster {

    int id() default -1;

    string name() default "";

    string address() default "";
}

animalinfoutil.java

package com.xiaojian.basics.annotation;

import java.lang.reflect.field;

/**
 * 返回动物信息
 */
public class animalinfoutil {

    public static void getinfo(class<?> clazz){

        stringbuilder stranimalname = new stringbuilder( "动物种类:");
        stringbuilder stranimalcolor = new stringbuilder( "动物颜色:");
        stringbuilder stranimalprovider = new stringbuilder( "主人信息:");

        field[] fields = clazz.getdeclaredfields();

        for(field field : fields){
            if(field.isannotationpresent(animalname.class)){
                animalname animalanimal = (animalname)field.getannotation(animalname.class);
                stranimalname.append(animalanimal.value());
                system.out.println(stranimalname);
            } else if(field.isannotationpresent(animalcolor.class)){
                animalcolor animalcolor = (animalcolor)field.getannotation(animalcolor.class);
                stranimalcolor.append(animalcolor.animalcolor().tostring());
                system.out.println(stranimalcolor);
            } else if(field.isannotationpresent(animalmaster.class)){
                animalmaster animalmaster = (animalmaster)field.getannotation(animalmaster.class);
                stranimalprovider.append("[编号:" + animalmaster.id() + ",主人名字:" + animalmaster.name() + ",地址:" + animalmaster.address() + "]");
                system.out.println(stranimalprovider);
            }

        }

    }
}

测试:

package com.xiaojian.basics.annotation;

/**
  * 测试类
  */
public class test {
    public static void main(string[] args) {
        animalinfoutil.getinfo(animal.class);
    }

}

返回:

动物种类:狗
动物颜色:黑色
主人信息:[编号:1,主人名字:小明,地址:浙江省杭州市xxxxxxxx]

【参考链接】

https://www.cnblogs.com/acm-bingzi/p/javaannotation.html

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网