Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Byte Buddy-java.lang.NoSuchMethodException-正确的defineMethod语法是什么?_Java_Code Generation_Byte Buddy - Fatal编程技术网

Byte Buddy-java.lang.NoSuchMethodException-正确的defineMethod语法是什么?

Byte Buddy-java.lang.NoSuchMethodException-正确的defineMethod语法是什么?,java,code-generation,byte-buddy,Java,Code Generation,Byte Buddy,我正在尝试使用Byte Buddy为字段创建setter和getter public class Sample { public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, NoSuchMethodException { Class<?> type

我正在尝试使用Byte Buddy为字段创建setter和getter

public class Sample {

    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())
                .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", int.class);
        System.out.println(m.getName());
    }
}
公共类示例{
publicstaticvoidmain(字符串[]args)抛出实例化异常、IllegalAccessException、NoSuchFieldException、SecurityException、NoSuchMethodException{
类类型=新的ByteBuddy()
.subclass(Object.class)
.名称(“域名”)
.defineField(“id”,int.class,Visibility.PRIVATE)
.defineMethod(“getId”,int.class,Visibility.PUBLIC).intercept(FieldAccessor.ofBeanProperty())
.make()
.load(Sample.class.getClassLoader(),ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
对象o=type.newInstance();
字段f=o.getClass().getDeclaredField(“id”);
f、 setAccessible(true);
System.out.println(o.toString());
方法m=o.getClass().getDeclaredMethod(“getId”,int.class);
System.out.println(m.getName());
}
}
在学习页面的访问字段部分中,说明了在定义方法后使用
实现,然后使用
FieldAccessor.ofBeanProperty()

方法m=o.getClass().getDeclaredMethod(“getId”,int.class)抛出NoSuchMethodException


创建getter和setter的正确语法是什么?

正确的方法调用应该是

Method m = o.getClass().getDeclaredMethod("getId");
int
是返回类型,您不必在
getDeclaredMethod
调用中指定返回类型-仅参数类型,而方法
getId
没有参数