我遵循了Kotlin Plugin for Eclipse的说明。 Hello World测试工作正常。然后,我做了一个File> New> Kotlin文件。
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知道足够多,可以建议import java.io.File
中的import java.io
将java.io.Serializable
放在同一位置,因此它将在系统上存在以便找到它。如果不是这样,我该在哪里找到和不应该在这里?
答案 0 :(得分:1)
尝试在Eclipse偏好设置-> kotlin->编译器上设置JDK home选项,它为我工作了