Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes comments not applying when inside nested classes. #4

Merged
merged 3 commits into from Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -154,6 +158,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";
}
}