Skip to content

Commit 4c5aac5

Browse files
author
jossc
committed
jvm-classloader
1 parent 17ed541 commit 4c5aac5

12 files changed

Lines changed: 373 additions & 4 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.enums;
2+
3+
public enum TestEnums {
4+
/**
5+
* hha
6+
*/
7+
H("11",1),
8+
A("22",2);
9+
10+
String name;
11+
int gae;
12+
13+
TestEnums(String name, int gae) {
14+
this.name = name;
15+
this.gae = gae;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public int getGae() {
23+
return gae;
24+
}
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.enums;
2+
3+
import java.lang.reflect.InvocationTargetException;
4+
import java.lang.reflect.Method;
5+
6+
/**
7+
* @ClassName TestMain
8+
* @Author chenzhuo
9+
* @Version 1.0
10+
* @Date 2019-06-18 22:26
11+
**/
12+
public class TestMain {
13+
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
14+
Class c = TestEnums.class;
15+
Object[] objects = c.getEnumConstants();
16+
Method name = c.getMethod("getName");
17+
Method age = c.getMethod("getGae");
18+
for (Object obj : objects) {
19+
System.out.println("name=" + name.invoke(obj) + "; age="
20+
+ age.invoke(obj));
21+
}
22+
}
23+
}

src/main/java/com/evething/TestOne.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
**/
99
public class TestOne {
1010
private int i;
11-
private char ch ;
11+
private char ch;
1212

1313
public int getI() {
1414
return i;
@@ -28,7 +28,29 @@ public void setCh(char ch) {
2828

2929
public static void main(String[] args) {
3030
TestOne testOne = new TestOne();
31-
System.out.println(testOne.getI());
32-
System.out.println(testOne.getI());
31+
for (int i = 0; i < 10; i++) {
32+
try {
33+
TestException testException = new TestException(i);
34+
testException.testException();
35+
} catch (Exception e) {
36+
System.out.println(e.getLocalizedMessage());
37+
}
38+
}
39+
}
40+
}
41+
42+
class TestException {
43+
private int i;
44+
45+
public TestException(int i) {
46+
this.i = i;
47+
}
48+
49+
void testException() {
50+
if (i == 0) {
51+
int c = 0 / i;
52+
} else {
53+
System.out.println(i);
54+
}
3355
}
3456
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.jvm.classloader.classloader;
2+
3+
import java.io.*;
4+
5+
/**
6+
* @ClassName ClassLoaderTest
7+
* @Author chenzhuo
8+
* @Version 1.0
9+
* @Date 2019-06-22 16:14
10+
**/
11+
public class ClassLoaderTest extends ClassLoader {
12+
13+
private String className;
14+
private final String fileExtension = ".class";
15+
private String path;
16+
17+
public String getPath() {
18+
return path;
19+
}
20+
21+
public void setPath(String path) {
22+
this.path = path;
23+
}
24+
25+
ClassLoaderTest(String className) {
26+
super();
27+
this.className = className;
28+
}
29+
30+
public ClassLoaderTest(ClassLoader parent, String className) {
31+
super(parent);
32+
this.className = className;
33+
}
34+
35+
public static void main(String[] args) throws Exception {
36+
ClassLoaderTest classLoaderTest = new ClassLoaderTest("loader");
37+
//classLoaderTest.setPath("/Users/demons/IdeaProjects/JavaCode/target/classes/com/jvm/classloader/relyclass/");
38+
classLoaderTest.setPath("/Users/demons/Desktop/");
39+
Class aClass = classLoaderTest.loadClass("com.jvm.classloader.relyclass.TestOne");
40+
Object object = aClass.newInstance();
41+
System.out.println(object.getClass().getName());
42+
43+
}
44+
45+
public static void test(ClassLoader classLoader) throws Exception {
46+
Class<?> clazz = classLoader.loadClass("com.jvm.classloader.relyclass.TestOne");
47+
48+
Object object = clazz.newInstance();
49+
System.out.println(object.getClass().getName());
50+
51+
}
52+
53+
@Override
54+
protected Class<?> findClass(String name) {
55+
System.out.println("find Class :" + name);
56+
byte[] data = loadClassData(name);
57+
return defineClass(className, data, 0, data.length);
58+
}
59+
60+
@Override
61+
public String toString() {
62+
return "ClassLoaderTest{" +
63+
"className='" + className + '\'' +
64+
'}';
65+
}
66+
67+
private byte[] loadClassData(String name) {
68+
System.out.println("name:" + name);
69+
InputStream is = null;
70+
byte[] data = null;
71+
ByteArrayOutputStream byteArrayOutputStream = null;
72+
try {
73+
name = name.replace(".", "/");
74+
is = new FileInputStream(new File(path + name + fileExtension));
75+
byteArrayOutputStream = new ByteArrayOutputStream();
76+
int ch;
77+
while (-1 != (ch = is.read())) {
78+
byteArrayOutputStream.write(ch);
79+
}
80+
data = byteArrayOutputStream.toByteArray();
81+
} catch (Exception e) {
82+
e.printStackTrace();
83+
} finally {
84+
try {
85+
is.close();
86+
byteArrayOutputStream.close();
87+
} catch (IOException e) {
88+
e.printStackTrace();
89+
}
90+
}
91+
92+
return data;
93+
}
94+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.jvm.classloader.classloader;
2+
3+
import java.io.IOException;
4+
import java.net.URL;
5+
import java.util.Enumeration;
6+
7+
/**
8+
* @ClassName TestClassLoader
9+
* @Author chenzhuo
10+
* @Version 1.0
11+
* @Date 2019-06-20 22:04
12+
**/
13+
public class TestClassLoader {
14+
public static void main(String[] args) throws IOException {
15+
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
16+
String resource = "com/jvm/classloader/relyclass/TestFive.class";
17+
Enumeration<URL> enumeration = classLoader.getResources(resource);
18+
while (enumeration.hasMoreElements()) {
19+
URL url = enumeration.nextElement();
20+
System.out.println(url);
21+
}
22+
System.out.println("-----");
23+
Class<?> c = TestClassLoader.class;
24+
System.out.println(c.getClassLoader());
25+
26+
TestClassLoader[] testClassLoaders = new TestClassLoader[10];
27+
System.out.println(testClassLoaders.getClass().getClassLoader());
28+
int[] ints = new int[10];
29+
System.out.println(ints.getClass().getClassLoader());
30+
String[] strings = new String[10];
31+
System.out.println(strings.getClass().getClassLoader());
32+
33+
34+
}
35+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.jvm.classloader.classloader;
2+
3+
import java.util.concurrent.ThreadFactory;
4+
5+
/**
6+
* @ClassName ThreadFatory
7+
* @Author chenzhuo
8+
* @Version 1.0
9+
* @Date 2019-06-20 22:17
10+
**/
11+
public class ThreadFatory implements ThreadFactory {
12+
13+
@Override
14+
public Thread newThread(Runnable r) {
15+
return new Thread(r);
16+
}
17+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.jvm.classloader.interfaceclass;
2+
3+
/**
4+
* @ClassName TestInterface
5+
* @Author chenzhuo
6+
* @Version 1.0
7+
* @Date 2019-06-18 22:40
8+
**/
9+
public class TestInterface {
10+
11+
public static void main(String[] args) {
12+
System.out.println(MyInterfaceImpl.i);
13+
}
14+
}
15+
16+
interface MyInterface {
17+
//int a =5;
18+
Thread thread = new Thread() {
19+
{
20+
System.out.println("is out?");
21+
}
22+
};
23+
}
24+
25+
interface MyInterfaceTest extends MyInterface {
26+
int b = 6;
27+
}
28+
29+
class MyInterfaceImpl implements MyInterface {
30+
public static int i = 10;
31+
}
32+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.jvm.classloader.relyclass;
2+
3+
/**
4+
* @ClassName TestFive
5+
* @Author chenzhuo
6+
* @Version 1.0
7+
* @Date 2019-06-19 22:44
8+
**/
9+
public class TestFive {
10+
11+
public static void main(String[] args) throws Exception {
12+
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
13+
Class clazz = classLoader.
14+
loadClass("com.jvm.classloader.relyclass.FiveTestClass");
15+
System.out.println(clazz);
16+
System.out.println("-------");
17+
clazz = Class.forName("com.jvm.classloader.relyclass.FiveTestClass");
18+
System.out.println(clazz.getName());
19+
}
20+
21+
}
22+
23+
class FiveTestClass {
24+
static {
25+
System.out.println("hello word");
26+
}
27+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.jvm.classloader.relyclass;
2+
3+
/**
4+
* @ClassName TestFour
5+
* @Author chenzhuo
6+
* @Version 1.0
7+
* @Date 2019-06-16 17:36
8+
**/
9+
public class TestFour {
10+
public static void main(String[] args) {
11+
Singleton singleton = Singleton.getSingleton();
12+
System.out.println("a :" + Singleton.a);
13+
System.out.println("b :" + Singleton.b);
14+
}
15+
}
16+
17+
class Singleton {
18+
public static int a;
19+
20+
21+
private static Singleton singleton = new Singleton();
22+
23+
private Singleton() {
24+
a++;
25+
b++;
26+
System.out.println("singleton :" + b);
27+
}
28+
29+
public static int b = 0;
30+
31+
static Singleton getSingleton() {
32+
return singleton;
33+
}
34+
}

src/main/java/com/jvm/classloader/relyclass/TestOne.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
public class TestOne {
1515

1616
public static void main(String[] args) {
17-
System.out.println(TestChild.sh);
17+
System.out.println(TestChild.s);
1818
}
1919
}
2020

0 commit comments

Comments
 (0)