kotlin - 是否可以指示 Kotlin 编译器默认导入其他包(就像它自己的包一样)?
问题描述
我在包中有一堆类my.framework
,我想在项目中的任何地方使用它们而不显式导入它们(不使用 usingimport my.framework.*
语句)。我可以以某种方式指示 Kotlin 编译器,使其在默认导入的包列表中包含其他包(就像它对 , 等所做的kotlin.collections
那样kotlin.text
)?
解决方案
我没有找到一个简单的方法来做到这一点,但根据这个答案,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
}
- 重新编译 kotlin 编译器:
gradle dist
- 使用它并查看它是否有效:kotlin 编译器 jar 位于
dist/kotlinc/lib/kotlin-compiler.jar
并运行您使用的编译器dist/kotlinc/bin/kotlinc
。
也许只导入所需的包而不是手动重新编译 kotlin 编译器(;
推荐阅读
- javascript - iframe 返回空值
- java - JSP 页面到 word 文件在 Google Docs 上显示“无法打开文档,文件似乎已损坏”
- java - 为什么通过反射调用时自动装箱不使用 valueOf()?
- python - Django - 如何在调度方法中访问过滤的查询集?
- c# - 如何以正确的编码获取在 WebBrowser 中打开的页面的 html 代码?
- git - 是否有可能找出提交被推送到 git origin Repo 的日期/时间?
- c# - 包含无限数学方程的 If 语句
- xamarin - 隐藏 Xamarin MAC 平台的 ScrollView 的 Scollbar
- python - 为每个文件分别制作具有输出和输入文件大小信息的新 txt 文件
- r - 使用 sets 包对 R 中的集合列表进行集合操作