File tree Expand file tree Collapse file tree 7 files changed +102
-2
lines changed
core-kotlin-modules/core-kotlin-lang-oop-4
main/kotlin/com/baeldung/iteratePropertiesOfDataClass Expand file tree Collapse file tree 7 files changed +102
-2
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
1
+ rootProject.name = " core-kotlin-lang-oop-4"
2
+
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
1
+ package com.baeldung.iteratePropertiesOfDataClass
2
+
3
+ import oliviazoe0.processor.AutoUnpack
4
+
5
+ @AutoUnpack
6
+ data class Person (val name : String , val age : Int )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
plugins {
2
2
kotlin(" jvm" ) version " 1.8.21"
3
+ kotlin(" kapt" ) version " 1.8.21"
3
4
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"
5
6
}
6
7
7
8
repositories {
8
9
mavenCentral()
9
10
}
10
11
12
+ buildscript {
13
+ dependencies {
14
+ classpath(" org.jlleitschuh.gradle:ktlint-gradle:7.1.0" )
15
+ }
16
+ }
17
+
11
18
dependencies {
12
19
detektPlugins(" io.gitlab.arturbosch.detekt:detekt-formatting:1.23.0" )
13
- classpath ( " org.jlleitschuh.gradle:ktlint-gradle:7.1.0 " )
20
+ testImplementation(kotlin( " test " ) )
14
21
}
22
+
You can’t perform that action at this time.
0 commit comments