From a6b99b3c0a4c2b99c827ffd7a33daa804338bad0 Mon Sep 17 00:00:00 2001 From: KrLite <68179735+KrLite@users.noreply.github.com> Date: Thu, 2 May 2024 18:35:08 +0800 Subject: [PATCH] enums --- .../nightautoconfig/spec/Specs.java | 70 ++++++++++++++----- 1 file changed, 52 insertions(+), 18 deletions(-) diff --git a/src/main/java/band/kessokuteatime/nightautoconfig/spec/Specs.java b/src/main/java/band/kessokuteatime/nightautoconfig/spec/Specs.java index 01530d1..a8fefec 100644 --- a/src/main/java/band/kessokuteatime/nightautoconfig/spec/Specs.java +++ b/src/main/java/band/kessokuteatime/nightautoconfig/spec/Specs.java @@ -13,9 +13,9 @@ import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.List; -import java.util.Locale; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Predicate; import java.util.stream.Stream; @@ -184,11 +184,11 @@ private void appendBasicSpecs(ConfigSpec spec) { return; } - boolean isFloat = List.of(float.class, Float.class).contains(field.getType()); + final boolean isFloat = List.of(float.class, Float.class).contains(field.getType()); if (field.isAnnotationPresent(SpecElementValidator.class)) { - SpecElementValidator annotation = field.getAnnotation(SpecElementValidator.class); - Predicate validator = annotation.value().getDeclaredConstructor().newInstance(); + SpecElementValidator elementValidator = field.getAnnotation(SpecElementValidator.class); + Predicate validator = elementValidator.value().getDeclaredConstructor().newInstance(); if (isFloat) { // Store float as double @@ -204,8 +204,7 @@ private void appendBasicSpecs(ConfigSpec spec) { spec.define(getPath(field), value); } } - } catch (IllegalAccessException | InvocationTargetException | InstantiationException | - NoSuchMethodException e) { + } catch (Exception e) { throw new RuntimeException(e); } }); @@ -234,9 +233,9 @@ private void appendInRangeSpecs(ConfigSpec spec) { .filter(typeChecker(SpecInRangeByte.associatedTypes)) .filter(field -> field.isAnnotationPresent(SpecInRangeByte.class)) .forEach(field -> { - SpecInRangeByte annotation = field.getAnnotation(SpecInRangeByte.class); + SpecInRangeByte inRange = field.getAnnotation(SpecInRangeByte.class); try { - appendInRangeSpec(spec, field, annotation.min(), annotation.max()); + appendInRangeSpec(spec, field, inRange.min(), inRange.max()); } catch (IllegalAccessException e) { throw new RuntimeException(e); } @@ -247,9 +246,9 @@ private void appendInRangeSpecs(ConfigSpec spec) { .filter(typeChecker(SpecInRangeShort.associatedTypes)) .filter(field -> field.isAnnotationPresent(SpecInRangeShort.class)) .forEach(field -> { - SpecInRangeShort annotation = field.getAnnotation(SpecInRangeShort.class); + SpecInRangeShort inRange = field.getAnnotation(SpecInRangeShort.class); try { - appendInRangeSpec(spec, field, annotation.min(), annotation.max()); + appendInRangeSpec(spec, field, inRange.min(), inRange.max()); } catch (IllegalAccessException e) { throw new RuntimeException(e); } @@ -260,9 +259,9 @@ private void appendInRangeSpecs(ConfigSpec spec) { .filter(typeChecker(SpecInRangeInt.associatedTypes)) .filter(field -> field.isAnnotationPresent(SpecInRangeInt.class)) .forEach(field -> { - SpecInRangeInt annotation = field.getAnnotation(SpecInRangeInt.class); + SpecInRangeInt inRange = field.getAnnotation(SpecInRangeInt.class); try { - appendInRangeSpec(spec, field, annotation.min(), annotation.max()); + appendInRangeSpec(spec, field, inRange.min(), inRange.max()); } catch (IllegalAccessException e) { throw new RuntimeException(e); } @@ -273,9 +272,9 @@ private void appendInRangeSpecs(ConfigSpec spec) { .filter(typeChecker(SpecInRangeLong.associatedTypes)) .filter(field -> field.isAnnotationPresent(SpecInRangeLong.class)) .forEach(field -> { - SpecInRangeLong annotation = field.getAnnotation(SpecInRangeLong.class); + SpecInRangeLong inRange = field.getAnnotation(SpecInRangeLong.class); try { - appendInRangeSpec(spec, field, annotation.min(), annotation.max()); + appendInRangeSpec(spec, field, inRange.min(), inRange.max()); } catch (IllegalAccessException e) { throw new RuntimeException(e); } @@ -286,9 +285,9 @@ private void appendInRangeSpecs(ConfigSpec spec) { .filter(typeChecker(List.of(Float.class, float.class))) .filter(field -> field.isAnnotationPresent(SpecInRangeDouble.class)) .forEach(field -> { - SpecInRangeDouble annotation = field.getAnnotation(SpecInRangeDouble.class); + SpecInRangeDouble inRange = field.getAnnotation(SpecInRangeDouble.class); try { - appendInRangeSpec(spec, field, safeDouble(field.get(t)), annotation.min(), annotation.max()); + appendInRangeSpec(spec, field, safeDouble(field.get(t)), inRange.min(), inRange.max()); } catch (IllegalAccessException e) { throw new RuntimeException(e); } @@ -299,12 +298,47 @@ private void appendInRangeSpecs(ConfigSpec spec) { .filter(typeChecker(List.of(Double.class, double.class))) .filter(field -> field.isAnnotationPresent(SpecInRangeDouble.class)) .forEach(field -> { - SpecInRangeDouble annotation = field.getAnnotation(SpecInRangeDouble.class); + SpecInRangeDouble inRange = field.getAnnotation(SpecInRangeDouble.class); try { - appendInRangeSpec(spec, field, (double) field.get(t), annotation.min(), annotation.max()); + appendInRangeSpec(spec, field, (double) field.get(t), inRange.min(), inRange.max()); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }); } + + private void appendInListSpecs(ConfigSpec spec) { + Arrays.stream(nonNestedFields()) + .filter(field -> field.isAnnotationPresent(SpecInList.class)) + .filter(field -> !field.getType().isEnum()) + .forEach(field -> { + SpecInList inList = field.getAnnotation(SpecInList.class); + }); + } + + private > void appendEnumGetMethodSpecs(ConfigSpec spec) { + Arrays.stream(nonNestedFields()) + .filter(field -> field.getType().isEnum()) + .filter(field -> field.isAnnotationPresent(SpecEnumGetMethod.class)) + .forEach(field -> { + try { + field.setAccessible(true); + Object value = field.get(t); + + SpecEnumGetMethod enumGetMethod = field.getAnnotation(SpecEnumGetMethod.class); + if (field.isAnnotationPresent(SpecInList.class)) { + // Restricted by acceptable values + SpecInList inList = field.getAnnotation(SpecInList.class); + Collection acceptableValues = (Collection) inList.value().getDeclaredConstructor().newInstance().acceptableValues(); + + spec.defineRestrictedEnum(getPath(field), (T) value, acceptableValues, enumGetMethod.value()); + } else { + // Unrestricted + spec.defineEnum(getPath(field), (T) value, enumGetMethod.value()); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + } }