java:javap
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
java:javap [2023/04/12 14:48] – created odefta | java:javap [2023/07/04 19:36] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== JDK's javap tool ====== | ====== JDK's javap tool ====== | ||
- | ====== What is it ====== | + | ===== What is it ===== |
- | "javap" is a tool in the Java Development Kit (JDK) that displays details about the internal structure of Java classes.\\ | + | 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.\\ | It allows us to inspect compiled Java code to better understand how Java works at the bytecode level.\\ | ||
Line 12: | Line 13: | ||
* The byte code of the method, which represents the bytecode instructions used by the JVM. | * The byte code of the method, which represents the bytecode instructions used by the JVM. | ||
* The fields of the class, including their names and types. | * The fields of the class, including their names and types. | ||
+ | |||
+ | ===== When to use it ===== | ||
+ | |||
+ | * To better understand how Java code works at the bytecode level. | ||
+ | * To check method signatures and parameter data types and return value types. | ||
+ | * To check if a class contains the expected fields and methods. | ||
+ | * 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< | ||
+ | } | ||
+ | |||
+ | public static <E> void doStuff2(List< | ||
+ | } | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | doStuff2(new ArrayList<> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | <note tip>We want to check if all the generic types are erased after the compilation.</ | ||
+ | <note important> | ||
+ | It is located in the JAVA_HOME\bin folder. | ||
+ | </ | ||
+ | |||
+ | Command to disassemble the Erasure class is: | ||
+ | < | ||
+ | C: | ||
+ | </ | ||
+ | |||
+ | <note tip>-c parameter is used to display the disassembled code.</ | ||
+ | |||
+ | Result: | ||
+ | < | ||
+ | public ro.medjava.generics.Erasure(); | ||
+ | Code: | ||
+ | 0: aload_0 | ||
+ | 1: invokespecial #1 // Method java/ | ||
+ | 4: return | ||
+ | |||
+ | public static void doStuff1(java.util.List< | ||
+ | Code: | ||
+ | 0: return | ||
+ | |||
+ | public static <E> void doStuff2(java.util.List< | ||
+ | Code: | ||
+ | 0: return | ||
+ | |||
+ | public static void main(java.lang.String[]); | ||
+ | Code: | ||
+ | 0: new # | ||
+ | 3: dup | ||
+ | 4: iconst_1 | ||
+ | 5: anewarray | ||
+ | 8: dup | ||
+ | 9: iconst_0 | ||
+ | 10: ldc # | ||
+ | 12: aastore | ||
+ | 13: invokestatic | ||
+ | 16: invokespecial #6 // Method java/ | ||
+ | 19: invokestatic | ||
+ | 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." | ||
+ | |||
+ | {{: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Next, right click on your class and select "Show Decompile Code" option. | ||
+ | Result: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | |||
+ | |||
+ | |||
java/javap.1681300094.txt.gz · Last modified: 2023/07/04 19:36 (external edit)