I tried using @JvmOverloads to created overloaded methods for Kotlin function. Just out of curiosity I tried to look at the bytecode and decompiled Java code. But, I could not make much sense of the Java code.
Here, is a simple Kotlin code
class MyClass {
@JvmOverloads
fun doSomething(a : Int = 200, b : Int = 300){
}
}
Below is the decompiled java code :
public final class MyClass {
@JvmOverloads
public final void doSomething(int a, int b) {
}
// $FF: synthetic method
// $FF: bridge method
@JvmOverloads
public static void doSomething$default(MyClass var0, int var1, int var2, int var3, Object var4) {
if((var3 & 1) != 0) { // What does this do ?
var1 = 200;
}
if((var3 & 2) != 0) { //Where did var3 come from ?
var2 = 300;
}
var0.doSomething(var1, var2);
}
@JvmOverloads
public final void doSomething(int a) {
doSomething$default(this, a, 0, 2, (Object)null);
}
@JvmOverloads
public final void doSomething() {
doSomething$default(this, 0, 0, 3, (Object)null);
}
}
Please explain or point me to the right resources which could help me learn more about the same. Thanks.
var3
is a bit mask indicating which parameters need to be substituted with default values. If bit 0 is set, the first parameter ( var1
, or a
in the source code) is substituted with the default value 200. If bit 1 is set, var2
takes the default value 300. The 1-parameter overload passes 2 as the bit mask (so only bit 1 is set), and the 0-parameter overload passes 3 (bits 0 and 1 are set).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.