Exceptions.kt:

@Suppress("NOTHING_TO_INLINE")
inline fun generateStyleNotCorrectException(key: String, value: String) =
        AOPException(key + " = " + value)

In kotlin:

fun inKotlin(key: String, value: String) {
    throw generateStyleNotCorrectException(key, value) }

It works in kotlin and the function is inlined.

But when used in Java code, It just cannot be inlined, and still a normal static method call (seen from the decompiled contents).

Something like this:

public static final void inJava(String key, String value) throws AOPException {
    throw ExceptionsKt.generateStyleNotCorrectException(key, value);
// when decompiled, it has the same contents as before , not the inlined contents.
}

推荐答案

The inlining that's done by the Kotlin compiler is not supported for Java files, since the Java compiler is unaware of this transformation (see this answer about why reified generics do not work from Java at all).

至于内联的其他用例(通常在将lambda作为参数传递时),正如您已经发现的,字节码包括一个public static方法,因此内联函数仍然可以从Java调用.然而,在这种情况下,不会发生内联.

Kotlin相关问答推荐

调用即发即忘方法--哪个作用域?

Gradle Jooq配置自定义生成器

升级使用jOOQ Gradle插件生成代码失败

为什么我的通用Kotlin函数中的这个转换未经判断?

创建包含 3 个相同项目的列表/使用返回类型重复

有什么方法可以要求在 kotlin 中的类型参数上进行注释?

每个 Kotlin 版本的默认 Kotlin 语言版本是什么?

如何使用成员引用在 Kotlin 中创建属性的分层路径

找不到引用的类 kotlin.internal.annotations.AvoidUninitializedObjectCopyingCheck

Map.mapTo 到另一个map

如何在 Kotlin 中使用 volatile

Kotlin boxed Int 不一样

带有迭代器函数的 Kotlin 无限序列

Kotlin 扩展函数 - 覆盖现有方法

当被Spring代理类访问时,Kotlin实例变量为null

Kotlin reflect proguard SmallSortedMap

Android:Exoplayer - ExtractorMediaSource 已弃用

查找是否在列表中找到具有特定属性值的元素

Kotlin替换字符串中的多个单词

将字符串编码为Kotlin中的UTF-8