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
Last revisionBoth sides next revision
java:javap [2023/04/12 15:04] odeftajava:javap [2023/04/12 15:34] – [Use javap in Intellij IDEA] odefta
Line 45: Line 45:
 } }
 </code> </code>
- +<note tip>We want to check if all the generic types are erased after the compilation.</note>
-We want to check if all the generic types are erased after the compilation.\\ +
 <note important>Be sure that the javap tool is added to the PATH environment variable before using it. <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. It is located in the JAVA_HOME\bin folder.
Line 55: Line 54:
 C:\JavaProject\src\main\java> javap -cp ../../../target/classes -c ro.medjava.generics.Erasure C:\JavaProject\src\main\java> javap -cp ../../../target/classes -c ro.medjava.generics.Erasure
 </code> </code>
 +
 +<note tip>-c parameter is used to display the disassembled code.</note>
  
 Result: Result:
 <code> <code>
 public ro.medjava.generics.Erasure(); public ro.medjava.generics.Erasure();
-    Code:+  Code:
        0: aload_0        0: aload_0
-       1: invokespecial #1                  // Method java/lang/Object."<init>":()V+       1: invokespecial #1                // Method java/lang/Object."<init>":()V
        4: return        4: return
  
Line 74: Line 75:
   public static void main(java.lang.String[]);   public static void main(java.lang.String[]);
     Code:     Code:
-       0: new           #                 // class java/util/ArrayList+       0: new           #              // class java/util/ArrayList
        3: dup        3: dup
        4: iconst_1        4: iconst_1
-       5: anewarray     #                 // class java/lang/String+       5: anewarray     #              // class java/lang/String
        8: dup        8: dup
        9: iconst_0        9: iconst_0
-      10: ldc           #                 // String Fred+      10: ldc           #              // String Fred
       12: aastore       12: aastore
-      13: invokestatic  #5                  // Method java/util/Arrays.asList:([Ljava/lang/Object;)Ljava/util/List; +      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 +      16: invokespecial #6               // Method java/util/ArrayList."<init>":(Ljava/util/Collection;)V 
-      19: invokestatic  #7                  // Method doStuff2:(Ljava/util/List;)V+      19: invokestatic  #7               // Method doStuff2:(Ljava/util/List;)V
       22: return       22: return
 </code> </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.txt · Last modified: 2023/07/04 19:36 by 127.0.0.1