将Kotlin类转换为Java

huangapple go评论77阅读模式
英文:

Convert Kotlin class to Java

问题

以下是已经翻译好的内容:

public abstract class RepoSearchResult {
    public static class Success extends RepoSearchResult {
        // 在这里添加必要的属性和方法
    }
    public static class Error extends RepoSearchResult {
        // 在这里添加必要的属性和方法
    }
}
英文:

How to convert this Kotlin class to Java

sealed class RepoSearchResult {
    data class Success(val data: List<Repo>) : RepoSearchResult()
    data class Error(val error: Exception) : RepoSearchResult()
}

<sup>code source: github</sup>

I created this but I don't know if it's correct, and if it is how to return data from it?

public abstract class RepoSearchResult {
    public static class Success extends RepoSearchResult {

    }
    public static class Error extends RepoSearchResult {

    }
}

答案1

得分: 1

使用IntelliJ将该类反编译为Java产生以下结果。我将Repo更改为String,以便在本地编译时不需要依赖项。

@元数据(
   mv = {1, 4, 0},
   bv = {1, 0, 3},
   k = 1,
   d1 = {"\u0000\u0016\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\b6\u0018\u00002\u00020\u0001:\u0002\u0003\u0004B\u0007\b\u0002¢¢\u0006\u0002\u0010\u0002¸\u0001\u0002\u0005\u0006¸\u0006\u0007"},
   d2 = {"LRepoSearchResult;", "", "()V", "错误", "成功", "LRepoSearchResult$Success;", "LRepoSearchResult$Error;", "test_kotlin"}
)
public abstract class RepoSearchResult {
   private RepoSearchResult() {
   }

   // $FF: synthetic method
   public RepoSearchResult(DefaultConstructorMarker $constructor_marker) {
      this();
   }

   @元数据(
      mv = {1, 4, 0},
      bv = {1, 0, 3},
      k = 1,
      d1 = {"\u0000*\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010 \n\u0002\u0010\u000e\n\u0002\b\u0006\n\u0002\u0010\u000b\n\u0000\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u000e\n\u0000\b†\b\u0018\u00002\u00020\u0001B\u0013\u0012\f\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003¸\u0006\u0002\u0010\u0005J\u000f\u0010\b\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003HÇ\u0003J\u0019\u0010\t\u001a\u00020\u00002\u000e\b\u0002\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003HÇ\u0001J\u0013\u0010\n\u001a\u00020\u000b2\b\u0010\f\u001a\u0004\u0018\u00010\rHÖ\u0003J\t\u0010\u000e\u001a\u00020\u000fHÖ\u0001J\t\u0010\u0010\u001a\u00020\u0004HÖ\u0001R\u0017\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003¸\u0006\b\n\u0000\u001a\u0004\b\u0006\u0010\u0007¸\u0006\u0011"},
   d2 = {"LRepoSearchResult$Success;", "LRepoSearchResult;", "data", "", "LRepoSearchResult;", "(Ljava/util/List;)V", "getData", "()Ljava/util/List;", "component1", "copy", "equals", "", "other", "hashCode", "", "toString", "test_kotlin"}
)
public static final class Success extends RepoSearchResult {
   @NotNull
   private final List data;

   @NotNull
   public final List getData() {
      return this.data;
   }

   public Success(@NotNull List data) {
      Intrinsics.checkParameterIsNotNull(data, "data");
      super((DefaultConstructorMarker)null);
      this.data = data;
   }

   @NotNull
   public final List component1() {
      return this.data;
   }

   @NotNull
   public final RepoSearchResult.Success copy(@NotNull List data) {
      Intrinsics.checkParameterIsNotNull(data, "data");
      return new RepoSearchResult.Success(data);
   }

   // $FF: synthetic method
   public static RepoSearchResult.Success copy$default(RepoSearchResult.Success var0, List var1, int var2, Object var3) {
      if ((var2 & 1) != 0) {
         var1 = var0.data;
      }

      return var0.copy(var1);
   }

   @NotNull
   public String toString() {
      return "Success(data=" + this.data + ")";
   }

   public int hashCode() {
      List var10000 = this.data;
      return var10000 != null ? var10000.hashCode() : 0;
   }

   public boolean equals(@Nullable Object var1) {
      if (this != var1) {
         if (var1 instanceof RepoSearchResult.Success) {
            RepoSearchResult.Success var2 = (RepoSearchResult.Success)var1;
            if (Intrinsics.areEqual(this.data, var2.data)) {
               return true;
            }
         }

         return false;
      } else {
         return true;
      }
   }
}

@元数据(
   mv = {1, 4, 0},
   bv = {1, 0, 3},
   k = 1,
   d1 = {"\u0000.\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0010\u000b\n\u0000\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u000e\n\u0000\b†\b\u0018\u00002\u00020\u0001B\u0011\u0012\n\u0010\u0002\u001a\u00060\u0003j\u0002`\u0004¸\u0006\u0002\u0010\u0005J\r\u0010\b\u001a\u00060\u0003j\u0002`\u0004HÇ\u0003J\u0017\u0010\t\u001a\u00020\u00002\f\b\u0002\u0010\u0002\u001a\u00060\u0003j\u0002`\u0004HÇ\u0001J\u0013\u0010\n\u001a\u00020\u000b2\b\u0010\f\u001a\u0004\u0018\u00010\rHÖ\u0003J\t\u0010\u000e\u001a\u00020\u000fHÖ

<details>
<summary>英文:</summary>

Decompiling that class to Java with IntelliJ produces this. I changed `Repo` to `String` so that it would compile locally without dependencies.

```java
@Metadata(
   mv = {1, 4, 0},
   bv = {1, 0, 3},
   k = 1,
   d1 = {&quot;\u0000\u0016\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\b6\u0018\u00002\u00020\u0001:\u0002\u0003\u0004B\u0007\b\u0002&#162;\u0006\u0002\u0010\u0002\u0082\u0001\u0002\u0005\u0006&#168;\u0006\u0007&quot;},
   d2 = {&quot;LRepoSearchResult;&quot;, &quot;&quot;, &quot;()V&quot;, &quot;Error&quot;, &quot;Success&quot;, &quot;LRepoSearchResult$Success;&quot;, &quot;LRepoSearchResult$Error;&quot;, &quot;test_kotlin&quot;}
)
public abstract class RepoSearchResult {
   private RepoSearchResult() {
   }

   // $FF: synthetic method
   public RepoSearchResult(DefaultConstructorMarker $constructor_marker) {
      this();
   }

   @Metadata(
      mv = {1, 4, 0},
      bv = {1, 0, 3},
      k = 1,
      d1 = {&quot;\u0000*\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010 \n\u0002\u0010\u000e\n\u0002\b\u0006\n\u0002\u0010\u000b\n\u0000\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\b\n\u0002\b\u0002\b\u0086\b\u0018\u00002\u00020\u0001B\u0013\u0012\f\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003&#162;\u0006\u0002\u0010\u0005J\u000f\u0010\b\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003H&#198;\u0003J\u0019\u0010\t\u001a\u00020\u00002\u000e\b\u0002\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003H&#198;\u0001J\u0013\u0010\n\u001a\u00020\u000b2\b\u0010\f\u001a\u0004\u0018\u00010\rH&#214;\u0003J\t\u0010\u000e\u001a\u00020\u000fH&#214;\u0001J\t\u0010\u0010\u001a\u00020\u0004H&#214;\u0001R\u0017\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003&#162;\u0006\b\n\u0000\u001a\u0004\b\u0006\u0010\u0007&#168;\u0006\u0011&quot;},
      d2 = {&quot;LRepoSearchResult$Success;&quot;, &quot;LRepoSearchResult;&quot;, &quot;data&quot;, &quot;&quot;, &quot;&quot;, &quot;(Ljava/util/List;)V&quot;, &quot;getData&quot;, &quot;()Ljava/util/List;&quot;, &quot;component1&quot;, &quot;copy&quot;, &quot;equals&quot;, &quot;&quot;, &quot;other&quot;, &quot;&quot;, &quot;hashCode&quot;, &quot;&quot;, &quot;toString&quot;, &quot;test_kotlin&quot;}
   )
   public static final class Success extends RepoSearchResult {
      @NotNull
      private final List data;

      @NotNull
      public final List getData() {
         return this.data;
      }

      public Success(@NotNull List data) {
         Intrinsics.checkParameterIsNotNull(data, &quot;data&quot;);
         super((DefaultConstructorMarker)null);
         this.data = data;
      }

      @NotNull
      public final List component1() {
         return this.data;
      }

      @NotNull
      public final RepoSearchResult.Success copy(@NotNull List data) {
         Intrinsics.checkParameterIsNotNull(data, &quot;data&quot;);
         return new RepoSearchResult.Success(data);
      }

      // $FF: synthetic method
      public static RepoSearchResult.Success copy$default(RepoSearchResult.Success var0, List var1, int var2, Object var3) {
         if ((var2 &amp; 1) != 0) {
            var1 = var0.data;
         }

         return var0.copy(var1);
      }

      @NotNull
      public String toString() {
         return &quot;Success(data=&quot; + this.data + &quot;)&quot;;
      }

      public int hashCode() {
         List var10000 = this.data;
         return var10000 != null ? var10000.hashCode() : 0;
      }

      public boolean equals(@Nullable Object var1) {
         if (this != var1) {
            if (var1 instanceof RepoSearchResult.Success) {
               RepoSearchResult.Success var2 = (RepoSearchResult.Success)var1;
               if (Intrinsics.areEqual(this.data, var2.data)) {
                  return true;
               }
            }

            return false;
         } else {
            return true;
         }
      }
   }

   @Metadata(
      mv = {1, 4, 0},
      bv = {1, 0, 3},
      k = 1,
      d1 = {&quot;\u0000.\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0006\n\u0002\u0010\u000b\n\u0000\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u000e\n\u0000\b\u0086\b\u0018\u00002\u00020\u0001B\u0011\u0012\n\u0010\u0002\u001a\u00060\u0003j\u0002`\u0004&#162;\u0006\u0002\u0010\u0005J\r\u0010\b\u001a\u00060\u0003j\u0002`\u0004H&#198;\u0003J\u0017\u0010\t\u001a\u00020\u00002\f\b\u0002\u0010\u0002\u001a\u00060\u0003j\u0002`\u0004H&#198;\u0001J\u0013\u0010\n\u001a\u00020\u000b2\b\u0010\f\u001a\u0004\u0018\u00010\rH&#214;\u0003J\t\u0010\u000e\u001a\u00020\u000fH&#214;\u0001J\t\u0010\u0010\u001a\u00020\u0011H&#214;\u0001R\u0015\u0010\u0002\u001a\u00060\u0003j\u0002`\u0004&#162;\u0006\b\n\u0000\u001a\u0004\b\u0006\u0010\u0007&#168;\u0006\u0012&quot;},
      d2 = {&quot;LRepoSearchResult$Error;&quot;, &quot;LRepoSearchResult;&quot;, &quot;error&quot;, &quot;Ljava/lang/Exception;&quot;, &quot;Lkotlin/Exception;&quot;, &quot;(Ljava/lang/Exception;)V&quot;, &quot;getError&quot;, &quot;()Ljava/lang/Exception;&quot;, &quot;component1&quot;, &quot;copy&quot;, &quot;equals&quot;, &quot;&quot;, &quot;other&quot;, &quot;&quot;, &quot;hashCode&quot;, &quot;&quot;, &quot;toString&quot;, &quot;&quot;, &quot;test_kotlin&quot;}
   )
   public static final class Error extends RepoSearchResult {
      @NotNull
      private final Exception error;

      @NotNull
      public final Exception getError() {
         return this.error;
      }

      public Error(@NotNull Exception error) {
         Intrinsics.checkParameterIsNotNull(error, &quot;error&quot;);
         super((DefaultConstructorMarker)null);
         this.error = error;
      }

      @NotNull
      public final Exception component1() {
         return this.error;
      }

      @NotNull
      public final RepoSearchResult.Error copy(@NotNull Exception error) {
         Intrinsics.checkParameterIsNotNull(error, &quot;error&quot;);
         return new RepoSearchResult.Error(error);
      }

      // $FF: synthetic method
      public static RepoSearchResult.Error copy$default(RepoSearchResult.Error var0, Exception var1, int var2, Object var3) {
         if ((var2 &amp; 1) != 0) {
            var1 = var0.error;
         }

         return var0.copy(var1);
      }

      @NotNull
      public String toString() {
         return &quot;Error(error=&quot; + this.error + &quot;)&quot;;
      }

      public int hashCode() {
         Exception var10000 = this.error;
         return var10000 != null ? var10000.hashCode() : 0;
      }

      public boolean equals(@Nullable Object var1) {
         if (this != var1) {
            if (var1 instanceof RepoSearchResult.Error) {
               RepoSearchResult.Error var2 = (RepoSearchResult.Error)var1;
               if (Intrinsics.areEqual(this.error, var2.error)) {
                  return true;
               }
            }

            return false;
         } else {
            return true;
         }
      }
   }
}

huangapple
  • 本文由 发表于 2020年10月27日 05:07:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64544981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定