We know that a class file can normally be divided into seven parts:

  • Magic number and class file version
  • Constant pool
  • Access tokens
  • Class index, superclass index, interface index
  • Set of field tables
  • Method table collection
  • Property sheet collection

This blog post will focus on understanding and code examples about access flags and class indexes, parent indexes, and interface indexes.

Let’s talk about these two functions in a colloquial way:

Access flags: Tell what type of class this class is. Is it a common class? Interfaces? Or enumeration? Or any other class, which class is decorated with a modifier.

Class index, superclass index, interface index: tells the constant pool address of the full name of the class, the constant pool address of the full name of the parent class if inherited, and the constant pool address of the full name of the interface if implemented (interfaces can be multiple).

An overview,

In addition to the last blog post, we talked about constant pools but didn’t specify the overall structure of the class file

A.class file is essentially a table consisting of the data items shown in the following table.

The figure above is the 7 components from the beginning.

Second, the visit mark

I found a lot of information about access flags. I also read the information about access flags in chapter 6 of the Book “Understanding the Java Virtual Machine”. Almost all the access flags on the Internet are shown in the following picture, and then WRITE a pulic class test. 0021 = ACC_PUBLIC+ACC_SUPER If you do not write an interface to verify, you will find that the following I circled in red says, after JDK1.2, this must be true is not true. Look at the picture first.

1. Access flags are interpreted in hexadecimal format

Think: like why ACC_PUBLIC is 00 01? How does that happen?

Access flags are actually a series of combinations, 16 flags can be used because there are 16 bits, but eight are currently defined and the remaining estimates are for JDk9 and 10…… Reserved, please. The eight are shown here.

After the theory, let’s test the code. In order to verify more accurately, I write a common class and interface test respectively:

2, public class modifier class

public class XiaoXiao {
  
}
Copy the code

Generate a class file in the same directory

javac XiaoXiao.java
Copy the code

Looking at the decompile class file

javap -v XiaoXiao.class
Copy the code

If the flags are ACC_PUBLIC, ACC_SUPER, then the hexadecimal should be 0021.

Now let’s look at the hexadecimal data for xiaoxia. class

A perfect match.

3. Interface verification

public interface DaDa {

}
Copy the code

Similarly, a class file is decompiled into a class file

Flags: ACC_PUBLIC, ACC_INTERFACE, ACC_ABSTRACT. Interfaces are abstract classes. So it adds up to 0601. The difference is that ACC_SUPER must be true for JDK1.2, which is obviously not true, so this is not accurate.

Let’s convert the class file to hexadecimal authentication.

And that’s how it works.

ACC_SUPER does not have to be true after JDK1.2. It should be described as follows:

Class index, parent index, interface index

1, concept,

These three pieces of data are used in the.class file to determine the class’s inheritance.

Class index: U2 data type, used to determine the fully qualified name of this class.

2. Parent index: U2 data type, used to determine the fully qualified name of the parent of this class.

3. Interface index: A collection of U2 data types that describe the interfaces implemented by the class. The implemented interfaces are listed in the index from left to right after the implements statement.

The interface index set is divided into two parts, the first part represents the interface counter (interfaces_count), which is a U2-type data, and the second part represents the interface information, followed by the interface counter.

If a class implements an interface of 0, the value of the interface counter is 0 and the interface index table does not occupy any bytes.

Again, here the test writes two test classes to test.

1. Common class tests

public class XiaoXiao {
  
}
Copy the code

Again, generate the class file and look at the hexadecimal data

We see that the class index of this class is in constant pool 0002, and the superclass index is in constant pool 0003. The interface 0000 means that the class does not implement any interface.

Then we decompile the xiaoxia. class file to find the constant pool.

Constant pool 0002 is the current class, and the parent class 0003 is the default Object ancestor.

Perfect!

2. Implement interface testing

To get a better understanding, let’s write another class that implements both interfaces.

Public class implements DaDa,LaLa{} public class implements DaDa,LaLa{}Copy the code

Xiaoxia.class file cannot be generated from javac xiaoxia. Java because an error will be reported. I analyze the reason because you can’t find DaDa,LaLa compilation information if you compile manually.

So we can start the whole project, go to the target directory and find the class file.

Open the hexadecimal file.

We see that the class index of this class is in constant pool 0002 and the parent index is in constant pool 0003 bits. If the interface is 0002, the class implements two interfaces, one in constant pool location 004 and the other in constant pool location 0005.

Looking at the decompiled class file.

Verification successful!