any way to modify existing instance's fields rather than creating new instances? #362
-
node.get(XXXConfig.class, emptyInstance); |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
You can get a Mutable ObjectFactory which you can then load an existing instance from a configuration node. ObjectMapper.Factory factory = (ObjectMapper.Factory) Objects.requireNonNull(node.options().serializers().get(type));
ObjectMapper.Mutable<T> mutable = (ObjectMapper.Mutable<T>) factory.get(type);
mutable.load(instance, node); |
Beta Was this translation helpful? Give feedback.
-
is this a right approach? @SneakyThrows
private void reload(final boolean ensureAsync) {
if (ensureAsync) {
PluginExceptions.ensureAsync();
}
synchronized (this.lock) {
final var loader = Configurate.yaml(
this.directory.resolve("messages.yaml")
);
final var node = loader.load();
final var factory = (ObjectMapper.Factory) Objects.requireNonNull(node.options().serializers().get(AnnouncementMessages2.class));
final var mutable = (ObjectMapper.Mutable<AnnouncementMessages2>) factory.get(AnnouncementMessages2.class);
mutable.load(this, node);
node.set(AnnouncementMessages2.class, this);
loader.save(node);
}
} |
Beta Was this translation helpful? Give feedback.
-
That design is strongly discouraged -- it's something you won't be able to do with, say, records, but it is something you can do if you have an existing application you are trying to migrate over. Note that the specific type returned by the call to |
Beta Was this translation helpful? Give feedback.
You can get a Mutable ObjectFactory which you can then load an existing instance from a configuration node.