Hutool Hutool
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
  • 快速入门

  • 核心(hutool-core)

    • AI(Hutool-ai)

    • 注解(annotation)

      • 简介
      • 注解工具-AnnotationUtil
        • 📚介绍
        • 🍭使用
          • 方法介绍
          • 1. 获取注解对象
          • 2. 获取注解属性
          • 3. 获取注解属性值
          • 4. 获取元注解相关信息
          • 5. 清空注解缓存
      • 层级注解工具-AnnotatedElementUtil
    • 数组(array)

    • JavaBean(bean)

    • 缓存(cache)

    • 类加载器(classloader)

    • 编码解码(codec)

    • 集合(collection)

    • 比较器(comparator)

    • 压缩(compress)

    • 类型转换(convert)

    • 数据(data)

    • 日期时间(date)

    • 异常(exception)

    • 函数(func)

    • IO流(io)

    • 语言特性(lang)

    • Map(map)

    • 数字数学(math)

    • 网络(net)

    • 对象池(pool)

    • 反射(reflect)

    • 正则(regex)

    • 服务提供(spi)

    • 聚合操作(stream)

    • 字符串文本(text)

    • 并发和线程(thread)

    • 树结构(tree)

    • 工具集合(util)

    • XML操作(xml)

  • 定时任务(hutool-cron)

  • 加密(hutool-crypto)

  • 数据库(hutool-db)

  • 扩展(hutool-extra)

  • HTTP(hutool-http)

  • 日志(hutool-log)

  • 配置文件(hutool-setting)

  • JSON(hutool-json)

  • Office办公(hutool-poi)

  • 套接字(hutool-socket)

  • GUI(hutool-swing)

  • 指南
  • 核心(hutool-core)
  • 注解(annotation)
Hutool
2026-01-03
目录

注解工具-AnnotationUtil

# 📚介绍

快速获取注解对象、注解值等工具封装。

# 🍭使用

# 方法介绍

首先定义两个注解:

@Target(ElementType.TYPE_USE)
@Retention(RetentionPolicy.RUNTIME)
private @interface MetaAnnotationForTest{
   @Alias(value = "alias")
    String value() default "";
    String alias() default "";
}

@MetaAnnotationForTest("foo")
@Target(ElementType.TYPE_USE)
@Retention(RetentionPolicy.RUNTIME)
private @interface AnnotationForTest{
    String value() default "";
    String[] names() default "";
}

定义一个类,加上注解:

@AnnotationForTest(value = "foo", names = {"测试1", "测试2"})
class ClassForTest{}

@AnnotationForTest(value = "foo", names = {"测试1", "测试2"})
class SubClassForTest extends ClassForTest{}

# 1. 获取注解对象

// 获取ClassForTest上标注的所有注解,不包括父类和接口上的注解
// 此处注解只有一个,即AnnotationForTest
final Annotation[] annotations = AnnotationUtil.getDeclaredAnnotations(ClassForTest.class);
// 获取ClassForTest上标注的所有注解,包括父类和接口上的注解
// 此处由于没有父类,注解只有一个,即AnnotationForTest
final Annotation[] annotations = AnnotationUtil.getAnnotations(ClassForTest.class, false);

Hutool中可以获取类似Spring中组合注解,即使用一个注解表示多个注解,在定义的注解和类中,我们已知ClassForTest上只有AnnotationForTest注解,AnnotationForTest上有MetaAnnotationForTest注解,AnnotationForTest这种注解套注解的形式我们称为组合注解。既然ClassForTest被组合注解标注,则组合注解上的所有注解我们同样也可以获取:

// ClassForTest -> AnnotationForTest -> MetaAnnotationForTest
MetaAnnotationForTest[] annotations = AnnotationUtil.getCombinationAnnotations(ClassForTest.class, MetaAnnotationForTest.class);

这种注解形式就好比把多个注解打了个包,使用getCombinationAnnotations我们可以方便的获取包中的注解。

# 2. 获取注解属性

AnnotationForTest注解有两个属性,都是方法,如果想获取这些方法,则调用getAnnotationAttributes:

Method[] methods = AnnotationUtil.getAnnotationAttributes(AnnotationForTest.class);

# 3. 获取注解属性值

getAnnotationValue用于获取指定注解的指定值,有多个重载方法。我们可以获取其默认值,即value的值:

// foo
AnnotationUtil.getAnnotationValue(ClassForTest.class, AnnotationForTest.class);

如果有其它值,可以调用重载指定属性名称:

// ["测试1", "测试2"]
AnnotationUtil.getAnnotationValue(ClassForTest.class, AnnotationForTest.class, "names");

当然,如果指定一个方法引用(函数),也可以动态获取其值,如想获取AnnotationForTest的names属性值:

// ["测试1", "测试2"]
AnnotationUtil.getAnnotationValue(ClassForTest.class, AnnotationForTest::names);

如果想获取所有属性和值的对应关系,可以使用getAnnotationValueMap:

// 获取AnnotationForTest注解的属性映射关系
// {value=foo, names=["测试1", "测试2"]}
Map<String, Object> valueMap = AnnotationUtil.getAnnotationValueMap(ClassForTest.class, AnnotationForTest.class);

Hutool中定义了@Alias注解,用于注解别名,可以通过getAnnotationAlias获取别名处理后的注解:

MetaAnnotationForTest annotationAlias = AnnotationUtil.getAnnotationAlias(AnnotationForTest.class, MetaAnnotationForTest.class);
// value的别名是alias,因此获取value值时,实际返回的是alias的值
annotationAlias.value();

# 4. 获取元注解相关信息

  • getRetentionPolicy 获取注解类的保留时间,可选值 SOURCE(源码时),CLASS(编译时),RUNTIME(运行时),默认为 CLASS
  • getTargetType 获取注解类可以用来修饰哪些程序元素,如 TYPE, METHOD, CONSTRUCTOR, FIELD, PARAMETER 等
  • isDocumented 是否会保存到 Javadoc 文档中
  • isInherited 是否可以被继承,默认为 false

# 5. 清空注解缓存

AnnotationUtil提供了clearCaches方法,用于清空注解缓存,这样下次获取注解时,会重新从类中获取注解。

AnnotationUtil.clearCaches();

更多方法见API文档:

https://plus.hutool.cn/apidocs/cn/hutool/core/annotation/AnnotationUtil.html (opens new window)

简介
层级注解工具-AnnotatedElementUtil

← 简介 层级注解工具-AnnotatedElementUtil→

Theme by Vdoing | Copyright © 2025-2026 Hutool | Apache-2.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式