Decompile Kotlin program: Data class

If you're a Java programmer, you've probably been fed up with the tedious implementation of setters, getters, hashcodes, and toStrings.

Isn't the IDE implementing it automatically or using tools like lombok to avoid pondering this issue?

Kotlin has a mechanism to automatically generate these troublesome declarations by adding a modifier called data when declaring a Class.

Person class


data class Person(val name:String, val age: Int)

Code that uses the Person class


    var bob =  Person("Bob",33)
    var bob2 = Person("Bob",33)
    println(bob===bob2)//false
    println(bob==bob2)//true
    println(bob.equals(bob2))//true
    println(bob.hashCode())//2075948
    println(bob.toString())//Person(name=Bob, age=33)

Decompile with JAD

Decompile the person class and convert the auto-implemented code to Java

$ jad Person.class 
Parsing Person.class... Generating Person.jad
Couldn't fully decompile method hashCode

The hashcode method could not be decompiled. This is [the code behavior is summarized in the blog](http://yuyubu.hatenablog.com/entry/2018/03/30/ Disassembled to the mystery of hashCode% 28% 29 generated by Kotlin data class) So please read it if you are interested.

Decompiled result of Person class



import kotlin.jvm.internal.Intrinsics;

public final class Person
{

    public final String getName()
    {
        return name;
    }

    public final int getAge()
    {
        return age;
    }

    public Person(String name, int age)
    {
        Intrinsics.checkParameterIsNotNull(name, "name");
        super();
        this.name = name;
        this.age = age;
    }

    public final String component1()
    {
        return name;
    }

    public final int component2()
    {
        return age;
    }

    public final Person copy(String name, int age)
    {
        Intrinsics.checkParameterIsNotNull(name, "name");
        return new Person(name, age);
    }

    public static volatile Person copy$default(Person person, String s, int i, int j, Object obj)
    {
        if((j & 1) != 0)
            s = person.name;
        if((j & 2) != 0)
            i = person.age;
        return person.copy(s, i);
    }

    public String toString()
    {
        return (new StringBuilder()).append("Person(name=").append(name).append(", age=").append(age).append(")").toString();
    }

    public int hashCode()
    {
        name;
        if(name == null) goto _L2; else goto _L1
_L1:
        hashCode();
          goto _L3
_L2:
        JVM INSTR pop ;
        false;
_L3:
        31;
        JVM INSTR imul ;
        age;
        JVM INSTR iadd ;
        return;
    }

    public boolean equals(Object obj)
    {
label0:
        {
            if(this != obj)
            {
                if(!(obj instanceof Person))
                    break label0;
                Person person = (Person)obj;
                if(!Intrinsics.areEqual(name, person.name) || (age != person.age))
                    break label0;
            }
            return true;
        }
        return false;
    }

    private final String name;
    private final int age;
}

We were able to confirm the automatic generation of hashCode, equals, toString, copy, and #copy.

Recommended Posts

Decompile Kotlin program: Data class
Disassemble and decompile .class files compiled from Kotlin
Kotlin Class to send to Java developers
How to decompile java class files