从Java调用Kotlin函数是Java中的关键字吗? [英] Calling kotlin functions which are keywords in java from java?
问题描述
由于new
不是kotlin中的关键字,因此我可以在kotlin中具有以下功能.
Since new
is not a keyword in kotlin, i can have the following function in kotlin.
fun new(): String {
return "just returns some string"
}
但是我无法从Java调用此函数,因为new
是Java中的关键字.我想知道java领域中是否存在此函数的别名.我没有发现任何intellij建议,可能是此功能的别名.
But i am unable to call this function from java since new
is a keyword in java. I would like to know if there is some alias for this function in java realm. I did not find any intellij suggestions that might be a possible alias to this function.
编辑1 :
我用kotlin编写了以下代码:
I have written the following code in kotlin:
fun new(): String {
return "just returns some string"
}
fun main(args:Array<String>){
new()
}
然后我看了一下Java字节码.如下.
And I had a look at the java bytecode. It was as follows.
// ================MainKt.class =================
// class version 50.0 (50)
// access flags 0x31
public final class MainKt {
// access flags 0x19
public final static new()Ljava/lang/String;
@Lorg/jetbrains/annotations/NotNull;() // invisible
L0
LINENUMBER 2 L0
LDC "just returns some string"
ARETURN
L1
MAXSTACK = 1
MAXLOCALS = 0
// access flags 0x19
public final static main([Ljava/lang/String;)V
@Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 0
L0
ALOAD 0
LDC "args"
INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull (Ljava/lang/Object;Ljava/lang/String;)V
L1
LINENUMBER 6 L1
INVOKESTATIC MainKt.new ()Ljava/lang/String;
POP
L2
LINENUMBER 7 L2
RETURN
L3
LOCALVARIABLE args [Ljava/lang/String; L0 L3 0
MAXSTACK = 2
MAXLOCALS = 1
@Lkotlin/Metadata;(mv={1, 1, 6}, bv={1, 0, 1}, k=2, d1={"\u0000\u0014\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u0011\n\u0002\u0010\u000e\n\u0002\u0008\u0003\u001a\u0019\u0010\u0000\u001a\u00020\u00012\u000c\u0010\u0002\u001a\u0008\u0012\u0004\u0012\u00020\u00040\u0003\u00a2\u0006\u0002\u0010\u0005\u001a\u0006\u0010\u0006\u001a\u00020\u0004\u00a8\u0006\u0007"}, d2={"main", "", "args", "", "", "([Ljava/lang/String;)V", "new", "production sources for module Srinivas"})
// compiled from: Main.kt
}
// ================META-INF/production sources for module Srinivas.kotlin_module =================
MainKt
这是反编译为java的字节码:
And here is the bytecode decompiled to java:
import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;
@Metadata(
mv = {1, 1, 6},
bv = {1, 0, 1},
k = 2,
d1 = {"\u0000\u0014\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u0011\n\u0002\u0010\u000e\n\u0002\b\u0003\u001a\u0019\u0010\u0000\u001a\u00020\u00012\f\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003¢\u0006\u0002\u0010\u0005\u001a\u0006\u0010\u0006\u001a\u00020\u0004¨\u0006\u0007"},
d2 = {"main", "", "args", "", "", "([Ljava/lang/String;)V", "new", "production sources for module Srinivas"}
)
public final class MainKt {
@NotNull
public static final String new() {
return "just returns some string";
}
public static final void main(@NotNull String[] args) {
Intrinsics.checkParameterIsNotNull(args, "args");
new();
}
}
看来,编写名为new的函数是有效的Java字节码.但是javac
不允许我编译代码.我可以通过调用此函数来获取javac
来编译java文件的注释或编译器标志吗?
It appears that writing a function named new is a valid java bytecode. But javac
is not letting me compile the code. Is there some annotation or compiler flag I can enable to get javac
to compile the java file with call to this function.
推荐答案
由于您尝试使用关键字,因此编译器不允许您将其用作方法名称
Since you're trying to use a keyword the compiler won't allow you to use it as a method name
我能想到的唯一解决方法是执行以下操作:
The only workaround I can think is to do something like:
fun new(): String {
return "just returns some string"
}
fun notAKeyWord() = new()
因此您可以在Java代码中使用WhatEverKt.notAKeyWord()
So you can use WhatEverKt.notAKeyWord()
from your java code
这篇关于从Java调用Kotlin函数是Java中的关键字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!