what's clinit
上文提到clinit關(guān)鍵字,這個到底是什么意思?
摘自jdk 官方文檔
2.9. Special Methods
At the level of the Java Virtual Machine, every constructor written in the Java programming language (JLS §8.8) appears as an instance initialization method that has the special name <init>. This name is supplied by a compiler. Because the name <init> is not a valid identifier, it cannot be used directly in a program written in the Java programming language. Instance initialization methods may be invoked only within the Java Virtual Machine by the invokespecial instruction (§invokespecial), and they may be invoked only on uninitialized class instances. An instance initialization method takes on the access permissions (JLS §6.6) of the constructor from which it was derived.
在JVM層面,任何一個被JAVA編寫顯示地初始化類的方法有一個特定的名稱叫"<init>"方法.這個名稱由編譯器提供,因為這個名稱在JAVA語言里不是一個有效的標(biāo)識符,所以它不能被java語言直接使用。<init>只能由JVM通過"invokespecial"指令調(diào)用.并且只能被未初始化的類實(shí)例調(diào)用.<init>方法要接受構(gòu)造方法的訪問權(quán)限。
A class or interface has at most one class or interface initialization method and is initialized (§5.5) by invoking that method. The initialization method of a class or interface has the special name <clinit>, takes no arguments, and is void (§4.3.3).
類或接口最多有一個"class or interface initialization method",該方法被調(diào)用時即初始化。這個方法有一個定的名字為"<clinit>",這個方法沒有參數(shù)并且返回值為void
Other methods named <clinit> in a class file are of no consequence. They are not class or interface initialization methods. They cannot be invoked by any Java Virtual Machine instruction and are never invoked by the Java Virtual Machine itself.
這段很重要
在class 文件中的其他任何名字為<cliinit>的方法都無所謂。它們都不是"clss or interface initializtion method".它們不會被任何JVM指令調(diào)用。也不可能被JVM本身調(diào)用.
In a class file whose version number is 51.0 or above, the method must additionally have its ACC_STATIC flag (§4.6) set in order to be the class or interface initialization method.
在51或更高的版本,這個方法必須加ACC_STATIC 標(biāo)記,以標(biāo)識為<clinit>方法.
This requirement is new in Java SE 7. In a class file whose version number is 50.0 or below, a method named <clinit> that is void and takes no arguments is considered the class or interface initialization method regardless of the setting of its ACC_STATIC flag.
在 JDK1.7有一個新需求,在class文件中如果版本號是50或50以下,則要求<clinit>方法返回值為void,且無參數(shù),但是否設(shè)置ACC_STATIC 標(biāo)記已經(jīng)無所謂了.
The name <clinit> is supplied by a compiler. Because the name <clinit> is not a valid identifier, it cannot be used directly in a program written in the Java programming language. Class and interface initialization methods are invoked implicitly by the Java Virtual Machine; they are never invoked directly from any Java Virtual Machine instruction, but are invoked only indirectly as part of the class initialization process.
<clinit>由編譯器提供,所以在JAVA源文件里,它不是一個有效的標(biāo)識符,它同樣也不能直接被JAVA源文件編寫。只能由JVM指令調(diào)用。
為什么叫clinit
class init ==>clinit
引用
https://stackoverflow.com/questions/8517121/java-what-is-the-difference-between-init-and-clinit
asm 用戶文檔第59 頁class init
asm4-guide.pdf P59
JDK1.6和1.7 50/51 版本對CLINIT方法限制驗證
package com.sparrow.jdk.asm;
import java.io.FileOutputStream;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
public class Helloworld extends ClassLoader implements Opcodes {
/**
* public class Example {
* <p>
* static {
* System.out.println("hello world");
* }
* public static void main (String[] args) {
* System.out.println("Hello world!");
* }
*
* @param args
* @throws Exception
*/
public static void main(final String args[]) throws Exception {
//定義一個叫做Example的類
ClassWriter cw = new ClassWriter(0);
//50 即為JDK 1.6
//通過個性版本號來驗證是否需要ACC_STATIC FLAG
cw.visit(V1_6, ACC_PUBLIC, "Example", null, "java/lang/Object", null);
//生成默認(rèn)的構(gòu)造方法
MethodVisitor initVisitor = cw.visitMethod(ACC_PUBLIC,
"<init>",
"()V",
null,
null);
//生成構(gòu)造方法的字節(jié)碼指令
initVisitor.visitVarInsn(ALOAD, 0);
initVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
initVisitor.visitInsn(RETURN);
initVisitor.visitMaxs(1, 1);
initVisitor.visitEnd();
//生成main方法
initVisitor = cw.visitMethod(ACC_PUBLIC + ACC_STATIC,
"main",
"([Ljava/lang/String;)V",
null,
null);
//生成main方法中的字節(jié)碼指令
initVisitor.visitFieldInsn(GETSTATIC,
"java/lang/System",
"out",
"Ljava/io/PrintStream;");
initVisitor.visitLdcInsn("Hello world!");
initVisitor.visitMethodInsn(INVOKEVIRTUAL,
"java/io/PrintStream",
"println",
"(Ljava/lang/String;)V", false);
initVisitor.visitInsn(RETURN);
initVisitor.visitMaxs(2, 2);
//字節(jié)碼生成完成
initVisitor.visitEnd();
MethodVisitor classInitVisitor = cw.visitMethod(ACC_PUBLIC, "<clinit>", "()V", null, null);
classInitVisitor.visitCode();
classInitVisitor.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
classInitVisitor.visitLdcInsn("static hello world");
classInitVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V",false);
classInitVisitor.visitInsn(RETURN);
classInitVisitor.visitMaxs(2, 0);
classInitVisitor.visitEnd();
// MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
// mv.visitCode();
// mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
// mv.visitLdcInsn("static hello world");
// mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V",false);
// mv.visitInsn(RETURN);
// mv.visitMaxs(2, 0);
// mv.visitEnd();
// 獲取生成的class文件對應(yīng)的二進(jìn)制流
byte[] code = cw.toByteArray();
//將二進(jìn)制流寫到本地磁盤上
FileOutputStream fos = new FileOutputStream("Example.class");
fos.write(code);
fos.close();
//直接將二進(jìn)制流加載到內(nèi)存中
Helloworld loader = new Helloworld();
Class<?> exampleClass = loader.defineClass("Example", code, 0, code.length);
//通過反射調(diào)用main方法
exampleClass.getMethods()[0].invoke(null, new Object[]{null});
}
}
靜態(tài)內(nèi)部類的單例模式
代碼略
?
clinit只調(diào)用一次,同步鎖驗證