Scala (package) object is decompiled as java - a static initializer containing "new ();" - what is it?

I have the following scala code

package x.y

package object config {

  type SomeType = AnotherType[YetAnotherOne]
}

The following describes what JDGUI does in Java

package x.y.config;

import scala.reflect.ScalaSignature;

@ScalaSignature(bytes="\006\001...")
public final class package {}

A...

package x.y.config;

public final class package$
{
  public static final  MODULE$;

  static
  {
    new ();
  }

  private package$()
  {
    MODULE$ = this;
  }
}

I have no problem with the disappearance of the definition type(I expect the rope to handle this)

I want to know what it is:

static
{
  new ();
}

MODULE $ is explained here .

The thing is, when I cut and paste Java code into my IDE, it does not compile. And not typed public static final MODULE$;, nor new ();.

Is this just some kind of miscompilation decompilation? Did I miss something?

What's going on here?


Edit:

upon request:

javap -c -l -private package\$.class
Compiled from "package.scala"
public final class x.y.config.package$ {
  public static final x.y.config.package$ MODULE$;

  public static {};
    Code:
       0: new           #2                  // class x/y/config/package$
       3: invokespecial #12                 // Method "<init>":()V
       6: return

  private x.y.config.package$();
    Code:
       0: aload_0
       1: invokespecial #13                 // Method java/lang/Object."<init>":()V
       4: aload_0
       5: putstatic     #15                 // Field MODULE$:Lx/y/config/package$;
       8: return
    LineNumberTable:
      line 9: 0
    LocalVariableTable:
      Start  Length  Slot  Name   Signature
          0       9     0  this   Lx/y/config/package$;
}
+4
source share
2 answers

@acartapanis,

package org.cakesolutions.scala.basic;

public final class EmptyObject$
{
    public static final EmptyObject$ MODULE$;

    static {
        new EmptyObject$();
    }

    private EmptyObject$() {
        MODULE$ = this;
    }
}

...

: Scala Dissection: . :

Scala ( JVM, Kotlin) , Procyon , . Procyon 5 , ByteCode Viewer ( DJ-GUI/Core, CFR, Fernflower Krakatau)

, Java-,

0

Source: https://habr.com/ru/post/1625167/


All Articles