Go language implements search class file tool

Search class file tool

Subject of this chapter

Discuss where the Java virtual machine looks for class files, and use the go language to implement the loading function of class files

Primer

Question: How to start a Java application? For example, start the code shown below

public class HelloWorld {
    
    
   public static void main(String[] args) {
    
    
      System.out.println("Hello, world!");
   }
}

Answer: To start a java application, you need to start the Java virtual machine first, then load the main class, and finally call the main() method of the main class. In the above code, if you want to print the string "Hello, world", you need to load the HelloWorld Class, and to load the HelloWorld class, you need to load the superclass of HelloWorld. The superclass of this class is java.lang.Object. Before loading the main method, you need to prepare an array of parameters, and you need to load java.lang.String and java.lang.String [] class, the specific code in the method calls the System class, so the java.lang.System class needs to be loaded in advance

load class diagram

Question: Where should the virtual machine JVM look for classes?

Answer: Different virtual machine implementations determine different search methods. Oracle's Java virtual machine implementation searches for classes based on the class path. According to the order of search, the class path can be divided into the following three parts:

1. Start the class path (the path defaults to the jre\lib directory, use the -Xbootclasspath option to modify the class path)

2. Extended class path (the path defaults to jre\lib\ext)

3. User class path (set the CLASSPATH environment variable to modify the user class path, or specify the path through the -classpath option of the java command)

Our Java Virtual Machine will use the JDK's startup classpath to find and load classes in the Java Standard Library, so some way of specifying the location of the jre directory is needed. Command line options are a good choice, so add a non-standard option -Xjre.

Go language implements class file loading ideas

Use the composite pattern in Design Patterns to design your code:

Composite Pattern UML Diagram

Composite mode is suitable for building tree models, which is convenient for clients to handle simple and complex elements in a unified way.

All classes implement the Entry interface, which has two methods. The readClass method accesses the class and byte array according to the class path, and the String method returns the name of the class.

The CompositeEntry class is an array of Entry classes. In the initialization method of this class, the parameters are divided into small paths according to the separator, and then each small path is converted into a specific Entry instance.

The ZipEntry class represents the class path in the form of a ZIP or JAR file. In the initialization method of this class, the ZIP file is opened, and then the ZIP archive file is traversed, and then the class file is found, and the class content is read out.

There is a parameter in the DirEntry class that has always stored the absolute path. readClass combines the directory and the class file into a complete path, and then reads the content of the class file.

The WildCardEntry class inherits the CompositeEntry class, rewrites the initialization method, removes the * symbol in the absolute path, then gets the baseDir, and then creates a ZipEntry based on the baseDir.

Classpath

type Classpath struct {
    
    
	bootClasspath Entry
	extClasspath  Entry
	userClasspath Entry
}
func Parse(jreOption, cpOption string) *Classpath {
    
    
	cp := &Classpath{
    
    }
	cp.parseBootAndExtClasspath(jreOption)
	cp.parseUserClasspath(cpOption)
	return cp
}

There are three member variables in the ClassPath class, which store three paths respectively, and the initial member variables through the Parse function. Related code please click

Guess you like

Origin blog.csdn.net/sinat_28199083/article/details/128895319