当前位置: 移动技术网 > IT编程>开发语言>Java > Java 自定义注解及利用反射读取注解的实例

Java 自定义注解及利用反射读取注解的实例

2019年07月19日  | 移动技术网IT编程  | 我要评论

一、自定义注解

元注解: 

@interface注解: 定义注解接口

@target注解: 用于约束被描述的注解的使用范围,当被描述的注解超出使用范围则编译失败。如:elementtype.method,elementtype.type;

@retention 注解:用于约束被定义注解的作用范围,作用范围有三个:

1、retentionpolicy.source:作用范围是源码,作用于java文件中,当执行javac时去除该注解。

2、retentionpolicy.class:作用范围是二进制码,就是存在于class文件中,当执行java时去除该注解。

3、retentionpolicy.runtime:作用范围为运行时,就是我们可以通过动态获取该注释。

@documented:用于指定javadoc生成api文档时显示该注释。

@inherited用于指定被描述的注释可以被其描述的类的子类继承,默认情况是不能被其子类继承。

自定义注解接口:

package com.java.annotation;

import java.lang.annotation.documented;
import java.lang.annotation.elementtype;
import java.lang.annotation.inherited;
import java.lang.annotation.retention;
import java.lang.annotation.retentionpolicy;
import java.lang.annotation.target;

@target({elementtype.method,elementtype.type})
@inherited
@documented
@retention(retentionpolicy.runtime)
public @interface annotation_my {
 
 string name() default "张三";//defalt 表示默认值
 
 string say() default "hello world";
 
 int age() default 21;
 
}

接下来我们定义一个接口:

package com.java.annotation;

@annotation_my //使用我们刚才定义的注解
public interface person {
 
 @annotation_my
 public void name();
 
 @annotation_my
 public void say();
 
 @annotation_my
 public void age();

}

接口定义好了,我们就可以写接口的实现类了(接口不能实例化)

package com.java.annotation;

@annotation_my
@suppresswarnings("unused")
public class student implements person {
 
 private string name;

 @override
 @annotation_my(name="流氓公子") //赋值给name 默认的为张三
//在定义注解时没有给定默认值时,在此处必须name赋初值
 public void name() {
  
 }


 @override
 @annotation_my(say=" hello world !")
 public void say() {
  
 }

 @override
 @annotation_my(age=20)
 public void age() {
  
 }
}

然后我们就编写一个测试类测试我们的注解

package com.java.annotation;

import java.lang.annotation.annotation;
import java.lang.reflect.field;
import java.lang.reflect.method;

public class text {
 annotation[] annotation = null;

 public static void main(string[] args) throws classnotfoundexception {
  new text().getannotation();
 }
 
 public void getannotation() throws classnotfoundexception{
  class<?> stu = class.forname("com.java.annotation.student");//静态加载类
  boolean isempty = stu.isannotationpresent(com.java.annotation.annotation_my.class);//判断stu是不是使用了我们刚才定义的注解接口if(isempty){
   annotation = stu.getannotations();//获取注解接口中的
   for(annotation a:annotation){
    annotation_my my = (annotation_my)a;//强制转换成annotation_my类型
    system.out.println(stu+":\n"+my.name()+" say: "+my.say()+" my age: "+my.age());
   }
  }
  method[] method = stu.getmethods();//
  system.out.println("method");
  for(method m:method){
   boolean ismempty = m.isannotationpresent(com.java.annotation.annotation_my.class);
   if(ismempty){
    annotation[] aa = m.getannotations();
    for(annotation a:aa){
     annotation_my an = (annotation_my)a;
     system.out.println(m+":\n"+an.name()+" say: "+an.say()+" my age: "+an.age());
    }
   }
  }
  //get fields by force
  system.out.println("get fileds by force !");
  field[] field = stu.getdeclaredfields();
  for(field f:field){
   f.setaccessible(true);
   system.out.println(f.getname());
  }
  system.out.println("get methods in interfaces !");
  class<?> interfaces[] = stu.getinterfaces();
  for(class<?> c:interfaces){
   method[] imethod = c.getmethods();
   for(method m:imethod){
    system.out.println(m.getname());
   }
  }
 }

}

以上这篇java 自定义注解及利用反射读取注解的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网