Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin compile error with JdbcOperations Extensions with variable arguments [SPR-16517] #21060

Closed
spring-issuemaster opened this issue Feb 19, 2018 · 4 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

commented Feb 19, 2018

Keith Donald opened SPR-16517 and commented

Hi :) I'm testing out Kotlin 1.2.21 in a small Spring Boot 2 SNAPSHOT application on JDK 9.0.4 and am encountering the following (unexpected) compilation error:

Compilation failure: 
[ERROR] /Users/steadykeith/IdeaProjects/change/src/main/kotlin/org/steadytown/change/ChangeApplication.kt:[24,53] None of the following functions can be called with the arguments supplied: 
[ERROR] public final fun query(p0: String!, @Nullable p1: ((PreparedStatement!) -> Unit)!, p2: ((ResultSet!) -> Unit)!): Unit defined in org.springframework.jdbc.core.JdbcOperations
[ERROR] @Nullable public final fun <T : Any!> query(p0: String!, @Nullable p1: ((PreparedStatement!) -> Unit)!, p2: ((ResultSet!) -> Collection<Person>!)!): Collection<Person>! defined in org.springframework.jdbc.core.JdbcOperations
[ERROR] public final fun <T : Any!> query(p0: String!, @Nullable p1: ((PreparedStatement!) -> Unit)!, p2: ((ResultSet!, Int) -> Person!)!): (Mutable)List<Person!>! defined in org.springframework.jdbc.core.JdbcOperations
[ERROR] public final fun query(p0: String!, p1: ((ResultSet!) -> Unit)!, @Nullable p2: Array<(out) Any!>!): Unit defined in org.springframework.jdbc.core.JdbcOperations
[ERROR] @Nullable public final fun <T : Any!> query(p0: String!, p1: ((ResultSet!) -> Collection<Person>!)!, @Nullable p2: Array<(out) Any!>!): Collection<Person>! defined in org.springframework.jdbc.core.JdbcOperations
[ERROR] public final fun <T : Any!> query(p0: String!, p1: ((ResultSet!, Int) -> Person!)!, @Nullable p2: Array<(out) Any!>!): (Mutable)List<Person!>! defined in org.springframework.jdbc.core.JdbcOperations
[ERROR] public final fun query(p0: String!, p1: Array<(out) Any!>!, p2: ((ResultSet!) -> Unit)!): Unit defined in org.springframework.jdbc.core.JdbcOperations
[ERROR] @Nullable public final fun <T : Any!> query(p0: String!, p1: Array<(out) Any!>!, p2: ((ResultSet!) -> Collection<Person>!)!): Collection<Person>! defined in org.springframework.jdbc.core.JdbcOperations
[ERROR] public final fun <T : Any!> query(p0: String!, p1: Array<(out) Any!>!, p2: ((ResultSet!, Int) -> Person!)!): (Mutable)List<Person!>! defined in org.springframework.jdbc.core.JdbcOperations
[ERROR] @Nullable public abstract fun <T : Any!> query(p0: String!, p1: Array<(out) Any!>!, p2: ResultSetExtractor<Collection<Person>!>!): Collection<Person>! defined in org.springframework.jdbc.core.JdbcOperations
[ERROR] public abstract fun query(p0: String!, p1: Array<(out) Any!>!, p2: RowCallbackHandler!): Unit defined in org.springframework.jdbc.core.JdbcOperations
[ERROR] public abstract fun <T : Any!> query(p0: String!, p1: Array<(out) Any!>!, p2: RowMapper<Person!>!): (Mutable)List<Person!>! defined in org.springframework.jdbc.core.JdbcOperations
[ERROR] @Nullable public abstract fun <T : Any!> query(p0: String!, @Nullable p1: PreparedStatementSetter!, p2: ResultSetExtractor<Collection<Person>!>!): Collection<Person>! defined in org.springframework.jdbc.core.JdbcOperations
[ERROR] public abstract fun query(p0: String!, @Nullable p1: PreparedStatementSetter!, p2: RowCallbackHandler!): Unit defined in org.springframework.jdbc.core.JdbcOperations
[ERROR] public abstract fun <T : Any!> query(p0: String!, @Nullable p1: PreparedStatementSetter!, p2: RowMapper<Person!>!): (Mutable)List<Person!>! defined in org.springframework.jdbc.core.JdbcOperations
[ERROR] /Users/steadykeith/IdeaProjects/change/src/main/kotlin/org/steadytown/change/ChangeApplication.kt:[24,113] Cannot infer a type for this parameter. Please specify it explicitly.
[ERROR] /Users/steadykeith/IdeaProjects/change/src/main/kotlin/org/steadytown/change/ChangeApplication.kt:[24,117] Cannot infer a type for this parameter. Please specify it explicitly.

The code that is failing to compile, both in an IDE and when separately compiled using Maven, is:

fun find(name: String): Collection<Person> = db.query("select * from PERSONS where FIRST_NAME = ?", name) { rs, _ ->
    Person(rs.getString("FIRST_NAME"), rs.getString("LAST_NAME"), rs.getDate("BIRTH_DATE").toLocalDate())
}

The following workaround (converting the argument to a single Object array parameter using arrayOf(...)) fixes the error:

fun find(name: String): Collection<Person> = db.query("select * from PERSONS where FIRST_NAME = ?", arrayOf(name)) { rs, _ ->
        Person(rs.getString("FIRST_NAME"), rs.getString("LAST_NAME"), rs.getDate("BIRTH_DATE").toLocalDate())
    }

What's really strange is I see Spring Framework's JdbcOperationsExtensionsTests.kt that cover this feature and presume they compile and run fine--however, the above similar code still fails, and even copying some of the test code into my environmental also fails with a similar compilation error.

I apologize if I'm missing something obvious, or if something basic is wrong with my environment, but I'am really at a loss at why this compilation error is occurring against Spring JDBC 5.0.4. Since this seems to be a fairly new feature, I figured I'd report it. Thanks! Keith


Affects: 5.0.4

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Feb 19, 2018

Juergen Hoeller commented

Hey Keith, nice to see you here :-)

Sébastien will have a look at this ASAP. We have a few plans for fine-tuned Kotlin extensions in 5.1 already but may also bring a refinement into 5.0.5 if necessary.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Feb 26, 2018

Sébastien Deleuze commented

Extensions need to be imported in order to be used if not in the same package like in the tests. Could you please check that you have added import org.springframework.jdbc.core.query or import org.springframework.jdbc.core.*?

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Feb 26, 2018

Keith Donald commented

Sebastian, thank you for reviewing this and for the pointer.

I checked my class imports, and I had not. Importing org.springframework.jdbc.core.* resolves the compilation error. I also see the JdbcOperationsExtensionsKt.class in that package within the spring-jdbc 5 jar.

IntelliJ's quick fix support is also able to suggest the extensions and then add imports for you - you just have to scroll through the list of all the other method signature variants... extensions appear to be listed last.

Thank you again for your help!

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Feb 26, 2018

Sébastien Deleuze commented

You're welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.