diff --git a/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationDefaults.java b/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationDefaults.java index 520b90a2..0ae7e3e3 100644 --- a/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationDefaults.java +++ b/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationDefaults.java @@ -33,6 +33,7 @@ import javax.lang.model.AnnotatedConstruct; import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.TypeElement; import javax.lang.model.type.TypeMirror; final class AnnotationDefaults implements AnnotationProcessor { @@ -48,6 +49,7 @@ public Set> processes() { @Override public void process( + final TypeElement targetInterface, final ExecutableElement element, final TypeMirror nodeType, final FieldSpecBuilderTracker fieldSpec @@ -56,7 +58,7 @@ public void process( // first, handle default value of a default method getter if (element.isDefault() && element.getParameters().isEmpty() && hasNoAnnotationDefaults(element)) { - fieldSpec.initializer("$T.super.$L()", element.getEnclosingElement(), element.getSimpleName()); + fieldSpec.initializer("$T.super.$L()", targetInterface, element.getSimpleName()); return; } diff --git a/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationHidden.java b/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationHidden.java index 9ea1c8b6..b55cf42b 100644 --- a/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationHidden.java +++ b/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationHidden.java @@ -25,6 +25,7 @@ import java.util.Set; import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.TypeElement; import javax.lang.model.type.TypeMirror; final class AnnotationHidden implements AnnotationProcessor { @@ -42,6 +43,7 @@ public Set> processes() { @Override public void process( + final TypeElement targetInterface, final ExecutableElement element, final TypeMirror nodeType, final FieldSpecBuilderTracker fieldSpec diff --git a/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationOthers.java b/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationOthers.java index f26a71c7..1452f96d 100644 --- a/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationOthers.java +++ b/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationOthers.java @@ -48,7 +48,11 @@ public Set> processes() { } @Override - public void process(final ExecutableElement element, final TypeMirror nodeType, final FieldSpecBuilderTracker fieldSpec) { + public void process( + final TypeElement targetInterface, + final ExecutableElement element, + final TypeMirror nodeType, + final FieldSpecBuilderTracker fieldSpec) { for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) { //noinspection UnstableApiUsage final TypeElement annotationType = MoreElements.asType(annotationMirror.getAnnotationType().asElement()); diff --git a/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationProcessor.java b/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationProcessor.java index e34218a3..c4468c70 100644 --- a/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationProcessor.java +++ b/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationProcessor.java @@ -20,6 +20,7 @@ import java.util.Set; import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.TypeElement; import javax.lang.model.type.TypeMirror; interface AnnotationProcessor { @@ -40,6 +41,11 @@ interface AnnotationProcessor { * @param fieldSpec the builder of the field that is being generated * @throws IllegalStateException when something goes wrong */ - void process(ExecutableElement element, TypeMirror nodeType, FieldSpecBuilderTracker fieldSpec) throws IllegalStateException; + void process( + TypeElement targetInterface, + ExecutableElement element, + TypeMirror nodeType, + FieldSpecBuilderTracker fieldSpec + ) throws IllegalStateException; } diff --git a/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationProcessorHandler.java b/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationProcessorHandler.java index a02ecf68..b16cd6a3 100644 --- a/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationProcessorHandler.java +++ b/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/AnnotationProcessorHandler.java @@ -22,6 +22,7 @@ import java.util.List; import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.TypeElement; import javax.lang.model.type.TypeMirror; final class AnnotationProcessorHandler { @@ -37,11 +38,15 @@ final class AnnotationProcessorHandler { private AnnotationProcessorHandler() {} - static void handle(final ExecutableElement element, final TypeMirror nodeType, final FieldSpec.Builder fieldSpec) { + static void handle( + final TypeElement targetInterface, + final ExecutableElement element, + final TypeMirror nodeType, + final FieldSpec.Builder fieldSpec) { final FieldSpecBuilderTracker fieldTracker = new FieldSpecBuilderTracker(fieldSpec); for (AnnotationProcessor handler : HANDLERS) { - handler.process(element, nodeType, fieldTracker); + handler.process(targetInterface, element, nodeType, fieldTracker); fieldTracker.processed(handler.processes()); } } diff --git a/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/ConfigImplementationGenerator.java b/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/ConfigImplementationGenerator.java index 19973aaf..bee1b4ba 100644 --- a/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/ConfigImplementationGenerator.java +++ b/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/ConfigImplementationGenerator.java @@ -47,10 +47,10 @@ class ConfigImplementationGenerator { ConfigImplementationGenerator( final ConfigImplementationGeneratorProcessor processor, - final TypeElement configInterfaceType + final TypeElement source ) { this.processor = processor; - this.source = configInterfaceType; + this.source = source; } /** @@ -158,8 +158,22 @@ private boolean gatherElementSpec( fieldSpec.addModifiers(Modifier.TRANSIENT); } + // set a default value for config subsections + final TypeElement nodeTypeElement = Utils.toBoxedTypeElement(nodeType, this.processor.typeUtils); + if (!element.isDefault() && hasAnnotation(nodeTypeElement, ConfigSerializable.class)) { + ClassName configClass = ClassName.get(nodeTypeElement); + if (nodeTypeElement.getKind().isInterface()) { + // first find the generated class for given type + String implName = this.processor.generatedClasses().getProperty(configClass.reflectionName()); + // make it canonical and replace superinterface type with source interface type if present + implName = implName.replace('$', '.').replace(type.getQualifiedName(), this.source.getQualifiedName()); + configClass = ClassName.bestGuess(implName); + } + fieldSpec.initializer("new $T()", configClass); + } + //todo add tests for hidden in both ap and interfaces and defaults in interfaces - AnnotationProcessorHandler.handle(element, nodeType, fieldSpec); + AnnotationProcessorHandler.handle(this.source, element, nodeType, fieldSpec); spec.add(simpleName, fieldSpec); } diff --git a/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/Utils.java b/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/Utils.java index 21fc5e04..8f914349 100644 --- a/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/Utils.java +++ b/extra/interface/ap/src/main/java/org/spongepowered/configurate/interfaces/processor/Utils.java @@ -27,6 +27,7 @@ import javax.lang.model.element.ElementKind; import javax.lang.model.element.TypeElement; import javax.lang.model.type.TypeMirror; +import javax.lang.model.util.Types; final class Utils { @@ -67,4 +68,11 @@ static boolean isNumeric(final TypeMirror typeMirror) { || MoreTypes.isTypeOf(Long.TYPE, typeMirror); } + public static TypeElement toBoxedTypeElement(final TypeMirror mirror, final Types typeUtils) { + if (mirror.getKind().isPrimitive()) { + return typeUtils.boxedClass(MoreTypes.asPrimitiveType(mirror)); + } + return MoreTypes.asTypeElement(mirror); + } + }