Skip to content

Commit

Permalink
Set a default value for config sections
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim203 committed Feb 12, 2024
1 parent 2add4c9 commit c2dfc96
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,20 @@ 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(this.source, element, nodeType, fieldSpec);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
}

}

0 comments on commit c2dfc96

Please sign in to comment.