Skip to content

Commit 00c8444

Browse files
committed
jooby-apt: print error on failure
1 parent 720643c commit 00c8444

File tree

3 files changed

+58
-35
lines changed

3 files changed

+58
-35
lines changed

modules/jooby-apt/src/main/java/io/jooby/apt/JoobyProcessor.java

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import java.nio.file.Files;
1616
import java.nio.file.Paths;
1717
import java.util.*;
18-
import java.util.function.Consumer;
18+
import java.util.function.BiConsumer;
1919
import java.util.stream.Collectors;
2020
import java.util.stream.Stream;
2121

@@ -71,10 +71,10 @@ static String string(ProcessingEnvironment environment, String option, String de
7171
}
7272

7373
protected MvcContext context;
74-
private Consumer<String> output;
74+
private BiConsumer<Diagnostic.Kind, String> output;
7575
private final Set<Object> processed = new HashSet<>();
7676

77-
public JoobyProcessor(Consumer<String> output) {
77+
public JoobyProcessor(BiConsumer<Diagnostic.Kind, String> output) {
7878
this.output = output;
7979
}
8080

@@ -88,38 +88,42 @@ public synchronized void init(ProcessingEnvironment processingEnvironment) {
8888
ofNullable(output)
8989
.orElseGet(
9090
() ->
91-
message ->
92-
processingEnvironment
93-
.getMessager()
94-
.printMessage(Diagnostic.Kind.OTHER, message)));
91+
(kind, message) ->
92+
processingEnvironment.getMessager().printMessage(kind, message)));
9593
super.init(processingEnvironment);
9694
}
9795

9896
@Override
9997
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
100-
if (roundEnv.processingOver()) {
101-
context.debug("Output:");
102-
context.getRouters().forEach(it -> context.debug(" %s.java", it.getGeneratedType()));
103-
if (context.generateServices()) {
104-
doServices(context.getProcessingEnvironment().getFiler(), context.getRouters());
105-
}
106-
return false;
107-
} else {
108-
var routeMap = buildRouteRegistry(annotations, roundEnv);
109-
for (var router : routeMap.values()) {
110-
try {
111-
var sourceCode = router.toSourceCode(null);
112-
var sourceLocation = router.getGeneratedFilename();
113-
onGeneratedSource(toJavaFileObject(sourceLocation, sourceCode));
114-
context.debug("router %s: %s", router.getTargetType(), router.getGeneratedType());
115-
router.getRoutes().forEach(it -> context.debug(" %s", it));
116-
writeSource(router, sourceLocation, sourceCode);
117-
context.add(router);
118-
} catch (IOException cause) {
119-
throw new RuntimeException("Unable to generate: " + router.getTargetType(), cause);
98+
try {
99+
if (roundEnv.processingOver()) {
100+
context.debug("Output:");
101+
context.getRouters().forEach(it -> context.debug(" %s.java", it.getGeneratedType()));
102+
if (context.generateServices()) {
103+
doServices(context.getProcessingEnvironment().getFiler(), context.getRouters());
104+
}
105+
return false;
106+
} else {
107+
var routeMap = buildRouteRegistry(annotations, roundEnv);
108+
for (var router : routeMap.values()) {
109+
try {
110+
var sourceCode = router.toSourceCode(null);
111+
var sourceLocation = router.getGeneratedFilename();
112+
onGeneratedSource(toJavaFileObject(sourceLocation, sourceCode));
113+
context.debug("router %s: %s", router.getTargetType(), router.getGeneratedType());
114+
router.getRoutes().forEach(it -> context.debug(" %s", it));
115+
writeSource(router, sourceLocation, sourceCode);
116+
context.add(router);
117+
} catch (IOException cause) {
118+
throw new RuntimeException("Unable to generate: " + router.getTargetType(), cause);
119+
}
120120
}
121+
return true;
121122
}
122-
return true;
123+
} catch (Exception cause) {
124+
context.error(
125+
Optional.ofNullable(cause.getMessage()).orElse("Unable to generate routes"), cause);
126+
throw sneakyThrow0(cause);
123127
}
124128
}
125129

modules/jooby-apt/src/main/java/io/jooby/internal/apt/MvcContext.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
*/
66
package io.jooby.internal.apt;
77

8+
import java.io.PrintWriter;
9+
import java.io.StringWriter;
810
import java.util.*;
911
import java.util.function.BiConsumer;
10-
import java.util.function.Consumer;
1112
import java.util.stream.Collectors;
1213

1314
import javax.annotation.processing.ProcessingEnvironment;
1415
import javax.lang.model.element.*;
1516
import javax.lang.model.type.DeclaredType;
1617
import javax.lang.model.type.TypeMirror;
18+
import javax.tools.Diagnostic;
1719

1820
import io.jooby.apt.JoobyProcessor.Options;
1921

@@ -30,13 +32,14 @@ private record ResultType(String type, String handler, boolean nonBlocking) {}
3032
private final boolean services;
3133
private final String routerPrefix;
3234
private final String routerSuffix;
33-
private final Consumer<String> output;
35+
private final BiConsumer<Diagnostic.Kind, String> output;
3436
private final List<MvcRouter> routers = new ArrayList<>();
3537
private final boolean returnType;
3638
private final boolean mvcMethod;
3739
private final Map<TypeElement, ResultType> handler = new HashMap<>();
3840

39-
public MvcContext(ProcessingEnvironment processingEnvironment, Consumer<String> output) {
41+
public MvcContext(
42+
ProcessingEnvironment processingEnvironment, BiConsumer<Diagnostic.Kind, String> output) {
4043
this.processingEnvironment = processingEnvironment;
4144
this.output = output;
4245
this.debug = Options.boolOpt(processingEnvironment, Options.DEBUG, false);
@@ -258,13 +261,29 @@ public boolean isIncremental() {
258261

259262
public void debug(String message, Object... args) {
260263
if (debug) {
261-
info(message, args);
264+
report(Diagnostic.Kind.OTHER, message, args);
262265
}
263266
}
264267

265-
public void info(String message, Object... args) {
268+
public void error(String message, Object... args) {
269+
Throwable cause =
270+
args.length > 0 && args[args.length - 1] instanceof Throwable
271+
? (Throwable) args[args.length - 1]
272+
: null;
273+
if (cause != null) {
274+
var str = new StringWriter();
275+
cause.printStackTrace(new PrintWriter(str, true));
276+
var errorMessage = cause.getMessage();
277+
args[args.length - 1] =
278+
(errorMessage == null || errorMessage.equals(message) ? "" : errorMessage) + ":\n" + str;
279+
message += ": %s";
280+
}
281+
report(Diagnostic.Kind.ERROR, message, args);
282+
}
283+
284+
private void report(Diagnostic.Kind kind, String message, Object... args) {
266285
var msg = args.length == 0 ? message : message.formatted(args);
267-
output.accept(msg);
286+
output.accept(kind, msg);
268287
}
269288

270289
public void generateStaticImports(MvcRouter mvcRouter, BiConsumer<String, String> consumer) {

modules/jooby-apt/src/test/java/io/jooby/apt/ProcessorRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private static class HookJoobyProcessor extends JoobyProcessor {
6060
private JavaFileObject source;
6161

6262
public HookJoobyProcessor(Consumer<String> console) {
63-
super(console);
63+
super((kind, message) -> console.accept(message));
6464
}
6565

6666
public GeneratedSourceClassLoader createClassLoader() {

0 commit comments

Comments
 (0)