https://kotlinlang.org logo
#getting-started
Title
# getting-started
k

Klitos Kyriacou

04/14/2022, 8:38 AM
I've got an error message that I don't understand:
Copy code
val sqlServerDataSource = SQLServerDataSource()
        sqlServerDataSource.url = myUrl
        sqlServerDataSource.user = myUser
        sqlServerDataSource.password = myPassword
The error is:
Copy code
Cannot access 'password': it is public/*package*/ for synthetic extension in '<library Maven: com.microsoft.sqlserver:mssql-jdbc:10.2.0.jre17>'
There is no error on url or user; only on password. If I replace it with
sqlServerDataSource.setPassword(myPassword)
it works. But why can't I use the property syntax, and what exactly does the error message mean, and why only password has this error?
It turns out that this seems to be caused by the Java class SQLServerDataSource having
public void setPassword(String password)
but only a package-private
String getPassword()
. This prevents Kotlin from synthesizing a
password
property. Is this intentional, or is it a bug?
m

Michael de Kaste

04/14/2022, 9:24 AM
It's definitely a weird visibility way sqlServerDataSource uses since it's literally impossible to recreate the same scenario in kotlin; this the field cannot be mapped properly to kotlin accessibility
image.png
so it seems that setPassword and getPassword should be actual functions you should access in kotlin as well. The fact you can directly access a "password" field is a bug I would say
v

Vampire

04/14/2022, 9:26 AM
https://kotlinlang.org/docs/java-interop.html#getters-and-setters
Note that, if the Java class only has a setter, it isn't visible as a property in Kotlin because Kotlin doesn't support set-only properties.
👍 1
🙏 1
e

ephemient

04/14/2022, 10:21 AM
the Java functions are always accessible (up to visibility) even when Kotlin synthesizes a property
5 Views