Sander_M Sander_M - 5 months ago 58
Java Question

Byte Buddy - Method Implementation.Context.Default is no bean property - creating a setter

How do I create a setter on a field using byte buddy? What is the recommended syntax?

I managed to create the getter from a field (my original question here), but using the

to create a setter is throwing a
Method Implementation.Context.Default
is no bean property

The suggested way to create a setter in this question seems to be outdated.

Here is my failing code using version 1.5.4 of byte-buddy:

public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, NoSuchMethodException {
Class<?> type = new ByteBuddy()
.defineField("id", int.class, Visibility.PRIVATE)
.defineMethod("getId", int.class, Visibility.PUBLIC).intercept(FieldAccessor.ofBeanProperty())
.defineMethod("setId", Void.TYPE, Visibility.PUBLIC).intercept(FieldAccessor.ofBeanProperty())
.load(sample.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)

Object o = type.newInstance();
Field f = o.getClass().getDeclaredField("id");
Method m = o.getClass().getDeclaredMethod("getId");
Method s = o.getClass().getDeclaredMethod("setId", int.class);


You have not defined a parameter for the setter. Byte Buddy does therefore not understand how to implement the method. You need to set withParameters(int.class) when defining the setId method.