diff options
author | nextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com> | 2022-07-25 11:39:45 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-25 12:39:45 +0200 |
commit | 64329ad7ccedba53c47ccd3a08b9eb97e756122c (patch) | |
tree | 296b1972e61f80b21f501f30d2489a894a70c5df /src/main/java/cc/polyfrost/oneconfig/config | |
parent | 80d2e24958526c8274ed9c58e56e60ab1aaaf731 (diff) | |
download | OneConfig-64329ad7ccedba53c47ccd3a08b9eb97e756122c.tar.gz OneConfig-64329ad7ccedba53c47ccd3a08b9eb97e756122c.tar.bz2 OneConfig-64329ad7ccedba53c47ccd3a08b9eb97e756122c.zip |
Config Checker (#67)
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/config')
4 files changed, 48 insertions, 1 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java b/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java index 2b3c90b..bacfe8b 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java @@ -1,6 +1,7 @@ package cc.polyfrost.oneconfig.config.core; -import cc.polyfrost.oneconfig.config.annotations.Exclude; +import cc.polyfrost.oneconfig.config.core.exceptions.InvalidTypeException; +import cc.polyfrost.oneconfig.config.data.OptionType; import cc.polyfrost.oneconfig.config.elements.BasicOption; import cc.polyfrost.oneconfig.config.elements.OptionCategory; import cc.polyfrost.oneconfig.config.elements.OptionPage; @@ -20,31 +21,49 @@ public class ConfigUtils { public static BasicOption getOption(Option option, Field field, Object instance) { switch (option.type()) { case SWITCH: + check(OptionType.SWITCH, field, boolean.class, Boolean.class); return ConfigSwitch.create(field, instance); case CHECKBOX: + check(OptionType.CHECKBOX, field, boolean.class, Boolean.class); return ConfigCheckbox.create(field, instance); case INFO: return ConfigInfo.create(field, instance); case HEADER: return ConfigHeader.create(field, instance); case COLOR: + check(OptionType.COLOR, field, OneColor.class); return ConfigColorElement.create(field, instance); case DROPDOWN: + check(OptionType.DROPDOWN, field, int.class, Integer.class); return ConfigDropdown.create(field, instance); case TEXT: + check(OptionType.TEXT, field, String.class); return ConfigTextBox.create(field, instance); case BUTTON: + check(OptionType.BUTTON, field, Runnable.class); return ConfigButton.create(field, instance); case SLIDER: + check(OptionType.SLIDER, field, int.class, float.class, Integer.class, Float.class); return ConfigSlider.create(field, instance); case KEYBIND: + check(OptionType.KEYBIND, field, OneKeyBind.class); return ConfigKeyBind.create(field, instance); case DUAL_OPTION: + check(OptionType.DUAL_OPTION, field, boolean.class, Boolean.class); return ConfigDualOption.create(field, instance); } return null; } + private static void check(OptionType type, Field field, Class<?>... expectedType) { + // I have tried to check for supertype classes like Boolean other ways. + // but they actually don't extend their primitive types (because that is impossible) so isAssignableFrom doesn't work. + for (Class<?> clazz : expectedType) { + if(field.getType().equals(clazz)) return; + } + throw new InvalidTypeException("Field " + field.getName() + " in config " + field.getDeclaringClass().getName() + " is annotated as a " + type.toString() + ", but is not of valid type, expected " + Arrays.toString(expectedType) + " (found " + field.getType() + ")"); + } + public static ArrayList<BasicOption> getClassOptions(Object object) { ArrayList<BasicOption> options = new ArrayList<>(); ArrayList<Field> fields = getClassFields(object.getClass()); diff --git a/src/main/java/cc/polyfrost/oneconfig/config/core/exceptions/InvalidTypeException.java b/src/main/java/cc/polyfrost/oneconfig/config/core/exceptions/InvalidTypeException.java new file mode 100644 index 0000000..4ccaaf8 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/config/core/exceptions/InvalidTypeException.java @@ -0,0 +1,26 @@ +package cc.polyfrost.oneconfig.config.core.exceptions; + +public class InvalidTypeException extends RuntimeException { + + public InvalidTypeException() { + super(); + } + + public InvalidTypeException(String message) { + super(message); + } + + public InvalidTypeException(String message, Throwable cause) { + super(message, cause); + } + + public InvalidTypeException(Throwable cause) { + super(cause); + } + + protected InvalidTypeException(String message, Throwable cause, + boolean enableSuppression, + boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/config/gson/NonProfileSpecificExclusionStrategy.java b/src/main/java/cc/polyfrost/oneconfig/config/gson/NonProfileSpecificExclusionStrategy.java index e773295..b737d70 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/gson/NonProfileSpecificExclusionStrategy.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/gson/NonProfileSpecificExclusionStrategy.java @@ -14,6 +14,7 @@ public class NonProfileSpecificExclusionStrategy extends ExclusionUtils implemen @Override public boolean shouldSkipField(FieldAttributes f) { if (isSuperClassOf(f.getDeclaredClass(), Config.class)) return true; + if (f.getDeclaredClass().isAssignableFrom(Runnable.class)) return true; if (f.getAnnotation(NonProfileSpecific.class) == null) return true; Exclude exclude = f.getAnnotation(Exclude.class); return exclude != null; diff --git a/src/main/java/cc/polyfrost/oneconfig/config/gson/ProfileExclusionStrategy.java b/src/main/java/cc/polyfrost/oneconfig/config/gson/ProfileExclusionStrategy.java index ad9722b..0df63b0 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/gson/ProfileExclusionStrategy.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/gson/ProfileExclusionStrategy.java @@ -14,6 +14,7 @@ public class ProfileExclusionStrategy extends ExclusionUtils implements Exclusio @Override public boolean shouldSkipField(FieldAttributes f) { if (isSuperClassOf(f.getDeclaredClass(), Config.class)) return true; + if (f.getDeclaredClass().isAssignableFrom(Runnable.class)) return true; if (f.getAnnotation(NonProfileSpecific.class) != null) return true; Exclude exclude = f.getAnnotation(Exclude.class); return exclude != null; |