java Quarkus native获取注释的值
当我使用命令将代码编译为quarkus native时,我尝试访问注释内的值/mvnw包-Pnative
。
我正在尝试使用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类中定义的注释的值。这是我的步骤定义类,以及我的要素文件
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.cumber.java.en.Given、io.cumber.java.en.And等)添加到反射中。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。黄瓜JAVAEN给定(value=“今天是星期日”)
在quarkus native中,它打印以下内容:
- 方法注释:1
- 注释:@io。黄瓜JAVAEN给定
在这一点上,我已经用尽了关于尝试什么的想法,我是quarkus生态系统的新手,我不确定这是一条正确的道路,或者是否有更好的选择,我愿意接受任何和所有建议
# 1 楼答案
好的,所以对于其他可能和我有同样问题的人,我在查看https://quarkusio.zulipchat.com的聊天历史时找到了解决方案。 因此,我面临的错误在以下Github问题评论中有所描述:
https://github.com/quarkusio/quarkus/pull/1983#issuecomment-483157822
https://github.com/quarkusio/quarkus/pull/1983#issuecomment-483686256
我删除了对处理器java文件所做的所有更改,只做了第一条评论中所述的更改