Skip to content

Commit

Permalink
Track processed classes. (#123)
Browse files Browse the repository at this point in the history
Second round of compilation results in visiting the same class again. This prevents that.

Fixes #122
  • Loading branch information
graemerocher authored Jul 23, 2024
1 parent 0281342 commit 3a884e6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import io.micronaut.sourcegen.model.TypeDef;
import io.micronaut.sourcegen.model.VariableDef;

import java.util.HashSet;
import java.util.Set;
import javax.lang.model.element.Modifier;
import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -52,13 +54,23 @@
@Internal
public final class BuilderAnnotationVisitor implements TypeElementVisitor<Builder, Object> {

private final Set<String> processed = new HashSet<>();

@Override
public @NonNull VisitorKind getVisitorKind() {
return VisitorKind.ISOLATING;
}

@Override
public void start(VisitorContext visitorContext) {
processed.clear();
}

@Override
public void visitClass(ClassElement element, VisitorContext context) {
if (processed.contains(element.getName())) {
return;
}
String simpleName = element.getSimpleName() + "Builder";
String builderClassName = element.getPackageName() + "." + simpleName;

Expand Down Expand Up @@ -125,6 +137,7 @@ public void visitClass(ClassElement element, VisitorContext context) {
}

ClassDef builderDef = builder.build();
processed.add(element.getName());
context.visitGeneratedSourceFile(
builderDef.getPackageName(),
builderDef.getSimpleName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import io.micronaut.sourcegen.model.TypeDef;
import io.micronaut.sourcegen.model.VariableDef;

import java.util.HashSet;
import java.util.Set;
import javax.lang.model.element.Modifier;
import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -54,13 +56,23 @@
@Internal
public final class WitherAnnotationVisitor implements TypeElementVisitor<Wither, Object> {

private final Set<String> processed = new HashSet<>();

@Override
public @NonNull VisitorKind getVisitorKind() {
return VisitorKind.ISOLATING;
}

@Override
public void start(VisitorContext visitorContext) {
processed.clear();
}

@Override
public void visitClass(ClassElement recordElement, VisitorContext context) {
if (processed.contains(recordElement.getName())) {
return;
}
if (!recordElement.isRecord()) {
throw new ProcessingException(recordElement, "Only records can be annotated with @Wither");
}
Expand Down Expand Up @@ -154,6 +166,7 @@ public void visitClass(ClassElement recordElement, VisitorContext context) {
}

InterfaceDef witherDef = wither.build();
processed.add(recordElement.getName());
context.visitGeneratedSourceFile(
witherDef.getPackageName(),
witherDef.getSimpleName(),
Expand Down

0 comments on commit 3a884e6

Please sign in to comment.