๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[Kotlin] @JvmOverloads - ๋””ํดํŠธ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ž๋ฐ”์—์„œ๋„ ์ด์šฉํ•˜๊ธฐ

์ถœ์ฒ˜

https://kotlinlang.org/docs/java-to-kotlin-interop.html#overloads-generation

 

Calling Kotlin from Java | Kotlin

 

kotlinlang.org

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-overloads/

 

JvmOverloads - Kotlin Programming Language

 

kotlinlang.org

 


์ฝ”ํ‹€๋ฆฐ์˜ ๋””ํดํŠธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์ž๋ฐ”๋กœ ์ปดํŒŒ์ผ๋˜๋ฉด ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ์ž๋ฐ”์—์„œ๋Š” ์ผ์ผ์ด ๋ชจ๋“  ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์„ ๊ธฐ์ž…ํ•ด์•ผ ํ•œ๋‹ค.@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);
   }
}