Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disassembling Scala code

How can I disassemble Scala code? Can it be done without first building the Jar and decompiling the resulting .class files? Is there an alternative, faster way to do this?

For Python there is dis which can be used as follows:

def myfunc(alist):
    return len(alist)

>>> dis.dis(myfunc)
  2           0 LOAD_GLOBAL              0 (len)
              3 LOAD_FAST                0 (alist)
              6 CALL_FUNCTION            1
              9 RETURN_VALUE
like image 764
Alper Turan Avatar asked Feb 17 '26 17:02

Alper Turan


1 Answers

You know you can do this in the REPL:

scala> def foo[A](xs: Seq[A]) = xs.size
foo: [A](xs: Seq[A])Int

scala> :javap foo
  Size 714 bytes
...
public <A extends java/lang/Object> int foo(scala.collection.Seq<A>);
  flags: ACC_PUBLIC
  Code:
    stack=1, locals=2, args_size=2
     0: aload_1       
     1: invokeinterface #20, 1 // InterfaceMethod scala/collection/Seq.size:()I
     6: ireturn       
...

You have crawl a bit through the output because it contains the outer module and many more information.

like image 160
0__ Avatar answered Feb 20 '26 12:02

0__



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!