diff --git a/src/main/java/io/github/fablabsmc/fablabs/api/fiber/v1/annotation/Setting.java b/src/main/java/io/github/fablabsmc/fablabs/api/fiber/v1/annotation/Setting.java index 7f3a2ad..18aaa31 100644 --- a/src/main/java/io/github/fablabsmc/fablabs/api/fiber/v1/annotation/Setting.java +++ b/src/main/java/io/github/fablabsmc/fablabs/api/fiber/v1/annotation/Setting.java @@ -70,6 +70,15 @@ * @return An empty string ({@code ""}) if no custom name was set, or the custom name if one was set. */ String name() default ""; + + /** + * Sets the comment that will be used for this setting group. + * + *

If empty, no comment will be set. + * + * @return An empty string ({@code ""}) if no comment was set, or the comment if one was set. + */ + String comment() default ""; } @Target({}) diff --git a/src/main/java/io/github/fablabsmc/fablabs/impl/fiber/annotation/AnnotatedSettingsImpl.java b/src/main/java/io/github/fablabsmc/fablabs/impl/fiber/annotation/AnnotatedSettingsImpl.java index 0206986..6ac6580 100644 --- a/src/main/java/io/github/fablabsmc/fablabs/impl/fiber/annotation/AnnotatedSettingsImpl.java +++ b/src/main/java/io/github/fablabsmc/fablabs/impl/fiber/annotation/AnnotatedSettingsImpl.java @@ -127,8 +127,9 @@ public void processListenerField(Object pojo, Field field, String name) { @Override public void processGroup(Object pojo, Field group) throws ProcessingMemberException { try { - String name = this.findName(group); - ConfigTreeBuilder sub = this.builder.fork(name); + String name = findSettingAnnotation(Setting.Group.class, group).map(Setting.Group::name).filter(s -> !s.isEmpty()).orElse(null); + String comment = findSettingAnnotation(Setting.Group.class, group).map(Setting.Group::comment).filter(s -> !s.isEmpty()).orElse(null); + ConfigTreeBuilder sub = this.builder.fork(name).withComment(comment); group.setAccessible(true); Object subPojo = group.get(pojo); @@ -167,11 +168,10 @@ private void processSetting(Object pojo, Field setting, ConfigType !s.isEmpty()).orElseGet( - () -> findSettingAnnotation(Setting.class, field).map(Setting::name).filter(s -> !s.isEmpty()).orElseGet( - () -> this.convention.name(field.getName()) - ) - ); + return findSettingAnnotation(Setting.class, field) + .map(Setting::name) + .filter(s -> !s.isEmpty()) + .orElseGet(() -> this.convention.name(field.getName())); } @Nullable @@ -304,24 +304,20 @@ private void applyAnnotationProcessors(Object pojo, Field field, C sub, Map< return (T) type.constrain(processor, annotation, annotated); } - @SuppressWarnings("unchecked") private T findDefaultValue(Object pojo, Field field) throws FiberException { - boolean accessible = field.isAccessible(); field.setAccessible(true); - T value; try { - value = (T) field.get(pojo); + @SuppressWarnings("unchecked") T value = (T) field.get(pojo); if (value == null) { throw new MalformedFieldException("Default value for field '" + field.getName() + "' is null"); } + + return value; } catch (IllegalAccessException e) { throw new FiberException("Couldn't get value for field '" + field.getName() + "'", e); } - - field.setAccessible(accessible); - return value; } private BiConsumer constructListenerFromMember(Object pojo, Member listener, Class wantedType) throws FiberException { diff --git a/src/test/java/io/github/fablabsmc/fablabs/api/fiber/v1/annotation/AnnotatedSettingsTest.java b/src/test/java/io/github/fablabsmc/fablabs/api/fiber/v1/annotation/AnnotatedSettingsTest.java index b28bead..1b777ef 100644 --- a/src/test/java/io/github/fablabsmc/fablabs/api/fiber/v1/annotation/AnnotatedSettingsTest.java +++ b/src/test/java/io/github/fablabsmc/fablabs/api/fiber/v1/annotation/AnnotatedSettingsTest.java @@ -21,6 +21,7 @@ import io.github.fablabsmc.fablabs.api.fiber.v1.schema.type.StringSerializableType; import io.github.fablabsmc.fablabs.api.fiber.v1.schema.type.derived.ConfigTypes; import io.github.fablabsmc.fablabs.api.fiber.v1.schema.type.derived.ListConfigType; +import io.github.fablabsmc.fablabs.api.fiber.v1.tree.ConfigBranch; import io.github.fablabsmc.fablabs.api.fiber.v1.tree.ConfigLeaf; import io.github.fablabsmc.fablabs.api.fiber.v1.tree.ConfigNode; import io.github.fablabsmc.fablabs.api.fiber.v1.tree.ConfigTree; @@ -226,9 +227,10 @@ void testSubNodes() throws FiberException { SubNodePojo pojo = new SubNodePojo(); this.annotatedSettings.applyToNode(this.node, pojo); assertEquals(1, this.node.getItems().size(), "Node has one item"); - ConfigTree subnode = (ConfigTree) this.node.lookup("a"); + ConfigBranch subnode = this.node.lookupBranch("a"); assertNotNull(subnode, "Subnode exists"); assertEquals(1, subnode.getItems().size(), "Subnode has one item"); + assertEquals("A subnode", subnode.getComment()); } @Test @@ -388,7 +390,7 @@ private static class IgnoredPojo { } private static class SubNodePojo { - @Setting.Group(name = "a") + @Setting.Group(name = "a", comment = "A subnode") public SubNode node = new SubNode(); // we want to test this edge case