User Tools

Site Tools


java:javap

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
java:javap [2023/04/12 14:51] odeftajava:javap [2023/07/04 19:36] (current) – external edit 127.0.0.1
Line 20: Line 20:
   * To check if a class contains the expected fields and methods.   * To check if a class contains the expected fields and methods.
   * To check for errors or performance issues in a program.   * To check for errors or performance issues in a program.
 +
 +===== Example of usage =====
 +
 +Given the following class:
 +
 +<code java>
 +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")));
 +    }
 +}
 +</code>
 +<note tip>We want to check if all the generic types are erased after the compilation.</note>
 +<note important>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.
 +</note>
 +
 +Command to disassemble the Erasure class is:
 +<code>
 +C:\JavaProject\src\main\java> javap -cp ../../../target/classes -c ro.medjava.generics.Erasure
 +</code>
 +
 +<note tip>-c parameter is used to display the disassembled code.</note>
 +
 +Result:
 +<code>
 +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           #              // class java/util/ArrayList
 +       3: dup
 +       4: iconst_1
 +       5: anewarray     #              // class java/lang/String
 +       8: dup
 +       9: iconst_0
 +      10: ldc           #              // 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
 +</code>
 +
 +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)
 +
 +{{:java:pasted:20230412-152124.png}}
 +
 +{{:java:pasted:20230412-152141.png}}
 +
 +Next, right click on your class and select "Show Decompile Code" option.
 +Result:
 +
 +{{:java:javap_erasure.png?nolink&1536×648}}
 +
 +
 +
  
  
  
java/javap.1681300289.txt.gz · Last modified: 2023/07/04 19:36 (external edit)