gpt4 book ai didi

java - 我可以阻止 Byte Buddy 发出注释的默认值吗?

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

有什么方法可以阻止 Byte Buddy 为我添加的注释发出默认值吗?使用以下基于构建插件的示例,我希望看到从 @XmlAttribute 的生成字节码中省略了冗余的 requirednamespacebaz 字段上的 注释。

foo/Bar.java:

package foo;

import javax.xml.bind.annotation.XmlAttribute;

public class Bar {
@XmlAttribute(name = "qux")
public String qux;
}

net/bytebuddy/test/SimplePlugin.java:

...    
public class SimplePlugin implements Plugin {
...
@Override
public DynamicType.Builder<?> apply(DynamicType.Builder<?> builder, TypeDescription typeDescription) {
return builder.defineField("baz", String.class, Visibility.PUBLIC)
.annotateField(AnnotationDescription.Builder.ofType(XmlAttribute.class)
.define("name", "baz")
.build());
}
}

foo/Bar.class:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package foo;

import javax.xml.bind.annotation.XmlAttribute;

public class Bar {
@XmlAttribute(
name = "qux"
)
public String qux;
@XmlAttribute(
name = "baz",
required = false,
namespace = "##default"
)
public String baz;

public Bar() {
}
}

最佳答案

Byte Buddy 可以配置为跳过默认注释值。但是,配置 Byte Buddy 超出了实现 Plugin 接口(interface)的转换构建插件的范围。 Byte Buddy API 提供了一个单独的 EntryPoint 接口(interface),可以实现该接口(interface)来控制 Byte Buddy 的初始化。

net/bytebuddy/test/SimpleEntryPoint.java:

package net.bytebuddy.test;

...

public class SimpleEntryPoint implements EntryPoint {
@Override
public ByteBuddy getByteBuddy() {
return new ByteBuddy()
.with(AnnotationValueFilter.Default.SKIP_DEFAULTS);
}

...
}

pom.xml:

...
<plugin>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>transform</goal>
</goals>
</execution>
</executions>
<configuration>
<initialization>
<entryPoint>net.bytebuddy.test.SimpleEntryPoint</entryPoint>
</initialization>
<transformations>
<transformation>
<plugin>net.bytebuddy.test.SimplePlugin</plugin>
</transformation>
</transformations>
</configuration>
</plugin>
...

foo/Bar.class:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package foo;

import javax.xml.bind.annotation.XmlAttribute;

public class Bar {
@XmlAttribute(
name = "qux"
)
public String qux;
@XmlAttribute(
name = "baz"
)
public String baz;

public Bar() {
}
}

关于java - 我可以阻止 Byte Buddy 发出注释的默认值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42354669/

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