3636import java .io .IOException ;
3737import java .io .PrintWriter ;
3838import java .io .StringWriter ;
39+ import java .lang .reflect .Method ;
3940import java .nio .file .Files ;
4041import java .nio .file .Path ;
42+ import java .nio .file .Paths ;
4143import java .util .ArrayList ;
44+ import java .util .Arrays ;
4245import java .util .Collection ;
4346import java .util .Comparator ;
4447import java .util .HashMap ;
4548import java .util .HashSet ;
4649import java .util .List ;
4750import java .util .Map ;
51+ import java .util .Optional ;
4852import java .util .Set ;
4953import java .util .SortedSet ;
5054import java .util .TreeSet ;
5155import java .util .function .Function ;
5256import java .util .regex .Matcher ;
5357import java .util .regex .Pattern ;
54- import java .util .spi .ToolProvider ;
5558
5659public 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