How to fix the conflict when implement two id interfaces?
Asked Answered
D

1

5

One interface is 'org.springframework.data.domain.Persistable', it's a java interface with a method ID getId() in 3rd-party lib.

Another interface is a Kotlin interface interface IdEntry { val id: String}.

Now my business entry needs to implement these two interfaces:

data class MyEntry(
  override val id: String,
  ....// more properties
) : IdEntry, Persistable<String>

I use IntelliJ IDE to code and the error is:

Class 'MyEntry' is not abstract and does not implement abstract member 
@Nullable public abstract fun getId(): String! 
defined in org.springframework.data.domain.Persistable

How can I fix this?

I also tried below code: (idea from here)

data class MyEntry(
  private val id: String,
  ....// more properties
) : IdEntry, Persistable<String> {
  override fun getId() = id
  ...
}

But this also failed:

Cannot weaken access privilege 'public' for 'id' in 'IdEntry'
Diphthongize answered 19/2, 2020 at 8:58 Comment(4)
What do you think this means: Cannot weaken access privilege 'public' for 'id' in 'IdEntry' ?Brittaney
That's the error tip from IntelliJ IDEA.Diphthongize
Yes, that I understand, but do you know what it means? Do you know what IntelliJ is trying to tell you?Brittaney
I know what it means but don not know how to fix.Diphthongize
D
6

This is a platform declaration clash that cannot easily be resolved as long as MyEntry implements both IdEntry and Persistable.

However, there is one way, if you declare the property that is inherited by IdEntry as a @JvmField:

import org.springframework.data.domain.Persistable

data class MyEntry(@JvmField override var id: String) :
        IdEntry, Persistable<String> {

    override fun getId(): String? = this.id

    override fun isNew(): Boolean {
        TODO("not implemented")
    }
}

interface IdEntry {
    val id: String
}

This works, because the clash occurs when the Kotlin compiler generates getters and setters for the id property as long as it has no @JvmField annotation.

The getter clashes with the function getId() inherited from Persistable.

By adding @JvmField, the generation of the getter for the id from IdEntry is avoided, thus preventing the clash.

Demmy answered 19/2, 2020 at 9:25 Comment(1)
When compile by kotlin-1.6.10,this way compile failed with 'JvmField cannot be applied to a property that overrides some other property'. See <youtrack.jetbrains.com/issue/KT-32753>. How to fixed it?Diphthongize

© 2022 - 2024 — McMap. All rights reserved.