java 之annotation
来源:互联网 发布:如何查看软件源码 编辑:程序博客网 时间:2024/05/22 00:05
1. 注解定义
package com.java.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface NotNull {String value();}
2. 应用及注解解释
package com.java.annotation;import java.lang.reflect.Field;public class ApplyMyAnnotation {@NotNull(value = "the username is not null")public String username;public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException { Class<?> cls = Class.forName("com.java.annotation.ApplyMyAnnotation"); Field field=cls.getField("username"); boolean flag=field.isAnnotationPresent(NotNull.class); if(flag){ System.out.println("属性是annotation"); NotNull notNull=field.getAnnotation(NotNull.class); System.out.println(notNull.value()); }}}
3. 其他人的test case
3.1 定义注解
package com.java.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface MyAnnotation1 { String value();}
package com.java.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface MyAnnotation2 { String description(); boolean isAnnotation();}
3.2 使用
package com.java.annotation;@MyAnnotation1("this is annotation1")public class AnnotationDemo { @MyAnnotation2(description="this is annotation2",isAnnotation=true) public void sayHello(){ System.out.println("hello world!"); }}
3.3 解释
package com.java.annotation;import java.lang.reflect.Method;import org.junit.Test;public class TestAnnotation {@Testpublic void test() throws NoSuchMethodException, SecurityException, ClassNotFoundException { Class<?> cls = Class.forName("com.java.annotation.AnnotationDemo"); boolean flag = cls.isAnnotationPresent(MyAnnotation1.class); if(flag){ System.out.println("判断类是annotation"); MyAnnotation1 annotation1 = cls.getAnnotation(MyAnnotation1.class); System.out.println(annotation1.value()); } Method method = cls.getMethod("sayHello"); flag = method.isAnnotationPresent(MyAnnotation2.class) ; if(flag){ System.out.println("判断方法也是annotation"); MyAnnotation2 annotation2 = method.getAnnotation(MyAnnotation2.class); System.out.println(annotation2.description()+"/t"+annotation2.isAnnotation()); } }}
0 0
- java学习之annotation
- Java之自定义Annotation
- java 之annotation
- JAVA之Annotation(注释)
- Java 之 Annotation
- Java之------注解 ( Annotation )
- Java之Annotation学习
- java之Annotation
- java基础之Annotation
- Java之Annotation(注解)
- Java之Annotation
- Java高新技术之Annotation(注解)
- Java 之 Annotation(注解)介绍
- Java基础之理解Annotation
- Java基础之理解Annotation
- Java基础之理解Annotation
- Java注解之Annotation详解
- Java基础之理解Annotation
- Ubuntu:LinkAggregation
- Mac环境下svn的使用
- 实现Instagram的Material Design概念设计
- 让程序员抓狂:需求是这样制定出来的
- linux 权限掩码函数 umask 介绍
- java 之annotation
- codeforces 287DIV2 E - Breaking Good(最短路)
- ubuntu14.04安装jdk
- Coursera台大机器学习课程笔记3 – 机器学习的可能性
- 南塔产业带新春提示:春天减肥新计划
- Xcode6为什么干掉pch(Precompile Prefix Header)&amp;如何添加pch文件
- Xilinx FPGA开发板 Digilent Spartan-3E 学习资料
- 基于百度地图的GPS简单定位——详细注释
- 黑马程序员——集合概述