Quarkus原生获取注解的值

Quarkus native get the value of an annotation

当我使用命令 ./mvnw package -Pnative 将我的代码编译为 quarkus native 时,我试图访问注释内部的值。 我正在尝试使用 Cucumber cli 来执行我的代码,如下所示:Main.run("-g", "com.hellocucumber", "file:/hellocucumber.feature")。 我对源代码进行了一些修改,以使其与 Quarkus 原生编译一起使用。我的主要问题是 io.cucumber.java.GlueAdaptor 中有这个代码块 Method expressionMethod = annotation.getClass().getMethod("value"); 失败并显示错误 java.lang.IllegalStateException: java.lang.NoSuchMethodException: io.cucumber.java.en.Given$$ProxyImpl.value() 因为我们正在尝试访问 StepDefinition [= 中定义的注释的值59=]。这是我的步骤定义 class,以及我的特征文件。

public class StepDefinitions {
    @Given("today is Sunday")
    public void today_is_Sunday() {
        //Print a message
    }

    @When("I ask whether it's Friday yet")
    public void i_ask_whether_it_s_Friday_yet() {
        //Print a message
    }
    
    @Then("I should be told {string}")
    public void i_should_be_told(String expectedAnswer) {
        //Print a message
    }
 
 }

Scenario: Sunday isn't Friday
    Given today is Sunday
    When I ask whether it's Friday yet
    Then I should be told Nope

我尝试将注解classes(io.cucumber.java.en.Given、io.cucumber.java.en.And等)添加到reflect.json并提供给本地编译,如下所示: quarkus.native.additional-build-args=-H:DynamicProxyConfigurationResources=reflect-config.json,这个好像不行。 我也将我修改的所有代码都移到了一个扩展中,并尝试像这样修改注释:

@BuildStep
    void addProxies(CombinedIndexBuildItem combinedIndexBuildItem,
                    BuildProducer<NativeImageProxyDefinitionBuildItem> proxies) {
        proxies.produce(new NativeImageProxyDefinitionBuildItem(StepDefinitionAnnotation.class.getName()));
        proxies.produce(new NativeImageProxyDefinitionBuildItem(Given.class.getName()));
        proxies.produce(new NativeImageProxyDefinitionBuildItem(When.class.getName()));
        proxies.produce(new NativeImageProxyDefinitionBuildItem(Then.class.getName()));
        proxies.produce(new NativeImageProxyDefinitionBuildItem(And.class.getName()));
        proxies.produce(new NativeImageProxyDefinitionBuildItem(But.class.getName()));
    }

这也不管用,所以最后我试了这个:

@BuildStep
void transformAnnotations(BuildProducer<AnnotationsTransformerBuildItem> transformers) {


    transformers.produce(new AnnotationsTransformerBuildItem(new AnnotationsTransformer() {
        @Override
        public boolean appliesTo(AnnotationTarget.Kind kind) {
            return kind == AnnotationTarget.Kind.METHOD;
        }

        @Override
        public void transform(TransformationContext ctx) {
            AnnotationTarget target = ctx.getTarget();
            MethodInfo methodInfo = target.asMethod();
            ClassInfo classInfo = methodInfo.declaringClass();

            AnnotationInstance annotation = methodInfo.annotation(GIVEN_ANNOTATION);
            if (annotation == null) {
                return;
            }
             ctx.transform()
                    .add(create(
                            CDI_NAMED_ANNOTATION,
                            annotation.target(),
                            List.of(AnnotationValue.createStringValue("value", annotation.value().toString()))))
                    .done();
        }
    }));
}

这也没有用。当我尝试打印方法注释时,我只看到注释 class 名称,没有值。 当我执行这段代码时:

Method[] method = StepDefinitions.class.getMethods();
    for (int j = 0; j < method.length; j++) {
        LOGGER.info("Method annotations: {}", method[j].getAnnotations().length);
        Annotation[] annos = method[j].getAnnotations();
        for (int i = 0; i < annos.length; i++) {
            LOGGER.info("Annotation: {}", annos[i]);
        }
    }

在 quarkus 开发模式下,它打印:

在 quarkus native 中打印如下:

在这一点上,我已经 运行 想出了尝试什么的想法,我是 quarkus 生态系统的新手,我不确定这是否是正确的选择,或者是否有更好的选择,我愿意接受任何和所有的建议。

好的,对于可能遇到与我相同问题的任何其他人,我在浏览 https://quarkusio.zulipchat.com 的聊天记录时找到了解决方案。 所以我面临的错误在这些 Github 问题评论中描述:

https://github.com/quarkusio/quarkus/pull/1983#issuecomment-483157822
https://github.com/quarkusio/quarkus/pull/1983#issuecomment-483686256

我删除了对我的处理器 java 文件所做的所有更改,并只进行了第一条评论中所述的更改。