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

Make PartitioningSerializer ignore static fields #199

Open
wants to merge 1 commit into
base: v8
Choose a base branch
from
Open
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 @@ -22,9 +22,12 @@
import me.shedaniel.autoconfig.ConfigData;
import me.shedaniel.autoconfig.annotation.Config;
import me.shedaniel.autoconfig.util.Utils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand All @@ -34,8 +37,12 @@
/**
* This serializer wraps another serializer and produces a folder with each field of the config
* corresponding to a single config file.
* The top level config must inherit from GlobalData.
* Each field of the top level config must be of a type inheriting from ConfigData.
* <br>
* The top level config must inherit from {@link GlobalData}.
* <br>
* Each field of the top level config must be of a type inheriting from {@link ConfigData}.
* <br>
* Fields marked {@code static} are ignored.
*/
public final class PartitioningSerializer<T extends PartitioningSerializer.GlobalData, M extends ConfigData> implements ConfigSerializer<T> {

Expand Down Expand Up @@ -101,6 +108,7 @@ private static boolean isValidModule(Field field) {

private static List<Field> getModuleFields(Class<?> configClass) {
return Arrays.stream(configClass.getDeclaredFields())
.filter(field -> !Modifier.isStatic(field.getModifiers()))
.filter(PartitioningSerializer::isValidModule)
.collect(Collectors.toList());
}
Expand Down Expand Up @@ -129,10 +137,18 @@ public T createDefault() {
public static abstract class GlobalData implements ConfigData {

public GlobalData() {
Arrays.stream(getClass().getDeclaredFields())
Logger logger = LogManager.getLogger();
Field[] fields = getClass().getDeclaredFields();

Arrays.stream(fields)
.filter(field -> Modifier.isStatic(field.getModifiers()))
.forEach(field -> logger.warn("Ignoring static field: %s".formatted(field)));

Arrays.stream(fields)
.filter(field -> !Modifier.isStatic(field.getModifiers()))
.filter(field -> !isValidModule(field))
.forEach(field -> {
throw new RuntimeException(String.format("Invalid module: %s", field));
throw new RuntimeException("Invalid module: %s".formatted(field));
});
}

Expand Down