From 4ed365a5b6c1b7885f2b1b49c07692c089670114 Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Thu, 14 Mar 2024 14:30:50 +0100 Subject: [PATCH] Core: fail if null is produced for a SimpleBuildItem - to avoid unexpected NullPointerException - build steps should consume java.util.Optional instead - follow-up to https://github.com/quarkusio/quarkus/issues/39408 --- .../quarkus/deployment/ExtensionLoader.java | 9 +++- .../deployment/console/ConsoleProcessor.java | 11 ++--- .../quarkus/deployment/ide/IdeProcessor.java | 41 ++++++++++--------- .../logging/LoggingResourceProcessor.java | 3 +- 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/core/deployment/src/main/java/io/quarkus/deployment/ExtensionLoader.java b/core/deployment/src/main/java/io/quarkus/deployment/ExtensionLoader.java index d4076b42abab7..1dd3effca397e 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/ExtensionLoader.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/ExtensionLoader.java @@ -496,7 +496,14 @@ private static Consumer loadStepsFromClass(Class clazz, final Class buildItemClass = parameterClass .asSubclass(SimpleBuildItem.class); methodStepConfig = methodStepConfig.andThen(bsb -> bsb.consumes(buildItemClass)); - methodParamFns.add((bc, bri) -> bc.consume(buildItemClass)); + methodParamFns.add((bc, bri) -> { + SimpleBuildItem bi = bc.consume(buildItemClass); + if (bi == null) { + throw reportError(parameter, + "Cannot consume 'null' build item, use java.util.Optional build step method parameter instead"); + } + return bi; + }); } else if (isAnEmptyBuildItemConsumer(parameterType)) { throw reportError(parameter, "Cannot consume an empty build item, use @Consume(class) on the build step method instead"); diff --git a/core/deployment/src/main/java/io/quarkus/deployment/console/ConsoleProcessor.java b/core/deployment/src/main/java/io/quarkus/deployment/console/ConsoleProcessor.java index 2def2767f3676..603d8ac9e7ece 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/console/ConsoleProcessor.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/console/ConsoleProcessor.java @@ -7,6 +7,7 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import java.util.function.BiConsumer; @@ -125,8 +126,8 @@ public void run() { @Consume(ConsoleInstalledBuildItem.class) @BuildStep void setupExceptionHandler(BuildProducer exceptionNotificationBuildItem, - EffectiveIdeBuildItem ideSupport, LaunchModeBuildItem launchModeBuildItem) { - if (launchModeBuildItem.isAuxiliaryApplication()) { + Optional ideSupport, LaunchModeBuildItem launchModeBuildItem) { + if (launchModeBuildItem.isAuxiliaryApplication() || ideSupport.isEmpty()) { return; } final AtomicReference lastUserCode = new AtomicReference<>(); @@ -162,7 +163,7 @@ public String get() { public void run() { StackTraceElement throwable = lastUserCode.get(); if (throwable == null) { - launchInIDE(ideSupport.getIde(), List.of(".")); + launchInIDE(ideSupport.get().getIde(), List.of(".")); return; } String className = throwable.getClassName(); @@ -177,10 +178,10 @@ public void run() { log.error("Unable to find file: " + file); return; } - List args = ideSupport.getIde().createFileOpeningArgs( + List args = ideSupport.get().getIde().createFileOpeningArgs( fileName.toAbsolutePath().toString(), "" + throwable.getLineNumber()); - launchInIDE(ideSupport.getIde(), args); + launchInIDE(ideSupport.get().getIde(), args); } })); } diff --git a/core/deployment/src/main/java/io/quarkus/deployment/ide/IdeProcessor.java b/core/deployment/src/main/java/io/quarkus/deployment/ide/IdeProcessor.java index b28d83e0e0414..a6a58c88fb5f9 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/ide/IdeProcessor.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/ide/IdeProcessor.java @@ -76,8 +76,8 @@ public class IdeProcessor { @BuildStep public EffectiveIdeBuildItem effectiveIde(LaunchModeBuildItem launchModeBuildItem, IdeConfig ideConfig, - IdeFileBuildItem ideFile, - IdeRunningProcessBuildItem ideRunningProcess) { + Optional ideFile, + Optional ideRunningProcess) { if (launchModeBuildItem.getDevModeType().orElse(null) != DevModeType.LOCAL) { return null; } @@ -86,26 +86,27 @@ public EffectiveIdeBuildItem effectiveIde(LaunchModeBuildItem launchModeBuildIte // the idea here is to auto-detect the special files that IDEs create // and also the running IDE process if need be - - if (ideFile.getDetectedIDEs().size() == 1) { - result = ideFile.getDetectedIDEs().iterator().next(); - } else { - Set runningIdes = ideRunningProcess.getDetectedIDEs(); - if (runningIdes.size() == 1) { - result = runningIdes.iterator().next(); - } else { - List matches = new ArrayList<>(); - for (Ide file : ideFile.getDetectedIDEs()) { - for (Ide process : runningIdes) { - if (file == process) { - matches.add(file); + if (ideFile.isPresent()) { + if (ideFile.get().getDetectedIDEs().size() == 1) { + result = ideFile.get().getDetectedIDEs().iterator().next(); + } else if (ideRunningProcess.isPresent()) { + Set runningIdes = ideRunningProcess.get().getDetectedIDEs(); + if (runningIdes.size() == 1) { + result = runningIdes.iterator().next(); + } else { + List matches = new ArrayList<>(); + for (Ide file : ideFile.get().getDetectedIDEs()) { + for (Ide process : runningIdes) { + if (file == process) { + matches.add(file); + } } } - } - if ((matches.size() == 0 && runningIdes.size() > 0)) { - result = runningIdes.iterator().next(); - } else if (matches.size() >= 1) { - result = matches.get(0); + if ((matches.size() == 0 && runningIdes.size() > 0)) { + result = runningIdes.iterator().next(); + } else if (matches.size() >= 1) { + result = matches.get(0); + } } } } diff --git a/core/deployment/src/main/java/io/quarkus/deployment/logging/LoggingResourceProcessor.java b/core/deployment/src/main/java/io/quarkus/deployment/logging/LoggingResourceProcessor.java index 4bc1ee15c7e94..1647a30795922 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/logging/LoggingResourceProcessor.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/logging/LoggingResourceProcessor.java @@ -82,7 +82,6 @@ import io.quarkus.deployment.dev.ExceptionNotificationBuildItem; import io.quarkus.deployment.dev.testing.MessageFormat; import io.quarkus.deployment.dev.testing.TestSetupBuildItem; -import io.quarkus.deployment.ide.EffectiveIdeBuildItem; import io.quarkus.deployment.metrics.MetricsCapabilityBuildItem; import io.quarkus.deployment.metrics.MetricsFactoryConsumerBuildItem; import io.quarkus.deployment.pkg.builditem.BuildSystemTargetBuildItem; @@ -367,7 +366,7 @@ private DiscoveredLogComponents discoverLogComponents(IndexView index) { @Produce(TestSetupBuildItem.class) @Produce(LogConsoleFormatBuildItem.class) @Consume(ConsoleInstalledBuildItem.class) - void setupStackTraceFormatter(ApplicationArchivesBuildItem item, EffectiveIdeBuildItem ideSupport, + void setupStackTraceFormatter(ApplicationArchivesBuildItem item, BuildSystemTargetBuildItem buildSystemTargetBuildItem, List exceptionNotificationBuildItems, CuratedApplicationShutdownBuildItem curatedApplicationShutdownBuildItem) {