purenothingform
V2EX  ›  Java

如何获取源码的 Target 不支持 RECORD_COMPONENT 的注解?

  •  
  •   purenothingform · Apr 4, 2023 · 1439 views
    This topic created in 1157 days ago, the information mentioned may be changed or developed.

    我创建了某个 Record 类,需要获取标注在该 Record 类属性上的注解。 可是该注解源码的 Target 的 value 里没有 [ ElementType.RECORD_COMPONENT ] 所以获取不到。 请问在不 hook 或修改源码的情况有没有什么方法获取该注解(甚至是读取字节码的方式)?

    2 replies    2023-04-19 21:11:28 +08:00
    termux
        1
    termux  
       Apr 4, 2023
    好像不管有没有设置 @Target(ElementType.RECORD_COMPONENT),都可以用反射获取属性上的所有注解:field.getAnnotations()
    purenothingform
        2
    purenothingform  
    OP
       Apr 19, 2023
    @termux 并不能不过貌似可以通过 javaagent 动态添加
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5572 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 06:37 · PVG 14:37 · LAX 23:37 · JFK 02:37
    ♥ Do have faith in what you're doing.