diff --git a/demo-project/groovy/build.gradle b/demo-project/groovy/build.gradle index 581c02f..b75627c 100644 --- a/demo-project/groovy/build.gradle +++ b/demo-project/groovy/build.gradle @@ -1,5 +1,5 @@ plugins { - id 'groovy' + id 'java' id 'com.github.gmazzo.buildconfig' id 'java-test-fixtures' } @@ -45,8 +45,8 @@ buildConfig { buildConfigField(byte[], "BYTE_NATIVE_ARRAY_PROVIDER", provider { [1, 2, 3] }) buildConfigField(Byte[], "BYTE_ARRAY", [1, 2, 3]) buildConfigField(Byte[], "BYTE_ARRAY_PROVIDER", provider { [1, 2, 3] }) - buildConfigField(Byte[], "BYTE_ARRAY_NULLABLE", [1, null, 3]) - buildConfigField(Byte[], "BYTE_ARRAY_NULLABLE_PROVIDER", provider { [1, null, 3] }) + buildConfigField('Byte?[]', "BYTE_ARRAY_NULLABLE", [1, null, 3]) + buildConfigField('Byte?[]', "BYTE_ARRAY_NULLABLE_PROVIDER", provider { [1, null, 3] }) buildConfigField('List', "BYTE_LIST", [1, null, 3]) buildConfigField('List', "BYTE_LIST_PROVIDER", provider { [1, null, 3] }) buildConfigField('Set', "BYTE_SET", [1, null, 3]) @@ -60,8 +60,8 @@ buildConfig { buildConfigField(short[], "SHORT_NATIVE_ARRAY_PROVIDER", provider { [1, 2, 3] }) buildConfigField(Short[], "SHORT_ARRAY", [1, 2, 3]) buildConfigField(Short[], "SHORT_ARRAY_PROVIDER", provider { [1, 2, 3] }) - buildConfigField(Short[], "SHORT_ARRAY_NULLABLE", [1, null, 3]) - buildConfigField(Short[], "SHORT_ARRAY_NULLABLE_PROVIDER", provider { [1, null, 3] }) + buildConfigField('Short?[]', "SHORT_ARRAY_NULLABLE", [1, null, 3]) + buildConfigField('Short?[]', "SHORT_ARRAY_NULLABLE_PROVIDER", provider { [1, null, 3] }) buildConfigField('List', "SHORT_LIST", [1, null, 3]) buildConfigField('List', "SHORT_LIST_PROVIDER", provider { [1, null, 3] }) buildConfigField('Set', "SHORT_SET", [1, null, 3]) @@ -75,8 +75,8 @@ buildConfig { buildConfigField(char[], "CHAR_NATIVE_ARRAY_PROVIDER", provider { ['a' as char, 'b' as char, 'c' as char] }) buildConfigField(Character[], "CHAR_ARRAY", ['a' as char, 'b' as char, 'c' as char]) buildConfigField(Character[], "CHAR_ARRAY_PROVIDER", provider { ['a' as char, 'b' as char, 'c' as char] }) - buildConfigField(Character[], "CHAR_ARRAY_NULLABLE", ['a' as char, null, 'c' as char]) - buildConfigField(Character[], "CHAR_ARRAY_NULLABLE_PROVIDER", provider { ['a' as char, null, 'c' as char] }) + buildConfigField('Character?[]', "CHAR_ARRAY_NULLABLE", ['a' as char, null, 'c' as char]) + buildConfigField('Character?[]', "CHAR_ARRAY_NULLABLE_PROVIDER", provider { ['a' as char, null, 'c' as char] }) buildConfigField('List', "CHAR_LIST", ['a' as char, null, 'c' as char]) buildConfigField('List', "CHAR_LIST_PROVIDER", provider { ['a' as char, null, 'c' as char] }) buildConfigField('Set', "CHAR_SET", ['a' as char, null, 'c' as char]) @@ -90,8 +90,8 @@ buildConfig { buildConfigField(int[], "INT_NATIVE_ARRAY_PROVIDER", provider { [1, 2, 3] }) buildConfigField(Integer[], "INT_ARRAY", [1, 2, 3]) buildConfigField(Integer[], "INT_ARRAY_PROVIDER", provider { [1, 2, 3] }) - buildConfigField(Integer[], "INT_ARRAY_NULLABLE", [1, null, 3]) - buildConfigField(Integer[], "INT_ARRAY_NULLABLE_PROVIDER", provider { [1, null, 3] }) + buildConfigField('Integer?[]', "INT_ARRAY_NULLABLE", [1, null, 3]) + buildConfigField('Integer?[]', "INT_ARRAY_NULLABLE_PROVIDER", provider { [1, null, 3] }) buildConfigField('List', "INT_LIST", [1, null, 3]) buildConfigField('List', "INT_LIST_PROVIDER", provider { [1, null, 3] }) buildConfigField('Set', "INT_SET", [1, null, 3]) @@ -105,8 +105,8 @@ buildConfig { buildConfigField(long[], "LONG_NATIVE_ARRAY_PROVIDER", provider { [1L, 2L, 3L] }) buildConfigField(Long[], "LONG_ARRAY", [1L, 2L, 3L]) buildConfigField(Long[], "LONG_ARRAY_PROVIDER", provider { [1L, 2L, 3L] }) - buildConfigField(Long[], "LONG_ARRAY_NULLABLE", [1L, null, 3L]) - buildConfigField(Long[], "LONG_ARRAY_NULLABLE_PROVIDER", provider { [1L, null, 3L] }) + buildConfigField('Long?[]', "LONG_ARRAY_NULLABLE", [1L, null, 3L]) + buildConfigField('Long?[]', "LONG_ARRAY_NULLABLE_PROVIDER", provider { [1L, null, 3L] }) buildConfigField('List', "LONG_LIST", [1L, null, 3L]) buildConfigField('List', "LONG_LIST_PROVIDER", provider { [1L, null, 3L] }) buildConfigField('Set', "LONG_SET", [1L, null, 3L]) @@ -120,8 +120,8 @@ buildConfig { buildConfigField(float[], "FLOAT_NATIVE_ARRAY_PROVIDER", provider { [1f, 2f, 3f] }) buildConfigField(Float[], "FLOAT_ARRAY", [1f, 2f, 3f]) buildConfigField(Float[], "FLOAT_ARRAY_PROVIDER", provider { [1f, 2f, 3f] }) - buildConfigField(Float[], "FLOAT_ARRAY_NULLABLE", [1f, null, 3f]) - buildConfigField(Float[], "FLOAT_ARRAY_NULLABLE_PROVIDER", provider { [1f, null, 3f] }) + buildConfigField('Float?[]', "FLOAT_ARRAY_NULLABLE", [1f, null, 3f]) + buildConfigField('Float?[]', "FLOAT_ARRAY_NULLABLE_PROVIDER", provider { [1f, null, 3f] }) buildConfigField('List', "FLOAT_LIST", [1f, null, 3f]) buildConfigField('List', "FLOAT_LIST_PROVIDER", provider { [1f, null, 3f] }) buildConfigField('Set', "FLOAT_SET", [1f, null, 3f]) @@ -135,8 +135,8 @@ buildConfig { buildConfigField(double[], "DOUBLE_NATIVE_ARRAY_PROVIDER", provider { [1.0, 2.0, 3.0] }) buildConfigField(Double[], "DOUBLE_ARRAY", [1.0, 2.0, 3.0]) buildConfigField(Double[], "DOUBLE_ARRAY_PROVIDER", provider { [1.0, 2.0, 3.0] }) - buildConfigField(Double[], "DOUBLE_ARRAY_NULLABLE", [1.0, null, 3.0]) - buildConfigField(Double[], "DOUBLE_ARRAY_NULLABLE_PROVIDER", provider { [1.0, null, 3.0] }) + buildConfigField('Double?[]', "DOUBLE_ARRAY_NULLABLE", [1.0, null, 3.0]) + buildConfigField('Double?[]', "DOUBLE_ARRAY_NULLABLE_PROVIDER", provider { [1.0, null, 3.0] }) buildConfigField('List', "DOUBLE_LIST", [1.0, null, 3.0]) buildConfigField('List', "DOUBLE_LIST_PROVIDER", provider { [1.0, null, 3.0] }) buildConfigField('Set', "DOUBLE_SET", [1.0, null, 3.0]) @@ -150,8 +150,8 @@ buildConfig { buildConfigField(boolean[], "BOOLEAN_NATIVE_ARRAY_PROVIDER", provider { [true, false, false] }) buildConfigField(Boolean[], "BOOLEAN_ARRAY", [true, false, false]) buildConfigField(Boolean[], "BOOLEAN_ARRAY_PROVIDER", provider { [true, false, false] }) - buildConfigField(Boolean[], "BOOLEAN_ARRAY_NULLABLE", [true, null, false]) - buildConfigField(Boolean[], "BOOLEAN_ARRAY_NULLABLE_PROVIDER", provider { [true, null, false] }) + buildConfigField('Boolean?[]', "BOOLEAN_ARRAY_NULLABLE", [true, null, false]) + buildConfigField('Boolean?[]', "BOOLEAN_ARRAY_NULLABLE_PROVIDER", provider { [true, null, false] }) buildConfigField('List', "BOOLEAN_LIST", [true, null, false]) buildConfigField('List', "BOOLEAN_LIST_PROVIDER", provider { [true, null, false] }) buildConfigField('Set', "BOOLEAN_SET", [true, null, false]) @@ -195,19 +195,12 @@ sourceSets { /** * A task that iterates over your classpath resources and generate constants for them */ -def buildResources = buildConfig.forClass("BuildResources") { +buildConfig.forClass("BuildResources") { buildConfigField('String', 'A_CONSTANT', '"aConstant"') -} -def generateResourcesConstants = tasks.register("generateResourcesConstants") { - doFirst { - sourceSets.main.resources.asFileTree.visit { file -> - def name = file.path.toUpperCase().replaceAll("\\W", "_") - buildResources.buildConfigField('java.io.File', name, "new File(\"$file.path\")") - } - } -} + sourceSets.main.resources.asFileTree.visit { + def name = it.path.toUpperCase().replaceAll("\\W", "_") -tasks.generateBuildConfig { - dependsOn(generateResourcesConstants) + buildConfigField('java.io.File', name, "new File(\"${it.path}\")") + } } diff --git a/demo-project/groovy/src/testFixtures/java/com/github/gmazzo/buildconfig/demos/groovy/BuildConfigBaseTest.java b/demo-project/groovy/src/testFixtures/java/com/github/gmazzo/buildconfig/demos/groovy/BuildConfigBaseTest.java index 596d12d..0a4387f 100644 --- a/demo-project/groovy/src/testFixtures/java/com/github/gmazzo/buildconfig/demos/groovy/BuildConfigBaseTest.java +++ b/demo-project/groovy/src/testFixtures/java/com/github/gmazzo/buildconfig/demos/groovy/BuildConfigBaseTest.java @@ -2,12 +2,9 @@ import org.junit.Test; -import java.lang.reflect.Array; -import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.HashSet; import java.util.Map; -import java.util.Set; import static org.junit.Assert.*; @@ -46,8 +43,8 @@ public void testBytes() { assertEquals(64, BuildConfig.BYTE_PROVIDER); assertArrayEquals(new byte[]{1, 2, 3}, BuildConfig.BYTE_NATIVE_ARRAY); assertArrayEquals(new byte[]{1, 2, 3}, BuildConfig.BYTE_NATIVE_ARRAY_PROVIDER); - assertArrayEquals(new Byte[]{1, 2, 3}, BuildConfig.BYTE_ARRAY); - assertArrayEquals(new Byte[]{1, 2, 3}, BuildConfig.BYTE_ARRAY_PROVIDER); + assertArrayEquals(new byte[]{1, 2, 3}, BuildConfig.BYTE_ARRAY); + assertArrayEquals(new byte[]{1, 2, 3}, BuildConfig.BYTE_ARRAY_PROVIDER); assertArrayEquals(new Byte[]{1, null, 3}, BuildConfig.BYTE_ARRAY_NULLABLE); assertArrayEquals(new Byte[]{1, null, 3}, BuildConfig.BYTE_ARRAY_NULLABLE_PROVIDER); assertEquals(Arrays.asList((byte) 1, null, (byte) 3), BuildConfig.BYTE_LIST); @@ -63,8 +60,8 @@ public void testShorts() { assertEquals(64, BuildConfig.SHORT_PROVIDER); assertArrayEquals(new short[]{1, 2, 3}, BuildConfig.SHORT_NATIVE_ARRAY); assertArrayEquals(new short[]{1, 2, 3}, BuildConfig.SHORT_NATIVE_ARRAY_PROVIDER); - assertArrayEquals(new Short[]{1, 2, 3}, BuildConfig.SHORT_ARRAY); - assertArrayEquals(new Short[]{1, 2, 3}, BuildConfig.SHORT_ARRAY_PROVIDER); + assertArrayEquals(new short[]{1, 2, 3}, BuildConfig.SHORT_ARRAY); + assertArrayEquals(new short[]{1, 2, 3}, BuildConfig.SHORT_ARRAY_PROVIDER); assertArrayEquals(new Short[]{1, null, 3}, BuildConfig.SHORT_ARRAY_NULLABLE); assertArrayEquals(new Short[]{1, null, 3}, BuildConfig.SHORT_ARRAY_NULLABLE_PROVIDER); assertEquals(Arrays.asList((short) 1, null, (short) 3), BuildConfig.SHORT_LIST); @@ -80,8 +77,8 @@ public void testChars() { assertEquals('a', BuildConfig.CHAR_PROVIDER); assertArrayEquals(new char[]{'a', 'b', 'c'}, BuildConfig.CHAR_NATIVE_ARRAY); assertArrayEquals(new char[]{'a', 'b', 'c'}, BuildConfig.CHAR_NATIVE_ARRAY_PROVIDER); - assertArrayEquals(new Character[]{'a', 'b', 'c'}, BuildConfig.CHAR_ARRAY); - assertArrayEquals(new Character[]{'a', 'b', 'c'}, BuildConfig.CHAR_ARRAY_PROVIDER); + assertArrayEquals(new char[]{'a', 'b', 'c'}, BuildConfig.CHAR_ARRAY); + assertArrayEquals(new char[]{'a', 'b', 'c'}, BuildConfig.CHAR_ARRAY_PROVIDER); assertArrayEquals(new Character[]{'a', null, 'c'}, BuildConfig.CHAR_ARRAY_NULLABLE); assertArrayEquals(new Character[]{'a', null, 'c'}, BuildConfig.CHAR_ARRAY_NULLABLE_PROVIDER); assertEquals(Arrays.asList('a', null, 'c'), BuildConfig.CHAR_LIST); @@ -97,8 +94,8 @@ public void testIntegers() { assertEquals(1, BuildConfig.INT_PROVIDER); assertArrayEquals(new int[]{1, 2, 3}, BuildConfig.INT_NATIVE_ARRAY); assertArrayEquals(new int[]{1, 2, 3}, BuildConfig.INT_NATIVE_ARRAY_PROVIDER); - assertArrayEquals(new Integer[]{1, 2, 3}, BuildConfig.INT_ARRAY); - assertArrayEquals(new Integer[]{1, 2, 3}, BuildConfig.INT_ARRAY_PROVIDER); + assertArrayEquals(new int[]{1, 2, 3}, BuildConfig.INT_ARRAY); + assertArrayEquals(new int[]{1, 2, 3}, BuildConfig.INT_ARRAY_PROVIDER); assertArrayEquals(new Integer[]{1, null, 3}, BuildConfig.INT_ARRAY_NULLABLE); assertArrayEquals(new Integer[]{1, null, 3}, BuildConfig.INT_ARRAY_NULLABLE_PROVIDER); assertEquals(Arrays.asList(1, null, 3), BuildConfig.INT_LIST); @@ -114,8 +111,8 @@ public void testLongs() { assertEquals(1, BuildConfig.LONG_PROVIDER); assertArrayEquals(new long[]{1L, 2L, 3L}, BuildConfig.LONG_NATIVE_ARRAY); assertArrayEquals(new long[]{1L, 2L, 3L}, BuildConfig.LONG_NATIVE_ARRAY_PROVIDER); - assertArrayEquals(new Long[]{1L, 2L, 3L}, BuildConfig.LONG_ARRAY); - assertArrayEquals(new Long[]{1L, 2L, 3L}, BuildConfig.LONG_ARRAY_PROVIDER); + assertArrayEquals(new long[]{1L, 2L, 3L}, BuildConfig.LONG_ARRAY); + assertArrayEquals(new long[]{1L, 2L, 3L}, BuildConfig.LONG_ARRAY_PROVIDER); assertArrayEquals(new Long[]{1L, null, 3L}, BuildConfig.LONG_ARRAY_NULLABLE); assertArrayEquals(new Long[]{1L, null, 3L}, BuildConfig.LONG_ARRAY_NULLABLE_PROVIDER); assertEquals(Arrays.asList(1L, null, 3L), BuildConfig.LONG_LIST); @@ -131,8 +128,8 @@ public void testFloats() { assertEquals(1, BuildConfig.FLOAT_PROVIDER, 0); assertArrayEquals(new float[]{1f, 2f, 3f}, BuildConfig.FLOAT_NATIVE_ARRAY, 0); assertArrayEquals(new float[]{1f, 2f, 3f}, BuildConfig.FLOAT_NATIVE_ARRAY_PROVIDER, 0); - assertArrayEquals(new Float[]{1f, 2f, 3f}, BuildConfig.FLOAT_ARRAY); - assertArrayEquals(new Float[]{1f, 2f, 3f}, BuildConfig.FLOAT_ARRAY_PROVIDER); + assertArrayEquals(new float[]{1f, 2f, 3f}, BuildConfig.FLOAT_ARRAY, 0); + assertArrayEquals(new float[]{1f, 2f, 3f}, BuildConfig.FLOAT_ARRAY_PROVIDER, 0); assertArrayEquals(new Float[]{1f, null, 3f}, BuildConfig.FLOAT_ARRAY_NULLABLE); assertArrayEquals(new Float[]{1f, null, 3f}, BuildConfig.FLOAT_ARRAY_NULLABLE_PROVIDER); assertEquals(Arrays.asList(1f, null, 3f), BuildConfig.FLOAT_LIST); @@ -148,8 +145,8 @@ public void testDoubles() { assertEquals(1, BuildConfig.DOUBLE_PROVIDER, 0); assertArrayEquals(new double[]{1.0, 2.0, 3.0}, BuildConfig.DOUBLE_NATIVE_ARRAY, 0); assertArrayEquals(new double[]{1.0, 2.0, 3.0}, BuildConfig.DOUBLE_NATIVE_ARRAY_PROVIDER, 0); - assertArrayEquals(new Double[]{1.0, 2.0, 3.0}, BuildConfig.DOUBLE_ARRAY); - assertArrayEquals(new Double[]{1.0, 2.0, 3.0}, BuildConfig.DOUBLE_ARRAY_PROVIDER); + assertArrayEquals(new double[]{1.0, 2.0, 3.0}, BuildConfig.DOUBLE_ARRAY, 0); + assertArrayEquals(new double[]{1.0, 2.0, 3.0}, BuildConfig.DOUBLE_ARRAY_PROVIDER, 0); assertArrayEquals(new Double[]{1.0, null, 3.0}, BuildConfig.DOUBLE_ARRAY_NULLABLE); assertArrayEquals(new Double[]{1.0, null, 3.0}, BuildConfig.DOUBLE_ARRAY_NULLABLE_PROVIDER); assertEquals(Arrays.asList(1.0, null, 3.0), BuildConfig.DOUBLE_LIST); @@ -165,8 +162,8 @@ public void testBooleans() { assertTrue(BuildConfig.BOOLEAN_PROVIDER); assertArrayEquals(new boolean[]{true, false, false}, BuildConfig.BOOLEAN_NATIVE_ARRAY); assertArrayEquals(new boolean[]{true, false, false}, BuildConfig.BOOLEAN_NATIVE_ARRAY_PROVIDER); - assertArrayEquals(new Boolean[]{true, false, false}, BuildConfig.BOOLEAN_ARRAY); - assertArrayEquals(new Boolean[]{true, false, false}, BuildConfig.BOOLEAN_ARRAY_PROVIDER); + assertArrayEquals(new boolean[]{true, false, false}, BuildConfig.BOOLEAN_ARRAY); + assertArrayEquals(new boolean[]{true, false, false}, BuildConfig.BOOLEAN_ARRAY_PROVIDER); assertArrayEquals(new Boolean[]{true, null, false}, BuildConfig.BOOLEAN_ARRAY_NULLABLE); assertArrayEquals(new Boolean[]{true, null, false}, BuildConfig.BOOLEAN_ARRAY_NULLABLE_PROVIDER); assertEquals(Arrays.asList(true, null, false), BuildConfig.BOOLEAN_LIST); diff --git a/demo-project/kts-multiplatform/build.gradle.kts b/demo-project/kts-multiplatform/build.gradle.kts index 0a2394b..df3d840 100644 --- a/demo-project/kts-multiplatform/build.gradle.kts +++ b/demo-project/kts-multiplatform/build.gradle.kts @@ -33,3 +33,7 @@ buildConfig { tasks.register("test") { dependsOn("allTests") } + +tasks.named("compileTestDevelopmentExecutableKotlinJs") { + notCompatibleWithConfigurationCache("uses Task.project") +} diff --git a/demo-project/kts/build.gradle.kts b/demo-project/kts/build.gradle.kts index ea80c64..5a02017 100644 --- a/demo-project/kts/build.gradle.kts +++ b/demo-project/kts/build.gradle.kts @@ -203,23 +203,17 @@ val versionsSS = buildConfig.sourceSets.register("Versions") { buildConfigField("myDependencyVersion", "1.0.1") } -val buildResources = buildConfig.forClass("BuildResources") { +/** + * A task that iterates over your classpath resources and generate constants for them + */ +buildConfig.forClass("BuildResources") { buildConfigField("A_CONSTANT", "aConstant") -} -val generateResourcesConstants by tasks.registering { - doFirst { - sourceSets["main"].resources.asFileTree.visit { - val name = path.uppercase().replace("\\W".toRegex(), "_") - with(buildResources) { - buildConfigField("java.io.File", name, "File(\"$path\")") - } - } - } -} + sourceSets["main"].resources.asFileTree.visit { + val name = path.uppercase().replace("\\W".toRegex(), "_") -tasks.generateBuildConfig { - dependsOn(generateResourcesConstants) + buildConfigField("java.io.File", name, "File(\"$path\")") + } } // example of a custom generator that builds into XML diff --git a/gradle.properties b/gradle.properties index b4e2545..d4e3970 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ org.gradle.jvmargs=-Xmx2g org.gradle.caching=true -org.gradle.configuration-cache=true FIXME `kotlin-multiplatform` are not compatible +org.gradle.configuration-cache=true diff --git a/plugin/build.gradle.kts b/plugin/build.gradle.kts index 638c167..dd5d120 100644 --- a/plugin/build.gradle.kts +++ b/plugin/build.gradle.kts @@ -65,3 +65,7 @@ tasks.check { tasks.publish { dependsOn(tasks.publishPlugins) } + +tasks.generateJacocoTestKitProperties { + notCompatibleWithConfigurationCache("uses Task.extensions") +} diff --git a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigClassSpec.kt b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigClassSpec.kt index 9a4d20d..fd4cbbe 100644 --- a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigClassSpec.kt +++ b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigClassSpec.kt @@ -59,7 +59,7 @@ interface BuildConfigClassSpec : Named { type: Class, name: String, value: Type?, - ) = addField(typeOf(type), name, valueOf(castToType(value, type) as Serializable)) + ) = addField(nameOf(type), name, valueOf(castToType(value, type) as Serializable)) fun buildConfigField( type: String, @@ -71,6 +71,6 @@ interface BuildConfigClassSpec : Named { type: Class, name: String, value: Provider, - ) = addField(typeOf(type), name, value.map { valueOf(castToType(it, type) as Serializable) }) + ) = addField(nameOf(type), name, value.map { valueOf(castToType(it, type) as Serializable) }) } diff --git a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigField.kt b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigField.kt index a053947..9ef1e2b 100644 --- a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigField.kt +++ b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigField.kt @@ -3,6 +3,7 @@ package com.github.gmazzo.buildconfig import org.gradle.api.Named import org.gradle.api.provider.Property import org.gradle.api.tasks.Input +import org.gradle.api.tasks.Optional interface BuildConfigField : Named { @@ -10,12 +11,13 @@ interface BuildConfigField : Named { override fun getName(): String @get:Input - val type: Property> + val type: Property @get:Input val value: Property @get:Input + @get:Optional val position: Property } diff --git a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigPlugin.kt b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigPlugin.kt index a4da54c..01c8afe 100644 --- a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigPlugin.kt +++ b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigPlugin.kt @@ -17,6 +17,7 @@ import org.gradle.api.plugins.PluginContainer import org.gradle.api.tasks.SourceSet import org.gradle.kotlin.dsl.add import org.gradle.kotlin.dsl.domainObjectContainer +import org.gradle.kotlin.dsl.newInstance import org.gradle.kotlin.dsl.register class BuildConfigPlugin : Plugin { @@ -93,13 +94,35 @@ class BuildConfigPlugin : Plugin { group = "BuildConfig" description = "Generates the build constants class for '${sourceSet.name}' source" - specs.add(sourceSet.classSpec) - specs.addAll(sourceSet.extraSpecs) generator.set(sourceSet.generator) outputDir.set(layout.buildDirectory.dir("generated/sources/buildConfig/${sourceSet.name}")) + specs.addAll(provider { + (sequenceOf(sourceSet) + sourceSet.extraSpecs) + .map { isolate(it) } + .toList() + }) } } + /** + * Helper method to create a copy of a BuildConfigClassSpec not linked to Gradle related extensions/DSL + * It makes it compatible with Configuration Cache + */ + private fun Project.isolate(source: BuildConfigClassSpec) = + objects.newInstance(source.name).apply { + className.set(source.className) + packageName.set(source.packageName) + documentation.set(source.documentation) + buildConfigFields.addAll(source.buildConfigFields.map { isolate(it) }) + } + + private fun Project.isolate(source: BuildConfigField) = + objects.newInstance(source.name).apply { + type.set(source.type) + value.set(source.value) + position.set(source.position) + } + private val Project.defaultPackage get() = provider { group diff --git a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigTask.kt b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigTask.kt index b70366b..1572865 100644 --- a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigTask.kt +++ b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigTask.kt @@ -47,7 +47,7 @@ abstract class BuildConfigTask : DefaultTask() { packageName = packageName, documentation = it.documentation.orNull, fields = it.buildConfigFields.sortedWith { a, b -> - when (val cmp = a.position.get().compareTo(b.position.get())) { + when (val cmp = a.position.getOrElse(0).compareTo(b.position.getOrElse(0))) { 0 -> a.name.compareTo(b.name) else -> cmp } diff --git a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigType.kt b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigType.kt index 4a567c8..0a3f4ce 100644 --- a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigType.kt +++ b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigType.kt @@ -2,25 +2,26 @@ package com.github.gmazzo.buildconfig import java.io.Serializable -sealed class BuildConfigType : Serializable { - - abstract val ref: RefType - - abstract val typeParameters: List> - - data class JavaRef( - override val ref: Class<*>, - override val typeParameters: List> = emptyList(), - ) : BuildConfigType>() - - data class NameRef( - override val ref: String, - override val typeParameters: List> = emptyList(), - ) : BuildConfigType() - - override fun toString(): String = when(typeParameters.size) { - 0 -> ref.toString() - else -> "$ref<${typeParameters.joinToString(", ")}>" +data class BuildConfigType @JvmOverloads constructor( + val className: String, + val typeArguments: List = emptyList(), + val nullable: Boolean = false, + val array: Boolean = false, +) : Serializable { + + private val text by lazy { + buildString { + append(className) + if (typeArguments.isNotEmpty()) { + append("<") + append(typeArguments.joinToString(", ")) + append(">") + } + if (nullable) append("?") + if (array) append("[]") + } } + override fun toString(): String = text + } diff --git a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigTypeUtils.kt b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigTypeUtils.kt index 9928df5..9b7abf3 100644 --- a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigTypeUtils.kt +++ b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigTypeUtils.kt @@ -2,12 +2,17 @@ package com.github.gmazzo.buildconfig import org.gradle.api.NamedDomainObjectProvider import org.gradle.api.provider.Provider +import org.jetbrains.annotations.VisibleForTesting import java.io.Serializable -import kotlin.reflect.KClass +import java.lang.reflect.GenericArrayType +import java.lang.reflect.ParameterizedType +import java.lang.reflect.Type import kotlin.reflect.KType +import kotlin.reflect.jvm.javaType private val regEx = "([^\\[\\]?]+?)(\\?)?(\\[])?".toRegex() +@VisibleForTesting internal fun String.parseTypename(): Triple { val match = regEx.matchEntire(this) checkNotNull(match) { @@ -19,18 +24,70 @@ internal fun String.parseTypename(): Triple { return Triple(type, nullable.isNotEmpty(), array.isNotEmpty()) } -internal fun typeOf(type: Class<*>) = - BuildConfigType.JavaRef(type) +@Suppress("RecursivePropertyAccessor") +private val Type.genericName: String + get() = when (this) { + Boolean::class.java, Boolean::class.javaObjectType, BooleanArray::class.java -> "Boolean" + Byte::class.java, Byte::class.javaObjectType, ByteArray::class.java -> "Byte" + Short::class.java, Short::class.javaObjectType, ShortArray::class.java -> "Short" + Char::class.java, Char::class.javaObjectType, CharArray::class.java -> "Char" + Int::class.java, Int::class.javaObjectType, IntArray::class.java -> "Int" + Long::class.java, Long::class.javaObjectType, LongArray::class.java -> "Long" + Float::class.java, Float::class.javaObjectType, FloatArray::class.java -> "Float" + Double::class.java, Double::class.javaObjectType, DoubleArray::class.java -> "Double" + String::class.java -> "String" + List::class.java -> "List" + Set::class.java -> "Set" + is GenericArrayType -> genericComponentType.genericName + is ParameterizedType -> rawType.genericName + is Class<*> -> if (isArray) componentType.genericName else name + else -> error("Unsupported type: $this") + } + +private val Type.isArray get() = when (this) { + is Class<*> -> isArray + is GenericArrayType -> true + else -> false +} + +internal fun nameOf(type: Type): BuildConfigType = when(type) { + is Class<*> -> BuildConfigType( + className = type.genericName, + typeArguments = type.typeParameters.map { nameOf(it.genericDeclaration) }, + nullable = false, + array = type.isArray + ) + is ParameterizedType -> BuildConfigType( + className = type.rawType.genericName, + typeArguments = type.actualTypeArguments.map { nameOf(it) }, + nullable = false, + array = false + ) + is GenericArrayType -> BuildConfigType( + className = type.genericComponentType.genericName, + typeArguments = checkNotNull(type.genericComponentType as? ParameterizedType) { + "Unsupported type: $type" + }.actualTypeArguments.map { nameOf(it) }, + nullable = false, + array = true + ) + else -> error("Unsupported type: $type") +} @PublishedApi -internal fun nameOf(type: KType): BuildConfigType.NameRef = (type.classifier!! as KClass<*>).let { kClass -> - BuildConfigType.NameRef( - kClass.qualifiedName!! + if (type.isMarkedNullable) "?" else "", - if (kClass.typeParameters.isEmpty()) emptyList() else type.arguments.map { nameOf(it.type!!) } +internal fun nameOf(type: KType): BuildConfigType { + val isArray = type.javaType.isArray + val targetType = type.takeIf { isArray }?.arguments?.singleOrNull()?.type ?: type + + return BuildConfigType( + className = type.javaType.genericName, + typeArguments = targetType.arguments.map { nameOf(it.type!!) }, + nullable = targetType.isMarkedNullable, + array = isArray ) } -internal fun nameOf(className: String): BuildConfigType.NameRef { +internal fun nameOf(className: String): BuildConfigType { val iterator = className.toList().listIterator() val nameRef = parseName(iterator, null) check(!iterator.hasNext()) { @@ -43,9 +100,9 @@ internal fun nameOf(className: String): BuildConfigType.NameRef { private fun parseName( iterator: ListIterator, - parentParameters: MutableList? -): BuildConfigType.NameRef { - val parameters = mutableListOf() + parentParameters: MutableList? +): BuildConfigType { + val parameters = mutableListOf() val name = buildString { while (iterator.hasNext()) { when (val ch = iterator.next()) { @@ -64,7 +121,9 @@ private fun parseName( } } } - return BuildConfigType.NameRef(name, parameters) + + val (typeName, nullable, array) = name.parseTypename() + return BuildConfigType(typeName, parameters, nullable, array) } @PublishedApi @@ -76,7 +135,7 @@ internal fun valueOf(value: Serializable?) = BuildConfigValue.Literal(value) private fun BuildConfigClassSpec.buildConfigField( - type: BuildConfigType<*>, + type: BuildConfigType, name: String, action: (BuildConfigField) -> Unit, ): NamedDomainObjectProvider = buildConfigFields.size.let { position -> @@ -88,12 +147,12 @@ private fun BuildConfigClassSpec.buildConfigField( } @PublishedApi -internal fun BuildConfigClassSpec.addField(type: BuildConfigType<*>, name: String, value: BuildConfigValue) = +internal fun BuildConfigClassSpec.addField(type: BuildConfigType, name: String, value: BuildConfigValue) = buildConfigField(type, name) { it.value.value(value).disallowChanges() } @PublishedApi internal fun BuildConfigClassSpec.addField( - type: BuildConfigType<*>, + type: BuildConfigType, name: String, value: Provider ) = diff --git a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/generators/BuildConfigJavaGenerator.kt b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/generators/BuildConfigJavaGenerator.kt index 0ad1fb3..30ecd67 100644 --- a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/generators/BuildConfigJavaGenerator.kt +++ b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/generators/BuildConfigJavaGenerator.kt @@ -4,7 +4,6 @@ import com.github.gmazzo.buildconfig.BuildConfigType import com.github.gmazzo.buildconfig.BuildConfigValue import com.github.gmazzo.buildconfig.asVarArg import com.github.gmazzo.buildconfig.elements -import com.github.gmazzo.buildconfig.parseTypename import com.squareup.javapoet.ArrayTypeName import com.squareup.javapoet.ClassName import com.squareup.javapoet.FieldSpec @@ -23,49 +22,27 @@ data class BuildConfigJavaGenerator( private val logger = Logging.getLogger(javaClass) - private fun BuildConfigType<*>.toTypeName(): TypeName { - fun TypeName.parameterized(parameters: List>) = - if (parameters.isEmpty()) this - else ParameterizedTypeName.get( - this as ClassName, - *parameters.map { it.toTypeName() }.toTypedArray() - ) - - return when (this) { - is BuildConfigType.JavaRef -> TypeName.get(ref).parameterized(typeParameters) - is BuildConfigType.NameRef -> { - val (typeName, isNullable, isArray) = ref.parseTypename() - - var type = when (typeName) { - "boolean" -> TypeName.BOOLEAN - "Boolean" -> TypeName.BOOLEAN.box() - "byte" -> TypeName.BYTE - "Byte" -> TypeName.BYTE.box() - "short" -> TypeName.SHORT - "Short" -> TypeName.SHORT.box() - "char" -> TypeName.CHAR - "Char" -> TypeName.CHAR.box() - "Character" -> TypeName.CHAR.box() - "int" -> TypeName.INT - "Int" -> TypeName.INT.box() - "Integer" -> TypeName.INT.box() - "long" -> TypeName.LONG - "Long" -> TypeName.LONG.box() - "float" -> TypeName.FLOAT - "Float" -> TypeName.FLOAT.box() - "double" -> TypeName.DOUBLE - "Double" -> TypeName.DOUBLE.box() - "String" -> STRING - "List" -> LIST - "Set" -> SET - else -> ClassName.bestGuess(typeName) - } - if (isNullable && type.isPrimitive) type = type.box() - type = type.parameterized(typeParameters) - if (isArray) type = ArrayTypeName.of(type) - return type - } + private fun BuildConfigType.toTypeName(): TypeName { + var type = when (className.lowercase()) { + "boolean" -> if (nullable) TypeName.BOOLEAN.box() else TypeName.BOOLEAN + "byte" -> if (nullable) TypeName.BYTE.box() else TypeName.BYTE + "short" -> if (nullable) TypeName.SHORT.box() else TypeName.SHORT + "char", "character" -> if (nullable) TypeName.CHAR.box() else TypeName.CHAR + "int", "integer" -> if (nullable) TypeName.INT.box() else TypeName.INT + "long" -> if (nullable) TypeName.LONG.box() else TypeName.LONG + "float" -> if (nullable) TypeName.FLOAT.box() else TypeName.FLOAT + "double" -> if (nullable) TypeName.DOUBLE.box() else TypeName.DOUBLE + "string" -> STRING + "list" -> LIST + "set" -> SET + else -> ClassName.bestGuess(className) } + if (typeArguments.isNotEmpty()) type = ParameterizedTypeName.get( + type as ClassName, + *typeArguments.map { it.copy(nullable = true).toTypeName() }.toTypedArray() + ) + if (array) type = ArrayTypeName.of(type) + return type } override fun execute(spec: BuildConfigGeneratorSpec) { diff --git a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/generators/BuildConfigKotlinGenerator.kt b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/generators/BuildConfigKotlinGenerator.kt index 63e9e05..39ccb1c 100644 --- a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/generators/BuildConfigKotlinGenerator.kt +++ b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/generators/BuildConfigKotlinGenerator.kt @@ -5,7 +5,6 @@ import com.github.gmazzo.buildconfig.BuildConfigType import com.github.gmazzo.buildconfig.BuildConfigValue import com.github.gmazzo.buildconfig.asVarArg import com.github.gmazzo.buildconfig.elements -import com.github.gmazzo.buildconfig.parseTypename import com.squareup.kotlinpoet.ARRAY import com.squareup.kotlinpoet.BOOLEAN import com.squareup.kotlinpoet.BOOLEAN_ARRAY @@ -91,41 +90,27 @@ data class BuildConfigKotlinGenerator( } } - private fun BuildConfigType<*>.toTypeName(): TypeName { - fun Class<*>.toTypeName(): TypeName = - if (isArray) ARRAY.parameterizedBy(componentType.toTypeName()) - else kotlin.asTypeName() - - fun TypeName.parameterized(parameters: List>) = - if (parameters.isEmpty()) this - else (this as ClassName).parameterizedBy(*parameters.map { it.toTypeName() }.toTypedArray()) - - return when (this) { - is BuildConfigType.JavaRef -> ref.toTypeName() - is BuildConfigType.NameRef -> { - val (typeName, isNullable, isArray) = ref.parseTypename() - - var type: TypeName = when (typeName.lowercase()) { - "boolean" -> if (isArray && !isNullable) BOOLEAN_ARRAY else BOOLEAN - "byte" -> if (isArray && !isNullable) BYTE_ARRAY else BYTE - "short" -> if (isArray && !isNullable) SHORT_ARRAY else SHORT - "char" -> if (isArray && !isNullable) CHAR_ARRAY else CHAR - "int" -> if (isArray && !isNullable) INT_ARRAY else INT - "integer" -> if (isArray && !isNullable) INT_ARRAY else INT - "long" -> if (isArray && !isNullable) LONG_ARRAY else LONG - "float" -> if (isArray && !isNullable) FLOAT_ARRAY else FLOAT - "double" -> if (isArray && !isNullable) DOUBLE_ARRAY else DOUBLE - "string" -> STRING - "list" -> LIST - "set" -> SET - else -> ClassName.bestGuess(typeName) - } - type = type.parameterized(typeParameters) - if (isNullable) type = type.copy(nullable = true) - if (isArray && !type.isPrimitiveArray) type = ARRAY.parameterizedBy(type) - return type - } + private fun BuildConfigType.toTypeName(): TypeName { + var type: TypeName = when (className.lowercase()) { + "boolean" -> if (array && !nullable) BOOLEAN_ARRAY else BOOLEAN + "byte" -> if (array && !nullable) BYTE_ARRAY else BYTE + "short" -> if (array && !nullable) SHORT_ARRAY else SHORT + "char" -> if (array && !nullable) CHAR_ARRAY else CHAR + "int" -> if (array && !nullable) INT_ARRAY else INT + "integer" -> if (array && !nullable) INT_ARRAY else INT + "long" -> if (array && !nullable) LONG_ARRAY else LONG + "float" -> if (array && !nullable) FLOAT_ARRAY else FLOAT + "double" -> if (array && !nullable) DOUBLE_ARRAY else DOUBLE + "string" -> STRING + "list" -> LIST + "set" -> SET + else -> ClassName.bestGuess(className) } + if (typeArguments.isNotEmpty()) + type = (type as ClassName).parameterizedBy(*typeArguments.map { it.toTypeName() }.toTypedArray()) + if (nullable) type = type.copy(nullable = true) + if (array && !type.isPrimitiveArray) type = ARRAY.parameterizedBy(type) + return type } private fun FileSpec.Builder.addFields(fields: List, kdoc: String?): FileSpec.Builder = when { diff --git a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/internal/GroovyNullValueWorkaround.kt b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/internal/GroovyNullValueWorkaround.kt index c26b27f..d694ef6 100644 --- a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/internal/GroovyNullValueWorkaround.kt +++ b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/internal/GroovyNullValueWorkaround.kt @@ -2,11 +2,10 @@ package com.github.gmazzo.buildconfig.internal import com.github.gmazzo.buildconfig.BuildConfigClassSpec import com.github.gmazzo.buildconfig.addField -import com.github.gmazzo.buildconfig.typeOf +import com.github.gmazzo.buildconfig.nameOf import com.github.gmazzo.buildconfig.valueOf import groovy.lang.GroovyObjectSupport import java.io.Serializable -import java.lang.reflect.Type /** * Workaround for Groovy's `null` value issue when calling overloaded methods limitation: @@ -24,6 +23,6 @@ internal abstract class GroovyNullValueWorkaround : BuildConfigClassSpec, Groovy name: String, value: Any?, // this should be `Serializable?` but Groovy fails to resolve the overloading when `null as Serializable` is passed as value ) = check(value is Serializable?) { "Value is not a Serializable: $value (${value!!::class.java.name})" } - .run { addField(typeOf(type), name, valueOf(value)) } + .run { addField(nameOf(type), name, valueOf(value)) } } diff --git a/plugin/src/main/kotlin/org/gradle/kotlin/dsl/BuildConfigClassSpecDSL.kt b/plugin/src/main/kotlin/org/gradle/kotlin/dsl/BuildConfigClassSpecDSL.kt index 9aee531..b1275d3 100644 --- a/plugin/src/main/kotlin/org/gradle/kotlin/dsl/BuildConfigClassSpecDSL.kt +++ b/plugin/src/main/kotlin/org/gradle/kotlin/dsl/BuildConfigClassSpecDSL.kt @@ -1,6 +1,7 @@ package org.gradle.kotlin.dsl import com.github.gmazzo.buildconfig.BuildConfigClassSpec +import com.github.gmazzo.buildconfig.BuildConfigDsl import com.github.gmazzo.buildconfig.addField import com.github.gmazzo.buildconfig.nameOf import com.github.gmazzo.buildconfig.valueOf @@ -8,46 +9,54 @@ import org.gradle.api.provider.Provider import java.io.Serializable import kotlin.reflect.typeOf +@BuildConfigDsl inline fun BuildConfigClassSpec.buildConfigField( name: String, value: Type?, ) = addField(nameOf(typeOf()), name, valueOf(value)) +@BuildConfigDsl inline fun BuildConfigClassSpec.buildConfigField( name: String, value: Provider, ) = addField(nameOf(typeOf()), name, value.map(::valueOf)) +@BuildConfigDsl @JvmName("buildConfigFieldArray") inline fun BuildConfigClassSpec.buildConfigField( name: String, value: Array, ) = addField(nameOf(typeOf>()), name, valueOf(value)) +@BuildConfigDsl @JvmName("buildConfigFieldList") inline fun BuildConfigClassSpec.buildConfigField( name: String, value: List, ) = addField(nameOf(typeOf>()), name, valueOf(if (value is Serializable) value else ArrayList(value))) +@BuildConfigDsl @JvmName("buildConfigFieldSet") inline fun BuildConfigClassSpec.buildConfigField( name: String, value: Set, ) = addField(nameOf(typeOf>()), name, valueOf(if (value is Serializable) value else LinkedHashSet(value))) +@BuildConfigDsl @JvmName("buildConfigFieldArray") inline fun BuildConfigClassSpec.buildConfigField( name: String, value: Provider>, ) = addField(nameOf(typeOf>()), name, value.map(::valueOf)) +@BuildConfigDsl @JvmName("buildConfigFieldList") inline fun BuildConfigClassSpec.buildConfigField( name: String, value: Provider>, ) = addField(nameOf(typeOf>()), name, value.map { valueOf(if (it is Serializable) it else ArrayList(it)) }) +@BuildConfigDsl @JvmName("buildConfigFieldSet") inline fun BuildConfigClassSpec.buildConfigField( name: String, diff --git a/plugin/src/test/kotlin/com/github/gmazzo/buildconfig/BuildConfigTypeUtilsTests.kt b/plugin/src/test/kotlin/com/github/gmazzo/buildconfig/BuildConfigTypeUtilsTests.kt index 8d7cb21..ddaee89 100644 --- a/plugin/src/test/kotlin/com/github/gmazzo/buildconfig/BuildConfigTypeUtilsTests.kt +++ b/plugin/src/test/kotlin/com/github/gmazzo/buildconfig/BuildConfigTypeUtilsTests.kt @@ -1,28 +1,71 @@ package com.github.gmazzo.buildconfig -import com.github.gmazzo.buildconfig.BuildConfigType.NameRef +import kotlin.reflect.typeOf import kotlin.test.Test import kotlin.test.assertEquals class BuildConfigTypeUtilsTests { - private val paramsStringInt = listOf(NameRef("String"), NameRef("Int")) - private val paramsStringListOfInt = listOf(NameRef("String"), NameRef("List", listOf(NameRef("Int?")))) + private val string = BuildConfigType("String") + private val int = BuildConfigType("Int") + private val mapStringInt = BuildConfigType("java.util.Map", listOf(string, int)) + private val mapStringListOfInt = + BuildConfigType("java.util.Map", listOf(string, BuildConfigType("List", listOf(int.copy(nullable = true))))) @Test - fun testNameOf() { - assertEquals(NameRef("String"), nameOf("String")) - assertEquals(NameRef("String?"), nameOf("String?")) - assertEquals(NameRef("String[]"), nameOf("String[]")) - assertEquals(NameRef("String?[]"), nameOf("String?[]")) - assertEquals(NameRef("Map", paramsStringInt), nameOf("Map")) - assertEquals(NameRef("Map?", paramsStringInt), nameOf("Map?")) - assertEquals(NameRef("Map[]", paramsStringInt), nameOf("Map[]")) - assertEquals(NameRef("Map?[]", paramsStringInt), nameOf("Map?[]")) - assertEquals(NameRef("Map", paramsStringListOfInt), nameOf("Map>")) - assertEquals(NameRef("Map?", paramsStringListOfInt), nameOf("Map>?")) - assertEquals(NameRef("Map[]", paramsStringListOfInt), nameOf("Map>[]")) - assertEquals(NameRef("Map?[]", paramsStringListOfInt), nameOf("Map>?[]")) + fun testNameOfByJavaClass() { + assertEquals(string, nameOf(String::class.java)) + assertEquals(string.copy(array = true), nameOf(Array::class.java)) + + assertEquals(int, nameOf(Int::class.java)) + assertEquals(int.copy(array = true), nameOf(Array::class.java)) + assertEquals(int.copy(array = true), nameOf(Array::class.java)) + assertEquals(int.copy(array = true), nameOf(IntArray::class.java)) + } + + @Test + fun testNameOfByKType() { + assertEquals(string, nameOf(typeOf())) + assertEquals(string.copy(nullable = true), nameOf(typeOf())) + assertEquals(string.copy(array = true), nameOf(typeOf>())) + assertEquals(string.copy(nullable = true, array = true), nameOf(typeOf>())) + + assertEquals(int, nameOf(typeOf())) + assertEquals(int.copy(nullable = true), nameOf(typeOf())) + assertEquals(int.copy(array = true), nameOf(typeOf>())) + assertEquals(int.copy(array = true), nameOf(typeOf())) + assertEquals(int.copy(nullable = true, array = true), nameOf(typeOf>())) + + assertEquals(mapStringInt, nameOf(typeOf>())) + assertEquals(mapStringInt.copy(nullable = true), nameOf(typeOf?>())) + assertEquals(mapStringInt.copy(array = true), nameOf(typeOf>>())) + assertEquals(mapStringInt.copy(nullable = true, array = true), nameOf(typeOf?>>())) + + assertEquals(mapStringListOfInt, nameOf(typeOf>>())) + assertEquals(mapStringListOfInt.copy(nullable = true), nameOf(typeOf>?>())) + assertEquals(mapStringListOfInt.copy(array = true), nameOf(typeOf>>>())) + assertEquals( + mapStringListOfInt.copy(nullable = true, array = true), + nameOf(typeOf>?>>()) + ) + } + + @Test + fun testNameOfByName() { + assertEquals(string, nameOf("String")) + assertEquals(string.copy(nullable = true), nameOf("String?")) + assertEquals(string.copy(array = true), nameOf("String[]")) + assertEquals(string.copy(nullable = true, array = true), nameOf("String?[]")) + + assertEquals(mapStringInt, nameOf("java.util.Map")) + assertEquals(mapStringInt.copy(nullable = true), nameOf("java.util.Map?")) + assertEquals(mapStringInt.copy(array = true), nameOf("java.util.Map[]")) + assertEquals(mapStringInt.copy(nullable = true, array = true), nameOf("java.util.Map?[]")) + + assertEquals(mapStringListOfInt, nameOf("java.util.Map>")) + assertEquals(mapStringListOfInt.copy(nullable = true), nameOf("java.util.Map>?")) + assertEquals(mapStringListOfInt.copy(array = true), nameOf("java.util.Map>[]")) + assertEquals(mapStringListOfInt.copy(nullable = true, array = true), nameOf("java.util.Map>?[]")) } }