Last active
October 19, 2021 19:01
-
-
Save lordgift/23708e0f99ab5d73909ebeb5429c69ed to your computer and use it in GitHub Desktop.
sample lambda expression in kotlin & decompile into java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package th.`in`.lordgift.test | |
class KtTestLambda { | |
companion object { | |
@JvmStatic | |
fun main(args: Array<String>) { | |
println("Hello, world!") | |
TestLambda.call() | |
} | |
fun call() { | |
//---------------------- Anonymous Function | |
val sum = { x: Int, y: Int -> x + y } | |
println(sum(2, 3)) | |
//---------------------- Passing Declared Anonymous Function | |
println(higherOrder(sum)) | |
//---------------------- Passing Anonymous Function | |
println(higherOrder { i1, i2 -> i1+i2+100 }) | |
//---------------------- Multi Function | |
higherOrder({ | |
println("ok callback") | |
}, { e -> | |
error(e) | |
}) | |
//---------------------- built-in receiver | |
val arr = ArrayList<String>() | |
arr.add("INDEX 1") | |
arr.forEach { e -> e + "XXX" } | |
} | |
/** | |
* single lambda receiver | |
*/ | |
fun higherOrder(f: (Int, Int) -> Int): String { | |
return "This is result of f(1, 2) : " + f(1,2) | |
} | |
/** | |
* multiple lambda receivers | |
*/ | |
fun higherOrder(ok: () -> Unit, fail: (Throwable) -> Unit) { | |
try { | |
ok() | |
throw Exception("fake error") | |
} catch (e: Throwable) { | |
fail(e) | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Decompile to Java from compiled KtTestLambda.kt | |
package th.in.lordgift.test; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import kotlin.Metadata; | |
import kotlin.jvm.JvmStatic; | |
import kotlin.jvm.functions.Function0; | |
import kotlin.jvm.functions.Function1; | |
import kotlin.jvm.functions.Function2; | |
import kotlin.jvm.internal.DefaultConstructorMarker; | |
import kotlin.jvm.internal.Intrinsics; | |
import org.jetbrains.annotations.NotNull; | |
@Metadata( | |
mv = {1, 1, 11}, | |
bv = {1, 0, 2}, | |
k = 1, | |
d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0003\u0018\u0000 \u00032\u00020\u0001:\u0001\u0003B\u0005¢\u0006\u0002\u0010\u0002¨\u0006\u0004"}, | |
d2 = {"Lth/in/lordgift/test/KtTestLambda;", "", "()V", "Companion", "KtDecompile"} | |
) | |
public final class KtTestLambda { | |
public static final KtTestLambda.Companion Companion = new KtTestLambda.Companion((DefaultConstructorMarker)null); | |
@JvmStatic | |
public static final void main(@NotNull String[] args) { | |
Companion.main(args); | |
} | |
@Metadata( | |
mv = {1, 1, 11}, | |
bv = {1, 0, 2}, | |
k = 1, | |
d1 = {"\u0000<\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0010\u0003\n\u0002\u0010\u000e\n\u0000\n\u0002\u0018\u0002\n\u0002\u0010\b\n\u0002\b\u0002\n\u0002\u0010\u0011\n\u0002\b\u0002\b\u0086\u0003\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002J\u0006\u0010\u0003\u001a\u00020\u0004J(\u0010\u0005\u001a\u00020\u00042\f\u0010\u0006\u001a\b\u0012\u0004\u0012\u00020\u00040\u00072\u0012\u0010\b\u001a\u000e\u0012\u0004\u0012\u00020\n\u0012\u0004\u0012\u00020\u00040\tJ \u0010\u0005\u001a\u00020\u000b2\u0018\u0010\f\u001a\u0014\u0012\u0004\u0012\u00020\u000e\u0012\u0004\u0012\u00020\u000e\u0012\u0004\u0012\u00020\u000e0\rJ\u001b\u0010\u000f\u001a\u00020\u00042\f\u0010\u0010\u001a\b\u0012\u0004\u0012\u00020\u000b0\u0011H\u0007¢\u0006\u0002\u0010\u0012¨\u0006\u0013"}, | |
d2 = {"Lth/in/lordgift/test/KtTestLambda$Companion;", "", "()V", "call", "", "higherOrder", "ok", "Lkotlin/Function0;", "fail", "Lkotlin/Function1;", "", "", "f", "Lkotlin/Function2;", "", "main", "args", "", "([Ljava/lang/String;)V", "KtDecompile"} | |
) | |
public static final class Companion { | |
@JvmStatic | |
public final void main(@NotNull String[] args) { | |
Intrinsics.checkParameterIsNotNull(args, "args"); | |
String var2 = "Hello, world!"; | |
System.out.println(var2); | |
KtTestLambda.Companion.call(); | |
} | |
public final void call() { | |
Function2 sum = (Function2)null.INSTANCE; | |
int var2 = ((Number)sum.invoke(2, 3)).intValue(); | |
System.out.println(var2); | |
String var9 = ((KtTestLambda.Companion)this).higherOrder(sum); | |
System.out.println(var9); | |
var9 = ((KtTestLambda.Companion)this).higherOrder((Function2)null.INSTANCE); | |
System.out.println(var9); | |
((KtTestLambda.Companion)this).higherOrder((Function0)null.INSTANCE, (Function1)null.INSTANCE); | |
ArrayList arr = new ArrayList(); | |
arr.add("INDEX 1"); | |
Iterable $receiver$iv = (Iterable)arr; | |
Iterator var4 = $receiver$iv.iterator(); | |
while(var4.hasNext()) { | |
Object element$iv = var4.next(); | |
String e = (String)element$iv; | |
(new StringBuilder()).append(e).append("XXX").toString(); | |
} | |
} | |
@NotNull | |
public final String higherOrder(@NotNull Function2 f) { | |
Intrinsics.checkParameterIsNotNull(f, "f"); | |
return "This is result of f(1, 2) : " + ((Number)f.invoke(1, 2)).intValue(); | |
} | |
public final void higherOrder(@NotNull Function0 ok, @NotNull Function1 fail) { | |
Intrinsics.checkParameterIsNotNull(ok, "ok"); | |
Intrinsics.checkParameterIsNotNull(fail, "fail"); | |
try { | |
ok.invoke(); | |
throw (Throwable)(new Exception("fake error")); | |
} catch (Throwable var4) { | |
fail.invoke(var4); | |
} | |
} | |
private Companion() { | |
} | |
// $FF: synthetic method | |
public Companion(DefaultConstructorMarker $constructor_marker) { | |
this(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment