16
16
import java .io .InputStreamReader ;
17
17
import java .io .Reader ;
18
18
import java .io .UnsupportedEncodingException ;
19
+ import java .lang .reflect .Method ;
19
20
import java .net .URI ;
20
21
import java .net .URISyntaxException ;
21
22
import java .net .URLEncoder ;
@@ -49,7 +50,7 @@ static Optional<Duration> durationSysPropertyOrEnvVariable(String sysPropertyNam
49
50
50
51
static Optional <String > envVariable (String name , ProviderFactory providers ) {
51
52
if (isGradle65OrNewer () && !isGradle74OrNewer ()) {
52
- @ SuppressWarnings ( "deprecation" ) Provider <String > variable = providers .environmentVariable (name ). forUseAtConfigurationTime ( );
53
+ Provider <String > variable = forUseAtConfigurationTime ( providers .environmentVariable (name ));
53
54
return Optional .ofNullable (variable .getOrNull ());
54
55
}
55
56
return Optional .ofNullable (System .getenv (name ));
@@ -65,7 +66,7 @@ static Optional<Duration> durationEnvVariable(String name, ProviderFactory provi
65
66
66
67
static Optional <String > sysProperty (String name , ProviderFactory providers ) {
67
68
if (isGradle65OrNewer () && !isGradle74OrNewer ()) {
68
- @ SuppressWarnings ( "deprecation" ) Provider <String > property = providers .systemProperty (name ). forUseAtConfigurationTime ( );
69
+ Provider <String > property = forUseAtConfigurationTime ( providers .systemProperty (name ));
69
70
return Optional .ofNullable (property .getOrNull ());
70
71
}
71
72
return Optional .ofNullable (System .getProperty (name ));
@@ -266,6 +267,20 @@ private static Optional<URI> toUri(String scheme, String host, String path) {
266
267
}
267
268
}
268
269
270
+ private static Provider <String > forUseAtConfigurationTime (Provider <String > provider ) {
271
+ if (isGradle65OrNewer () && !isGradle74OrNewer ()) {
272
+ try {
273
+ // Use reflection to access the forUseAtConfigurationTime method as it was removed in Gradle 9.
274
+ Method method = Provider .class .getMethod ("forUseAtConfigurationTime" );
275
+ return (Provider <String >) method .invoke (provider );
276
+ } catch (Exception e ) {
277
+ throw new RuntimeException ("Failed to invoke forUseAtConfigurationTime via reflection" , e );
278
+ }
279
+ } else {
280
+ return provider ;
281
+ }
282
+ }
283
+
269
284
private Utils () {
270
285
}
271
286
0 commit comments