Disassemble and decompile .class files compiled from Kotlin

This is an experiment using javap (disassembler) and jad (decompiler) in this Kotlin code.

hello.kt


fun main(args: Array<String>) {
    println("Hello, World!")
}

Code source https://kotlinlang.org/docs/tutorials/command-line.html

Kotlin, like any other JVM language, is compiled into a class file that can be run as Java. I tried to verify that because it is possible to display the JVM mnemonics with javap and decompile it as java source code with jad.

compile


kotlinc hello.kt -include-runtime -d hello.jar

Extract the jar and use javap, jad for hello.class

jar xf hello.jar

environment

macOS High Sierra 10

javap (disassembler)

Preparation

Not required as it is included in the JDK from the beginning

Run

command


javap -c HelloKt.class

result


Compiled from "hello.kt"
public final class HelloKt {
  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #9                  // String args
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull:(Ljava/lang/Object;Ljava/lang/String;)V
       6: ldc           #17                 // String Hello, World!
       8: astore_1
       9: getstatic     #23                 // Field java/lang/System.out:Ljava/io/PrintStream;
      12: aload_1
      13: invokevirtual #29                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      16: return
}

jad (decompiler)

Preparation

How to install jad

brew tap caskroom/cask
brew install caskroom/cask/jad

Referenced site http://inarizuuuushi.hatenablog.com/entry/2017/05/09/231600

Run

You can display mnemonics as comments with the -a option.

command


$ jad  -a HelloKt.class 
$ cat HelloKt.jad 

python


import java.io.PrintStream;
import kotlin.jvm.internal.Intrinsics;

public final class HelloKt
{

    public static final void main(String args[])
    {
        Intrinsics.checkParameterIsNotNull(args, "args");
    //    0    0:aload_0         
    //    1    1:ldc1            #9   <String "args">
    //    2    3:invokestatic    #15  <Method void Intrinsics.checkParameterIsNotNull(Object, String)>
        String s = "Hello, World!";
    //    3    6:ldc1            #17  <String "Hello, World!">
    //    4    8:astore_1        
        System.out.println(s);
    //    5    9:getstatic       #23  <Field PrintStream System.out>
    //    6   12:aload_1         
    //    7   13:invokevirtual   #29  <Method void PrintStream.println(Object)>
    //    8   16:return          
    }
}

It was fairly easy to introduce and analyze. In the future, I will try to verify at hand how Kotlin-specific functions are expressed and converted as Java.

Recommended Posts

Disassemble and decompile .class files compiled from Kotlin
Decompile class files
Decompile Kotlin program: Data class
Get TypeElement and TypeMirror from Class
How to disassemble Java class files
How to decompile java class files
[Kotlin] 3 ways to get Class from KClass
Class and model
[Java] Get metadata from files with Apathce Tika, and get image / video width and height from metadata [Kotlin]
Java language from the perspective of Kotlin and C #
Write a class in Kotlin and call it in Java