From 82ec5d71872212364a321cd3700d0ecadf9162d2 Mon Sep 17 00:00:00 2001 From: Matej Novotny Date: Wed, 25 May 2022 16:49:56 +0200 Subject: [PATCH] Make sure CDI and BuildServicesResolves both guard against repeated invocations of set method --- .../build/compatible/spi/BuildServicesResolver.java | 12 ++++++++++-- .../main/java/jakarta/enterprise/inject/spi/CDI.java | 10 +++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/api/src/main/java/jakarta/enterprise/inject/build/compatible/spi/BuildServicesResolver.java b/api/src/main/java/jakarta/enterprise/inject/build/compatible/spi/BuildServicesResolver.java index 0b819539..702bf860 100644 --- a/api/src/main/java/jakarta/enterprise/inject/build/compatible/spi/BuildServicesResolver.java +++ b/api/src/main/java/jakarta/enterprise/inject/build/compatible/spi/BuildServicesResolver.java @@ -12,7 +12,6 @@ import java.util.Collections; import java.util.Comparator; -import java.util.Objects; import java.util.ServiceConfigurationError; import java.util.ServiceLoader; import java.util.Set; @@ -76,8 +75,17 @@ private static void discoverFactories() { * this class will no longer attempt to look it up using service loader. * * @param instance a {@link BuildServices} instance that should be used, must not be {@code null} + * @throws IllegalArgumentException if the provided argument is null + * @throws IllegalStateException if the {@link BuildServices} are already set */ public static void setBuildServices(BuildServices instance) { - configuredBuildServices = Objects.requireNonNull(instance, "BuildServices instance must not be null"); + if (instance == null) { + throw new IllegalArgumentException("BuildServices instance must not be null"); + } + if (configuredBuildServices != null) { + configuredBuildServices = instance; + } else { + throw new IllegalStateException("BuildServices cannot be set repeatedly. Existing BuildServices are " + configuredBuildServices); + } } } diff --git a/api/src/main/java/jakarta/enterprise/inject/spi/CDI.java b/api/src/main/java/jakarta/enterprise/inject/spi/CDI.java index b50f192d..f044a75a 100644 --- a/api/src/main/java/jakarta/enterprise/inject/spi/CDI.java +++ b/api/src/main/java/jakarta/enterprise/inject/spi/CDI.java @@ -68,7 +68,7 @@ public static CDI current() { /** * * Obtain the {@link CDIProvider} the user set with {@link #setCDIProvider(CDIProvider)} or the last returned - * {@link CDIProvider} if it returns valid CDI container. Otherwise use the serviceloader to retrieve the + * {@link CDIProvider} if it returns valid CDI container. Otherwise, use service loader mechanism to retrieve the * {@link CDIProvider} with the highest priority. * * @return the {@link CDIProvider} set by user or retrieved by serviceloader @@ -118,13 +118,17 @@ private static boolean checkProvider(CDIProvider c) { * * @param provider the provider to use * @throws IllegalStateException if the {@link CDIProvider} is already set + * @throws IllegalArgumentException if the provided argument is null */ public static void setCDIProvider(CDIProvider provider) { - if (provider != null) { + if (provider == null) { + throw new IllegalArgumentException("CDIProvider must not be null"); + } + if (configuredProvider != null) { providerSetManually = true; configuredProvider = provider; } else { - throw new IllegalStateException("CDIProvider must not be null"); + throw new IllegalStateException("CDIProvider cannot be set repeatedly. Existing provider is " + configuredProvider); } }