繁体   English   中英

Quarkus native 获取注释的值

[英]Quarkus native get the value of an annotation

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

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

我尝试将注释类(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();
        }
    }));
}

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

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 开发模式下,它打印:

  • 方法注释:1
  • 注释:@io.cucumber.java.en.Given(value="今天是星期天")

在 quarkus native 中,它会打印:

  • 方法注释:1
  • 注释:@io.cucumber.java.en.Given

在这一点上,我已经没有了关于尝试什么的想法,我是 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 文件所做的所有更改,并仅进行了第 1 条评论中所述的更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM