gpt4 book ai didi

java - 动态加载类并获取带注释的类

转载 作者:行者123 更新时间:2023-11-30 06:24:04 24 4
gpt4 key购买 nike

import com.this.class.Factory;
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

// Itearate over all @Factory annotated elements
for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(Factory.class)) {
...
}
}

当我在注释处理器中导入注释类时,这会起作用。但是动态加载注解类时就不行了

Class<?> Factorry = class.forName("com.this.class.Factory")
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

// Itearate over all @Factory annotated elements
for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(Factory.class)) {
...
}
}

当使用class.forName()加载类时如何将带注释的Factory.class传递给getElementsAnnotatedWith

最佳答案

您需要更改两件事:

  1. 工厂变量应转换为注释:

    Class<Annotation> Factorry = (Class<Annotation>)class.forName("com.this.class.Factory")

  2. 使用 Factory 变量而不是 Factory.class:

    roundEnv.getElementsAnnotatedWith(Factorry)

关于java - 动态加载类并获取带注释的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47581121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com