I have assigned an annotation to my field, namely SubDocument. Saving and loading is not a problem.
Under the method read() I say dataSetting.currentValue(value); This all works and also the variable value has the correct value. This is followed by the call field.set(classObject, dataSetting). The variable dataSetting should now update the field.
Debug results:
As you can see, the variable value contains the value "Übersicht2" and it is set successfully. When debugging field.set(classObject, dataSetting), you can also see that the currentValue=Übersicht2
Now the problem is that if I call the field via the variable and call the method #getCurrentValue, I still get back null.
Thank you for any help.
Field:
@SubDocument(save = "master_inventory_title", read = "master_inventory_title")
private final DataSetting<String> title = new DataSetting<String>().builder()
.defaultValue("Übersicht")
.build();
How I update:
private void initSubDocuments() {
for (Class<?> clazz : reflections.getTypesAnnotatedWith(Document.class)) {
YamlConfiguration config = getConfig(clazz);
if (config == null) continue;
for (Field field : clazz.getDeclaredFields()) {
SubDocument subDocument = field.getAnnotation(SubDocument.class);
if (subDocument == null) continue;
try {
Constructor<?> constructor = getEmptyConstructor(clazz);
if (constructor == null) continue;
Object classObject = constructor.newInstance();
boolean isAccessible = field.canAccess(classObject);
field.setAccessible(true);
Object dataObject = field.get(classObject);
if (dataObject instanceof Setting<?>)
loadSetting((Setting<?>) dataObject, clazz, classObject, field, subDocument, config);
if (dataObject instanceof OptionSetting<?>)
loadOptionSetting((OptionSetting<?>) dataObject, clazz, classObject, field, subDocument, config);
if (dataObject instanceof DataSetting<?>) {
loadDataSetting((DataSetting<?>) dataObject, clazz, classObject, field, subDocument, config);
}
field.setAccessible(isAccessible);
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
throw new RuntimeException(e);
}
this.subDocuments.add(subDocument);
}
}
}
private void loadDataSetting(@NotNull DataSetting<?> dataSetting, @NotNull Class<?> clazz, @NotNull Object classObject, @NotNull Field field, @NotNull SubDocument subDocument, @NotNull YamlConfiguration config) throws IllegalAccessException {
save(subDocument, config, clazz, dataSetting.getDefaultValue());
read(null, null, dataSetting, field, subDocument, classObject, config.get(subDocument.read()));
}
private <T> void save(@NotNull SubDocument subDocument, @NotNull YamlConfiguration config, @NotNull Class<?> clazz, @NotNull T defaultValue) {
if (!subDocument.save().equals("unknown") && !config.isSet(subDocument.save())) {
config.set(subDocument.save(), defaultValue);
saveConfig(clazz);
}
}
private void read(@Nullable Setting<?> setting, @Nullable OptionSetting<?> optionSetting, @Nullable DataSetting<?> dataSetting, Field field, SubDocument subDocument, Object classObject, Object value) throws IllegalAccessException {
if (setting != null && !subDocument.read().equals("unknown")) {
setting.currentValue(value);
field.set(classObject, setting);
return;
}
if (optionSetting != null && !subDocument.read().equals("unknown")) {
optionSetting.currentValue(value);
field.set(classObject, optionSetting);
return;
}
if (dataSetting != null && !subDocument.read().equals("unknown")) {
dataSetting.currentValue(value);
field.set(classObject, dataSetting);
}
}
Aucun commentaire:
Enregistrer un commentaire