|
1 | 1 | // SPDX-License-Identifier: Apache-2.0 |
2 | 2 | package org.gradlex.javamodule.testing.internal.actions; |
3 | 3 |
|
| 4 | +import java.lang.reflect.Method; |
4 | 5 | import java.util.ArrayList; |
5 | 6 | import java.util.List; |
6 | 7 | import javax.inject.Inject; |
@@ -31,10 +32,22 @@ public void execute(Task task) { |
31 | 32 |
|
32 | 33 | // Since for Gradle this sources set does not look like a module, we have to define the module path ourselves |
33 | 34 | 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)); |
38 | 37 | javaCompile.getOptions().setCompilerArgs(compilerArgs); |
39 | 38 | } |
| 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 | + } |
40 | 53 | } |
0 commit comments