mirror of
https://github.com/FranzHaidnor/haidnorJVM.git
synced 2026-03-13 21:43:42 +08:00
fix bug execute <clinit>
This commit is contained in:
@@ -1,12 +1,8 @@
|
||||
package haidnor.jvm.classloader;
|
||||
|
||||
import haidnor.jvm.core.JavaExecutionEngine;
|
||||
import haidnor.jvm.rtda.heap.Klass;
|
||||
import haidnor.jvm.rtda.heap.KlassMethod;
|
||||
import haidnor.jvm.rtda.metaspace.Metaspace;
|
||||
import org.apache.bcel.classfile.ClassParser;
|
||||
import org.apache.bcel.classfile.JavaClass;
|
||||
import org.apache.bcel.classfile.Method;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
@@ -63,36 +59,13 @@ public class ClassLoader {
|
||||
}
|
||||
|
||||
JavaClass javaClass = classParser.parse();
|
||||
Klass klass = new Klass(this, javaClass);
|
||||
Metaspace.registerJavaClass(klass);
|
||||
|
||||
if (!classPath.startsWith("java/")) {
|
||||
// do clinit
|
||||
for (Method method : javaClass.getMethods()) {
|
||||
if (method.toString().equals("static void <clinit>()")) {
|
||||
KlassMethod klassMethod = new KlassMethod(klass, method);
|
||||
JavaExecutionEngine.callMethod(null, klassMethod);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return klass;
|
||||
return new Klass(this, javaClass);
|
||||
}
|
||||
|
||||
public Klass loadClassWithAbsolutePath(String absolutePath) throws IOException {
|
||||
ClassParser classParser = new ClassParser(absolutePath);
|
||||
JavaClass javaClass = classParser.parse();
|
||||
Klass klass = new Klass(this, javaClass);
|
||||
Metaspace.registerJavaClass(klass);
|
||||
// do clinit
|
||||
for (Method method : javaClass.getMethods()) {
|
||||
if (method.toString().equals("static void <clinit>()")) {
|
||||
KlassMethod klassMethod = new KlassMethod(klass, method);
|
||||
JavaExecutionEngine.callMethod(null, klassMethod);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return klass;
|
||||
return new Klass(this, javaClass);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
package haidnor.jvm.rtda.heap;
|
||||
|
||||
import haidnor.jvm.classloader.ClassLoader;
|
||||
import haidnor.jvm.core.JavaExecutionEngine;
|
||||
import haidnor.jvm.rtda.metaspace.Metaspace;
|
||||
import lombok.SneakyThrows;
|
||||
import org.apache.bcel.classfile.Field;
|
||||
import org.apache.bcel.classfile.JavaClass;
|
||||
import org.apache.bcel.classfile.Method;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -53,6 +56,19 @@ public class Klass {
|
||||
}
|
||||
}
|
||||
|
||||
Metaspace.registerJavaClass(this);
|
||||
|
||||
// execute <clinit> method
|
||||
if (!javaClass.getClassName().startsWith("java.")) {
|
||||
for (Method method : javaClass.getMethods()) {
|
||||
if (method.toString().equals("static void <clinit>()")) {
|
||||
KlassMethod klassMethod = new KlassMethod(this, method);
|
||||
JavaExecutionEngine.callMethod(null, klassMethod);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// loader super class
|
||||
JavaClass superJavaClass = javaClass.getSuperClass();
|
||||
if (superJavaClass != null) {
|
||||
|
||||
@@ -1,17 +1,5 @@
|
||||
package haidnor.jvm.test.clazz;
|
||||
|
||||
public class Student extends Human {
|
||||
|
||||
public static String school = "家里蹲大学";
|
||||
|
||||
public String name;
|
||||
|
||||
public void printSchool() {
|
||||
System.out.println(school);
|
||||
}
|
||||
|
||||
public static Student newStudent() {
|
||||
return new Student();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,10 +5,8 @@ import haidnor.jvm.test.clazz.Student;
|
||||
public class Demo4 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Student student = Student.newStudent();
|
||||
student.eat();
|
||||
student.name = "张三";
|
||||
System.out.println(student.name);
|
||||
Student student = new Student();
|
||||
System.out.println(Student.school);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user