Table of Contents

JDK's javap tool

What is it

The javap command disassembles one or more class files.
It displays details about the internal structure of Java classes.
It allows us to inspect compiled Java code to better understand how Java works at the bytecode level.

“javap” displays information such as:

When to use it

Example of usage

Given the following class:

package ro.medjava.generics;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class Erasure {
 
    public static void doStuff1(List<String> ls) {
    }
 
    public static <E> void doStuff2(List<E> ls){
    }
 
    public static void main(String[] args) {
        doStuff2(new ArrayList<>(Arrays.asList("Fred")));
    }
}
We want to check if all the generic types are erased after the compilation.
Be sure that the javap tool is added to the PATH environment variable before using it. It is located in the JAVA_HOME\bin folder.

Command to disassemble the Erasure class is:

C:\JavaProject\src\main\java> javap -cp ../../../target/classes -c ro.medjava.generics.Erasure
-c parameter is used to display the disassembled code.

Result:

public ro.medjava.generics.Erasure();
  Code:
       0: aload_0
       1: invokespecial #1                // Method java/lang/Object."<init>":()V
       4: return

  public static void doStuff1(java.util.List<java.lang.String>);
    Code:
       0: return

  public static <E> void doStuff2(java.util.List<E>);
    Code:
       0: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #2               // class java/util/ArrayList
       3: dup
       4: iconst_1
       5: anewarray     #3               // class java/lang/String
       8: dup
       9: iconst_0
      10: ldc           #4               // String Fred
      12: aastore
      13: invokestatic  #5               // Method java/util/Arrays.asList:([Ljava/lang/Object;)Ljava/util/List;
      16: invokespecial #6               // Method java/util/ArrayList."<init>":(Ljava/util/Collection;)V
      19: invokestatic  #7               // Method doStuff2:(Ljava/util/List;)V
      22: return

As we can see in the parameter list of doStuff1 and doStuff2 methods, the generic types survived after compilation.
Some consistency checking information still remains in the binary.
But in the main method, the array list passed to the doStuff2 method does not contain any generic type after compilation.

Use javap in Intellij IDEA

Install the plugin “Class Decompile.” (File / Settings / Plugins)

Next, right click on your class and select “Show Decompile Code” option. Result: