Skip to content

Commit f5640d0

Browse files
committed
Add a method to safely call the now-deprecated forUseAtConfigurationTime method
1 parent 0a9cff3 commit f5640d0

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

src/main/java/net/kyori/mammoth/GradleCompat.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@
2424
package net.kyori.mammoth;
2525

2626
import org.gradle.api.Project;
27+
import org.gradle.api.provider.Provider;
2728

2829
final class GradleCompat {
29-
static final boolean HAS_CONVENTION;
30+
static final boolean HAS_CONVENTION = hasMethod(Project.class, "getConvention");
31+
static final boolean HAS_FOR_USE_AT_CONFIGURATION_TIME = hasMethod(Provider.class, "forUseAtConfigurationTime");
3032

31-
static {
32-
boolean hasConvention = false;
33-
try {
34-
Project.class.getMethod("getConvention");
35-
hasConvention = true;
36-
} catch (final NoSuchMethodException ex) {
37-
// no-op
38-
}
39-
HAS_CONVENTION = hasConvention;
33+
private static boolean hasMethod(final Class<?> clazz, final String name, final Class<?>... args) {
34+
try {
35+
clazz.getMethod(name, args);
36+
return true;
37+
} catch (final NoSuchMethodException ex) {
38+
return false;
39+
}
4040
}
4141
}

src/main/java/net/kyori/mammoth/Properties.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,38 @@
2424
package net.kyori.mammoth;
2525

2626
import org.gradle.api.provider.HasConfigurableValue;
27+
import org.gradle.api.provider.Provider;
2728
import org.jetbrains.annotations.NotNull;
2829

2930
public final class Properties {
3031
private Properties() {
3132
}
3233

34+
/**
35+
* Mark a property as being for use at configuration time.
36+
*
37+
* <p>Gradle has deprecated this method, but it is required on older Gradle versions.</p>
38+
*
39+
* @param <T> the provider value type
40+
* @param provider provider to get for use at configuration time
41+
* @return a configuration time-safe view of the provided provider
42+
* @since 1.1.0
43+
*/
44+
public static <T> @NotNull Provider<T> forUseAtConfigurationTime(final @NotNull Provider<T> provider) {
45+
if (GradleCompat.HAS_FOR_USE_AT_CONFIGURATION_TIME) {
46+
return provider.forUseAtConfigurationTime();
47+
} else {
48+
return provider;
49+
}
50+
}
51+
3352
/**
3453
* Touches {@code property} to mark it as {@link HasConfigurableValue#finalizeValue() finalized}.
3554
*
3655
* @param property the property
3756
* @param <T> the type
3857
* @return the property
39-
* @since 2.0.0
58+
* @since 1.0.0
4059
*/
4160
public static <T extends HasConfigurableValue> @NotNull T finalized(final @NotNull T property) {
4261
property.finalizeValue();
@@ -49,7 +68,7 @@ private Properties() {
4968
* @param property the property
5069
* @param <T> the type
5170
* @return the property
52-
* @since 2.0.0
71+
* @since 1.0.0
5372
*/
5473
public static <T extends HasConfigurableValue> @NotNull T finalizedOnRead(final @NotNull T property) {
5574
property.finalizeValueOnRead();
@@ -62,7 +81,7 @@ private Properties() {
6281
* @param property the property
6382
* @param <T> the type
6483
* @return the property
65-
* @since 2.0.0
84+
* @since 1.0.0
6685
*/
6786
public static <T extends HasConfigurableValue> @NotNull T changesDisallowed(final @NotNull T property) {
6887
property.disallowChanges();

0 commit comments

Comments
 (0)