From 5c2201d85a727e1d68ab64b1abdc3fd8518c816b Mon Sep 17 00:00:00 2001 From: isXander Date: Fri, 17 Nov 2023 16:40:15 +0000 Subject: Warn when using old @ConfigEntry annotation when using config v2 --- .../config/v2/impl/ConfigClassHandlerImpl.java | 26 +++++++++++++++++++--- .../v2/impl/serializer/GsonConfigSerializer.java | 2 +- 2 files changed, 24 insertions(+), 4 deletions(-) (limited to 'common/src/main/java/dev/isxander/yacl3') diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/ConfigClassHandlerImpl.java b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/ConfigClassHandlerImpl.java index c363a7d..813b3ab 100644 --- a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/ConfigClassHandlerImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/ConfigClassHandlerImpl.java @@ -1,6 +1,7 @@ package dev.isxander.yacl3.config.v2.impl; import dev.isxander.yacl3.api.*; +import dev.isxander.yacl3.config.ConfigEntry; import dev.isxander.yacl3.config.v2.api.*; import dev.isxander.yacl3.config.v2.api.autogen.AutoGen; import dev.isxander.yacl3.config.v2.api.autogen.OptionAccess; @@ -46,12 +47,24 @@ public class ConfigClassHandlerImpl implements ConfigClassHandler { this.instance = createNewObject(); this.defaults = createNewObject(); - this.fields = Arrays.stream(configClass.getDeclaredFields()) + detectOldAnnotation(configClass.getDeclaredFields()); + + this.fields = discoverFields(); + this.serializer = serializerFactory.apply(this); + } + + private ConfigFieldImpl[] discoverFields() { + return Arrays.stream(configClass.getDeclaredFields()) .peek(field -> field.setAccessible(true)) .filter(field -> field.isAnnotationPresent(SerialEntry.class) || field.isAnnotationPresent(AutoGen.class)) - .map(field -> new ConfigFieldImpl<>(new ReflectionFieldAccess<>(field, instance), new ReflectionFieldAccess<>(field, defaults), this, field.getAnnotation(SerialEntry.class), field.getAnnotation(AutoGen.class))) + .map(field -> new ConfigFieldImpl<>( + new ReflectionFieldAccess<>(field, instance), + new ReflectionFieldAccess<>(field, defaults), + this, + field.getAnnotation(SerialEntry.class), + field.getAnnotation(AutoGen.class) + )) .toArray(ConfigFieldImpl[]::new); - this.serializer = serializerFactory.apply(this); } @Override @@ -212,6 +225,13 @@ public class ConfigClassHandlerImpl implements ConfigClassHandler { } } + private void detectOldAnnotation(Field[] fields) { + boolean hasOldConfigEntry = Arrays.stream(fields) + .anyMatch(field -> field.isAnnotationPresent(ConfigEntry.class)); + + Validate.isTrue(!hasOldConfigEntry, "At least one field in %s is still annotated with the deprecated @ConfigEntry annotation. This is incorrect. Use @SerialEntry.".formatted(configClass.getName())); + } + public static class BuilderImpl implements Builder { private final Class configClass; private ResourceLocation id; diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/serializer/GsonConfigSerializer.java b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/serializer/GsonConfigSerializer.java index a60bcb1..49f2a47 100644 --- a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/serializer/GsonConfigSerializer.java +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/serializer/GsonConfigSerializer.java @@ -1,7 +1,6 @@ package dev.isxander.yacl3.config.v2.impl.serializer; import com.google.gson.*; -import dev.isxander.yacl3.config.GsonConfigInstance; import dev.isxander.yacl3.config.v2.api.*; import dev.isxander.yacl3.config.v2.api.serializer.GsonConfigSerializerBuilder; import dev.isxander.yacl3.gui.utils.ItemRegistryHelper; @@ -168,6 +167,7 @@ public class GsonConfigSerializer extends ConfigSerializer { @Override @Deprecated + @SuppressWarnings("deprecation") public void load() { YACLConstants.LOGGER.warn("Calling ConfigSerializer#load() directly is deprecated. Please use ConfigClassHandler#load() instead."); config.load(); -- cgit