本文介绍了解决Kotlin中的意外覆盖错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我最近开始与Kotlin到期,并使用Kotlin启动了Spring Boot宠物项目.

I've recently started expirementing with Kotlin and started a Spring Boot pet project using Kotlin.

我正在尝试将自定义用户域对象集成到Spring Security,因此想要实现界面.

I'm trying to integrate a custom User domain object to Spring Security and thus want to implement the UserDetails inteface.

在下面给出我的域User对象:

Given my domain User object below:

import org.springframework.data.annotation.Id as DocumentId
import org.springframework.data.mongodb.core.mapping.Document
import org.springframework.security.core.GrantedAuthority
import org.springframework.security.core.authority.AuthorityUtils
import org.springframework.security.core.userdetails.UserDetails

@Document
data class User(@DocumentId val id: String? = null,
                val username: String = "",
                val password: String = "",
                val email: String = "",
                val name: String? = null,
                val surname: String? = null) : UserDetails {

    override fun isCredentialsNonExpired(): Boolean = true

    override fun isAccountNonExpired(): Boolean = true

    override fun isAccountNonLocked(): Boolean = true

    override fun getAuthorities(): MutableCollection<out GrantedAuthority> = AuthorityUtils.createAuthorityList("USER")

    override fun isEnabled(): Boolean = true
}

我收到以下错误:

  1. 意外覆盖:以下声明具有相同的JVM签名(getUsername()Ljava/lang/String;):公共最终乐趣< get-username>():Kotlin.String,公共抽象乐趣getUsername():Kotlin.String!

  1. Accidental override: The following declarations have the same JVMsignature (getUsername()Ljava/lang/String;): public final fun < get-username>(): Kotlin.String, public abstract fun getUsername(): Kotlin.String!

意外覆盖:以下声明具有相同的JVM签名(getPassword()Ljava/lang/String;):公共最终乐趣< get-password>():Kotlin.String,公共抽象乐趣getPassword():Kotlin.String!

Accidental override: The following declarations have the same JVMsignature (getPassword()Ljava/lang/String;): public final fun < get-password>(): Kotlin.String, public abstract fun getPassword(): Kotlin.String!

由于我的User类已经具有方法getUsername():Kotlin.String,所以也实现了方法getUsername():Kotlin.String! ?

Since my User class already has a method getUsername(): Kotlin.String also implement the method getUsername(): Kotlin.String! ?

除了使用是否位于属性的getter和setter上?

How am I supposed to resolve such an error, other than using the @JvmName on the property's getter and setter?

推荐答案

这里的问题是,从Kotlin的角度来看,属性获取器无法从超类型重写函数.要解决此问题,可以通过使属性为private并手动从超类型实现所需的方法来防止编译器生成getter,例如:

The problem here is that it's impossible for a property getter to override a function from a supertype, from Kotlin's point of view. To workaround it, you can prevent the compiler from generating getters by making your properties private and implement required methods from supertypes manually, for example:

data class User(
    private val username: String = ""
    ...
): UserDetails {
    override fun getUsername() = username
    ...
}

这篇关于解决Kotlin中的意外覆盖错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 16:12