Search code examples
javakotlininstantiation

Kotlin java.lang.NoSuchMethodException: <init>()


I'm using kotlin to store in a database a class from a library. The problem is that this class, haven't got a constructor with no arguments(It is a Java class). When I retrieve the object from the database, I get the following error, as It has no constructor:

java.lang.NoSuchMethodException: org.springframework.security.oauth2.core.OAuth2AccessToken.<init>()

The only solutions I think It will solve the problem, are the following:

  • Change the class where I retrieve the object to Java.
  • Store a different object instead of org.springframework.security.oauth2.core.OAuth2AccessToken class

Any more thoughts on how to solve this problem?

This is my class in the database:

@Document(collection = "authorizedClient")
data class AuthorizedClientDatabase(
    @Id
    var id: ObjectId = ObjectId.get(),
    var name: String? = null,
    var clientRegistration: ClientRegistration,
    var accessToken: OAuth2AccessToken,
    var refreshToken: OAuth2RefreshToken? = null
)

This is the repository class:

@Repository
interface AuthorizedClientDatabaseRepository : MongoRepository<AuthorizedClientDatabase, ObjectId> {
}

This is the OAuth2AccessToken

And I'm simply making a:

authorizedClientDatabaseRepository.findById(...)

Solution

  • I finally changed the OAuth2AccessToken class and created a custom one:

    data class DatabaseOauth2AccessToken(
        val tokenValue: String,
        val issuedAt: Instant?,
        val expiredAt: Instant?,
        val scopes: MutableSet<String>
    )
    

    AFAIK, this is the most viable solution I have found.