ReZero's Utopia.

jvmLearn

字数统计: 673阅读时长: 4 min
2018/08/28 Share

How java code run?

ab5c3523af08e0bf2f689c1d6033ef77

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ echo '
public class Foo {
public static void main(String[] args) {
boolean flag = true;
if (flag) System.out.println("Hello, Java!");
if (flag == true) System.out.println("Hello, JVM!");
}
}' > Foo.java
$ javac Foo.java
$ java Foo
$ java -cp ./asmtools-6.0/lib/asmtools.jar org.openjdk.asmtools.jdis.Main Foo.class > Foo.jasm.1
$ awk 'NR==1,/iconst_1/{sub(/iconst_1/, "iconst_2")} 1' Foo.jasm.1 > Foo.jasm
$ java -cp ./asmtools-6.0/lib/asmtools.jar org.openjdk.asmtools.jasm.Main Foo.jasm # recompile
$ java Foo

Check the change:

1
2
3
4
5
$ diff Foo.jasm Foo.jasm.1 
18c18
< iconst_2;
---
> iconst_1;

Java basic types

basic types

Class Loader

java_classloader_hierarchy

load

tips: In this time,JVM has generated the class structure and write in the method area

  1. Every class loader inherit java.lang.ClassLoader except boot class loader.

    • Boot: jre/lib, Xbootclasspath
    • Extension: ext, dirs
    • Application:
  2. JAVA9: change extension to platform which would load almost all modules except important moudles such as java.base.

  3. Class unique ID: result of class loader object and class full path name.

tips: get the address of method instead of class.

  1. Valid byte for jvm criterion.

  2. Prepare

    • Allocate memory
    • Construct layer about class such as method table which realize dynamic binding of virtual method.
  3. analyze

    • resolve these symbols to actual refference

Besides: Java machine specification not require finish the analyze in the link process, it just has provisions that the necessity of resolving symbol refference before execute the corresponding byte code.

init process

  1. final static [basic type or String] will be signed ConstantValue and init directly by jvm. Besides, all of directly assignment and static block code will be put in the same method and rename as <clinit>.

  2. JVM will use block method to ensure teh clinit method only be executed once.(And the feature always be used to realized singleton lazy init process)

  3. When will the init process be triggered:

    • JVM start, init user settle Main class
    • new the aim class
    • instructions: call static method, init over class
    • instructions: visit static field init over class
    • child class init trigger parent class init
    • default method defined in interface will result in interface init process.
    • reflection init class
    • the first call MethodHandle object, init the class where methodHandle locate.
1
2
3
4
5
6
7
8
9
public class Singleton {
private Singleton() {}
private static class LazyHolder {
static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return LazyHolder.INSTANCE;
}
}

The above code:
Only class Singleton.getInstance, program will visit LazyHolder.INSTANCE, then trigger the process of LazyHolder init and create a Singleton object.

Practice:

JVM: -verbose:class will print the order of class loading and special symbol before LazyHolder method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

$ echo '
public class Singleton {
private Singleton() {}
private static class LazyHolder {
static final Singleton INSTANCE = new Singleton();
static {
System.out.println("LazyHolder.<clinit>");
}
}
public static Object getInstance(boolean flag) {
if (flag) return new LazyHolder[2];
return LazyHolder.INSTANCE;
}
public static void main(String[] args) {
getInstance(true);
System.out.println("----");
getInstance(false);
}
}' > Singleton.java
$ javac Singleton.java
$ java -verbose:class Singleton

Problem:
new array whether result in LazyHolder load or init?

A: load not init.

1
2
3
4
$ java -cp /path/to/asmtools.jar org.openjdk.asmtools.jdis.Main Singleton\$LazyHolder.class > Singleton\$LazyHolder.jasm.1
$ awk 'NR==1,/stack 1/{sub(/stack 1/, "stack 0")} 1' Singleton\$LazyHolder.jasm.1 > Singleton\$LazyHolder.jasm
$ java -cp /path/to/asmtools.jar org.openjdk.asmtools.jasm.Main Singleton\$LazyHolder.jasm
$ java -verbose:class Singleton

Problem2:
new array will result in link of LazyHolder?

A: No.(while getInstance(false) will link and init)

How do JVM call method?

1
2
3
4
5
6
void invoke(Object obj, Object... args) { ... }
void invoke(String s, Object obj, Object... args) { ... }

invoke(null, 1); // call second
invoke(null, 1, 2); // call second
invoke(null, new Object[]{1}); // call first invoke
CATALOG
  1. 1. How java code run?
  2. 2. Java basic types
  3. 3. Class Loader
    1. 3.1. load
    2. 3.2. link
    3. 3.3. init process
  4. 4. How do JVM call method?