Skip to content

Commit

Permalink
Merge pull request #4 from alkyaly/fix/comments-in-nested-classes
Browse files Browse the repository at this point in the history
Fixes comments not applying when inside nested classes.
  • Loading branch information
Draylar authored Dec 9, 2021
2 parents 3ecbae5 + 228c1bb commit 6ed97d4
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class OmegaConfig {

Expand Down Expand Up @@ -109,10 +114,9 @@ public static <T extends Config> void writeConfig(Class<T> configClass, T instan
addFieldComments(field, keyToComments);
}

// get inner-class fields
// TODO: recursively get inner classes?
for (Class<?> innerClass : configClass.getDeclaredClasses()) {
for (Field field : innerClass.getDeclaredFields()) {
// "flattens" all the inner classes of the Config class into a single list
for (Class<?> clazz : flatten(configClass.getDeclaredClasses())) {
for (Field field : clazz.getDeclaredFields()) {
addFieldComments(field, keyToComments);
}
}
Expand Down Expand Up @@ -158,6 +162,28 @@ public static <T extends Config> void writeConfig(Class<T> configClass, T instan
}
}

private static List<Class<?>> flatten(Class<?>[] array) {
List<Class<?>> list = new ArrayList<>();

for (Class<?> clazz : array) {
populateRecursively(list, clazz);
}

return list;
}

private static void populateRecursively(List<Class<?>> list, Class<?> aClass) {
list.add(aClass);

Class<?>[] classes = aClass.getDeclaredClasses();

if (classes.length != 0) {
for (Class<?> clazz : classes) {
populateRecursively(list, clazz);
}
}
}

private static void addFieldComments(Field field, Map<String, String> keyToComments) {
String fieldName = field.getName();
Annotation[] annotations = field.getDeclaredAnnotations();
Expand Down
2 changes: 2 additions & 0 deletions src/testmod/java/draylar/omegatest/OmegaTestMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import draylar.omegaconfig.OmegaConfig;
import draylar.omegatest.config.ClassConfigTest;
import draylar.omegatest.config.CommentedNestedClassConfig;
import draylar.omegatest.config.NestedConfigTest;
import draylar.omegatest.config.SimpleConfigTest;
import draylar.omegatest.config.StructuresConfigTest;
Expand All @@ -13,6 +14,7 @@ public class OmegaTestMain implements ModInitializer {
public static final StructuresConfigTest MO_CONFIG = OmegaConfig.register(StructuresConfigTest.class);
public static final NestedConfigTest NESTED = OmegaConfig.register(NestedConfigTest.class);
public static final ClassConfigTest CLASS = OmegaConfig.register(ClassConfigTest.class);
public static final CommentedNestedClassConfig COMMENTED_NESTED_CLASS = OmegaConfig.register(CommentedNestedClassConfig.class);

@Override
public void onInitialize() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package draylar.omegatest.config;

import draylar.omegaconfig.api.Comment;
import draylar.omegaconfig.api.Config;

public class CommentedNestedClassConfig implements Config {

@Comment("Very nested test")
public First first = new First();

@Comment("True")
public boolean test = false;

@Comment("First-Level")
public VeryNested veryNested = new VeryNested();

public static class First {
@Comment("Cucumbers")
public boolean cucumbers = true;

@Comment("InnerFirst")
public InnerFirst innerFirst = new InnerFirst();

@Comment("InnerSecond")
public InnerSecond innerSecond = new InnerSecond();

public static class InnerFirst {
@Comment("Number of Tomatoes")
public int numberOfTomatoes = 10;
}

public static class InnerSecond {
@Comment("Inner Second First")
public InnerSecondFirst innerSecondFirst = new InnerSecondFirst();

public static class InnerSecondFirst {
@Comment("Olive Oil")
public String oliveOil = "oliveOil";
}
}
}

public static class VeryNested {
@Comment("Second-Level")
public Very very = new Very();
public static class Very {
@Comment("Third-Level")
public VeryVery veryVery = new VeryVery();
public static class VeryVery {
@Comment("Forth-Level")
public VeryVeryVery veryVeryVery = new VeryVeryVery();
public static class VeryVeryVery {
@Comment("Fifth-Level")
public VeryVeryVeryVery veryVeryVeryVery = new VeryVeryVeryVery();
public static class VeryVeryVeryVery {
@Comment("Sixth-Level")
public String last = "last";
}
}
}
}
}

@Override
public String getName() {
return "commented-inner-classes";
}

@Override
public String getExtension() {
return "json5";
}
}

0 comments on commit 6ed97d4

Please sign in to comment.