|  | 
| 25 | 25 |  */ | 
| 26 | 26 | import javax.inject.Inject; | 
| 27 | 27 | import javax.inject.Named; | 
|  | 28 | +import javax.inject.Provider; | 
| 28 | 29 | 
 | 
| 29 | 30 | import java.util.Map; | 
| 30 | 31 | 
 | 
| 31 | 32 | import org.codehaus.plexus.compiler.Compiler; | 
|  | 33 | +import org.slf4j.Logger; | 
|  | 34 | +import org.slf4j.LoggerFactory; | 
| 32 | 35 | 
 | 
| 33 | 36 | /** | 
| 34 | 37 |  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a> | 
| 35 | 38 |  */ | 
| 36 | 39 | @Named | 
| 37 | 40 | public class DefaultCompilerManager implements CompilerManager { | 
|  | 41 | +    private static final String ERROR_MESSAGE = "Compiler '{}' could not be instantiated or injected properly. " | 
|  | 42 | +            + "Running the build with -Dsisu.debug, looking for exceptions might help."; | 
|  | 43 | +    private static final String ERROR_MESSAGE_DETAIL = "TypeNotPresentException caused by UnsupportedClassVersionError " | 
|  | 44 | +            + "might indicate, that the compiler needs a more recent Java runtime. " | 
|  | 45 | +            + "IllegalArgumentException in ClassReader.<init> might mean, that you need to upgrade Maven."; | 
|  | 46 | + | 
| 38 | 47 |     @Inject | 
| 39 |  | -    private Map<String, Compiler> compilers; | 
|  | 48 | +    private Map<String, Provider<Compiler>> compilers; | 
|  | 49 | + | 
|  | 50 | +    private final Logger log = LoggerFactory.getLogger(getClass()); | 
| 40 | 51 | 
 | 
| 41 | 52 |     // ---------------------------------------------------------------------- | 
| 42 | 53 |     // CompilerManager Implementation | 
| 43 | 54 |     // ---------------------------------------------------------------------- | 
| 44 | 55 | 
 | 
| 45 | 56 |     public Compiler getCompiler(String compilerId) throws NoSuchCompilerException { | 
| 46 |  | -        Compiler compiler = compilers.get(compilerId); | 
|  | 57 | +        // Provider<Class> is lazy -> presence of provider means compiler is present, but not yet constructed | 
|  | 58 | +        Provider<Compiler> compilerProvider = compilers.get(compilerId); | 
| 47 | 59 | 
 | 
| 48 |  | -        if (compiler == null) { | 
|  | 60 | +        if (compilerProvider == null) { | 
|  | 61 | +            // Compiler could not be injected for some reason | 
|  | 62 | +            log.error(ERROR_MESSAGE + " " + ERROR_MESSAGE_DETAIL, compilerId); | 
| 49 | 63 |             throw new NoSuchCompilerException(compilerId); | 
| 50 | 64 |         } | 
| 51 | 65 | 
 | 
| 52 |  | -        return compiler; | 
|  | 66 | +        // Provider exists, but compiler was not created yet | 
|  | 67 | +        try { | 
|  | 68 | +            return compilerProvider.get(); | 
|  | 69 | +        } catch (Exception e) { | 
|  | 70 | +            // DI could not construct compiler | 
|  | 71 | +            log.error(ERROR_MESSAGE, compilerId); | 
|  | 72 | +            throw new NoSuchCompilerException(compilerId); | 
|  | 73 | +        } | 
| 53 | 74 |     } | 
| 54 | 75 | } | 
0 commit comments