Skip to content

Commit a7e5990

Browse files
committed
commit my first homework
1 parent 201be61 commit a7e5990

4 files changed

Lines changed: 157 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.example.demo;
2+
3+
public class Hello {
4+
public static void main(String[] args){
5+
System.out.println("Hello this is my first Classload!!");
6+
int a = 1;
7+
long b = 2l;
8+
float c = 3.0f;
9+
double d = 4.0d;
10+
char e = 'a';
11+
byte f = 5;
12+
short g = 6;
13+
boolean h = true;
14+
int[] i = {1,2,3,4,5,6};
15+
16+
int sum1 = (int) (a + b);
17+
int sum2 = e - a;
18+
int sum3 = (int) (c * d);
19+
int sum4 = a / f;
20+
21+
System.out.println("sum1 = " + sum1);
22+
System.out.println("sum2 = " + sum2);
23+
System.out.println("sum3 = " + sum3);
24+
System.out.println("sum4 = " + sum4);
25+
26+
if (a == b){
27+
System.out.println("a = b");
28+
}
29+
30+
for (int j = 0; j < i.length; j++){
31+
System.out.println("for" + j + " = " + i[j]);
32+
}
33+
34+
}
35+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5EA���������������������������������Ö��������֩���������𳖑�����������������������������������������ѕ������������������췚����ߜ�������������������������������Г���а���������Г���Ь�������������곕���Ж�Я�������������앞��Ж�Я�����������������������׳����Г���Ь������֩�����������������������������������H��N��������������������������������������M����I��N������������������������������
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.example.demo;
2+
3+
4+
import java.util.Base64;
5+
6+
public class HelloClassLoader extends ClassLoader{
7+
8+
public static void main(String[] args){
9+
try {
10+
new HelloClassLoader().findClass("com.example.demo.Hello").newInstance();
11+
} catch (InstantiationException e) {
12+
e.printStackTrace();
13+
} catch (IllegalAccessException e) {
14+
e.printStackTrace();
15+
} catch (ClassNotFoundException e) {
16+
e.printStackTrace();
17+
}
18+
}
19+
20+
@Override
21+
protected Class<?> findClass(String name) throws ClassNotFoundException {
22+
String helloBase64 = "yv66vgAAADgAHQoABgAPCQAQABEIABIKABMAFAcAFQcAFgEABjxpbml0PgEAAygpVgEABENvZGUBAA9MaW5lTnVtYmVyVGFibGUBAARtYWluAQAWKFtMamF2YS9sYW5nL1N0cmluZzspVgEAClNvdXJjZUZpbGUBAApIZWxsby5qYXZhDAAHAAgHABcMABgAGQEAIkhlbGxvIHRoaXMgaXMgbXkgZmlyc3QgQ2xhc3Nsb2FkISEHABoMABsAHAEAFmNvbS9leGFtcGxlL2RlbW8vSGVsbG8BABBqYXZhL2xhbmcvT2JqZWN0AQAQamF2YS9sYW5nL1N5c3RlbQEAA291dAEAFUxqYXZhL2lvL1ByaW50U3RyZWFtOwEAE2phdmEvaW8vUHJpbnRTdHJlYW0BAAdwcmludGxuAQAVKExqYXZhL2xhbmcvU3RyaW5nOylWACEABQAGAAAAAAACAAEABwAIAAEACQAAAB0AAQABAAAABSq3AAGxAAAAAQAKAAAABgABAAAAAwAJAAsADAABAAkAAAAlAAIAAQAAAAmyAAISA7YABLEAAAABAAoAAAAKAAIAAAAFAAgABgABAA0AAAACAA4=";
23+
byte[] bytes = decode(helloBase64);
24+
return defineClass(name, bytes, 0, bytes.length);
25+
}
26+
27+
public byte[] decode(String base64){
28+
return Base64.getDecoder().decode(base64);
29+
}
30+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.example.demo;
2+
3+
import java.io.Closeable;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.lang.reflect.InvocationTargetException;
7+
import java.lang.reflect.Method;
8+
9+
public class XlassLoader extends ClassLoader{
10+
11+
public static void main(String[] args){
12+
String className = "com.example.demo.Hello";
13+
String methodName = "hello";
14+
15+
XlassLoader xlassLoader = new XlassLoader();
16+
try {
17+
Class<?> cxlazz = xlassLoader.loadClass(className);
18+
19+
for (Method method : cxlazz.getMethods()){
20+
System.out.println(cxlazz.getSimpleName() + "." + method.getName());
21+
}
22+
23+
Object instance = cxlazz.getDeclaredConstructor().newInstance();
24+
25+
//调用实例
26+
Method method = cxlazz.getMethod(methodName);
27+
method.invoke(instance);
28+
} catch (ClassNotFoundException e) {
29+
e.printStackTrace();
30+
} catch (InvocationTargetException e) {
31+
e.printStackTrace();
32+
} catch (InstantiationException e) {
33+
e.printStackTrace();
34+
} catch (IllegalAccessException e) {
35+
e.printStackTrace();
36+
} catch (NoSuchMethodException e) {
37+
e.printStackTrace();
38+
}
39+
};
40+
41+
@Override
42+
protected Class<?> findClass(String name) throws ClassNotFoundException {
43+
String resourcePath = name.replace(".", "/");
44+
String suffix = ".xlass";
45+
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(resourcePath + suffix);
46+
47+
try {
48+
int length = inputStream.available();
49+
byte[] byteArray = new byte[length];
50+
inputStream.read(byteArray);
51+
52+
//转换
53+
byte[] classBytes = decode(byteArray);
54+
55+
return defineClass(name, classBytes, 0, classBytes.length);
56+
} catch (IOException e) {
57+
e.printStackTrace();
58+
throw new ClassNotFoundException(name, e);
59+
} finally {
60+
close(inputStream);
61+
}
62+
63+
}
64+
65+
66+
/**
67+
* 解码
68+
* @param byteArray
69+
* @return
70+
*/
71+
public static byte[] decode(byte[] byteArray){
72+
byte[] bytes = new byte[byteArray.length];
73+
for (int i = 0; i < byteArray.length; i++){
74+
bytes[i] = (byte) (255 - byteArray[i]);
75+
}
76+
77+
return bytes;
78+
}
79+
80+
public static void close(Closeable res){
81+
if (res != null){
82+
try {
83+
res.close();
84+
} catch (IOException e) {
85+
e.printStackTrace();
86+
}
87+
}
88+
}
89+
90+
91+
}

0 commit comments

Comments
 (0)