我在my.framework
包中有很多类,我想在项目中的任何地方使用它们,而无需显式导入它们(不使用import my.framework.*
语句)。我可以以某种方式指示Kotlin编译器,以便它在默认导入的列表中包括其他软件包(与kotlin.collections
,kotlin.text
等类似)吗?
答案 0 :(得分:0)
我没有找到简单的方法,但是根据this的回答,在Kotlin编译器中有一个specific file,您可以更改以修改默认导入。
您将必须:
1.克隆kotlin repository:
git clone https://github.com/JetBrains/kotlin
2.修改特定文件,默认情况下导入一些其他软件包。
例如,默认情况下要导入kotlin.reflect
,您可以从项目根目录修改相对于项目根目录下位于编译器/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatform.kt中的kotlin文件: / p>
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.resolve.jvm.platform
import org.jetbrains.kotlin.builtins.DefaultBuiltIns
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.ImportPath
import org.jetbrains.kotlin.resolve.PlatformConfigurator
import org.jetbrains.kotlin.resolve.TargetPlatform
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import java.util.*
object JvmPlatform : TargetPlatform("JVM") {
override val defaultImports: List<ImportPath> = ArrayList<ImportPath>().apply {
add(ImportPath("java.lang.*"))
add(ImportPath("kotlin.*"))
add(ImportPath("kotlin.annotation.*"))
add(ImportPath("kotlin.jvm.*"))
add(ImportPath("kotlin.collections.*"))
add(ImportPath("kotlin.coroutines.*"))
add(ImportPath("kotlin.ranges.*"))
add(ImportPath("kotlin.sequences.*"))
add(ImportPath("kotlin.text.*"))
add(ImportPath("kotlin.io.*"))
add(ImportPath("kotlin.reflect.*")) //This is the important change!!!
fun addAllClassifiersFromScope(scope: MemberScope) {
for (descriptor in scope.getContributedDescriptors(DescriptorKindFilter.CLASSIFIERS, MemberScope.ALL_NAME_FILTER)) {
add(ImportPath(DescriptorUtils.getFqNameSafe(descriptor), false))
}
}
val builtIns = DefaultBuiltIns.Instance
for (builtinPackageFragment in builtIns.builtInsPackageFragments) {
addAllClassifiersFromScope(builtinPackageFragment.getMemberScope())
}
}
override val platformConfigurator: PlatformConfigurator = JvmPlatformConfigurator
}
gradle dist
dist/kotlinc/lib/kotlin-compiler.jar
中,要运行编译器,请使用dist/kotlinc/bin/kotlinc
。也许导入所需的软件包而不是手动重新编译kotlin编译器(;