转自:https://xuwenjin666.iteye.com/blog/1637247

1. 自定义注解

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.Documented; @Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Haha {
String author();
String desc();
String date();
String[] checkPoint();
}

2. 使用自定义注解

public class MyAnnotation {
@Haha(author = "hahaAuthor1",
desc = "hahaDesc",
date="2019-03-01",
checkPoint = {"1","2"}
)
public void useMyAnnotation1(){
System.out.println("MyAnnotation.useMyAnnotation1");
} @Haha(author = "hahaAuthor2",
desc = "hahaDesc",
date="2019-03-01",
checkPoint = {"1","2"}
)
public void useMyAnnotation2(){
System.out.println("MyAnnotation.useMyAnnotation2");
} public void notUseAnnotation(){
System.out.println("MyAnnotation.notUseMyAnnotation"); }
}

3. 信息提取

import java.lang.reflect.Method;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson; public class GetMyAnnotatioinInfo {
public static GetMyAnnotatioinInfo info = null; public static GetMyAnnotatioinInfo getInstance(){
if(info == null) info = new GetMyAnnotatioinInfo();
return info;
} public Map<String, String> getAnnotationInfo(Class annotationClass, String annotationField, String className) throws Exception{
Method[] methods = Class.forName(className).getDeclaredMethods(); System.out.println("类中所有方法名");
for(Method m : methods) System.out.println(m.getName()); System.out.println("目标注解名:" + annotationClass.getName() + " 类中使用了目标注解的方法有:");
Map<String, String> map = new HashMap<>();
Gson gson = new Gson();
for (Method m : methods){
if(m.isAnnotationPresent(annotationClass)){
System.out.println(m.getName());
Annotation an = m.getAnnotation(annotationClass);
Method anMethod = an.getClass().getDeclaredMethod(annotationField, null);
Object object = anMethod.invoke(an, null);
map.put(m.getName() + "." + annotationField, gson.toJson(object));
}
}
return map;
} public static void main(String[] args) throws Exception{
GetMyAnnotatioinInfo anInfo = GetMyAnnotatioinInfo.getInstance(); Map<String, String> res = anInfo.getAnnotationInfo(Haha.class, "author", MyAnnotation.class.getName());
res.putAll(anInfo.getAnnotationInfo(Haha.class, "checkPoint", MyAnnotation.class.getName())); System.out.println("以上注释名及内容如下:");
for(Map.Entry<String, String> entry : res.entrySet()){
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
}

4. 执行结果

类中所有方法名
useMyAnnotation2
notUseAnnotation
useMyAnnotation1
目标注解名:Haha 类中使用了目标注解的方法有:
useMyAnnotation2
useMyAnnotation1
类中所有方法名
useMyAnnotation2
notUseAnnotation
useMyAnnotation1
目标注解名:Haha 类中使用了目标注解的方法有:
useMyAnnotation2
useMyAnnotation1
以上注释名及内容如下:
useMyAnnotation1.author = "hahaAuthor1"
useMyAnnotation2.author = "hahaAuthor2"
useMyAnnotation2.checkPoint = ["1","2"]
useMyAnnotation1.checkPoint = ["1","2"]

5. 补充

5.1.注解的定义:Java文件叫做Annotation,用@interface表示。

5.2.元注解:@interface上面按需要注解上一些东西,包括@Retention、@Target、@Document、@Inherited四种。

5.3.注解的保留策略:

  •   @Retention(RetentionPolicy.SOURCE)   // 注解仅存在于源码中,在class字节码文件中不包含
  •   @Retention(RetentionPolicy.CLASS)     // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得
  •   @Retention(RetentionPolicy.RUNTIME)  // 注解会在class字节码文件中存在,在运行时可以通过反射获取到

5.4.注解的作用目标:

  •   @Target(ElementType.TYPE)                      // 接口、类、枚举、注解
  •   @Target(ElementType.FIELD)                     // 字段、枚举的常量
  •   @Target(ElementType.METHOD)                 // 方法
  •   @Target(ElementType.PARAMETER)            // 方法参数
  •   @Target(ElementType.CONSTRUCTOR)       // 构造函数
  •   @Target(ElementType.LOCAL_VARIABLE)   // 局部变量
  •   @Target(ElementType.ANNOTATION_TYPE) // 注解
  •   @Target(ElementType.PACKAGE)               // 包

5.5.注解包含在javadoc中:

  •   @Documented

5.6.注解可以被继承:

  •   @Inherited

5.7.注解解析器:用来解析自定义注解。

java自定义注释及其信息提取的更多相关文章

  1. java自定义注释

    一.什么是注释 说起注释,得先提一提什么是元数据(metadata).所谓元数据就是数据的数据.也就是说,元数据是描述数据的.就象数据表中的字段一样,每个字段描述了这个字段下的数据的含义.而J2SE5 ...

  2. JAVA自定义注释(Target,Retention,Documented,Inherit)

    java自定义注解 Java注解是附加在代码中的一些元信息,用于一些工具在编译.运行时进行解析和使用,起到说明.配置的功能.注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用.包含在 java.l ...

  3. Java 自定义注释@interface的用法

    最简单的待校验的注解定义 @Documented @Constraint(validatedBy = ExistBlankByListValidator.class) @Target({PARAMET ...

  4. java中自定义注释@interface的用法

    一.什么是注释     说起注释,得先提一提什么是元数据(metadata).所谓元数据就是数据的数据.也就是说,元数据是描述数据的.就象数据表中的字段一样,每个字段描述了这个字段下的数据的含义.而J ...

  5. Java annotation 自定义注释@interface的用法

    最近看到很多项目都是用了自定义注解,例如 1.什么是注解? 元数据(metadata),就是指数据的数据,元数据是描述数据的,就像数据库中的,表的字段,每一个 字段描述这个字段下面·的数据的含义,j2 ...

  6. java自定义注解类

    一.前言 今天阅读帆哥代码的时候,看到了之前没有见过的新东西, 比如java自定义注解类,如何获取注解,如何反射内部类,this$0是什么意思? 于是乎,学习并整理了一下. 二.代码示例 import ...

  7. java自定义注解注解方法、类、属性等等【转】

    http://anole1982.iteye.com/blog/1450421 http://www.open-open.com/doc/view/51fe76de67214563b20b385320 ...

  8. Android Studio自定义注释模板及生成JavaDoc

    刚开始学习Android,使用了Android Studio IDE.为了将来生产JavaDoc,学习一下如何自定义注释模板. . 自定义注释模板 1. 通过 File –>Settings 或 ...

  9. java自定义注解知识实例及SSH框架下,拦截器中无法获得java注解属性值的问题

    一.java自定义注解相关知识 注解这东西是java语言本身就带有的功能特点,于struts,hibernate,spring这三个框架无关.使用得当特别方便.基于注解的xml文件配置方式也受到人们的 ...

随机推荐

  1. Ubuntu命令行下缩小磁盘镜像img文件尺寸

    要解决的问题 一个固件包里的system.img, 尺寸是1GB, 里面的内容只有470MB, 在设备上写入的时候报超出大小了, 所以想把这个img调整为512MB 网上查了很多, 没有一个好用的, ...

  2. 如何将业务代码写得像诗一样(使用注解+单例+工厂去掉一大波if和else判断)

    1.订单控制器,提供一个根据商品id和银行渠道id计算商品折后价格的接口: import org.springframework.web.bind.annotation.GetMapping; imp ...

  3. java面试题实战三

    华为优招面试经验. 1.笔试(这部分按照华为以前的风格不会为难人的,认真做AC一道题就可以进面试了,我编程能力一般吧,做了一道半而已,-_-||!) 2.测评(性格测试,不要太偏激就行了) 3.面试分 ...

  4. Qt之如何自定义model

    Qt之如何自定义model https://blog.csdn.net/wei375653972/article/details/86592209

  5. 【npm permission denied错误】npm ERR! Error: EACCES: permission denied, access

    在命令前加上 sudo sudo npm install --save-dev grunt 不过这样子可能还是不行,你需要这样: sudo npm install --unsafe-perm=true ...

  6. go 调度机制简介

    goroutine是go中最重要的功能之一,正是因为有了goroutine这样强大的工具,go在并发方面表现的特别优秀. 那么goroutine和普通的线程和协程有什么区别呢?首先,我们需要明白线程和 ...

  7. Zipkin+Sleuth 链路追踪整合

    1.Zipkin 是一个开放源代码分布式的跟踪系统 它可以帮助收集服务的时间数据,以解决微服务架构中的延迟问题,包括数据的收集.存储.查找和展现 每个服务向zipkin报告计时数据,zipkin会根据 ...

  8. django使用https

    根据以下内容总结了下: http://www.voidcn.com/article/p-xxdfvetx-da.html http://www.voidcn.com/article/p-ezmbnny ...

  9. JVM方法栈的工作过程,方法栈和本地方法栈有什么区别。

    JVM的本地方法栈   对于一个运行中的Java程序而言,它还可能会用到一些跟本地方法相关的数据区.当某个线程调用一个本地方法时,它就进入了一个全新的并且不再受虚拟机限制的世界.本地方法可以通过本地方 ...

  10. [转帖]Zookeeper入门看这篇就够了

    Zookeeper入门看这篇就够了 https://my.oschina.net/u/3796575/blog/1845035 Zookeeper是什么 官方文档上这么解释zookeeper,它是一个 ...