Java元注解及代码示例
文章目录
- 简介
- 元注解
- @Documented
- @Target
- @Retention
- @Inherited
- 代码示例
- @Repeatable
- 代码示例
简介
注解是 JDK5.0 引入的一种注释机制,本文基于JDK1.8的基础上主要讲解注解的语法及使用。
元注解
@Documented
在生成JavaDoc的时候,会将该注解显示出来。
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {}
@Target
用于设定该注解适用范围。
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {/*** 返回注释可应用于某种元素类型的数组。*/ElementType[] value();
}public enum ElementType {/** 可用于类、接口、枚举 */TYPE,/** 可用于字段(包含枚举常量) */FIELD,/** 可用于方法 */METHOD,/** 可用于参数 */PARAMETER,/** 可用于构造方法 */CONSTRUCTOR,/** 可用于局部变量 */LOCAL_VARIABLE,/** 可用于注解 */ANNOTATION_TYPE,/** 可用于包上,用于记录Java文件的package信息 */PACKAGE,/*** 可用于泛型参数** @since 1.8*/TYPE_PARAMETER,/*** 用于类型声明和类型参数声明** @since 1.8*/TYPE_USE
}
@Retention
用来标识这个注解将如何保留。
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {/*** 返回注解的保留策略* @return 保留策略*/RetentionPolicy value();
}/*** 注解保留策略。* 与@Retention元注解一起使用,指定注解要保留多长时间*/
public enum RetentionPolicy {/*** 注解将被编译器丢弃,只在源码中保留。*/SOURCE,/*** 注解由编译器记录在类文件中,但在JVM将被忽略。*/CLASS,/*** 注解由编译器记录在类文件中并且在JVM在运行时保留,因此可以通过反射读取。*/RUNTIME
}
周期长度升序为:RetentionPolicy.
SOURCE
< RetentionPolicy.CLASS
< RetentionPolicy.RUNTIME
@Inherited
该注解作用于某父类时,该注解会被父类的子类继承。
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {}
代码示例
import java.lang.annotation.*;@Documented
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Properties {}
@Properties
public abstract class Parent {}
public class Child extends Parent {}
public class Run {public static void main(String[] args) throws ClassNotFoundException {Class<?> parentClass = Class.forName("demo1.Parent");System.out.println(parentClass.isAnnotationPresent(Properties.class));Class<?> childClass = Class.forName("demo1.Child");System.out.println(childClass.isAnnotationPresent(Properties.class));}
}
执行结果如下:
true
true
@Repeatable
@Repeatable用于声明当前注解是可以重复的。
@Repeatable声明注解的保留周期要小于或等于@Repeatable的周期。
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Repeatable {Class<? extends Annotation> value();
}
代码示例
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)
public @interface Roles {Role[] value();
}
@Repeatable(Roles.class)
public @interface Role {String value();
}
@Role("女儿")
@Role("妻子")
@Role("母亲")
public class People {}
public class Run {public static void main(String[] args) throws ClassNotFoundException {Class people = Class.forName("demo2.People");Role[] roles = (Role[]) people.getAnnotationsByType(Class.forName("demo2.Role"));for (Role role : roles) {System.out.println(role.value());}}
}
输出信息如下:
女儿
妻子
母亲
Java元注解及代码示例相关推荐
- SpringAOP 注解方式代码示例
SpringAOP 注解方式代码示例 package com.zf.aspect;import org.aspectj.lang.JoinPoint; import org.aspectj.lang. ...
- Java IOUtils.copy方法代码示例(亲测)
本文整理汇总了Java中org.apache.commons.io.IOUtils.copy方法的典型用法代码示例.如果您正苦于以下问题:Java IOUtils.copy方法的具体用法?Java I ...
- JAVA元注解@interface详解(@Target,@Documented,@Retention,@Inherited)
转载自 JAVA元注解@interface详解(@Target,@Documented,@Retention,@Inherited) jdk1.5起开始提供了4个元注解,用来定义自定义注解的注解,它们 ...
- java基础之----java常见异常及代码示例
java基础之----java常见异常及代码示例 参考文章: (1)java基础之----java常见异常及代码示例 (2)https://www.cnblogs.com/gunduzi/p/1203 ...
- java语音验证码_Java代码示例_Java语音验证接口 | 微米-中国领先的短信彩信接口平台服务商...
Java语音验证接口代码示例 请求 import java.util.HashMap; import java.util.Map; public class IvrDemo { /** * 语音验证接 ...
- Java时间类型转换详细代码示例
Java时间类型转换详细代码示例 一.时间类型转换代码示例 二.时间标准简介 1.UTC(世界标准时间) 2.GMT(格林尼治平时) 3.CST(北京时间) 三.SimpleDateFormat 参数 ...
- java 元注解 @Retention @Inherited @Documented
目录 一 笔记 二 使用java 元注解 @Retention @Inherited @Documented,自定义注解类@MyAnnotation6 三 使用自定义注解类@MyAnnotation6 ...
- java元注解-@Documented
java元注解-@Documented @Documented @Retention(value=RUNTIME) @Target(value=ANNOTATION_TYPE) public @int ...
- c JAVA 注解,Java元注解作用及使用
元注解是负责对其它注解进行说明的注解,自定义注解时可以使用元注解.Java 5 定义了 4 个注解,分别是 @Documented.@Target.@Retention 和 @Inherited.Ja ...
最新文章
- 1所更名、3所新设!山西4所高校获批复
- RNA-Seq入门 如何跑通一个Rna-Seq流程
- “人工智能治理公共服务平台”在2020年中关村论坛发布
- android自定义布局的使用!
- 关于idea打开项目没有目录
- php mysql 插入多条数据_雷林鹏分享:PHP MySQL 插入多条数据
- 【PAT甲级 补全前导0 vector作为函数参数】1025 PAT Ranking (25 分) Java、C++
- 前端学习(3139):react-hello-react之生命周期组件挂载过程
- 打开Visual Studio 2010,左下角显示加载工具箱内容
- 【渝粤教育】广东开放大学 计算机组成原理 形成性考核 (21)
- 面对技术难题,都是硬着头皮向前冲
- 哈尔滨平面模特,哈尔滨平面模特招聘,哈尔滨平面模特兼职,哈尔滨当平面模特,哈尔滨儿童平面模特,
- 数据挖掘技术基本任务
- 问题 C: [入门OJ]和||
- 香农编码的gui编码_香农编码
- SEO和SEM的区别是什么
- java 不变类_[探讨java深入的不变模式] java中String类的用法
- linux操作系统 第12章 网络与通信应用
- JavaScript 对象大全
- matlab线性方程组求解