์ถ์ฒ
https://kotlinlang.org/docs/java-to-kotlin-interop.html#overloads-generation
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-overloads/
์ฝํ๋ฆฐ์ ๋ํดํธ ํ๋ผ๋ฏธํฐ๋ ์๋ฐ๋ก ์ปดํ์ผ๋๋ฉด ์ฌ์ฉํ ์ ์๋ค. ์๋ฐ์์๋ ์ผ์ผ์ด ๋ชจ๋ ํ๋ผ๋ฏธํฐ๋ค์ ๊ธฐ์ ํด์ผ ํ๋ค.@JvmOverlaods๋ฅผ ์ฌ์ฉํ๋ฉด ๋ํดํธ ํ๋ผ๋ฏธํฐ๊ฐ ์ค๋ฅธ์ชฝ๋ถํฐ ํ๋์ฉ ์ค์ด๋๋ ์ค๋ฒ๋ก๋๋ ํจ์๋ค์ ์์ฑํ๋ค. ์์ฑ์, ์คํ ํฑ ๋ฉ์๋ ๋ฑ์๋ ์ฌ์ฉ ๊ฐ๋ฅํ๋, ์ถ์ ๋ฉ์๋, ์ธํฐํ์ด์ค ์ ๋ฉ์๋์๋ ์ฌ์ฉ ๋ถ๊ฐ๋ฅํ๋ค.
์ฝํ๋ฆฐ ์์ ์ฝ๋ : @JvmOverloads ์์ด
class Circle constructor(centerX: Int, centerY: Int, radius: Double = 1.0) {
fun draw(label: String, lineWidth: Int = 1, color: String = "red") { /*...*/ }
}
-> ์๋ฐ ์ฝ๋ ๋ณํ
// Circle.java
import kotlin.Metadata;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;
@Metadata(
mv = {1, 7, 0},
k = 1,
d1 = {"\u0000(\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\b\n\u0002\b\u0002\n\u0002\u0010\u0006\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0003\u0018\u00002\u00020\u0001B\u001f\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0003\u0012\b\b\u0002\u0010\u0005\u001a\u00020\u0006¢\u0006\u0002\u0010\u0007J\"\u0010\b\u001a\u00020\t2\u0006\u0010\n\u001a\u00020\u000b2\b\b\u0002\u0010\f\u001a\u00020\u00032\b\b\u0002\u0010\r\u001a\u00020\u000b¨\u0006\u000e"},
d2 = {"LCircle;", "", "centerX", "", "centerY", "radius", "", "(IID)V", "draw", "", "label", "", "lineWidth", "color", "MyCodeForCodingTest"}
)
public final class Circle {
public final void draw(@NotNull String label, int lineWidth, @NotNull String color) {
Intrinsics.checkNotNullParameter(label, "label");
Intrinsics.checkNotNullParameter(color, "color");
}
// $FF: synthetic method
public static void draw$default(Circle var0, String var1, int var2, String var3, int var4, Object var5) {
if ((var4 & 2) != 0) {
var2 = 1;
}
if ((var4 & 4) != 0) {
var3 = "red";
}
var0.draw(var1, var2, var3);
}
public Circle(int centerX, int centerY, double radius) {
}
// $FF: synthetic method
public Circle(int var1, int var2, double var3, int var5, DefaultConstructorMarker var6) {
if ((var5 & 4) != 0) {
var3 = 1.0;
}
this(var1, var2, var3);
}
}
์ฝํ๋ฆฐ ์์ ์ฝ๋ : @JvmOverloads ์ฌ์ฉ
class Circle @JvmOverloads constructor(centerX: Int, centerY: Int, radius: Double = 1.0) {
@JvmOverloads fun draw(label: String, lineWidth: Int = 1, color: String = "red") { /*...*/ }
}
-> ์๋ฐ ์ฝ๋ ๋ณํ
// Circle.java
import kotlin.Metadata;
import kotlin.jvm.JvmOverloads;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;
@Metadata(
mv = {1, 7, 0},
k = 1,
d1 = {"\u0000(\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\b\n\u0002\b\u0002\n\u0002\u0010\u0006\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0003\u0018\u00002\u00020\u0001B!\b\u0007\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0003\u0012\b\b\u0002\u0010\u0005\u001a\u00020\u0006¢\u0006\u0002\u0010\u0007J$\u0010\b\u001a\u00020\t2\u0006\u0010\n\u001a\u00020\u000b2\b\b\u0002\u0010\f\u001a\u00020\u00032\b\b\u0002\u0010\r\u001a\u00020\u000bH\u0007¨\u0006\u000e"},
d2 = {"LCircle;", "", "centerX", "", "centerY", "radius", "", "(IID)V", "draw", "", "label", "", "lineWidth", "color", "MyCodeForCodingTest"}
)
public final class Circle {
@JvmOverloads
public final void draw(@NotNull String label, int lineWidth, @NotNull String color) {
Intrinsics.checkNotNullParameter(label, "label");
Intrinsics.checkNotNullParameter(color, "color");
}
// $FF: synthetic method
public static void draw$default(Circle var0, String var1, int var2, String var3, int var4, Object var5) {
if ((var4 & 2) != 0) {
var2 = 1;
}
if ((var4 & 4) != 0) {
var3 = "red";
}
var0.draw(var1, var2, var3);
}
@JvmOverloads
public final void draw(@NotNull String label, int lineWidth) {
draw$default(this, label, lineWidth, (String)null, 4, (Object)null);
}
@JvmOverloads
public final void draw(@NotNull String label) {
draw$default(this, label, 0, (String)null, 6, (Object)null);
}
@JvmOverloads
public Circle(int centerX, int centerY, double radius) {
}
// $FF: synthetic method
public Circle(int var1, int var2, double var3, int var5, DefaultConstructorMarker var6) {
if ((var5 & 4) != 0) {
var3 = 1.0;
}
this(var1, var2, var3);
}
@JvmOverloads
public Circle(int centerX, int centerY) {
this(centerX, centerY, 0.0, 4, (DefaultConstructorMarker)null);
}
}
'๋น ๊ตฌ๋ฉ ์ฑ์ฐ๊ธฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Kotlin] interface ์ default method๋ฅผ ์๋ฐ๋ก ๋ณํํ๊ธฐ (2) | 2024.10.29 |
---|---|
[Kotlin] const val (0) | 2024.10.29 |
[Kotlin] @JvmField : ์ปดํ์ผ์ ๊ฒํฐ, ์ธํฐ ์์ ๊ธฐ (1) | 2024.10.29 |
[Kotlin][Java] Kotlin ๋ฌธ์์ด ์ฐ๊ฒฐ์ด ์๋ฐ๋ก ์ปดํ์ผ ์์ ๋์ (3) | 2024.10.29 |
[Kotlin][Java] Java์ Stream์ด ์์์๋ Kotlin์ Sequence๋ ์ ๋ง๋ค์์๊น (1) | 2024.10.17 |