1
Byte Buddyを使用してフィールドのセッターとゲッターを作成しようとしています。学びページhereのアクセスフィールドセクションでByte Buddy - 正当なdefineMethod構文は何ですか?
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());
}
}
セッターとゲッターを作成する方法を定義し、その後Method m = o.getClass().getDeclaredMethod("getId", int.class);
はないNoSuchMethodExceptionをスローFieldAccessor.ofBeanProperty()
を使用した後implementation
を使用して、些細であることが述べられています。
getterとsetterを作成する正しい構文は何ですか?