Skip to content
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

Improve errors and make super builders public #128

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.io.service.SoftServiceLoader;
import io.micronaut.inject.ast.Element;
import io.micronaut.inject.processing.ProcessingException;
import io.micronaut.inject.visitor.VisitorContext;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

/**
* The source generators.
Expand Down Expand Up @@ -51,4 +55,25 @@ public static Optional<SourceGenerator> findByLanguage(VisitorContext.Language l
return getAll().stream().filter(s -> s.getLanguage() == language).findAny();
}

/**
* Utility method for handling exceptions.
*
* @param originatingElement The originating element.
* @param annotation The annotation
* @param exception The exception
* @param postponeCallback A call back if compilation needs to be delayed to the next round
*/
public static void handleFatalException(
@NonNull Element originatingElement,
@NonNull Class<? extends Annotation> annotation,
@NonNull Exception exception,
Consumer<RuntimeException> postponeCallback) {
if (exception.getClass().getSimpleName().equals("PostponeToNextRoundException") && exception instanceof RuntimeException runtimeException) {
postponeCallback.accept(runtimeException);
} else {
String message = exception.getMessage() != null ? exception.getMessage() : exception.getClass().getSimpleName();
throw new ProcessingException(originatingElement, "Failed to generate a @" + annotation.getSimpleName() + ": " + message, exception);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import io.micronaut.sourcegen.model.TypeDef;
import io.micronaut.sourcegen.model.VariableDef;

import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;
import javax.lang.model.element.Modifier;
Expand Down Expand Up @@ -82,6 +83,11 @@ public void start(VisitorContext visitorContext) {
processed.clear();
}

@Override
public Set<String> getSupportedAnnotationNames() {
return Set.of(Builder.class.getName());
}

@Override
public void visitClass(ClassElement element, VisitorContext context) {
if (processed.contains(element.getName())) {
Expand Down Expand Up @@ -130,7 +136,15 @@ public void visitClass(ClassElement element, VisitorContext context) {
} catch (ProcessingException e) {
throw e;
} catch (Exception e) {
throw new ProcessingException(element, "Failed to generate a builder: " + e.getMessage(), e);
SourceGenerators.handleFatalException(
element,
Builder.class,
e,
(exception -> {
processed.remove(element.getName());
throw exception;
})
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public void visitClass(ClassElement element, VisitorContext context) {
ClassTypeDef builderType = ClassTypeDef.of(builderClassName);

ClassDef.ClassDefBuilder builder = ClassDef.builder(builderClassName)
.addModifiers(Modifier.PUBLIC)
.superclass(new ClassTypeDef.Parameterized(
ClassTypeDef.of(abstractBuilderDef),
List.of(
Expand All @@ -165,7 +166,15 @@ public void visitClass(ClassElement element, VisitorContext context) {
} catch (ProcessingException e) {
throw e;
} catch (Exception e) {
throw new ProcessingException(element, "Failed to generate a super builder: " + e.getMessage(), e);
SourceGenerators.handleFatalException(
element,
SuperBuilder.class,
e,
(exception -> {
processed.remove(element.getName());
throw exception;
})
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,15 @@ public void visitClass(ClassElement recordElement, VisitorContext context) {
} catch (ProcessingException e) {
throw e;
} catch (Exception e) {
throw new ProcessingException(recordElement, "Failed to generate a wither: " + e.getMessage(), e);
SourceGenerators.handleFatalException(
recordElement,
Wither.class,
e,
(exception -> {
processed.remove(recordElement.getName());
throw exception;
})
);
}
}

Expand Down
7 changes: 7 additions & 0 deletions test-suite-java/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ dependencies {
testImplementation(libs.junit.jupiter.engine)
testAnnotationProcessor(mn.micronaut.inject.java)
}
//
//tasks {
// compileJava {
// options.isFork = true
// options.forkOptions.jvmArgs = listOf("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005")
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.micronaut.sourcegen.example;

import io.micronaut.core.annotation.Vetoed;
import io.micronaut.sourcegen.annotations.SuperBuilder;

//tag::clazz[]
Expand Down Expand Up @@ -44,5 +45,10 @@ public void setBread(String bread) {
this.bread = bread;
}
//tag::clazz[]

@Vetoed // vetoed is currently required.
public static CatSuperBuilder builder() {
return new CatSuperBuilder();
}
}
//end::clazz[]
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.micronaut.sourcegen.example;

import java.lang.reflect.Modifier;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -62,6 +63,7 @@ public void testDog() {

@Test
public void internalTest() {
assertTrue(Modifier.isPublic(CatSuperBuilder.class.getModifiers()));
assertEquals(3, AbstractAnimalSuperBuilder.class.getDeclaredFields().length);

assertEquals(0, CatSuperBuilder.class.getDeclaredFields().length);
Expand Down
Loading