-
-
Notifications
You must be signed in to change notification settings - Fork 10
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 ReflectiveConfig.Section
implement ValueTreeNode.Section
#49
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,13 +23,15 @@ | |
import org.quiltmc.config.impl.ConfigFieldAnnotationProcessors; | ||
import org.quiltmc.config.api.exceptions.ConfigCreationException; | ||
import org.quiltmc.config.api.exceptions.ConfigFieldException; | ||
import org.quiltmc.config.api.metadata.MetadataType; | ||
import org.quiltmc.config.impl.tree.TrackedValueImpl; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Modifier; | ||
import java.util.Objects; | ||
|
||
public class ReflectiveConfigCreator<C> implements Config.Creator { | ||
private final Class<C> creatorClass; | ||
|
@@ -39,6 +41,31 @@ public ReflectiveConfigCreator(Class<C> creatorClass) { | |
this.creatorClass = creatorClass; | ||
} | ||
|
||
public static class SectionMarker { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Im not sure if we should use metadata for pure implementation details. You can get all metadata an elements holds, so it would leak implementation details I think. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I'm not a huge fan of it either, but this was a quick fix and trying to keep track of this information in the creator looked like a pain. I could take a look at doing that in the future, but I'm not sure when I'll have the time... |
||
public static MetadataType<SectionMarker, Builder> TYPE = MetadataType.create(Builder::new); | ||
public final ReflectiveConfig.Section self; | ||
|
||
private SectionMarker(ReflectiveConfig.Section self) { | ||
this.self = self; | ||
} | ||
|
||
// ReadWriteCycleTest needs these to be equal | ||
@Override | ||
public boolean equals(Object obj) { | ||
return obj instanceof SectionMarker && ((SectionMarker) obj).self.getClass().equals(this.self.getClass()); | ||
} | ||
|
||
public static class Builder implements MetadataType.Builder<SectionMarker> { | ||
private ReflectiveConfig.Section self; | ||
|
||
@Override | ||
public ReflectiveConfigCreator.SectionMarker build() { | ||
Objects.requireNonNull(this.self); | ||
return new SectionMarker(this.self); | ||
} | ||
} | ||
} | ||
|
||
@SuppressWarnings({"unchecked", "rawtypes"}) | ||
private void createField(Config.SectionBuilder builder, Object object, Field field) throws IllegalAccessException { | ||
if (!Modifier.isStatic(field.getModifiers()) && !Modifier.isTransient(field.getModifiers())) { | ||
|
@@ -127,6 +154,8 @@ private void createField(Config.SectionBuilder builder, Object object, Field fie | |
} | ||
} | ||
} | ||
|
||
b.metadata(SectionMarker.TYPE, metaBuilder -> metaBuilder.self = (ReflectiveConfig.Section) defaultValue); | ||
}); | ||
} else if (defaultValue == null) { | ||
throw new ConfigFieldException("Default value for field '" + field.getName() + "' cannot be null"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this should either be put into
ReflectiveConfig.setWrappedConfig
, or into the impl package with anothersetWrappedSection
in here.