Do you know of any library that generate builder p...
# announcements
d
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
https://github.com/Masabi/kotlinbuilder — note: i am the author
c
d
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
Mine doesn’t. Happy to accept PR’s though 🙂
s
we just add a builder to each data class, since we are slowly getting rid of all java.
Copy code
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
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
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
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
does it use KotlinPoet?
t
yep
s
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
kapt
s
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
I’ve got a rep. in our team now for being obsessed with them 😄
s
haha 😂
t
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
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
Not really late, I'll probably start refactoring in 2 weeks :-) just scouting for now
g
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