Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Class files

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to understand Class files". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Background introduction

After the Java source code is compiled into a Class file, the Class file structure is an important basis for JVM to load Class, instantiate objects, and make method calls.

Each Class file consists of a byte stream of 8 bytes, and all 16-bit, 32-bit, and 64-bit length numbers will be constructed into 2, 4, and 8 8-byte units. Multi-byte data items are always stored in Big-Endian1 order. In Java SDK, access to data in this format can be achieved using interfaces such as java.io.DataInput and java.io.DataOutput and classes such as java.io.DataInputStream and java.io.DataOutputStream.

The contents of the Class file can be represented by a set of private data types, including U1PowerU2 and U4, which represent 1, 2, and 4-byte unsigned numbers, respectively. These types of data in Java SDK can be read by implementing the readUnsignedByte, readUnsignedShort, and readInt methods in the interface java.io.DataInput.

ClassFile structure each Class file corresponds to a ClassFile structure as shown below, which contains the following attributes:

A few questions

What is the difference between a running constant pool and a static constant pool?

What are the contents of the Class file?

What is in the disassembled format of the Class file? try to interpret the assembly instructions in the method.

How does the local variable table and Operand stack work?

Tips for viewing Class files in hexadecimal Class files:

Vim + xxd = hexadecimal editor vim + xxd = hexadecimal editor

Vim-b xxx.class can open class files in binary

Call within vim::%! xxd displays the current file in hexadecimal

After the modification is complete, if you want to save, execute the following command to convert the hexadecimal back to binary -:%! xxd-r

The output includes line number, disassembly of local variables and other information javap

-v-verbose: outputs additional information (including line number, local variable table, disassembly, etc.)

-c: disassemble the code, such as:

Javap-c xxx.class

Javap-verbose Test.class

More introduction to javap: https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javap.html

With regard to disassembly:

Disassembly (Disassembly): the process of converting object code into assembly code, which can also be said to mean converting machine language to assembly language code, low-level to high-level. All the mysterious operation mechanism of the software is in the disassembly code.

Class file interpretation of Class file in JVM specification interpretation of ClassFile {U4 magic; / / magic number U2 minor_version; / / minor version number U2 major_version; / / major version number U2 constant_pool_count; / / constant pool counter cp_info constant_ Pool [constant _ pool_count-1] / / constant pool data area U2 access_flags; / / access flag U2 this_class; / / class index U2 super_class; / / parent class index U2 interfaces_count; / / interface counter U2 interfaces [interfaces _ count]; / / interface table U2 fields_count / / Field counters field_info fields [fields _ count]; / / Field tables U2 methods_count; / / method counters method_info methods [methods _ count]; / / method tables U2 attributes_count; / / attribute counters attribute_info attributions [attributes _ count]; / / attribute sheets}

A Class file is a set of binary streams based on 8-bit bytes. There are two data types for class structures:

Unsigned numbers: unsigned numbers belong to the basic attribute type. U1, U2, U4 and U8 represent 1-byte, 2-byte, 4-byte and 8-byte unsigned numbers, respectively, and can be used to describe numbers, index references, quantity values, or utf8-encoded string values.

Table: a compound data type consisting of multiple unsigned numbers or other tables as data items, ending with the name _ info.

Cafe babe 0000 0034 001d 0a00 0600 0f090010 0011 0800 120a 0013 0014 0700 15070016 0100 063c 696e 6974 3e01 0003 28295601 0004 436f 6465 0100 0f4c 696e 654e756d 6265 7254 6162 6c65 0100 046d 61696e01 0016 285b 4c6a 6176 612f 6c61 6e672f53 7472 696e 673b 2956 0100 0a53 6f757263 6546 696c 6501 0008 4c6f 672e 6a617661 0c00 0700 0807 0017 0c00 1800 1901000c 6865 6c6c 6f20 776f 726c 6421 07001a0c 001b 001c 0100 1263 6f6d 2f68 656c6c6f 2f74 6573 742f 4c6f 6701 0010 6a617661 2f6c 616e 672f 4f62 6a65 6374 0100106a 6176 612f 6c61 6e672f53 7973 74656d01 0003 6f75 7401 0015 4c6a 6176 612f696f 2f50 7269 6e74 5374 7265 616d 3b010013 6a617661 2f69 6f2f 5072 696e 74537472 6561 6d01 0007 7072 696e 746c 6e010015 284c 6a61 7661 2f6c 616e 672f 53747269 6e67 3b29 5600 2100 0500 0600 00000000 0200 0100 0700 0800 0100 0900 00001d00 0100 0100 0000 052a b700 01b1 00000001 000a 0000 0006 00010000 0003 0009000b 000c 0001 0009 0000 0025 0002 00010000 0009 b200 0212 03b6 0004 b100 00000100 0a00 0000 0a00 0200 0000 0500 08000600 0100 0d00 0000 0200 0e

Based on the above Class file structure, we can sort out the following Class file structure diagram:

The magic number magic is used to identify the format of this file, and the magic number of the Class file format is 0xCAFEBABE. The 1st-4th bytes of the Class file represent the magic number of the file.

The only function of the magic number is to determine whether this file is an Class file that can be accepted by the virtual machine. The magic value is fixed to 0xCAFEBABE and will not change. The only function of the magic number is to determine whether this file is an Class file that can be accepted by the virtual machine. The magic value is fixed to 0xCAFEBABE and will not change.

Minor version number minor_version, major version number major_version

The 5th-6th bytes of the Class file represent the minor version number of the Class file.

The 7-8 bytes of the Class file represent the major version number of the Class file.

The major version number and the minor version number together determine the version of the class file format. If the major version number of the class file is M and the minor version number is m, the version of the class file format is represented as M.m.

Therefore, the version of the class file format can be sorted in dictionary order, for example, 1.5

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 255

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report