为Scala代码生成的奇怪的字节码行为

时间:2018-04-07 12:13:16

标签: scala interface abstract-class traits bytecode

我正在玩Scala特征。

trait Animal{
  def speak = println("speaking..")
  def comeToMaster: Unit
}

class Cat extends Animal{
   override def speak: Unit = println("meow....")
   def comeToMaster = println("catch me if you can..")
}

object Sample extends App{
 val kity = new Cat
  kity.speak
  kity.comeToMaster
}

但是当我看到Scala编译器生成的字节码时,

javap -c Animal.class 
Compiled from "Sample.scala"
public abstract class Animal$class {
  public static void speak(Animal);
    Code:
       0: getstatic     #13                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
       3: ldc           #15                 // String speaking..
       5: invokevirtual #19                 // Method scala/Predef$.println:(Ljava/lang/Object;)V
       8: return

  public static void $init$(Animal);
    Code:
       0: return
}

请注意,字节码中只有speak方法。 comeToMaster方法在哪里?删除所有类文件后我再次尝试。不过,它给了我相同的结果。所以,我没有得到Scala编译器生成的Java字节码这种奇怪的行为。

但是当我看到Cat类的字节码时,有两种方法。

javap -c Cat.class 
Compiled from "Sample.scala"
public class Cat implements Animal {
  public void speak();
    Code:
       0: getstatic     #18                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
       3: ldc           #20                 // String meow....
       5: invokevirtual #24                 // Method scala/Predef$.println:(Ljava/lang/Object;)V
       8: return

  public void comeToMaster();
    Code:
       0: getstatic     #18                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
       3: ldc           #29                 // String catch me if you can..
       5: invokevirtual #24                 // Method scala/Predef$.println:(Ljava/lang/Object;)V
       8: return

  public Cat();
    Code:
       0: aload_0
       1: invokespecial #32                 // Method java/lang/Object."<init>":()V
       4: aload_0
       5: invokestatic  #38                 // Method Animal$class.$init$:(LAnimal;)V
       8: return
}

1 个答案:

答案 0 :(得分:2)

这应该取决于版本。我的输出是

$ javap -c Cat.class

Compiled from "Animal.scala"
public class Cat implements Animal {
  public void speak();
    Code:
       0: getstatic     #18                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
       3: ldc           #20                 // String meow....
       5: invokevirtual #24                 // Method scala/Predef$.println:(Ljava/lang/Object;)V
       8: return

  public void comeToMaster();
    Code:
       0: getstatic     #18                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
       3: ldc           #29                 // String catch me if you can..
       5: invokevirtual #24                 // Method scala/Predef$.println:(Ljava/lang/Object;)V
       8: return

  public Cat();
    Code:
       0: aload_0
       1: invokespecial #32                 // Method java/lang/Object."<init>":()V
       4: aload_0
       5: invokestatic  #36                 // InterfaceMethod Animal.$init$:(LAnimal;)V
       8: return
}

$ javap -c Animal.class

Compiled from "Animal.scala"
public interface Animal {
  public static void speak$(Animal);
    Code:
       0: aload_0
       1: invokespecial #15                 // InterfaceMethod speak:()V
       4: return

  public void speak();
    Code:
       0: getstatic     #22                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
       3: ldc           #24                 // String speaking..
       5: invokevirtual #28                 // Method scala/Predef$.println:(Ljava/lang/Object;)V
       8: return

  public abstract void comeToMaster();

  public static void $init$(Animal);
    Code:
       0: return
}

Scala 2.12.4(jdk 1.8.0_162)

尝试使用类查看目录。 可能除了抽象类Animal$class之外,您还有接口Animal

Animal.scala放在一个目录中,将Animal$class.scala放在另一个目录中,然后在那里调用javap。使用Scala 2.11.12输出

$ javap -c Animal.class

Compiled from "Animal.scala"
public interface Animal {
  public abstract void speak();

  public abstract void comeToMaster();
}

$ javap -c Animal$class.class

Compiled from "Animal.scala"
public abstract class Animal$class {
  public static void speak(Animal);
    Code:
       0: getstatic     #13                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
       3: ldc           #15                 // String speaking..
       5: invokevirtual #19                 // Method scala/Predef$.println:(Ljava/lang/Object;)V
       8: return

  public static void $init$(Animal);
    Code:
       0: return
}