disassemble

How to disassemble and decompile Scala code (javap, scalac, jad)

Table of Contents1 - Problem2 - Solution3 - Using javap4 - Using scalac print options5 - Use a decompiler6 - Discussion7 - See Also8 - Sponsored by ...

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.6, “How to disassemble and decompile Scala code.”

Back to top

Problem

In the process of learning Scala, or trying to understand a particular problem, you want to examine the source code and bytecode the Scala compiler generates from your original source code.

Back to top

Solution

You can use several different approaches to see how your Scala source code is translated:

Converting a Scala class file to decompiled Java source code

As a Scala newbie, I'm curious about how the process of converting a Scala class back to Java source code works. What I really want to see is how my Scala source code is converted to Java source code. Besides plain old curiosity, I think that understanding more about how Scala works can also be very important to my understanding of Scala (such as the apply() method, and so on).

How to disassemble Java code with ‘javap -c’

One of my favorite Java subjects is code optimization and performance. Here I'd like to show you a couple of neat things you can learn with the javap -c command. This command lets you disassemble Java bytecode.

The first thing you need to have for this exercise is a little sample Java code. So in the examples below I create two test Java classes, appropriately named Test1.java and Test2.java. Although it's not explicitly stated below, the steps I'm going to follow are these: