Skip to content

Commit 945203f

Browse files
committed
Throw EcjFailureException in performCompile, not in constructor
This shifts the focus from a supposed Maven component wiring error to the root cause, expressed in the error message: "ECJ needs JRE 17+". The new static boolean field EclipseJavaCompiler.isJdkSupported also enables us to write a simple unit test, asserting on the error message, if the field value is false. Relates to #347.
1 parent d372db6 commit 945203f

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

plexus-compilers/plexus-compiler-eclipse/src/main/java/org/codehaus/plexus/compiler/eclipse/EclipseJavaCompiler.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
public class EclipseJavaCompiler extends AbstractCompiler {
6565
public EclipseJavaCompiler() {
6666
super(CompilerOutputStyle.ONE_OUTPUT_FILE_PER_INPUT_FILE, ".java", ".class", null);
67-
if (Runtime.version().feature() < 17) throw new EcjFailureException("ECJ only works on Java 17+");
67+
if (!isJdkSupported) return;
6868
try {
6969
// Do not directly import EclipseJavaCompilerDelegate or any ECJ classes compiled to target 17.
7070
// This ensures that the plugin still runs on Java 11 and can report the error above.
@@ -82,9 +82,10 @@ public EclipseJavaCompiler() {
8282
// ----------------------------------------------------------------------
8383
// Compiler Implementation
8484
// ----------------------------------------------------------------------
85+
static boolean isJdkSupported = Runtime.version().feature() >= 17;
8586
boolean errorsAsWarnings = false;
86-
private final MethodHandle getClassLoaderMH;
87-
private final MethodHandle batchCompileMH;
87+
private MethodHandle getClassLoaderMH;
88+
private MethodHandle batchCompileMH;
8889

8990
@Override
9091
public String getCompilerId() {
@@ -93,6 +94,8 @@ public String getCompilerId() {
9394

9495
@Override
9596
public CompilerResult performCompile(CompilerConfiguration config) throws CompilerException {
97+
if (!isJdkSupported) throw new EcjFailureException("ECJ needs JRE 17+");
98+
9699
List<String> args = new ArrayList<>();
97100
args.add("-noExit"); // Make sure ecj does not System.exit on us 8-/
98101

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.codehaus.plexus.compiler.eclipse;
2+
3+
import javax.inject.Inject;
4+
import javax.inject.Named;
5+
6+
import java.io.File;
7+
import java.util.Set;
8+
import java.util.stream.Collectors;
9+
10+
import org.codehaus.plexus.compiler.Compiler;
11+
import org.codehaus.plexus.compiler.CompilerConfiguration;
12+
import org.codehaus.plexus.testing.PlexusTest;
13+
import org.codehaus.plexus.util.FileUtils;
14+
import org.hamcrest.MatcherAssert;
15+
import org.hamcrest.Matchers;
16+
import org.junit.jupiter.api.AfterAll;
17+
import org.junit.jupiter.api.BeforeAll;
18+
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.api.parallel.Isolated;
20+
21+
import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
24+
@PlexusTest
25+
@Isolated("changes static variable")
26+
public class EclipseCompilerUnsupportedJdkTest {
27+
static final boolean IS_JDK_SUPPORTED = EclipseJavaCompiler.isJdkSupported;
28+
29+
@Inject
30+
@Named("eclipse")
31+
Compiler compiler;
32+
33+
private CompilerConfiguration getConfig() throws Exception {
34+
String sourceDir = getBasedir() + "/src/test-input/src/main";
35+
Set<File> sourceFiles = FileUtils.getFileNames(new File(sourceDir), "**/*.java", null, false, true).stream()
36+
.map(File::new)
37+
.collect(Collectors.toSet());
38+
CompilerConfiguration compilerConfig = new CompilerConfiguration();
39+
compilerConfig.addSourceLocation(sourceDir);
40+
compilerConfig.setOutputLocation(getBasedir() + "/target/eclipse/classes");
41+
compilerConfig.setSourceFiles(sourceFiles);
42+
compilerConfig.setTargetVersion("1.8");
43+
compilerConfig.setSourceVersion("1.8");
44+
return compilerConfig;
45+
}
46+
47+
@BeforeAll
48+
public static void setUpClass() {
49+
EclipseJavaCompiler.isJdkSupported = false;
50+
}
51+
52+
@AfterAll
53+
public static void cleanUpClass() {
54+
EclipseJavaCompiler.isJdkSupported = IS_JDK_SUPPORTED;
55+
}
56+
57+
@Test
58+
public void testUnsupportedJdk() {
59+
EcjFailureException error = assertThrows(EcjFailureException.class, () -> compiler.performCompile(getConfig()));
60+
MatcherAssert.assertThat(error.getEcjOutput(), Matchers.containsString("ECJ needs JRE 17+"));
61+
}
62+
}

0 commit comments

Comments
 (0)