Generic Top

I just announced the new Spring Boot 2 material, coming in REST With Spring:


1. Intro

When working with a legacy code base, using an external library or integrating against a framework, we regularly have use cases where we want to map between different objects or data structures.

In this tutorial, we will look at how to easily achieve this goal using built-in Kotlin features.

2. Simple Extension Function

Let’s use the following example: We have a class User, which might be a class from our core domain. It’s also possible that it’s an entity we load from a relational database.

data class User(
  val firstName: String,
  val lastName: String,
  val street: String,
  val houseNumber: String,
  val phone: String,
  val age: Int,
  val password: String)

Now we want to provide a different view on this data. We’ve decided to call this class UserView and we can imagine it being used as response send from a web controller. While it’s representing the same data in our domain, some fields are an aggregate of the fields of our User class and some fields simply have a different name:

data class UserView(
  val name: String,
  val address: String,
  val telephone: String,
  val age: Int

What we need now is a mapping function, that will map User->UserView. Since UserView is on the outer layer of our application, we don’t want to add this function to our User class. We also don’t want to break the encapsulation of our User class and use a helper class to reach into our User object and pull out its data, in order to create an UserView object.

Fortunately, Kotlin provides a language feature called Extension Functions. We can define an Extension Function on our User class and make it only accessible inside the package scope it which we defined it:

fun User.toUserView() = UserView(
  name = "$firstName $lastName",
  address = "$street $houseNumber",
  telephone = phone,
  age = age

Let’s use this function inside a test to get a feeling of how to use it:

class UserTest {

    fun `maps User to UserResponse using extension function`() {
        val p = buildUser()
        val view = p.toUserView()

    private fun buildUser(): User {
        return User(

    private fun assertUserView(pr: UserView) {
          { assertEquals("Java Duke", },
          { assertEquals("Javastreet 42", pr.address) },
          { assertEquals("1234567", pr.telephone) },
          { assertEquals(30, pr.age) }

3. Kotlin Reflection Features

While the example above is very simple (and therefore recommended for most use cases), it still involves a bit of boilerplate code. What if we have a class with a lot of fields (maybe hundreds) and most of them have to be mapped to the field with the same name in the target class?

In this case, we can think about using the Kotlin Reflection features to avoid writing most of the mapping code.

The mapping function using reflection looks like this:

fun User.toUserViewReflection() = with(::UserView) {
    val propertiesByName = User::class.memberProperties.associateBy { }
    callBy(parameters.associate { parameter ->
        parameter to when ( {
   -> "$firstName $lastName"
   -> "$street $houseNumber"
   -> phone
            else -> propertiesByName[]?.get([email protected])

We are using the UserView default constructor as the method call receiver by using the Kotlin with() function. Inside the lambda function provided to with(), we use reflection to obtain a Map of member properties (with the member name as the key and the member property as the value) using User::class.memberProperties.associateBy { }.

Next, we call the UserView constructor with a custom parameter mapping. Inside the lambda we provide a conditional mapping, using the when keyword.

An interesting fact is, that we can map the actual parameter names we retrieve using reflection, like instead of simple Strings. This means we can completely leverage the Kotlin compiler here, helping us in case of refactorings without fearing that our code may break.

We have some special mappings for the parameters name, address, and telephone, while we use a default name based mapping for every other field.

While the reflection-based approach seems very interesting at first sight, keep in mind that this introduces additional complexity into the code base and using reflection might have a negative impact on runtime performance.

4. Conclusion

We’ve seen, that we can easily solve simple data mapping use cases using built-in Kotlin language features. While writing the mapping code by hand is fine for simple use cases, we can also write more complex solutions using reflection.

You can find all code examples over on GitHub.

Generic bottom

I just announced the new Spring Boot 2 material, coming in REST With Spring:


newest oldest most voted
Notify of

Thank you for the post. May I use annotation in this case?

Loredana Crusoveanu

Hi Ria, which annotation do you want to use?