-
-
Notifications
You must be signed in to change notification settings - Fork 69
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
Interface-based config support #412
Draft
Tim203
wants to merge
36
commits into
SpongePowered:master
Choose a base branch
from
Tim203:feature/interfaces
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 11 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
f24998d
Initial work to support interface-based configs
Tim203 efcb213
Allow all tests to run properly
Tim203 b29d241
Split some classes and added Range annotations
Tim203 e4b7f7f
Added default value annotations and restructured tests
Tim203 948cebe
Allow setter return type to be non-void
Tim203 ba11369
Auto-generate simple mappings
Tim203 7f6059f
Added Hidden annotation and added Processor.AdvancedFactory to aid it
Tim203 78bcaf5
Added support for some build-in annotations and added another addProc…
Tim203 c8524c8
Started working on adding tests for interfaces runtime
Tim203 336de1c
Merge remote-tracking branch 'origin/master' into feature/interfaces
Tim203 6f23846
Use correct impl name for mappings
Tim203 f07dc2f
chore(build): Only set test flags on newer JDK versions
zml2008 6c7f27c
Add all annotations that support fields. Use messager for errors
Tim203 c009449
Made AnnotationDefaults easier to follow
Tim203 a9c0e2f
Added support for default getters and default setters
Tim203 e0d9d42
Notify users about Hidden limitation. Optimized Hidden constraint
Tim203 224e87c
Exit gracefully on failure
Tim203 b62cab5
Add support for Gradle incremental annotation processing
Tim203 2d89b9d
Added Field annotation
Tim203 40d1c07
Apply spotless
Tim203 ab13924
Applied forbiddenApi fixes
Tim203 c5533d5
Renamed error to printError to trick PMD
Tim203 e9c0dfc
spotlessApply
Tim203 ebed0c5
Fix pmdTest
Tim203 27786f0
Set core as api dependency
Tim203 2add4c9
Use superinterface instead of enclosed element
Tim203 c2dfc96
Set a default value for config sections
Tim203 1af436d
Update test
Tim203 7e45e31
Added serialization to InterfaceTypeSerializer
Tim203 6945a5d
Respect superclasses' declaration of Exclude
Camotoy edd0685
Friendly error if implementation name cannot be found
Camotoy 4347700
Made it easier to use the interface's default options
Tim203 e79e4d8
Oops, it's the other way around woo
Tim203 31c63fd
Superclasses with ConfigSerializable define order
Camotoy 7543cd4
Don't try to initialize ConfigSerializable if @Field is marked
Camotoy 7b4769c
InterfaceDefaultOptions#addTo with ObjectMapper modification
Camotoy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
plugins { | ||
id "org.spongepowered.configurate.build.component" | ||
} | ||
|
||
description = "Annotation processor for Configurate to generate an implementation for config interfaces" | ||
|
||
dependencies { | ||
implementation projects.core | ||
implementation projects.extra.extraInterface | ||
implementation libs.javapoet | ||
implementation libs.auto.service | ||
annotationProcessor libs.auto.service | ||
|
||
testImplementation libs.compile.testing | ||
} | ||
|
||
// there is no javadoc | ||
tasks.withType(Javadoc).configureEach { enabled = false } | ||
|
||
tasks.withType(Test).configureEach { | ||
// See: https://github.com/google/compile-testing/issues/222 | ||
jvmArgs '--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED' | ||
jvmArgs '--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED' | ||
jvmArgs '--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED' | ||
} |
30 changes: 30 additions & 0 deletions
30
...c/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationConfigurate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.spongepowered.configurate.interfaces.processor; | ||
|
||
import com.squareup.javapoet.AnnotationSpec; | ||
import com.squareup.javapoet.FieldSpec; | ||
import java.lang.annotation.Annotation; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import javax.lang.model.element.ExecutableElement; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
import org.spongepowered.configurate.objectmapping.meta.Comment; | ||
import org.spongepowered.configurate.objectmapping.meta.Matches; | ||
import org.spongepowered.configurate.objectmapping.meta.Required; | ||
|
||
final class AnnotationConfigurate { | ||
|
||
private static final List<Class<? extends Annotation>> annotations = Arrays.asList(Comment.class, Matches.class, Required.class); | ||
|
||
private AnnotationConfigurate() {} | ||
|
||
static void process(final ExecutableElement element, final FieldSpec.Builder fieldSpec) { | ||
for (final Class<? extends Annotation> annotation : annotations) { | ||
final @Nullable Annotation current = element.getAnnotation(annotation); | ||
//noinspection ConstantValue | ||
if (current != null) { | ||
fieldSpec.addAnnotation(AnnotationSpec.get(current)); | ||
} | ||
} | ||
} | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
.../src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationDefaults.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package org.spongepowered.configurate.interfaces.processor; | ||
|
||
import com.google.auto.common.MoreTypes; | ||
import com.squareup.javapoet.AnnotationSpec; | ||
import com.squareup.javapoet.CodeBlock; | ||
import com.squareup.javapoet.FieldSpec; | ||
import java.lang.annotation.Annotation; | ||
import javax.lang.model.element.ExecutableElement; | ||
import javax.lang.model.type.TypeMirror; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
import org.spongepowered.configurate.interfaces.meta.defaults.DefaultBoolean; | ||
import org.spongepowered.configurate.interfaces.meta.defaults.DefaultDecimal; | ||
import org.spongepowered.configurate.interfaces.meta.defaults.DefaultNumeric; | ||
import org.spongepowered.configurate.interfaces.meta.defaults.DefaultString; | ||
|
||
final class AnnotationDefaults { | ||
|
||
private AnnotationDefaults() {} | ||
|
||
static void process(final ExecutableElement element, final TypeMirror nodeType, final FieldSpec.Builder fieldSpec) { | ||
final @Nullable DefaultBoolean defaultBoolean = element.getAnnotation(DefaultBoolean.class); | ||
final @Nullable DefaultDecimal defaultDecimal = element.getAnnotation(DefaultDecimal.class); | ||
final @Nullable DefaultNumeric defaultNumeric = element.getAnnotation(DefaultNumeric.class); | ||
final @Nullable DefaultString defaultString = element.getAnnotation(DefaultString.class); | ||
//noinspection ConstantValue not everything is nonnull by default | ||
final boolean hasDefault = defaultBoolean != null || defaultDecimal != null || defaultNumeric != null || defaultString != null; | ||
|
||
@Nullable Object defaultValue = null; | ||
boolean isString = false; | ||
//noinspection ConstantValue | ||
if (hasDefault) { | ||
if (MoreTypes.isTypeOf(Boolean.TYPE, nodeType)) { | ||
//noinspection ConstantValue | ||
if (defaultBoolean == null) { | ||
throw new IllegalStateException("A default value of the incorrect type was provided for " + element); | ||
} | ||
defaultValue = defaultBoolean.value(); | ||
buildAndAddAnnotation(fieldSpec, DefaultBoolean.class, defaultValue, false); | ||
|
||
} else if (Utils.isDecimal(nodeType)) { | ||
//noinspection ConstantValue | ||
if (defaultDecimal == null) { | ||
throw new IllegalStateException("A default value of the incorrect type was provided for " + element); | ||
} | ||
defaultValue = defaultDecimal.value() + (MoreTypes.isTypeOf(Float.TYPE, nodeType) ? "F" : "D"); | ||
buildAndAddAnnotation(fieldSpec, DefaultDecimal.class, defaultValue, false); | ||
|
||
} else if (Utils.isNumeric(nodeType)) { | ||
//noinspection ConstantValue | ||
if (defaultNumeric == null) { | ||
throw new IllegalStateException("A default value of the incorrect type was provided for " + element); | ||
} | ||
defaultValue = defaultNumeric.value(); | ||
if (MoreTypes.isTypeOf(Long.TYPE, nodeType)) { | ||
defaultValue += "L"; | ||
} | ||
buildAndAddAnnotation(fieldSpec, DefaultNumeric.class, defaultValue, false); | ||
|
||
} else if (MoreTypes.isTypeOf(String.class, nodeType)) { | ||
//noinspection ConstantValue | ||
if (defaultString == null) { | ||
throw new IllegalStateException("A default value of the incorrect type was provided for " + element); | ||
} | ||
defaultValue = defaultString.value(); | ||
isString = true; | ||
buildAndAddAnnotation(fieldSpec, DefaultString.class, defaultValue, true); | ||
} | ||
} | ||
|
||
if (defaultValue != null) { | ||
fieldSpec.initializer(isString ? "$S" : "$L", defaultValue); | ||
} | ||
} | ||
|
||
private static void buildAndAddAnnotation( | ||
final FieldSpec.Builder fieldSpec, | ||
final Class<? extends Annotation> annotationClass, | ||
final Object value, | ||
final boolean isString | ||
) { | ||
fieldSpec.addAnnotation( | ||
AnnotationSpec.builder(annotationClass) | ||
.addMember("value", CodeBlock.of(isString ? "$S" : "$L", value)) | ||
.build() | ||
); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationHidden.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.spongepowered.configurate.interfaces.processor; | ||
|
||
import com.squareup.javapoet.FieldSpec; | ||
import javax.lang.model.element.ExecutableElement; | ||
import org.spongepowered.configurate.interfaces.meta.Hidden; | ||
|
||
final class AnnotationHidden { | ||
|
||
private AnnotationHidden() {} | ||
|
||
static void process(final ExecutableElement element, final FieldSpec.Builder fieldSpec) { | ||
if (Utils.hasAnnotation(element, Hidden.class)) { | ||
fieldSpec.addAnnotation(Hidden.class); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Locally you just test with whatever your current JVM runtime is -- this fails on CI because we execute tests on a bunch of different JVMs, including J8 where these module args don't exist. You can simulate this conditional locally with the
-PstrictMultireleaseVersions=true
gradle property (or just setting the CI environment variable).