注释修饰符

自定义注释

元注释

用来注释自定义注释的注释

@Target 限定注释允许加载的目标
@Target(ElementType.METHOD) 只能用于方法
@Target({ElementType.FIELD, ElementType.TYPE}) 可以用于字段和类型 ANNOTATION_TYPE 注解其他的注释
CONSTRUCTOR 注解构造函数
FIELD 注解字段和枚举常量
LOCAL_VARIABLE 注解局部变量
PACKAGE
METHOD 注解方法
PARAMETER 注解方法参数
TYPE 注解class,interface,enum
TYPE_PARAMETER 注解泛型参数
TYPE_USE 注解强转类型 @Retention 设置注解在程序runtime时期能否被反射访问到
@Retention(RetentionPolicy.RUNTIME) 运行反射访问 CLASS 值运行在class文件中访问,而不是runtime
RUNTIME
SOURCE 能够在compiletime配访问到,但是不会添加到class文件中 @Documented 让文档生成器能够识别 @Inherited 这个注释用来改变注释查询方式,从而只用来检查父类,直到发现目标 @Repeatable 多个注释实例能够被附加到注释目标

通过反射在runtime访问注释

声明注释
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface MyAnnotation {
String param1() default "someValue";
boolean param2() default true;
int[] param3() default {};
} 使用注释
@MyAnnotation
public static void Test() throws Exception {
Annotation[] annotationsByType = MyAnnotation.class.getAnnotations();
System.out.println("----------" + Arrays.toString(annotationsByType));
}

内置注释

@Override                           重写父类方法
@Deprecated 废弃api
@SuppressWarnings("unchecked") 取消系统的某些警告信息,尽量少用
@SafeVarargs 抑制可变参数警告
@SafeVarargs
public static<T> void Test(T... lists) throws Exception {
}
@FunctionalInterface 用来声明函数式接口,也就是只能包含一个方法
@FunctionalInterface
public interface Test<T> {
boolean test(T t);
}

多注释实例

错误写法

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MyAnnotation1 {
String param();
} @Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2 {
String param() default "someValue";
} 报错
@MyAnnotation1(param="someValue")
@MyAnnotation1(param="someValue")

使用容器改写

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MyAnnotation1 {
String param();
} @Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2 {
MyAnnotation1[] param();
} @MyAnnotation2(param={
@MyAnnotation1(param="someValue1"),
@MyAnnotation1(param="someValue2"),
}) 获取注释的值
MyAnnotation1[] MyAnnotations = TestController.class.getAnnotation(MyAnnotation2.class).param(); for (MyAnnotation1 item : MyAnnotations) {
System.out.println(item.param());
}

使用@Repeatable元注释

@Repeatable(MyAnnotation2.class)
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation1 {
String value();
} @Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2 {
MyAnnotation1[] value();
} @MyAnnotation1(value="someValue1")
@MyAnnotation1(value="someValue2") 获取注释值 MyAnnotation1[] MyAnnotations = TestController.class.getAnnotationsByType(MyAnnotation1.class);
for (MyAnnotation1 item : MyAnnotations) {
System.out.println(item.value());
}

注释继承

添加了@Inherited,继承的子类可以访问父类的注释

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation1 {
String value();
} @MyAnnotation1(value="someValue1")
class Father { } public class Test extends Father{
MyAnnotation1[] MyAnnotations = Test.class.getAnnotationsByType(MyAnnotation1.class);
for (MyAnnotation1 item : MyAnnotations) {
System.out.println(item.value());
}
}

使用反射获取注释

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation1 {
String value();
}

获取类的注释

@MyAnnotation1(value="someValue1")
public class Test{} MyAnnotation1 annotation = Test.class.getAnnotation(MyAnnotation1.class);
System.out.println(annotation.value());

获取方法的注释

@MyAnnotation1(value="someValue1")
public Object Test() throws Exception {} Method method = TestController.class.getMethod("Test", null);
MyAnnotation1 annotation = (MyAnnotation1)method.getAnnotation(MyAnnotation1.class);
System.out.println(annotation.value());

结语

本文章是java成神的系列文章之一

如果你想知道,但是本文没有的,请下方留言

我会第一时间总结出来并发布填充到本文

java成神之——注释修饰符的更多相关文章

  1. java成神之——ImmutableClass,null检查,字符编码,defaultLogger,可变参数,JavaScriptEngine,2D图,类单例,克隆,修饰符基本操作

    ImmutableClass null检查 字符编码 default logger 函数可变参数 Nashorn JavaScript engine 执行脚本文件 改变js文件输出流 全局变量 2D图 ...

  2. java成神之——接口,泛型,类

    接口 接口定义 默认方法 函数式接口 泛型 泛型类 泛型类继承 类型限定 泛型方法 泛型接口 类 构造函数 类的继承 抽象类 instanceof运算符 内部类 equals 结语 接口 接口定义 j ...

  3. JAVA基础语法——标识符、修饰符、关键字(个人整理总结)

    JAVA基础语法——标识符.修饰符.关键字 一 . 标识符 1.1    什么是标识符 就是程序员在定义java程序时,自定义的一些名字,例如helloworld 程序里关键字class 后跟的Dem ...

  4. java成神之——正则表达式基本使用

    正则表达式 常用匹配规则 基本使用 标记符的使用 部分正则标记 正则表达式在字符串方法中的使用 结语 正则表达式 常用匹配规则 [abc] abc其中一个 [^abc] abc之外的一个 [a-z] ...

  5. java成神之——java中string的用法

    java中String的用法 String基本用法 String分割 String拼接 String截取 String换行符和format格式化 String反转字符串和去除空白字符 String获取 ...

  6. java成神之——java常识

    java常识 简单DOS命令 java基础名词 java编译和运行流程 Eclipse常用快捷键 Eclipse相关操作 java基本数据类型 java基本数据类型转换 java常用运算符 java流 ...

  7. Java入门 - 语言基础 - 07.修饰符

    原文地址:http://www.work100.net/training/java-modifier-type.html 更多教程:光束云 - 免费课程 修饰符 序号 文内章节 视频 1 概述 2 访 ...

  8. 转载_2016,Java成神初年

    原文地址:http://blog.csdn.net/chenssy/article/details/54017826 2016,Java成神初年.. -------------- 时间2016.12. ...

  9. Java成神路上之设计模式系列教程之一

    Java成神路上之设计模式系列教程之一 千锋-Feri 在Java工程师的日常中,是否遇到过如下问题: Java 中什么叫单例设计模式?请用Java 写出线程安全的单例模式? 什么是设计模式?你是否在 ...

随机推荐

  1. PhpStorm怎么用,PhpStorm常用快捷键教程

    设置快捷键:File -> Settings -> IDE Settings -> Keymap -> 选择“Eclipse” -> 然后“Copy”一份 ->再个 ...

  2. jmeter导入jar包后在beanshell中import失效的问题解决

    最近一直很忙,没有时间来更新了,今天抽空把之前遇到的问题记录下来. 之前在使用jmeter做http请求性能压测时,因为要对所有入参做排序再加密作为一个入参,所以写了一段java代码,用来处理入参,打 ...

  3. mvp和mvc的区别

    一句话总结:你代码逻辑有没有写在View中的,有就是MVC,没有就是MVP MVP模式: View不直接与Model交互,而是通过与Presenter交互来与Model间接交互 Presenter与V ...

  4. ZOJ 3204 Connect them(最小生成树+最小字典序)

    Connect them Time Limit: 1 Second      Memory Limit: 32768 KB You have n computers numbered from 1 t ...

  5. UI-自定义TabBar

    MyCustomTabBar.h文件 #import <UIKit/UIKit.h> @interface MyCustomTabBar : UITabBarController @end ...

  6. HTML中的颜色简写

    1.HTML中颜色的五种写法 1)直接用颜色英文名字表示 例如表示背景颜色为白色: 2.通过16进制数表示 例如表示背景颜色为黑色 3).通过RGB方式表示 RGB:是红色(red)绿色(green) ...

  7. Maven入门:Maven的基本概念

    本文转自:http://www.tianmaying.com/tutorial/maven-basic 看这篇文章之前,你应该先Run起来一个简单的Maven项目,先有个感性认识,然后再来听听对这些基 ...

  8. HAWQ取代传统数仓实践(八)——维度表技术之角色扮演维度

    单个物理维度可以被事实表多次引用,每个引用连接逻辑上存在差异的角色维度.例如,事实表可以有多个日期,每个日期通过外键引用不同的日期维度,原则上每个外键表示不同的日期维度视图,这样引用具有不同的含义.这 ...

  9. Windows10使用Chocolatey安装mysql之后无法使用的解决办法

    问题背景:使用了一台新的虚拟机,并且安装了Chocolatey作为Windows的包管理器,之后安装mysql 那么问题发生了,使用mysql命令根本没有任何反应,也不报错,但是安装的时候是提示安装成 ...

  10. 从无到有开发自己的Wordpress博客主题---代码环境准备

    注意这里说的是代码环境准备哦,而不是L(M)AMP运行环境哦,运行环境会在后述文章中在写. 一.在本地初始化git环境并且链接上gitee 1.在gitee上创建一个项目托管你的代码 这个不在赘述,按 ...