クラスのロードと方法はノートを割り当てます.
17534 ワード
内容はすべて深い理解から来ています.理解力は限られています.間違いがあるかもしれません.個人のメモです.忘れないようにしてください.
クラスの読み込みプロセス:
クラスのキャリアの両親の委任モデル:
クラスの読み込みプロセス:
package org.gerry.classLoader;
/**
* -XX:+TraceClassLoading
* :
* 1.
* 2.
* 3. java.lang.Class ,
*
* :
* 1. ———— ,
* 2. ———— JAVA , ,
* 3. ———— , iadd long
* 4. ———— , private
* ,
* 2-4
*
* :
* ( ConstantValue ),
* ( , )
*
* :
*
* : , ( index)
* : 、 。 ,
*
* Class Class_info,Fieldref_info,Methodref_info,InterfaceMethodref_info
* , 13 , :
* anewarray multianewarray new getfield putfield getstatic putstatic
* invokeinterface invokespecial invokestatic invokevirtual checkcast instanceof
*
* , , , ,
*
* :
* 1. ———— CONSTANT_Class_info
* 2. ———— CONSTANT_Fieldref_info
* 3. ———— CONSTANT_Methodref_info
* 4. ———— CONSTANT_InterfaceMethodref_info
*
* : Demo Son
* :
* const #32 = class #33; // org/gerry/classLoader/Son
* const #33 = Asciz org/gerry/classLoader/Son;
* :
* 0: ldc #32; //class org/gerry/classLoader/Son
* 2: invokevirtual #34; //Method java/lang/Object.getClass:()Ljava/lang/Class;
* #32 #32 Constant_Class_info #33,
* ( ), ,
* ( java.lang.IllegalAccessError)
* ,
*
* :
* 1. Constant_Field_ref class_index C( )
* 2. C NameAndTyperef_info
* 3. 2 , , ( , ) ,
* 4. 3 , java.lang.Object, ,
* 5. java.lang.NoSuchFieldError
* ,
*
* :
* 1. C
* 2. C java.lang.IncompatibleClassChangeError
* 3.
* 4.
* 5. ( , , java.lang.AbstractMethodError)
* 6. java.lang.NoSuchMethodError
*
* :
* 1. C
* 2. C java.lang.IncompatibleClassChangeError
* 3.
* 4.
* 5. java.lang.NoSuchMethodError
*
* :
* clinit()
* 1. clinit static static ,static static
* 2. clinit clinit , clinit java.lang.Object
* 3. static , 2
* 4. static , clinit
* 5. , , , clinit
* 6. clinit,
*/
public class Demo
{
static
{
System.out.println( ConstantA.love );// ,
}
// ,
public static void main(String[] args) throws ClassNotFoundException
{
// System.out.println( Son.value );// , ,
// Son[] sons = new Son[10];//new , ,[Lorg.gerry.classLoader.Son;
// Class.forName("org.gerry.classLoader.Parent");// ,
Son.class.getClass();// , ,
// SonInterface.class.getClass();// , ,
// Demo.class.getClassLoader().loadClass("org.gerry.classLoader.Parent");// ,
}
/**
* :
* 1. new, getstatic, putstatic, invokestatic :final 、
* 2. java.lang.refelect
* 3. , ,
* 4.
*
*
* :
* 1. , System.out.println( ConstantA.love ); , Constant
* 2. ,static , , System.out.println( Son.value );
* 3. , , , Son[] sons = new Son[10];
*
* , ,
*/
/**
:System.out.println( Son.value );
[Loaded org.gerry.classLoader.Demo from file:/D:/work/workspace/readCode/bin/]
1314
[Loaded org.gerry.classLoader.Parent from file:/D:/work/workspace/readCode/bin/]
[Loaded org.gerry.classLoader.Son from file:/D:/work/workspace/readCode/bin/]
parent init...
123
, , , ,
*/
/**
:Son[] sons = new Son[10];
[Loaded org.gerry.classLoader.Demo from file:/D:/work/workspace/readCode/bin/]
1314
[Loaded org.gerry.classLoader.Parent from file:/D:/work/workspace/readCode/bin/]
[Loaded org.gerry.classLoader.Son from file:/D:/work/workspace/readCode/bin/]
Son Parent,
*/
/**
Class.forName("org.gerry.classLoader.Parent") Parent
[Loaded org.gerry.classLoader.Demo from file:/D:/work/workspace/readCode/bin/]
1314
[Loaded org.gerry.classLoader.Parent from file:/D:/work/workspace/readCode/bin/]
parent init...
*/
/**
Demo.class.getClassLoader().loadClass("org.gerry.classLoader.Parent"); ,
[Loaded org.gerry.classLoader.Demo from file:/D:/work/workspace/readCode/bin/]
1314
[Loaded org.gerry.classLoader.Parent from file:/D:/work/workspace/readCode/bin/]
*/
/**
*
Son.class.getClass();
Demo.class.getClassLoader().loadClass("org.gerry.classLoader.Parent"); ,
[Loaded org.gerry.classLoader.Demo from file:/D:/work/workspace/readCode/bin/]
1314
[Loaded org.gerry.classLoader.Parent from file:/D:/work/workspace/readCode/bin/]
[Loaded org.gerry.classLoader.Son from file:/D:/work/workspace/readCode/bin/]
*/
}
クラスのキャリアの両親の委任モデル:
package org.gerry.classLoader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
/**
*
* , (loadClass ), , ,
* , ,
*
* Parent , , loadClass
*
* AppClassLoader Parent,
* , Parent, AppClassLoader Parent
*
*
* , SPI( JDBC) , JDBC ,
* , ,
* System.out.println( ( Parent )sonCl.newInstance() );
* :
* ArrayList MyList
* MyList AppClassLoader
* ArrayList
* ArrayList ar = new MyList(); ( Parent )sonCl.newInstance()
* , findLoadedClass
*
* Parent AppClassLoader , Son , Parent,
* Parent, Parent , ,
* Parent AppClassLoader ,
*
*/
public class TestClassLoader
{
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException
{
/**
* findClass , , ,
* ,
* loadClass ,
*/
// ,
ClassLoader cl = new ClassLoader()
{
@Override
public Class<?> loadClass(String name)
throws ClassNotFoundException {
String fileName = name.substring( name.lastIndexOf(".") + 1 ) + ".class";
// System.out.println(getClass().getClassLoader().getResource(""));// bin
//getClass().getResource("")
InputStream is = getClass().getResourceAsStream( fileName );
if( is == null || fileName.equals("Parent.class" ) )
{
return super.loadClass(name);// ClassNotFoundException
}
// else
// {
// return super.loadClass(name);
// , ,TestClassLoader.class.getClassLoader AppClassLoader
// }
byte[] bt;
try {
bt = new byte[is.available()];
is.read(bt);
return defineClass(name, bt, 0, bt.length );// NoClassDefFoundException
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.loadClass(name);
}
};
// Thread.currentThread().setContextClassLoader( cl );// ,
// System.out.println(Parent.class.getClassLoader());
ClassLoader l = TestClassLoader.class.getClassLoader();
// ,
Class tClass = l.loadClass("org.gerry.classLoader.TestClassLoader");
System.out.println( TestClassLoader.class == tClass );
// ,
Class tClass2 = cl.loadClass("org.gerry.classLoader.TestClassLoader");
System.out.println( TestClassLoader.class == tClass2 );
System.out.println( tClass2.getClassLoader() );
Class thisClass = TestClassLoader.class;
// 64: ldc #1; //class org/gerry/classLoader/TestClassLoader
// 66: astore 5
// , , ,
// #1 , TestClassLoader.class
System.out.println( TestClassLoader.class.getClassLoader() );
// ,
// loadClassr
// defineClass , class , ,
// org.gerry.classLoader.TestClassLoader$1 TestClassLoader AppClassLoader
/**
* rt ,JAVA
*/
System.out.println( String.class.getClass().getClassLoader() );//null
System.out.println( Thread.currentThread().getClass().getClassLoader() );//null
Class ParentCl = cl.loadClass("org.gerry.classLoader.Parent");
Class sonCl = cl.loadClass("org.gerry.classLoader.Son");// ParentCl ,
//
System.out.println( ( Parent )sonCl.newInstance());// , checkcast
System.out.println( "=============== ===============" );
System.out.println( sonCl.getClassLoader().toString() );
System.out.println( Son.class.getClassLoader().toString() );
System.out.println( (Son)new Son());
System.out.println( (Son)sonCl.newInstance() );//
System.out.println( "=============================" );
Class myListCl = l.loadClass("org.gerry.classLoader.MyList");
ArrayList ar = new MyList();
/**
* ArrayList MyList ,
* , ,
*/
System.out.println( ArrayList.class.getClassLoader() );
System.out.println( MyList.class.getClassLoader() );
// Class pl = cl.loadClass("org.gerry.classLoader.Parent");//
System.out.println( Parent.class.toString() + '|' + ParentCl.getClassLoader().toString() );
}
}
class MyList extends ArrayList
{
}
メソッドの割り当て:package org.gerry.codeExecute;
/**
* , ,
* :
* 1. 0 slot ( this)
* 2. , setValue(1,2) setValue(x,y)
* 1 2 x y, this , slot
*
* ( )
*
*
*
* 1.
* 2. ,
* , (invokestatic,invokespecial ,
* ———— , ),
* , (invokevirtual ,
* , )
*
* :
* 1. ( ) (main test, test main)
* 2. , , PC , 。
*
* 3. , , , ,
* PC 。
*
*
* :
* 1. ( ),
* 2. , , ,
* 3. (invokestatic,invokespecial ,
* , , , , ,final , , )
* 4. , , ?
* .
* : (Parent p = new Son(),Parent ,son )
* —————————— ( )
* , invoke(Human human) invoke(Parent parent) invoke(Son son)
* invoke(son) ,son parent human,
* , invoke(Son son) , Parent
* char-->int-->long-->character-->serializable-->object-->args... char
*
* : , ( A.test() A) ( )
* invokevirtual :
* 1.
* 2. , OK,
*
* :
* ,
* : ,
* : , ,
*
* :
* , , ,
* : , , ,
*
* Parent Object Son
* toString-------------------->toString<------------------toString
* getClass-------------------->getClass<------------------getClass
* test<-----------------------------------------------------test( )
* abc ------------------------------------------------------abc( )
* , Son Parent
*
* , ,
*
* :
* int i = 100; :bipush 100; istore_1;
* ( 1) (maxLocals 2)
* bipush 100; 1 100 this
* istore_1; 2 this 100( )
*/
public class Demo
{
{
int a= 1;
System.out.println( a );
}
public Demo()
{
}
public Demo( int i)
{
}
/**
* maxLocals:4( long slot, this i 4 slot)
* maxStack:2( , long )
*/
public void maxStackNum()
{
// final int a = 12;// final ,
long l = 2l;//lconst_1 , idc_w, , 2l Long_info
int i = 65535;//iconst_5 , 5 , bipush(-128~127), sipush(-32768~32767),
// int m = i;// iload_3 istore 4, iload_3 , 4 ,
// long a1 = 1l;
// long a2 = 2l;
// long a3 = 3l;
// long a4 = 4l;
// long a = a4;
// int[] barr = new int[ 5 ];//astore 4 aload 4
// int[] carr = barr;
// synchronized (this) {
// monitorenter // monitor,
// monitorexit // monitor
// }
}
public static void main(String[] args) {
StaticDispatch.Human parent = new StaticDispatch.StaticParent();
StaticDispatch.StaticSon son = new StaticDispatch.StaticSon();
StaticDispatch sd = new StaticDispatch();
sd.invoke( son );
// StaticDispatch.invoke( son );
// invokestatic, , invoke(parent) invoke(human)
// , , ?
// static
// ( ), ( , ?)
}
}
// public 1
// final 1 super 2
// interface abstract 24
// synthetic annotation enum 1 2 4
class StaticDispatch
{
//public private protected static 1248
//final 1
//interface abstract 24
//synthetic annotation enum 1 2 4
static class Human
{
// public private protected static 1248
// final volatile transient 148
//
// synthetic enum 14
int i;
}
static class StaticParent extends Human
{
}
static class StaticSon extends StaticParent
{
}
//public private protected static 1248
//final synchronize bridge varargs 1248
//native abstract strict 148
//synthetic 1
public void invoke( Human human)
{
System.out.println("Human");
}
public static void invoke( StaticParent parent)
{
System.out.println("Parent");
}
public void invoke( StaticSon son)
{
System.out.println("Son");
}
}