Calling kotlin functions which are keywords in java from java?

安稳与你 提交于 2019-12-10 00:42:47

问题


Since new is not a keyword in kotlin, i can have the following function in kotlin.

fun new(): String {
    return "just returns some string"
}

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.

Edit 1:

I have written the following code in kotlin:

fun new(): String {
    return "just returns some string"
}

fun main(args:Array<String>){
    new()
}

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

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();
   }
}

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.


回答1:


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()

So you can use WhatEverKt.notAKeyWord() from your java code




回答2:


For these sorts of problems, you can use the @JvmName annotation.

@JvmName("neww")
fun new(): String {
    return "just returns some string"
}

The name you pass to it will be the name that you can use to refer to the method from Java:

String s = something.neww();

In general, you're probably better off not using Java keywords as Kotlin identifiers if you need to interop with Java code.




回答3:


You can use @JvmName to provide a different name for your function to be called form Java:

@JvmName("myNew")
fun new(): String {
    return "just returns some string"
}

And the usage in Java:

String bar = foo.myNew();


来源:https://stackoverflow.com/questions/44456977/calling-kotlin-functions-which-are-keywords-in-java-from-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!