从 kotlin.text 包中的 Java 调用 Kotlin 扩展函数

Calling Kotlin extension function from Java from kotlin.text package

我正在尝试使用 Java 代码中 kotlin.text 包中的 toRegex() 扩展函数。这是该文件的代码:

/*
 * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
 * that can be found in the license/LICENSE.txt file.
 */

@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("StringsKt")

package kotlin.text

/**
 * Converts the string into a regular expression [Regex] with the default options.
 */
@kotlin.internal.InlineOnly
public inline fun String.toRegex(): Regex = Regex(this)

/**
 * Converts the string into a regular expression [Regex] with the specified single [option].
 */
@kotlin.internal.InlineOnly
public inline fun String.toRegex(option: RegexOption): Regex = Regex(this, option)

/**
 * Converts the string into a regular expression [Regex] with the specified set of [options].
 */
@kotlin.internal.InlineOnly
public inline fun String.toRegex(options: Set<RegexOption>): Regex = Regex(this, options)

不幸的是,我没有成功从 Java 代码中调用该函数。我想我错过了什么。这是我的尝试:

import kotlin.text.StringsKt;
...

StringsKt.toRegex("some_string"); // Doesn't compile.

不编译。

这是由该函数上的 @InlineOnly 注释引起的。

InlineOnly means that the Java method corresponding to this Kotlin function is marked private so that Java code can not access it (which is the only way to call an inline function without actually inlining it).


此外,这可能是 的重复项,尽管它没有直接询问 @InlineOnly