Skip to content

Commit

Permalink
Core: fail if null is produced for a SimpleBuildItem
Browse files Browse the repository at this point in the history
- to avoid unexpected NullPointerException
- build steps should consume java.util.Optional instead
- follow-up to quarkusio#39408
  • Loading branch information
mkouba committed Mar 14, 2024
1 parent 8713495 commit 4ed365a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,14 @@ private static Consumer<BuildChainBuilder> loadStepsFromClass(Class<?> clazz,
final Class<? extends SimpleBuildItem> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -125,8 +126,8 @@ public void run() {
@Consume(ConsoleInstalledBuildItem.class)
@BuildStep
void setupExceptionHandler(BuildProducer<ExceptionNotificationBuildItem> exceptionNotificationBuildItem,
EffectiveIdeBuildItem ideSupport, LaunchModeBuildItem launchModeBuildItem) {
if (launchModeBuildItem.isAuxiliaryApplication()) {
Optional<EffectiveIdeBuildItem> ideSupport, LaunchModeBuildItem launchModeBuildItem) {
if (launchModeBuildItem.isAuxiliaryApplication() || ideSupport.isEmpty()) {
return;
}
final AtomicReference<StackTraceElement> lastUserCode = new AtomicReference<>();
Expand Down Expand Up @@ -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();
Expand All @@ -177,10 +178,10 @@ public void run() {
log.error("Unable to find file: " + file);
return;
}
List<String> args = ideSupport.getIde().createFileOpeningArgs(
List<String> args = ideSupport.get().getIde().createFileOpeningArgs(
fileName.toAbsolutePath().toString(),
"" + throwable.getLineNumber());
launchInIDE(ideSupport.getIde(), args);
launchInIDE(ideSupport.get().getIde(), args);
}
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public class IdeProcessor {

@BuildStep
public EffectiveIdeBuildItem effectiveIde(LaunchModeBuildItem launchModeBuildItem, IdeConfig ideConfig,
IdeFileBuildItem ideFile,
IdeRunningProcessBuildItem ideRunningProcess) {
Optional<IdeFileBuildItem> ideFile,
Optional<IdeRunningProcessBuildItem> ideRunningProcess) {
if (launchModeBuildItem.getDevModeType().orElse(null) != DevModeType.LOCAL) {
return null;
}
Expand All @@ -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<Ide> runningIdes = ideRunningProcess.getDetectedIDEs();
if (runningIdes.size() == 1) {
result = runningIdes.iterator().next();
} else {
List<Ide> 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<Ide> runningIdes = ideRunningProcess.get().getDetectedIDEs();
if (runningIdes.size() == 1) {
result = runningIdes.iterator().next();
} else {
List<Ide> 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);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ExceptionNotificationBuildItem> exceptionNotificationBuildItems,
CuratedApplicationShutdownBuildItem curatedApplicationShutdownBuildItem) {
Expand Down

0 comments on commit 4ed365a

Please sign in to comment.