Skip to content

Commit 164348f

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 4a7a8ff commit 164348f

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,51 @@
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 {
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+
3847
@Inject
39-
private Map<String, Compiler> compilers;
48+
private Map<String, Provider<Compiler>> compilers;
49+
50+
private final Logger log = LoggerFactory.getLogger(getClass());
4051

4152
// ----------------------------------------------------------------------
4253
// CompilerManager Implementation
4354
// ----------------------------------------------------------------------
4455

4556
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);
4759

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);
4963
throw new NoSuchCompilerException(compilerId);
5064
}
5165

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+
}
5374
}
5475
}

0 commit comments

Comments
 (0)