Skip to content

Commit 554fdcc

Browse files
cstamaskriegaex
andcommitted
Lazy providers and better error reporting
If scanning, injection or construction fails, log a comprehensive error message on top of throwing a NoSuchCompilerException. Fixes #347. Co-authored-by: Alexander Kriegisch <Alexander@Kriegisch.name>
1 parent c5edddc commit 554fdcc

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

plexus-compiler-manager/src/main/java/org/codehaus/plexus/compiler/manager/DefaultCompilerManager.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,52 @@
2525
*/
2626
import javax.inject.Inject;
2727
import javax.inject.Named;
28+
import javax.inject.Provider;
2829

2930
import java.util.Map;
3031

3132
import org.codehaus.plexus.compiler.Compiler;
33+
import org.slf4j.Logger;
34+
import org.slf4j.LoggerFactory;
3235

3336
/**
3437
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
3538
*/
3639
@Named
3740
public class DefaultCompilerManager implements CompilerManager {
3841
@Inject
39-
private Map<String, Compiler> compilers;
42+
private Map<String, Provider<Compiler>> compilers;
43+
44+
private final Logger log = LoggerFactory.getLogger(getClass());
4045

4146
// ----------------------------------------------------------------------
4247
// CompilerManager Implementation
4348
// ----------------------------------------------------------------------
4449

4550
public Compiler getCompiler(String compilerId) throws NoSuchCompilerException {
46-
Compiler compiler = compilers.get(compilerId);
51+
// Provider<Class> is lazy -> presence of provider means compiler is present, but not yet constructed
52+
Provider<Compiler> compilerProvider = compilers.get(compilerId);
4753

48-
if (compiler == null) {
54+
if (compilerProvider == null) {
55+
// Compiler could not be injected for some reason
56+
String errorMessage = "Compiler '{}' either was not found (e.g. wrong compiler ID, missing dependencies) "
57+
+ "or its class could not be scanned. "
58+
+ "Running the build with -Dsisu.debug, looking for exceptions might help. "
59+
+ "A TypeNotPresentException might indicate, that the compiler needs more recent Java runtime. "
60+
+ "An IllegalArgumentException in ClassReader.<init> might mean, that you need to upgrade Maven.";
61+
log.error(errorMessage, compilerId);
4962
throw new NoSuchCompilerException(compilerId);
5063
}
5164

52-
return compiler;
65+
// Provider exists, but compiler was not created yet
66+
try {
67+
return compilerProvider.get();
68+
} catch (Exception e) {
69+
// DI could not construct compiler
70+
String errorMessage = "Compiler '{}' could not be instantiated or injected properly. "
71+
+ "Running the build with -Dsisu.debug, looking for exceptions might help.";
72+
log.error(errorMessage, compilerId);
73+
throw new NoSuchCompilerException(compilerId);
74+
}
5375
}
5476
}

0 commit comments

Comments
 (0)