From 6ecefbe247b848c98eee256e54074688084c231f Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Thu, 25 Apr 2024 08:51:40 +0200 Subject: [PATCH] ArC: prevent NPE when EagerInstanceHandle.UNAVAILABLE is closed --- .../main/java/io/quarkus/arc/impl/AbstractInstanceHandle.java | 4 ++-- .../main/java/io/quarkus/arc/impl/EagerInstanceHandle.java | 4 +++- .../src/main/java/io/quarkus/arc/impl/LazyInstanceHandle.java | 2 ++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/AbstractInstanceHandle.java b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/AbstractInstanceHandle.java index e9e04ef605da8..1225141452304 100644 --- a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/AbstractInstanceHandle.java +++ b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/AbstractInstanceHandle.java @@ -18,7 +18,7 @@ abstract class AbstractInstanceHandle implements InstanceHandle { private static final AtomicIntegerFieldUpdater DESTROYED_UPDATER = AtomicIntegerFieldUpdater .newUpdater(AbstractInstanceHandle.class, "destroyed"); - private final InjectableBean bean; + protected final InjectableBean bean; private final CreationalContext creationalContext; private final CreationalContext parentCreationalContext; private final Consumer destroyLogic; @@ -56,7 +56,7 @@ public void destroy() { if (isInstanceCreated() && DESTROYED_UPDATER.compareAndSet(this, 0, 1)) { if (destroyLogic != null) { destroyLogic.accept(instanceInternal()); - } else { + } else if (bean != null) { if (bean.getScope().equals(Dependent.class)) { destroyInternal(); } else { diff --git a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/EagerInstanceHandle.java b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/EagerInstanceHandle.java index 1ba2cf08159a6..df2430fcf5505 100644 --- a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/EagerInstanceHandle.java +++ b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/EagerInstanceHandle.java @@ -8,8 +8,10 @@ import io.quarkus.arc.InstanceHandle; /** + * Instance handle that is initialized eagerly when created. * * @param + * @see LazyInstanceHandle */ class EagerInstanceHandle extends AbstractInstanceHandle { @@ -34,7 +36,7 @@ public static final InstanceHandle unavailable() { @Override protected boolean isInstanceCreated() { - return true; + return instance != null; } @Override diff --git a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/LazyInstanceHandle.java b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/LazyInstanceHandle.java index 891a1c8397257..4be2d86d85e36 100644 --- a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/LazyInstanceHandle.java +++ b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/LazyInstanceHandle.java @@ -8,8 +8,10 @@ import io.quarkus.arc.InjectableBean; /** + * Instance handle that is initialized lazily when first used. * * @param + * @see EagerInstanceHandle */ class LazyInstanceHandle extends AbstractInstanceHandle {