samedi 12 août 2017

Is there a way to get a parameter's default value via reflection?

KParameter doesn't seem to have any related properties/methods, so I am wondering if this is even possible.

If I decompile a function with a default parameter it seems to transform it into a method called name$default which creates the default parameters and a normal method name. Is it maybe not stored anywhere in the compiled code?

e.g.

fun method(text: String = "Hello") = println(text)

is decompiled to

public final void method(@NotNull String text) {
    Intrinsics.checkParameterIsNotNull(text, "text");
    System.out.println(text);
}

// $FF: synthetic method
// $FF: bridge method
public static void method$default(SignatureCommand var0, String var1, int var2, Object var3) {
    if ((var2 & 1) != 0) {
        var1 = "Hello";
    }

    var0.method(var1);
}





Aucun commentaire:

Enregistrer un commentaire