是否有任何方法可以防止字节好友为我添加的注释发出默认值?使用以下示例,基于构建插件,我想查看从baz
字段上的@XmlAttribute
注释中省略的冗余required
和namespace
值。
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() {
}
}
字节好友可以配置为跳过默认注释值。但是,配置字节伙伴在实现Plugin
接口的转换构建插件的范围之外。字节好友API提供了一个单独的EntryPoint
接口,可以实现该接口以控制字节好友的初始化。
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() {
}
}