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