|
16 | 16 | import groovy.lang.Closure; |
17 | 17 | import groovy.lang.GroovyObjectSupport; |
18 | 18 | import groovy.lang.MissingPropertyException; |
| 19 | +import org.gradle.api.provider.ProviderConvertible; |
| 20 | +import org.jetbrains.annotations.Contract; |
| 21 | +import org.jetbrains.annotations.Nullable; |
| 22 | + |
19 | 23 | import java.util.Map; |
20 | 24 |
|
21 | 25 | import javax.inject.Inject; |
@@ -85,15 +89,43 @@ public void mappings(String channel, String version) { |
85 | 89 | getMappingVersion().set(version); |
86 | 90 | } |
87 | 91 |
|
88 | | - public void mappings(Map<String, ? extends CharSequence> mappings) { |
89 | | - CharSequence channel = mappings.get("channel"); |
90 | | - CharSequence version = mappings.get("version"); |
| 92 | + public void mappings(Object channel, Object version) { |
| 93 | + try { |
| 94 | + getMappingChannel().set(valueOf(channel, String.class)); |
| 95 | + getMappingVersion().set(valueOf(version, String.class)); |
| 96 | + } catch (IllegalArgumentException e) { |
| 97 | + throw new IllegalArgumentException("Tried to set mappings using non-Strings", e); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public void mappings(Map<String, ?> mappings) { |
| 102 | + Object channel = mappings.get("channel"); |
| 103 | + Object version = mappings.get("version"); |
91 | 104 |
|
92 | 105 | if (channel == null || version == null) { |
93 | 106 | throw new IllegalArgumentException("Must specify both mappings channel and version"); |
94 | 107 | } |
95 | 108 |
|
96 | | - mappings(channel.toString(), version.toString()); |
| 109 | + mappings(channel, version); |
| 110 | + } |
| 111 | + |
| 112 | + @Contract("!null, _ -> !null") |
| 113 | + private static <T> @Nullable T valueOf(@Nullable Object object, Class<T> castTo) { |
| 114 | + if (object instanceof ProviderConvertible<?>) |
| 115 | + object = ((ProviderConvertible<?>) object).asProvider().get(); |
| 116 | + else if (object instanceof Provider<?>) |
| 117 | + object = ((Provider<?>) object).get(); |
| 118 | + |
| 119 | + if (object == null) |
| 120 | + return null; |
| 121 | + |
| 122 | + try { |
| 123 | + return castTo.cast(object); |
| 124 | + } catch (ClassCastException e) { |
| 125 | + throw new IllegalArgumentException(String.format( |
| 126 | + "Expected class %s but got class %s for value: %s", castTo.getName(), object.getClass().getName(), object) |
| 127 | + ); |
| 128 | + } |
97 | 129 | } |
98 | 130 |
|
99 | 131 | public ConfigurableFileCollection getAccessTransformers() { |
|
0 commit comments