Skip to content

Commit 7b110ba

Browse files
authored
KTLN-450: Iterating All Fields of a Data Class without Reflection in Kotlin (#869)
* added unit tests * fixed unit tests * made some changes * code refactor
1 parent cb4bd4d commit 7b110ba

File tree

7 files changed

+102
-2
lines changed

7 files changed

+102
-2
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
plugins {
2+
kotlin("jvm") version "1.8.21"
3+
kotlin("kapt") version "1.8.21"
4+
id("java")
5+
}
6+
7+
group = "org.example"
8+
version = "1.0-SNAPSHOT"
9+
10+
repositories {
11+
mavenCentral()
12+
maven { url = uri("https://jitpack.io") }
13+
}
14+
15+
buildscript {
16+
dependencies {
17+
classpath("org.jlleitschuh.gradle:ktlint-gradle:7.1.0")
18+
}
19+
}
20+
21+
dependencies {
22+
// testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1")
23+
// testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1")
24+
compileOnly("com.github.LunarWatcher:KClassUnpacker:v1.0.1")
25+
testImplementation(kotlin("test"))
26+
}
27+
28+
kapt {
29+
dependencies {
30+
kapt("com.github.LunarWatcher:KClassUnpacker:v1.0.1")
31+
}
32+
}
33+
tasks.getByName<Test>("test") {
34+
useJUnitPlatform()
35+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
4+
networkTimeout=10000
5+
zipStoreBase=GRADLE_USER_HOME
6+
zipStorePath=wrapper/dists
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rootProject.name = "core-kotlin-lang-oop-4"
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.iteratePropertiesOfDataClass
2+
3+
fun getFields(person: Person): List<String> {
4+
var list = mutableListOf<String>()
5+
val cls = person
6+
for(field in cls) {
7+
list.add(field.toString())
8+
}
9+
10+
return list
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.iteratePropertiesOfDataClass
2+
3+
import oliviazoe0.processor.AutoUnpack
4+
5+
@AutoUnpack
6+
data class Person(val name: String, val age: Int)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import com.baeldung.iteratePropertiesOfDataClass.Person
2+
import com.baeldung.iteratePropertiesOfDataClass.getFields
3+
import kotlin.test.Test
4+
import kotlin.test.assertEquals
5+
6+
class IteratePropertiesOfDataClassUnitTest {
7+
@Test
8+
fun `iterate fields using destructuring declaration`() {
9+
val person = Person("Robert", 28)
10+
val (name, age) = person
11+
assertEquals("Robert", name)
12+
assertEquals(28, age)
13+
}
14+
15+
@Test
16+
fun `iterate fields using componentN methods`() {
17+
val person = Person("Robert", 28)
18+
val fields = listOf(person.component1(), person.component2())
19+
20+
assertEquals("Robert", fields[0])
21+
assertEquals(28, fields[1])
22+
}
23+
24+
@Test
25+
fun `iterate fields using KClassUnpacker plugin`() {
26+
val person = Person("Robert", 28)
27+
val list = getFields(person)
28+
29+
assertEquals("Robert", list[0])
30+
assertEquals("28", list[1])
31+
}
32+
}

kotlin-build-plugins/build.gradle.kts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
plugins {
22
kotlin("jvm") version "1.8.21"
3+
kotlin("kapt") version "1.8.21"
34
id("io.gitlab.arturbosch.detekt") version "1.23.0"
4-
id "org.jlleitschuh.gradle.ktlint" version "7.1.0"
5+
id("org.jlleitschuh.gradle.ktlint") version "7.1.0"
56
}
67

78
repositories {
89
mavenCentral()
910
}
1011

12+
buildscript {
13+
dependencies {
14+
classpath("org.jlleitschuh.gradle:ktlint-gradle:7.1.0")
15+
}
16+
}
17+
1118
dependencies {
1219
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.23.0")
13-
classpath ("org.jlleitschuh.gradle:ktlint-gradle:7.1.0")
20+
testImplementation(kotlin("test"))
1421
}
22+

0 commit comments

Comments
 (0)