Skip to content

Commit

Permalink
Merge remote-tracking branch 'Tim203/feature/interfaces'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim203 committed Feb 12, 2024
2 parents f773ee3 + c2dfc96 commit f63b345
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -48,6 +49,7 @@ public Set<Class<? extends Annotation>> processes() {

@Override
public void process(
final TypeElement targetInterface,
final ExecutableElement element,
final TypeMirror nodeType,
final FieldSpecBuilderTracker fieldSpec
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -42,6 +43,7 @@ public Set<Class<? extends Annotation>> processes() {

@Override
public void process(
final TypeElement targetInterface,
final ExecutableElement element,
final TypeMirror nodeType,
final FieldSpecBuilderTracker fieldSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ public Set<Class<? extends Annotation>> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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

Please sign in to comment.