Skip to content

Commit 9a6801b

Browse files
committed
Use reflective access for JavaModuleDetector methods
1 parent 758f704 commit 9a6801b

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/main/java/org/gradlex/javamodule/testing/internal/actions/JavaCompileSetModulePathAction.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
package org.gradlex.javamodule.testing.internal.actions;
33

4+
import java.lang.reflect.Method;
45
import java.util.ArrayList;
56
import java.util.List;
67
import javax.inject.Inject;
@@ -31,10 +32,22 @@ public void execute(Task task) {
3132

3233
// Since for Gradle this sources set does not look like a module, we have to define the module path ourselves
3334
compilerArgs.add("--module-path");
34-
compilerArgs.add(getJavaModuleDetector()
35-
.inferModulePath(true, classpathAndModulePath)
36-
.getAsPath());
37-
javaCompile.setClasspath(getJavaModuleDetector().inferClasspath(true, classpathAndModulePath));
35+
compilerArgs.add(infer("ModulePath", classpathAndModulePath).getAsPath());
36+
javaCompile.setClasspath(infer("Classpath", classpathAndModulePath));
3837
javaCompile.getOptions().setCompilerArgs(compilerArgs);
3938
}
39+
40+
/**
41+
* Use reflective access for JavaModuleDetector methods so that it does not matter if JavaModuleDetector is a
42+
* class (<9.5.0) or an interface (>9.5.0).
43+
*/
44+
private FileCollection infer(String pathType, FileCollection classpathAndModulePath) {
45+
try {
46+
Method inferModulePath =
47+
JavaModuleDetector.class.getDeclaredMethod("infer" + pathType, boolean.class, FileCollection.class);
48+
return (FileCollection) inferModulePath.invoke(getJavaModuleDetector(), true, classpathAndModulePath);
49+
} catch (ReflectiveOperationException e) {
50+
throw new RuntimeException(e);
51+
}
52+
}
4053
}

0 commit comments

Comments
 (0)