Skip to content

Commit 57ecad4

Browse files
committed
Use reflection to access Java 9+ APIs in ModuleDescriptorRecommendation
This allows us to use 'options.release = 8' for Java compilation as we do in other GradleX projects.
1 parent 1ca7331 commit 57ecad4

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

src/main/java/org/gradlex/javamodule/moduleinfo/tasks/ModuleDescriptorRecommendation.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,25 @@
3636
import java.io.IOException;
3737
import java.io.PrintWriter;
3838
import java.io.StringWriter;
39+
import java.lang.reflect.Method;
3940
import java.nio.file.Files;
4041
import java.nio.file.Path;
42+
import java.nio.file.Paths;
4143
import java.util.ArrayList;
44+
import java.util.Arrays;
4245
import java.util.Collection;
4346
import java.util.Comparator;
4447
import java.util.HashMap;
4548
import java.util.HashSet;
4649
import java.util.List;
4750
import java.util.Map;
51+
import java.util.Optional;
4852
import java.util.Set;
4953
import java.util.SortedSet;
5054
import java.util.TreeSet;
5155
import java.util.function.Function;
5256
import java.util.regex.Matcher;
5357
import java.util.regex.Pattern;
54-
import java.util.spi.ToolProvider;
5558

5659
public abstract class ModuleDescriptorRecommendation extends DefaultTask {
5760

@@ -72,7 +75,7 @@ private static final class Artifact {
7275

7376
final SortedSet<String> provides = new TreeSet<>();
7477

75-
String moduleName;
78+
String moduleName = "";
7679

7780
boolean automatic;
7881

@@ -125,14 +128,22 @@ interface Java8SafeToolProvider {
125128

126129
int run(PrintWriter out, PrintWriter err, String... args);
127130

128-
@SuppressWarnings("Since15")
129131
static Java8SafeToolProvider findFirst(String name) {
130132
try {
131-
ToolProvider tool = ToolProvider.findFirst(name)
132-
.orElseThrow(() -> new RuntimeException("The JDK does not bundle " + name));
133-
return tool::run;
134-
} catch (NoClassDefFoundError e) {
135-
throw new RuntimeException("This functionality requires Gradle to be powered by JDK 11+", e);
133+
Class<?> toolProviderClass = Class.forName("java.util.spi.ToolProvider");
134+
Method findFirst = toolProviderClass.getMethod("findFirst", String.class);
135+
Method run = toolProviderClass.getMethod("run", PrintWriter.class, PrintWriter.class, String[].class);
136+
Optional<?> toolReference = (Optional<?>) findFirst.invoke(null, name);
137+
Object tool = toolReference.orElseThrow(() -> new RuntimeException("The JDK does not bundle " + name));
138+
return (out, err, args) -> {
139+
try {
140+
return (int) run.invoke(tool, out, err, args);
141+
} catch (ReflectiveOperationException e) {
142+
throw new RuntimeException("This functionality requires Gradle to run with JDK 11+", e);
143+
}
144+
};
145+
} catch (ReflectiveOperationException e) {
146+
throw new RuntimeException("This functionality requires Gradle to run with JDK 11+", e);
136147
}
137148
}
138149

@@ -256,13 +267,13 @@ private void storeJdepsToolParsedMetadata(Java8SafeToolProvider jdeps, Path outp
256267
StringWriter err = new StringWriter();
257268
List<String> args = new ArrayList<>();
258269
if (!modulePath.isEmpty()) {
259-
args.addAll(List.of( "--module-path", String.join(File.pathSeparator, modulePath)));
270+
args.addAll(Arrays.asList("--module-path", String.join(File.pathSeparator, modulePath)));
260271
}
261-
args.addAll(List.of("--generate-module-info", outputPath.toString()));
262-
args.addAll(List.of("--multi-release", String.valueOf(getRelease().get())));
272+
args.addAll(Arrays.asList("--generate-module-info", outputPath.toString()));
273+
args.addAll(Arrays.asList("--multi-release", String.valueOf(getRelease().get())));
263274
args.add("--ignore-missing-deps");
264275
args.add(targetArtifact.jar.getAbsolutePath());
265-
int retVal = jdeps.run(new PrintWriter(out, true), new PrintWriter(err, true), args.toArray(String[]::new));
276+
int retVal = jdeps.run(new PrintWriter(out, true), new PrintWriter(err, true), args.toArray(new String[0]));
266277
if (retVal != 0) {
267278
throw new RuntimeException(String.format("jdeps returned error %d\n%s\n%s", retVal, out, err));
268279
}
@@ -271,7 +282,7 @@ private void storeJdepsToolParsedMetadata(Java8SafeToolProvider jdeps, Path outp
271282
? result[1] // Skipping "Warning: --ignore-missing-deps specified. Missing dependencies from xyz are ignored"
272283
: result[0];
273284
String path = writingToMessage.replace("writing to ", "");
274-
String moduleInfoJava = Files.readString(Path.of(path));
285+
String moduleInfoJava = new String(Files.readAllBytes(Paths.get(path)));
275286
String[] parts = moduleInfoJava.split("\\R");
276287
for (String part : parts) {
277288
Matcher requiresMatcher = REQUIRES_PATTERN.matcher(part);

0 commit comments

Comments
 (0)