Skip to content

Commit 677e393

Browse files
committed
classloader
1 parent fe27f3a commit 677e393

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,7 @@ hs_err_pid*
111111

112112
*.iml
113113
.idea/*
114+
*.project
115+
*.prefs
116+
*.classpath
117+
*.factorypath
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.di1shuai.base.jvm;
2+
3+
/**
4+
* @author: shea
5+
* @date: 2021/7/13
6+
* @description:
7+
*/
8+
public class ByteCode {
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.di1shuai.base.jvm.classloader;
2+
3+
import java.io.*;
4+
5+
/**
6+
* @author: shea
7+
* @date: 2021/7/29
8+
* @description: classloader应用:
9+
* 加密classloader
10+
* <p>
11+
* x ^ y ^ y = x
12+
* 生成class文件后进行加密
13+
* 使用解密的classloader进行读取
14+
*/
15+
public class EncriptionClassLoader extends ClassLoader {
16+
17+
18+
private static final int seed = 0B10110110;
19+
20+
private static final String basepath = "/Users/shea/Documents/GitRepo/mine/back-end/java-versions/java-8/target/classes/";
21+
// String basepath = "/Users/shea/Documents/GitRepo/mine/back-end/java-versions/java-base/target/classes";
22+
23+
@Override
24+
protected Class<?> findClass(String name) throws ClassNotFoundException {
25+
System.out.println("======进入自定义ClassLoader - findClass =====");
26+
File file = new File(basepath, name.replaceAll("\\.", "/").concat(".di1shuaiclass"));
27+
try {
28+
FileInputStream fileInputStream = new FileInputStream(file);
29+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
30+
int b = 0;
31+
while ((b = fileInputStream.read()) != -1) {
32+
byteArrayOutputStream.write(b ^ seed);
33+
}
34+
fileInputStream.close();
35+
byteArrayOutputStream.close();
36+
byte[] bytes = byteArrayOutputStream.toByteArray();
37+
return defineClass(name, bytes, 0, bytes.length);
38+
} catch (Exception e) {
39+
e.printStackTrace();
40+
} finally {
41+
System.out.println("======结束自定义ClassLoader - findClass=====");
42+
}
43+
return super.findClass(name);
44+
}
45+
46+
47+
public static void main(String[] args) throws Exception {
48+
// String className = "com.di1shuai.base.gc.HelloJVM";
49+
String className = "com.diyishuai.java8.Student";
50+
encFile(className);
51+
EncriptionClassLoader encriptionClassLoader = new EncriptionClassLoader();
52+
Class<?> aClass = encriptionClassLoader.loadClass(className);
53+
System.out.println(aClass);
54+
}
55+
56+
private static void encFile(String name) throws Exception {
57+
File file = new File(basepath, name.replaceAll("\\.", "/").concat(".class"));
58+
FileInputStream fileInputStream = new FileInputStream(file);
59+
FileOutputStream fileOutputStream = new FileOutputStream(new File(basepath, name.replaceAll("\\.", "/").concat(".di1shuaiclass")));
60+
int b = 0;
61+
while ((b = fileInputStream.read()) != -1) {
62+
fileOutputStream.write(b ^ seed);
63+
}
64+
fileInputStream.close();
65+
fileOutputStream.close();
66+
}
67+
68+
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.di1shuai.base.jvm.classloader;
2+
3+
import java.io.*;
4+
5+
/**
6+
* @author: shea
7+
* @date: 2021/7/29
8+
* @description: 自定义类加载器
9+
* 指定文件路径进行加载
10+
*/
11+
public class HelloClassLoader extends ClassLoader {
12+
13+
String basepath = "/Users/shea/Documents/GitRepo/mine/back-end/java-versions/java-8/target/classes/";
14+
// String basepath = "/Users/shea/Documents/GitRepo/mine/back-end/java-versions/java-base/target/classes";
15+
16+
@Override
17+
protected Class<?> findClass(String name) throws ClassNotFoundException {
18+
System.out.println("======进入自定义ClassLoader - findClass =====");
19+
File file = new File(basepath, name.replaceAll("\\.", "/").concat(".class"));
20+
try {
21+
FileInputStream fileInputStream = new FileInputStream(file);
22+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
23+
int b = 0;
24+
while ((b = fileInputStream.read()) != -1) {
25+
byteArrayOutputStream.write(b);
26+
}
27+
fileInputStream.close();
28+
byteArrayOutputStream.close();
29+
byte[] bytes = byteArrayOutputStream.toByteArray();
30+
return defineClass(name, bytes, 0, bytes.length);
31+
} catch (Exception e) {
32+
e.printStackTrace();
33+
} finally {
34+
System.out.println("======结束自定义ClassLoader - findClass=====");
35+
}
36+
return super.findClass(name);
37+
}
38+
39+
40+
public static void main(String[] args) throws ClassNotFoundException {
41+
// String className = "com.di1shuai.base.gc.HelloJVM";
42+
String className = "com.diyishuai.java8.Student";
43+
44+
HelloClassLoader helloClassLoader = new HelloClassLoader();
45+
Class<?> aClass = helloClassLoader.loadClass(className);
46+
System.out.println(aClass);
47+
48+
49+
}
50+
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.di1shuai.base.jvm.classloader;
2+
3+
/**
4+
* @author: shea
5+
* @date: 2021/7/28
6+
* @description:
7+
*/
8+
public class JVMClassLoader {
9+
10+
public static void main(String[] args) {
11+
// null -> Bootstrap
12+
// 为什么为Null,因为Bootstrap使用C++写的,在Java中没有与之相对应的类,所以返回了空值
13+
System.out.println(String.class.getClassLoader());
14+
15+
//sun.misc.Launcher$ExtClassLoader@3d4eac69 -> Extention
16+
System.out.println(sun.net.spi.nameservice.dns.DNSNameService.class.getClassLoader());
17+
18+
//sun.misc.Launcher$AppClassLoader@6d06d69c -> App
19+
System.out.println(JVMClassLoader.class.getClassLoader());
20+
21+
22+
23+
//双亲委派
24+
System.out.println("===================");
25+
// null -> Bootstrap
26+
System.out.println(sun.net.spi.nameservice.dns.DNSNameService.class.getClassLoader().getClass().getClassLoader());
27+
// null -> Bootstrap
28+
System.out.println(JVMClassLoader.class.getClassLoader().getClass().getClassLoader());
29+
30+
// app -> extention -> bootstrap
31+
System.out.println(JVMClassLoader.class.getClassLoader().getParent());
32+
System.out.println(JVMClassLoader.class.getClassLoader().getParent().getParent());
33+
34+
// 范围
35+
System.out.println(System.getProperty("sun.boot.class.path"));
36+
System.out.println(System.getProperty("java.ext.dirs"));
37+
System.out.println(System.getProperty("java.class.path"));
38+
39+
40+
}
41+
42+
}

0 commit comments

Comments
 (0)