From a03e5b39656f1b214c6d8e3b89457a2821f6ac2b Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Mon, 14 Oct 2024 11:20:46 +0200 Subject: [PATCH] ArC: throw NPE when null value is used for AdditionalBeanBuildItem --- .../deployment/AdditionalBeanBuildItem.java | 15 +++++++------- .../AdditionalBeanBuildItemTest.java | 20 +++++++++++++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 extensions/arc/deployment/src/test/java/io/quarkus/arc/test/deployment/AdditionalBeanBuildItemTest.java diff --git a/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/AdditionalBeanBuildItem.java b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/AdditionalBeanBuildItem.java index e71fff1413121..468949062cd49 100644 --- a/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/AdditionalBeanBuildItem.java +++ b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/AdditionalBeanBuildItem.java @@ -5,7 +5,6 @@ import java.util.Collection; import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; import org.jboss.jandex.DotName; @@ -42,7 +41,7 @@ public static Builder builder() { */ @CheckReturnValue public static AdditionalBeanBuildItem unremovableOf(Class beanClass) { - return new AdditionalBeanBuildItem(Collections.singletonList(beanClass.getName()), false, null); + return new AdditionalBeanBuildItem(List.of(beanClass.getName()), false, null); } /** @@ -53,7 +52,7 @@ public static AdditionalBeanBuildItem unremovableOf(Class beanClass) { */ @CheckReturnValue public static AdditionalBeanBuildItem unremovableOf(String beanClass) { - return new AdditionalBeanBuildItem(Collections.singletonList(beanClass), false, null); + return new AdditionalBeanBuildItem(List.of(beanClass), false, null); } private final List beanClasses; @@ -61,21 +60,21 @@ public static AdditionalBeanBuildItem unremovableOf(String beanClass) { private final DotName defaultScope; public AdditionalBeanBuildItem(String... beanClasses) { - this(Arrays.asList(beanClasses), true, null); + this(List.of(beanClasses), true, null); } public AdditionalBeanBuildItem(Class... beanClasses) { - this(Arrays.stream(beanClasses).map(Class::getName).collect(Collectors.toList()), true, null); + this(Arrays.stream(beanClasses).map(Class::getName).toArray(String[]::new)); } - AdditionalBeanBuildItem(List beanClasses, boolean removable, DotName defaultScope) { + private AdditionalBeanBuildItem(List beanClasses, boolean removable, DotName defaultScope) { this.beanClasses = beanClasses; this.removable = removable; this.defaultScope = defaultScope; } public List getBeanClasses() { - return Collections.unmodifiableList(beanClasses); + return beanClasses; } public boolean contains(String beanClass) { @@ -151,7 +150,7 @@ public Builder setDefaultScope(DotName defaultScope) { } public AdditionalBeanBuildItem build() { - return new AdditionalBeanBuildItem(new ArrayList<>(beanClasses), removable, defaultScope); + return new AdditionalBeanBuildItem(List.copyOf(beanClasses), removable, defaultScope); } } diff --git a/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/deployment/AdditionalBeanBuildItemTest.java b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/deployment/AdditionalBeanBuildItemTest.java new file mode 100644 index 0000000000000..2c218085bf493 --- /dev/null +++ b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/deployment/AdditionalBeanBuildItemTest.java @@ -0,0 +1,20 @@ +package io.quarkus.arc.test.deployment; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +import io.quarkus.arc.deployment.AdditionalBeanBuildItem; + +public class AdditionalBeanBuildItemTest { + + @Test + public void testNullNames() { + assertThrows(NullPointerException.class, () -> AdditionalBeanBuildItem.unremovableOf((String) null)); + assertThrows(NullPointerException.class, () -> AdditionalBeanBuildItem.unremovableOf((Class) null)); + assertThrows(NullPointerException.class, () -> AdditionalBeanBuildItem.builder().addBeanClasses(null, "Foo").build()); + assertThrows(NullPointerException.class, () -> new AdditionalBeanBuildItem("Bar", null)); + assertThrows(NullPointerException.class, () -> new AdditionalBeanBuildItem(String.class, null)); + } + +}