From f9e3ce6ee4dfb7c0613d84f780b2a86f0a35c6f7 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 2 Feb 2024 08:58:07 +0100 Subject: [PATCH] Compiler manager should use providers To figure out presence of component, and if construction fails, report some sane(r) message tham obvious false information: "compiler not present". --- .../manager/DefaultCompilerManager.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/plexus-compiler-manager/src/main/java/org/codehaus/plexus/compiler/manager/DefaultCompilerManager.java b/plexus-compiler-manager/src/main/java/org/codehaus/plexus/compiler/manager/DefaultCompilerManager.java index 29af62f7..29078e93 100644 --- a/plexus-compiler-manager/src/main/java/org/codehaus/plexus/compiler/manager/DefaultCompilerManager.java +++ b/plexus-compiler-manager/src/main/java/org/codehaus/plexus/compiler/manager/DefaultCompilerManager.java @@ -25,6 +25,7 @@ */ import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Provider; import java.util.Map; @@ -36,19 +37,27 @@ @Named public class DefaultCompilerManager implements CompilerManager { @Inject - private Map compilers; + private Map> compilers; // ---------------------------------------------------------------------- // CompilerManager Implementation // ---------------------------------------------------------------------- public Compiler getCompiler(String compilerId) throws NoSuchCompilerException { - Compiler compiler = compilers.get(compilerId); + // Provider is lazy + // presence of provider means component is present (but not yet constructed) + Provider compiler = compilers.get(compilerId); + // it exists: as provider is found if (compiler == null) { throw new NoSuchCompilerException(compilerId); } - - return compiler; + // provider is lazy: if we are here, it exists but not yet created + try { + return compiler.get(); + } catch (Exception e) { + // if we are here: DI could not construct it: so report proper error + throw new RuntimeException(e); + } } }