gpt4 book ai didi

eclipse - Eclipse/Kotlin仅识别一些需要的导入

转载 作者:行者123 更新时间:2023-12-02 12:52:50 24 4
gpt4 key购买 nike

我按照Kotlin Plugin for Eclipse的说明进行操作。 Hello World测试工作正常。然后,我做了一个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的功能:
import java.util.concurrent.ConcurrentHashMap
import kotlin.reflect.KClass
import java.io.File

当我运行它时,结果如下:
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问题而不是其他问题?

Eclipse>窗口>首选项> Java>构建路径>类路径变量
Eclipse > Window > Preferences > Java > Build Path > Classpath Variables

我假设由于Eclipse知道足够多,可以建议 import java.io.File位于 import java.io中,因此 java.io.Serializable将位于同一位置,因此它将存在于系统中以便找到它。如果这不是真的,我应该在哪里找到和不应该在这里?

最佳答案

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

关于eclipse - Eclipse/Kotlin仅识别一些需要的导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57015559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com