Skip to content

Commit 8ec49db

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 00f4b44 + 66e5c9d commit 8ec49db

File tree

6 files changed

+189
-0
lines changed

6 files changed

+189
-0
lines changed

java-classloader/java-classloader.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.zyj.test;
2+
3+
/**
4+
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
5+
*
6+
* @author : 张勇杰
7+
* @date : 2018/12/3 16:16
8+
* @Version : v1.0
9+
* @description
10+
**/
11+
public class Demo01 {
12+
public static void main(String[] args) {
13+
System.out.println(ClassLoader.getSystemClassLoader());
14+
System.out.println(ClassLoader.getSystemClassLoader().getParent());
15+
System.out.println(ClassLoader.getSystemClassLoader().getParent().getParent());
16+
17+
System.out.println(System.getProperty("java.class" + ".path"));
18+
String a = "zyj";
19+
System.out.println(a.getClass().getClassLoader());
20+
21+
}
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.zyj.test;
2+
3+
/**
4+
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
5+
*
6+
* @author : 张勇杰
7+
* @date : 2018/12/6 15:08
8+
* @Version : v1.0
9+
* @description
10+
**/
11+
public class Demo02 {
12+
public static void main(String[] args) throws ClassNotFoundException {
13+
FileSystemClassLoader loader = new FileSystemClassLoader("d:/myjava");
14+
FileSystemClassLoader loader1 = new FileSystemClassLoader("d:/myjava");
15+
Class<?> c = loader.loadClass("com.zyj.test.HelloWorld");
16+
Class<?> c1 = loader.loadClass("com.zyj.test.HelloWorld");
17+
Class<?> c2 = loader1.loadClass("com.zyj.test.HelloWorld");
18+
Class c3 = loader.loadClass("java.lang.String");
19+
System.out.println(c.hashCode());
20+
System.out.println(c1.hashCode());
21+
System.out.println(c2.hashCode());
22+
System.out.println(c3.hashCode());
23+
//JVM认为被同一个加载器加载的同一个类是相同的类
24+
}
25+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.zyj.test;
2+
3+
import java.io.*;
4+
5+
/**
6+
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
7+
*
8+
* @author : 张勇杰
9+
* @date : 2018/12/6 14:47
10+
* @Version : v1.0
11+
* @description
12+
**/
13+
public class FileSystemClassLoader extends ClassLoader {
14+
//d:/myjava/com/zyj/test/User.class -->com.zyj.test.User
15+
private String rootDir;
16+
17+
public FileSystemClassLoader(String rootDir){
18+
this.rootDir = rootDir;
19+
}
20+
21+
@Override
22+
protected Class<?> findClass(String name) throws ClassNotFoundException {
23+
Class<?> c = findLoadedClass(name);
24+
25+
//应该先查询有没有加载过这个类,如果已经加载,则返回已经加载好的类。如果没有,则加载新的类
26+
if(c != null){
27+
return c;
28+
}else{
29+
ClassLoader parent = this.getParent();
30+
System.out.println(parent);
31+
//委派给父类加载
32+
try {
33+
c = parent.loadClass(name);
34+
}catch (Exception e){
35+
36+
}
37+
38+
if(c != null){
39+
return c;
40+
}else{
41+
byte[] classData = getClassData(name);
42+
if(classData == null){
43+
throw new ClassNotFoundException();
44+
}else{
45+
c = defineClass(name,classData,0,classData.length);
46+
}
47+
}
48+
}
49+
return c;
50+
}
51+
52+
private byte[] getClassData(String className){
53+
String path = rootDir + "/"+className.replace(".","/")+".class";
54+
55+
//IOUtils,可以使用他将流中的数据转成字节数组
56+
InputStream is = null;
57+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
58+
try {
59+
is = new FileInputStream(path);
60+
byte[] buffer = new byte[1024];
61+
int temp = 0;
62+
while((temp = is.read(buffer))!= -1){
63+
baos.write(buffer,0,temp);
64+
}
65+
return baos.toByteArray();
66+
} catch (Exception e) {
67+
e.printStackTrace();
68+
return null;
69+
}finally {
70+
if(is != null){
71+
try {
72+
is.close();
73+
} catch (IOException e) {
74+
e.printStackTrace();
75+
}
76+
}
77+
if (baos != null) {
78+
79+
try {
80+
baos.close();
81+
} catch (IOException e) {
82+
e.printStackTrace();
83+
}
84+
}
85+
}
86+
}
87+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.zyj.test;
2+
3+
/**
4+
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
5+
*
6+
* @author : 张勇杰
7+
* @date : 2018/12/6 15:52
8+
* @Version : v1.0
9+
* @description
10+
**/
11+
public class Test02 extends Test1 {
12+
@Override
13+
protected String test01() throws Exception {
14+
return "我才是返回值";
15+
}
16+
17+
public static void main(String[] args) {
18+
Test02 test02 = new Test02();
19+
try {
20+
System.out.println(test02.test02());
21+
} catch (Exception e) {
22+
e.printStackTrace();
23+
}
24+
}
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.zyj.test;
2+
3+
/**
4+
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
5+
*
6+
* @author : 张勇杰
7+
* @date : 2018/12/6 15:51
8+
* @Version : v1.0
9+
* @description
10+
**/
11+
public abstract class Test1 {
12+
protected String test01() throws Exception {
13+
throw new Exception("666");
14+
}
15+
protected String test02() throws Exception{
16+
String c = test01();
17+
return c;
18+
}
19+
}

0 commit comments

Comments
 (0)