Byte Buddy - implementation method .Context.Default is not a bean property - setter creation

How to create a setter in a field using a byte? What is the recommended syntax?

I managed to create a getter from the field (my original question is here ), but using defineMethodto create a setter throws Method Implementation.Context.Default... an is no bean propertyexception.

The proposed way to create a setter in this matter seems outdated.

Here is my unsuccessful code using version 1.5.4 byte-buddy:

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

        Object o = type.newInstance();
        Field f = o.getClass().getDeclaredField("id");
        f.setAccessible(true);
        System.out.println(o.toString());       
        Method m = o.getClass().getDeclaredMethod("getId");
        System.out.println(m.getName());
        Method s = o.getClass().getDeclaredMethod("setId", int.class);
        System.out.println(s.getName());
    }
+4
source share
1 answer

. , . setId Parameters (int.class).

+3

Source: https://habr.com/ru/post/1661287/


All Articles