首页 > 解决方案 > 是否可以指示 Kotlin 编译器默认导入其他包(就像它自己的包一样)?

问题描述

我在包中有一堆类my.framework,我想在项目中的任何地方使用它们而不显式导入它们(不使用 usingimport my.framework.*语句)。我可以以某种方式指示 Kotlin 编译器,使其在默认导入的包列表中包含其他包(就像它对 , 等所做的kotlin.collections那样kotlin.text)?

标签: kotlin

解决方案


我没有找到一个简单的方法来做到这一点,但根据这个答案,Kotlin 编译器中有一个特定的文件,您可以更改以修改默认导入。您将必须:
1. 克隆kotlin 存储库git clone https://github.com/JetBrains/kotlin
2. 修改特定文件,默认导入一些额外的包。
例如,要kotlin.reflect默认导入,您可以从项目根目录中修改位于 compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatform.kt 中的 kotlin 文件,如下所示:

/*
 * 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
}
  1. 重新编译 kotlin 编译器: gradle dist
  2. 使用它并查看它是否有效:kotlin 编译器 jar 位于dist/kotlinc/lib/kotlin-compiler.jar并运行您使用的编译器dist/kotlinc/bin/kotlinc

也许只导入所需的包而不是手动重新编译 kotlin 编译器(;


推荐阅读