Posts

Showing posts with the label Java Class Viewer

Java Native Methods Essentials, from Binary code point of view, with example of java.lang.Runtime

Image
Java Native method provides a mechanism for Java code to call OS native code, either due to functional or performance reasons. Binary Code is always the best document. Let's take the an example from OpenJDK of the class java.lang.Rutime to discover the details in binary code manner. Here is the source code of the Runtime class: master/src/java.base/share/classes/java/lang/Runtime.java In this class we have the following methods defined as native : 606 public native int availableProcessors(); 617 public native long freeMemory(); 630 public native long totalMemory(); 641 public native long maxMemory(); 664 public native void gc(); For the native Java methods, the corresponding generated methods in the class file will set the ACC_NATIVE ( 0x0100 ) flag, and the there is no bytecode generated in the .class file as other none-native Java methods. The access_flags field is a mask of flags used to denote access permission to and properties of an method: access...

Java module-info Essentials, from Binary code point of view

Image
Java module system has been introduced since Java 9, via the project Jigsaw . Each java module contains a module-info.class file, for the specification of current module. Here we are trying to make an introduction of the module-info file. Binary Code is always the best document . So let's inspect an example module-info: module info in java.base.jmod file, which is the biggest java module in current OpenJDK . Source code: master/src/java.base/share/classes/module-info.java   The binary file is located at: JAVA_HOME/jmods/java.base.jmod - classes/module-info.class We can open the module-info.class file using Java Class Viewer , as the screen shots bellow. The module-info.class file is a special type of .class file, and it has the following key differences from other normal .class file: 1. module-info.class do not have super class, interfaces, fields, methods. From the screen shot bellow, we can find that the super_class node's value is " 0 ", whic...