@@ -47,8 +47,7 @@ public void apply(Project project) {
4747 + "https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_cross_compilation" );
4848 }
4949 // if -PspotlessModern=true, then use the modern stuff instead of the legacy stuff
50- Provider <String > spotlessModernProperty = safeForUseAtConfigurationTime (
51- project .getProviders ().gradleProperty (SPOTLESS_MODERN ));
50+ Provider <String > spotlessModernProperty = forUseAtConfigurationTime (project .getProviders ().gradleProperty (SPOTLESS_MODERN ));
5251 if (spotlessModernProperty .isPresent ()) {
5352 project .getLogger ().warn ("'spotlessModern' has no effect as of Spotless 5.0, recommend removing it." );
5453 }
@@ -72,14 +71,29 @@ static String capitalize(String input) {
7271 return Character .toUpperCase (input .charAt (0 )) + input .substring (1 );
7372 }
7473
75- static <T > Provider <T > safeForUseAtConfigurationTime (@ Nonnull Provider <T > provider ) {
76- try {
77- Method method = Provider .class .getMethod ("forUseAtConfigurationTime" );
78- @ SuppressWarnings ("unchecked" )
79- Provider <T > configuredProvider = (Provider <T >) method .invoke (provider );
80- return configuredProvider ;
81- } catch (ReflectiveOperationException e ) {
74+ static Provider <String > forUseAtConfigurationTime (@ Nonnull Provider <String > provider ) {
75+ if (isGradle73OrNewer () && !isGradle74OrNewer ()) {
76+ try {
77+ // Use reflection to access the forUseAtConfigurationTime method as it was removed in Gradle 9.
78+ Method method = Provider .class .getMethod ("forUseAtConfigurationTime" );
79+ return (Provider <String >) method .invoke (provider );
80+ } catch (Exception e ) {
81+ throw new RuntimeException ("Failed to invoke forUseAtConfigurationTime via reflection" , e );
82+ }
83+ } else {
8284 return provider ;
8385 }
8486 }
87+
88+ static boolean isGradle73OrNewer () {
89+ return isGradleNewerThan ("7.3" );
90+ }
91+
92+ static boolean isGradle74OrNewer () {
93+ return isGradleNewerThan ("7.4" );
94+ }
95+
96+ private static boolean isGradleNewerThan (String version ) {
97+ return GradleVersion .current ().compareTo (GradleVersion .version (version )) >= 0 ;
98+ }
8599}
0 commit comments