简体   繁体   English

从 java 转换时 kotlin 接收器出现问题

[英]Problem with kotlin receiver when converting from java

I'm trying to convert a java class into a kotlin class我正在尝试将 java class 转换为 kotlin ZA2F2ED4F8EBC2CBB16C21A29DC40


    @Override
    protected Context constructContext(long entityId, String author, String changesSetId) {
        Context context = constructDefaultContext(author, changesSetId);
        try (Connection connection = datasourceConfig.getDataSource().getConnection()) {
            try (Statement stm = connection.createStatement()) {
                try(ResultSet rs = stm.executeQuery("select * from modules where id = " + entityId)){
                    if (rs.next()) {
                        ModuleDto moduleDto = constructModuleDto(rs);
                        context.put("module", moduleDto);
                    } else
                        throw new IllegalArgumentException("No module with id: " + entityId);
                }
            }
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
        return context;    }

the intellij conversion produces intellij 转换产生

override fun constructContext(entityId: Long, author: String, changesSetId: String): Context {
        val context = constructDefaultContext(author, changesSetId)
        try {
            datasourceConfig.dataSource!!.connection.use { connection ->
                connection.createStatement().use { stm ->
                    stm.executeQuery(
                        "select * from modules where id = $entityId"
                    ).use { rs ->
                        if (rs.next()) {
                            val moduleDto = constructModuleDto(rs)
                            context.put("module", moduleDto)
                        } else throw IllegalArgumentException("No module with id: $entityId")
                    }
                }
            }
        } catch (throwables: SQLException) {
            throw RuntimeException(throwables)
        }
        return context
    }

But when I build it, I get the following error:但是当我构建它时,我收到以下错误:

Unresolved reference.未解决的参考。 None of the following candidates is applicable because of receiver type mismatch:由于接收器类型不匹配,以下候选均不适用:

public inline fun <T : Closeable?, R> TypeVariable(T).use(block: (TypeVariable(T)) -> TypeVariable(R)): TypeVariable(R) defined in kotlin.io

Pointing at this line:指向这一行:

datasourceConfig.dataSource!!.connection.use { connection ->

I admit I'm stumped and any advice would be greatly appreciated...我承认我很难过,任何建议将不胜感激......

EDIT:编辑:

dataSourceConfig is a simple class to get a datasource, that I had converted using the Intellij converter at an earlier date: dataSourceConfig 是一个简单的 class 来获取数据源,我之前使用 Intellij 转换器进行了转换:

class DatasourceConfig {
    var ds: DataSource? = null

    @get:Throws(SQLException::class)
    val dataSource: DataSource?
        get() {
            if (null == ds) {
                val state = CodeGenerationConfigState.instance
                val mySQLDataSource = MysqlDataSource()
                mySQLDataSource.setUrl(String.format("jdbc:mysql://localhost:3306/%s", state.dbName))
                mySQLDataSource.user = state.username
                mySQLDataSource.password = state.password
                ds = mySQLDataSource
            }
            return ds
        }
}

and context is a VelocityContext并且上下文是一个 VelocityContext

So it seems I needed to include the kotlin jdk 8 libraries in my gradle build file:所以看来我需要在我的 gradle 构建文件中包含 kotlin jdk 8 库:

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM