any way to modify existing instance's fields rather than creating new instances? #362
|
node.get(XXXConfig.class, emptyInstance); |
Answered by
Machine-Maker
Dec 5, 2022
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); |
0 replies
Answer selected by
zml2008
|
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);
}
} |
0 replies
|
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 |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can get a Mutable ObjectFactory which you can then load an existing instance from a configuration node.