I want to create a set of classes with variable number of fields. What I tried:
private Class<?> generateClass(String clazzName, Map<String, Class<?>> fields) {
DynamicType.Builder<Object> buddy = new ByteBuddy()
.subclass(Object.class)
.name(clazzName);
fields.forEach((k, v) -> {
buddy.defineField(k, v, Visibility.PRIVATE);
});
return buddy.make().load(ClassLoader.getSystemClassLoader()).getLoaded();
}
where parameter clazzName is the class name to be created and key of the fields represent the field name and the value represent the field type.
The issue with the above code is that it creates the class but without any fields. What am I missing?
Also it's possible to generate the public getters from the fields above?
question from:
https://stackoverflow.com/questions/65936920/create-classes-with-variable-number-of-fields 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…