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; /**
* 定义作者信息,name和group
* @author sprinng
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface Author { String name();
String group();
}
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; /**
* @author sprinng
*
* 定义描述信息 value
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented public @interface Description {
String value();
}
@Description(value = "这是一个有用的工具类")
public class Utility { @Author(name = "haoran_202",group="com.magc")
public String work()
{
return "work over!";
} }
import java.lang.reflect.Method;

public class AnalysisAnnotation {

    /**
* 在运行时分析处理annotation类型的信息
*/
public static void main(String[] args) {
try {
//通过运行时反射API获得annotation信息
Class<?> rt_class = Class.forName("com.demo.Utility");
Method[] methods = rt_class.getMethods(); boolean flag = rt_class.isAnnotationPresent(Description.class); if(flag)
{
Description description = (Description)rt_class.getAnnotation(Description.class);
System.out.println("Utility's Description--->"+description.value());
for (Method method : methods) {
if(method.isAnnotationPresent(Author.class))
{
Author author = (Author)method.getAnnotation(Author.class);
System.out.println("Utility's Author--->"+author.name()+" from "+author.group()); }
}
} } catch (ClassNotFoundException e) {
e.printStackTrace();
}
} }

最新文章

  1. Java中static的理解
  2. 【生活】已经从官网购买iPad,单独购买AppleCare+服务
  3. centos 安装php-fpm , nginx二级域名配置 ,但为什么必须要 域名提供商 哪里解析新的二级域名一下 才能用呢?
  4. 关于jQuery的cookies插件2.2.0版设置过期时间的说明
  5. C# C++ Java接口类型转换
  6. JSP与Servlet的中文乱码处理
  7. 使用SignalR打造消息总线
  8. ubuntu16.04, Matlab2016b caffe编译安装
  9. Oracle自动备份数据
  10. java与数据库
  11. High Availability手册(3): 配置
  12. Mysql外键的使用
  13. Java开发笔记(十)一元运算符的技巧
  14. Oracle的RowId和Rownum
  15. shell的命令替换和命令组合
  16. redis 分布式锁实现
  17. [django]详情页列表页
  18. 学习JS的心路历程-参数的传递(下)
  19. leetcode 6. ZigZag Conversion [java]
  20. cocos2d-x游戏引擎核心之三——主循环和定时器

热门文章

  1. explain mysql的type字段,索引的类型
  2. Swift-打开其它Storyboard中的自定义模态窗口
  3. Asp.net 网站防攻击安全设置
  4. c语言小知识点
  5. Java Io 字符流
  6. 第一篇博客 iframe自适应高度
  7. Oracle 多表查询优化
  8. pro*c调用过程
  9. 28 GroupSock(NetAddress)——live555源码阅读(四)网络
  10. Python自动化之select、greenlet和gevent和事件驱动模型初探