I would like to generate an enum containing a field using Byte Buddy, where each enum constant passes a different argument to the constructor.
public enum MyEnum {
private final String blah;
A("foo")
B("bar");
private MyEnum(String blah) {
this.blah = blah;
}
}
I tried this:
new ByteBuddy()
.makeEnumeration("A", "B")
.name("com.foo.MyEnum")
.defineField("blah", String.class, Modifier.FINAL | Modifier.PRIVATE)
.defineConstructor(Visibility.PRIVATE)
.withParameters(String.class)
.intercept(
FieldAccessor.ofField("blah").setsArgumentAt(0)
)
.make()
Which generates this (decompiled). The two constructors with the same signature are a bit weird, but anyway.
public enum MyEnum {
private final String blah;
A,
B;
private MyEnum() {
}
private MyEnum() {
this.blah = var1;
}
}
I couldn't see any way to associate each enum constant with a set of constructor arguments. The makeEnumeration
method only has two signatures, both of which take a collection of strings.
Is this possible with Byte Buddy?