1. import java.lang.annotation.Documented;
  2. import java.lang.annotation.ElementType;
  3. import java.lang.annotation.Retention;
  4. import java.lang.annotation.RetentionPolicy;
  5. import java.lang.annotation.Target;
  6.  
  7. /**
  8. * 定义作者信息,name和group
  9. * @author sprinng
  10. *
  11. */
  12. @Retention(RetentionPolicy.RUNTIME)
  13. @Target(ElementType.METHOD)
  14. @Documented
  15. public @interface Author {
  16.  
  17. String name();
  18. String group();
  19. }
  1. import java.lang.annotation.Documented;
  2. import java.lang.annotation.ElementType;
  3. import java.lang.annotation.Retention;
  4. import java.lang.annotation.RetentionPolicy;
  5. import java.lang.annotation.Target;
  6.  
  7. /**
  8. * @author sprinng
  9. *
  10. * 定义描述信息 value
  11. */
  12. @Retention(RetentionPolicy.RUNTIME)
  13. @Target(ElementType.TYPE)
  14. @Documented
  15.  
  16. public @interface Description {
  17. String value();
  18. }
  1. @Description(value = "这是一个有用的工具类")
  2. public class Utility {
  3.  
  4. @Author(name = "haoran_202",group="com.magc")
  5. public String work()
  6. {
  7. return "work over!";
  8. }
  9.  
  10. }
  1. import java.lang.reflect.Method;
  2.  
  3. public class AnalysisAnnotation {
  4.  
  5. /**
  6. * 在运行时分析处理annotation类型的信息
  7. */
  8. public static void main(String[] args) {
  9. try {
  10. //通过运行时反射API获得annotation信息
  11. Class<?> rt_class = Class.forName("com.demo.Utility");
  12. Method[] methods = rt_class.getMethods();
  13.  
  14. boolean flag = rt_class.isAnnotationPresent(Description.class);
  15.  
  16. if(flag)
  17. {
  18. Description description = (Description)rt_class.getAnnotation(Description.class);
  19. System.out.println("Utility's Description--->"+description.value());
  20. for (Method method : methods) {
  21. if(method.isAnnotationPresent(Author.class))
  22. {
  23. Author author = (Author)method.getAnnotation(Author.class);
  24. System.out.println("Utility's Author--->"+author.name()+" from "+author.group());
  25.  
  26. }
  27. }
  28. }
  29.  
  30. } catch (ClassNotFoundException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34.  
  35. }

java annotation的更多相关文章

  1. Java Annotation概述

    @(Java)[Annotation|Java] Java Annotation概述 用途 编译器的相关信息,如用于检测错误和一些警告 编译时和部署时的处理,如一些软件用于自动生成代码之类的 运行时处 ...

  2. Java Annotation 注解

    java_notation.html div.oembedall-githubrepos { border: 1px solid #DDD; list-style-type: none; margin ...

  3. paip.Java Annotation注解的作用and 使用

    paip.Java Annotation注解的作用and 使用 作者Attilax 艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog. ...

  4. Java Annotation认知(包括框架图、详细介绍、示例说明)

    摘要 Java Annotation是JDK5.0引入的一种注释机制. 网上很多关于Java Annotation的文章,看得人眼花缭乱.Java Annotation本来很简单的,结果说的人没说清楚 ...

  5. Java Annotation原理分析(一)

    转自:http://blog.csdn.net/blueheart20/article/details/18725801 小引: 在当下的Java语言层面上,Annotation已经被应用到了语言的各 ...

  6. Java Annotation 及几个常用开源项目注解原理简析

    PDF 版: Java Annotation.pdf, PPT 版:Java Annotation.pptx, Keynote 版:Java Annotation.key 一.Annotation 示 ...

  7. Java Annotation 机制源码分析与使用

    1 Annotation 1.1 Annotation 概念及作用      1.  概念 An annotation is a form of metadata, that can be added ...

  8. Java Annotation手册

    Java Annotation手册 作者:cleverpig(作者的Blog:http://blog.matrix.org.cn/page/cleverpig) 原文:http://www.matri ...

  9. Java Annotation 必须掌握的特性

    什么是Annotation? Annotation翻译为中文即为注解,意思就是提供除了程序本身逻辑外的额外的数据信息.Annotation对于标注的代码没有直接的影响,它不可以直接与标注的代码产生交互 ...

  10. Java Annotation认知(包括框架图、详细介绍、示例说明)(转)

    本文转自:http://www.cnblogs.com/skywang12345/p/3344137.html 网上很多关于Java Annotation的文章,看得人眼花缭乱.Java Annota ...

随机推荐

  1. CSS3圆角边框的使用-遁地龙卷风

    0.快速入门 border-radius:50px; 1.border-radius详解 border-radius:50px; 上右下左,水平和垂直距离都是50px border-radius:50 ...

  2. shareSDK

    一. 编写sharesdk代码 在AppDelegate.m中 //shareSDK相关 #import <ShareSDK/ShareSDK.h> #import "Weibo ...

  3. [codeforces 317]A. Perfect Pair

    [codeforces 317]A. Perfect Pair 试题描述 Let us call a pair of integer numbers m-perfect, if at least on ...

  4. Java 读取excel 文件 Unable to recognize OLE stream 错误

    原因:不支出读取 excel 2007 文件(*.xlsx).只支持 excel 2003 (*.xls).

  5. (集成电路卡)ID卡

    IC卡(intergrated Circuit Card,集成电路卡),又称为智能卡,智慧卡,微电路卡,微芯片卡 等等. 它是将一个微电子芯片嵌入符合ISO 7816标准的卡基中,做成卡片形状. IC ...

  6. GPS坐标互转:WGS-84(GPS)、GCJ-02(Google地图)、BD-09(百度地图)

    WGS-84:是国际标准,GPS坐标(Google Earth使用.或者GPS模块)GCJ-02:中国坐标偏移标准,Google Map.高德.腾讯使用BD-09:百度坐标偏移标准,Baidu Map ...

  7. 数据结构大二课程设计:QT实现线段树

    源码以及编译文件下载地址:http://download.csdn.net/detail/zhiyanpianyu1234/9445909#comment 加入了一些小东西,一直觉得课设是做给自己看的 ...

  8. idea 排除编译文件,恢复编译

  9. phpcms不能后台编辑模板

    刚安装的phpcms后台无法编辑模板,实际上只需要改一个系统文件的值即可 打开phpcms项目根目录下的: caches/configs/system.php 找到第20行,代码如下: 'tpl_ed ...

  10. iOS 深入理解UINavigationController 和 UIViewController 之间的关系

    创建三个类 BasicViewController : UIViewController SecondViewController : UIViewController ThirdViewContro ...