Eclipse/Kotlin仅识别一些需要的导入 [英] Eclipse/Kotlin identifying just some needed imports

查看:188
本文介绍了Eclipse/Kotlin仅识别一些需要的导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了 Kotlin Eclipse插件的说明. Hello World测试工作正常.然后,我做了一个File> New> Kotlin文件.

I followed the instructions for Kotlin Plugin for Eclipse. A Hello World test works fine. I then did a File>New>Kotlin File.

package so2
object LineParserRegistry {
    val parsers = ConcurrentHashMap<KClass<*>, (String) -> Any?>()
    inline fun <reified T> register(noinline parser : (String) -> T?) {
        parsers[T::class] = parser
    }
    inline fun <reified T> get(): (String) -> T? {
        // force companion initializer
        Class.forName(T::class.java.name)
        return parsers[T::class] as (String) -> T??
    }
}
data class College(val id: String, val name: String) {
    companion object {
        init {
            val collegeLineParser: (String) -> College? = { line ->
                val regex = Regex("(\\d+) (.+)")
                regex.matchEntire(line)?.let {
                    College(it.groupValues[1], it.groupValues[2])
                }
            }
            LineParserRegistry.register(collegeLineParser)
        }
    }
}
inline fun <reified T : Any> File.parseLines(): List<T> =
    useLines { it.mapNotNull(LineParserRegistry.get<T>()).toList() }
fun main(){
    val colleges = File("/home/cwhii/work/input.txt").parseLines<College>()
    println("colleges: $colleges")
    println("OK.")
}

Eclipse提供了添加这些imports的功能:

Eclipse offered to add these imports which I had it do:

import java.util.concurrent.ConcurrentHashMap
import kotlin.reflect.KClass
import java.io.File

当我运行它时,结果如下:

When I run it here is the result:

No Location
    ERROR: Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
    class kotlin.text.Regex, unresolved supertypes: java.io.Serializable

/home/cwhii/work/sw/kaptcp/src/so/loadClass.kt
    ERROR: Unresolved reference: File (14, 30)
    ERROR: Unresolved reference: File (18, 17)
/home/cwhii/work/sw/kaptcp/src/so2/hiddenReg.kt
    ERROR: Unresolved reference: java (2, 8)
    ERROR: Unresolved reference: java (4, 8)
    ERROR: Unresolved reference: ConcurrentHashMap (6, 19)
    ERROR: Unresolved reference: Class (12, 9)
    ERROR: Cannot access class 'java.lang.Class'. Check your module classpath for missing or conflicting dependencies (12, 32)
    ERROR: Unresolved reference: name (12, 37)
    ERROR: Unresolved reference: File (29, 30)
    ERROR: Unresolved reference: File (32, 20)

首先,如何解决?其次,为什么Eclipse指出了import问题而不是其他问题?

First how is this resolved? Secondly, why did Eclipse point out the import issues but not these others?

Eclipse>窗口>首选项> Java>构建路径>类路径变量

Eclipse > Window > Preferences > Java > Build Path > Classpath Variables

我认为,由于Eclipse知道足够多的东西可以建议import java.io中的import java.io.File位于import java.io所在的位置,因此它会存在于系统中以便找到.如果这不是真的,我该在哪里找到和不应该在这里?

I assume that because Eclipse knew enough to suggest import java.io.File which is in import java.io that java.io.Serializable would be in the same place, that it would exist on the system for it to be found. If this is not true where do I locate what is and is not supposed to be here?

推荐答案

尝试在Eclipse偏好设置上设置JDK home选项->科特林->编译器,对我有用

Try to set JDK home option on eclipse preferences -> kotlin -> compiler, it has worked for me

这篇关于Eclipse/Kotlin仅识别一些需要的导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆