Skip to content

Commit

Permalink
jvm引擎完成
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick authored and Patrick committed Apr 26, 2017
1 parent 9c1f2f4 commit 2c0eaa6
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public String getSuperClassName(){
public Method getMethod(String methodName, String paramAndReturnType){
List<Method> list = getMethods();
for(Method m : list){

String name = ((UTF8Info)pool.getConstantInfo(m.getNameIndex())).getValue();
String desc = ((UTF8Info)pool.getConstantInfo(m.getDescriptorIndex())).getValue();
if(name.equalsIgnoreCase(methodName) && desc.equalsIgnoreCase(paramAndReturnType)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void execute(StackFrame frame, ExecutionResult result) {
String currentClassName = javaObject.getClassName();
while(null != currentClassName){
ClassFile currentClassFile = methodArea.findClassFile(currentClassName);
m = currentClassFile.getMethod(methodRefInfo.getClassName(), methodRefInfo.getParamAndReturnType());
m = currentClassFile.getMethod(methodRefInfo.getMethodName(), methodRefInfo.getParamAndReturnType());
if(null != m){
break;
}else{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public void execute(Method mainMethod){
nextFrame.setCallerFrame(frame);
setupFunctionCallParams(frame,nextFrame);
stack.push(nextFrame);
}else{
stack.pop();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.ipk2015.coding2017.minijvm.engine;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -42,7 +43,7 @@ public Method getMainMethod(String className){


public ClassFile findClassFile(String className){

className = className.replace('.', File.separatorChar);
if(map.get(className) != null){
return map.get(className);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ public class ClassFileLoader {
public byte[] readBinaryCode(String className) {

className = className.replace('.', File.separatorChar) +".class";

for(String path : this.clzPaths){

String clzFileName = path + File.separatorChar + className;
byte[] codes = loadClassFile(clzFileName);
if(codes != null){
if(codes != null && codes.length>0){
return codes;
}
}
Expand All @@ -53,7 +51,7 @@ private byte[] loadClassFile(String clzFileName) {
return IOUtils.toByteArray(new FileInputStream(f));

} catch (IOException e) {
e.printStackTrace();
// e.printStackTrace();
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private void meetUTF8Info(ConstantPool pool,ByteCodeIterator iter){
private void parseInterfaces(ByteCodeIterator iter) {
int interfaceCount = iter.nextUNToInt(2);

System.out.println("interfaceCount:" + interfaceCount);
// System.out.println("interfaceCount:" + interfaceCount);

// TODO : 如果实现了interface, 这里需要解析
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class ClassFileloaderTest {
// String className = "com.coderising.jvm.test.EmployeeV1";
String className = "EmployeeV1";//老师的class文件单独放在这里,只有类名

clzFile = loader.loadClass(className);
clzFile = loader.loadClass(FULL_QUALIFIED_CLASS_NAME);
clzFile.print();
}

Expand Down Expand Up @@ -79,9 +79,7 @@ public void testClassFileLength() {
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path3);

String className = "EmployeeV1";

byte[] byteCodes = loader.readBinaryCode(className);
byte[] byteCodes = loader.readBinaryCode(FULL_QUALIFIED_CLASS_NAME);

// 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大
Assert.assertEquals(1056, byteCodes.length);
Expand All @@ -93,8 +91,7 @@ public void testClassFileLength() {
public void testMagicNumber(){
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path3);
String className = "EmployeeV1";
byte[] byteCodes = loader.readBinaryCode(className);
byte[] byteCodes = loader.readBinaryCode(FULL_QUALIFIED_CLASS_NAME);
byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]};


Expand Down Expand Up @@ -137,7 +134,7 @@ public void testConstantPool(){


ConstantPool pool = clzFile.getConstantPool();
System.out.println(""+pool.getSize());
System.out.println("pool size:"+pool.getSize());
Assert.assertEquals(53, pool.getSize());

{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.github.ipk2015.coding2017.minijvm.test;

import static org.junit.Assert.*;

import java.io.IOException;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.github.ipk2015.coding2017.minijvm.engine.MiniJVM;

public class MiniJVMTest {

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void test() {
MiniJVM jvm = new MiniJVM();
String[] classPaths = new String[3];
classPaths[0] = ClassFileloaderTest.path1;
classPaths[1] = ClassFileloaderTest.path2;
classPaths[2] = ClassFileloaderTest.path3;
String className = "com.coderising.jvm.test.EmployeeV1";
try {
jvm.run(classPaths, className);
Assert.assertEquals(0,0);
} catch (IOException e) {
// TODO Auto-generated catch block

e.printStackTrace();
Assert.assertEquals(0,1);
}
}

}

0 comments on commit 2c0eaa6

Please sign in to comment.