Eclipse/Kotlin identifying just some needed import

2019-07-31 22:18发布

问题:

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 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)

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

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

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?