https://kotlinlang.org logo
Title
d

Daniele Segato

02/18/2019, 4:45 PM
Do you know of any library that generate builder pattern for Java over a kotlin data class? I was using Immutables (https://immutables.github.io/) library in Java. Kotlin data classes are so much better but they aren't very comfortable to use from Java, specially if you have big constructors or many ways of creating the object. Thanks in advance (please answer in thread, i subscribed it)
t

tddmonkey

02/18/2019, 4:50 PM
https://github.com/Masabi/kotlinbuilder — note: i am the author
c

Czar

02/18/2019, 4:53 PM
d

Daniele Segato

02/18/2019, 5:03 PM
Thanks you both, I'll have a look at both and try them out!
Do they have a builder from another object to mimic the behavior of kotlin
copy
?
t

tddmonkey

02/18/2019, 5:23 PM
Mine doesn’t. Happy to accept PR’s though 🙂
s

snowe

02/18/2019, 5:26 PM
we just add a builder to each data class, since we are slowly getting rid of all java.
data class AssetReturnValueObject(
        val assetId: AssetId?,
        val candidateAssetId: CandidateAssetId?,
        val qualifyingAssetId: QualifyingAssetId?,
        val qualifyingAssetAdjustmentId: QualifyingAssetAdjustmentId?
) {
    class Builder {
        private var assetId: AssetId? = null
        private var candidateAssetId: CandidateAssetId? = null
        private var qualifyingAssetId: QualifyingAssetId? = null
        private var qualifyingAssetAdjustmentId: QualifyingAssetAdjustmentId? = null

        fun assetId(v: AssetId?) = apply { assetId = v }
        fun candidateAssetId(v: CandidateAssetId?) = apply { candidateAssetId = v }
        fun qualifyingAssetId(v: QualifyingAssetId?) = apply { qualifyingAssetId = v }
        fun qualifyingAssetAdjustmentId(v: QualifyingAssetAdjustmentId) = apply { qualifyingAssetAdjustmentId = v }

        fun build() = AssetReturnValueObject(assetId, candidateAssetId, qualifyingAssetId, qualifyingAssetAdjustmentId)
    }

    companion object {
        @JvmStatic fun builder() = Builder()
    }
}
t

tddmonkey

02/18/2019, 5:30 PM
Mine will do that for you. Warning tho: right now it relies on reflection to perform construction. I’m still thinking of a way around that
s

snowe

02/18/2019, 5:31 PM
yes, we just didn't want to depend on another library and started this years before your library existed. (I actually saw your library last year, I think you might have posted about it on here). Since we're getting rid of java it wasn't too useful for us.
t

tddmonkey

02/18/2019, 5:32 PM
It actually generates kotlin code. We put it together as we use Spock for all our testing and needed something to bridge the Groovy -> Kotlin gap
s

snowe

02/18/2019, 5:33 PM
does it use KotlinPoet?
t

tddmonkey

02/18/2019, 5:33 PM
yep
s

snowe

02/18/2019, 5:33 PM
very nice, I had actually considered doing that but we just went with the above example code.
does it use kapt or just a regular annotation processor?
t

tddmonkey

02/18/2019, 5:34 PM
kapt
s

snowe

02/18/2019, 5:35 PM
very nice. at the time there were a lot of issues with kapt as well. I haven't had time to go back to switching our other annotation processors over to kapt either. 😢
t

tddmonkey

02/18/2019, 5:48 PM
I’ve got a rep. in our team now for being obsessed with them 😄
s

snowe

02/18/2019, 5:48 PM
haha 😂
t

tddmonkey

02/18/2019, 5:49 PM
I built a swagger spec generator one when I got sick of the reflection based one giving us problems
I’m actually just modifying that one to use the kotlin metadata to better infer optionality
j

Jeff Gulbronson

02/18/2019, 7:11 PM
Little late to the party on this one, but at work we use Wire (https://github.com/square/wire) for this kind of thing.
d

Daniele Segato

02/18/2019, 7:49 PM
Not really late, I'll probably start refactoring in 2 weeks :-) just scouting for now
g

gildor

02/19/2019, 2:19 AM
does it use kapt or just a regular annotation processor?
What do you mean? Annotation processors know nothing about kapt or APT, they just use jsr269 API, so it’s transparent for AP itself But if you want to use AP with Kotlin code (like in this case, because this AP works only with Kotlin code), you need Kapt, because it’s an adapter that provides jsr269 compatible API for Kotlin code and allow APs work with Kotlin in a transparent way