How java code run?
1 | $ echo ' |
Check the change:
1 | $ diff Foo.jasm Foo.jasm.1 |
Java basic types
Class Loader
load
tips: In this time,JVM has generated the class structure and write in the method area
Every class loader inherit java.lang.ClassLoader except boot class loader.
- Boot: jre/lib, Xbootclasspath
- Extension: ext, dirs
- Application:
JAVA9: change extension to platform which would load almost all modules except important moudles such as java.base.
Class unique ID: result of class loader object and class full path name.
link
tips: get the address of method instead of class.
Valid byte for jvm criterion.
Prepare
- Allocate memory
- Construct layer about class such as method table which realize dynamic binding of virtual method.
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
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>
.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)
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 | public class Singleton { |
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 |
|
Problem:
new array whether result in LazyHolder load or init?
A: load not init.
1 | $ java -cp /path/to/asmtools.jar org.openjdk.asmtools.jdis.Main Singleton\$LazyHolder.class > Singleton\$LazyHolder.jasm.1 |
Problem2:
new array will result in link of LazyHolder?
A: No.(while getInstance(false) will link and init)
How do JVM call method?
1 | void invoke(Object obj, Object... args) { ... } |