Skip to content

Commit

Permalink
补交第四次作业
Browse files Browse the repository at this point in the history
  • Loading branch information
oneces committed Apr 9, 2017
1 parent bc287a6 commit 08f0ba9
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 33 deletions.
19 changes: 6 additions & 13 deletions group15/1521_653895972/src/task4/jvm/loader/ClassFileLoader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package task4.jvm.loader;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
Expand All @@ -15,10 +17,7 @@ public class ClassFileLoader {
private List<String> clzPaths = new ArrayList<String>();

public byte[] readBinaryCode(String className) throws Exception {
URL base = this.getClass().getResource("/");
String baseToString = ""+base;
String filePath = baseToString.replaceAll("file:/", "")+className.replace(".", "\\")+".class";
//String filePath = clzPaths.get(0)+"\\"+className.replace(".", "\\")+".class"; //符合Junit测试调用addClassPath方法
String filePath = clzPaths.get(0)+File.separatorChar+className.replace('.',File.separatorChar)+".class";
File file = new File(filePath);

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
Expand All @@ -33,19 +32,13 @@ public byte[] readBinaryCode(String className) throws Exception {
}

public void addClassPath(String path) {
if (clzPaths.contains(path))
return;
clzPaths.add(path);
}

public String getClassPath(){
StringBuffer strBuffer = new StringBuffer();
for(int i=0;i<clzPaths.size();i++){
if(i == (clzPaths.size() - 1)){
strBuffer.append(clzPaths.get(i));
}else{
strBuffer.append(clzPaths.get(i)+";");
}
}
return strBuffer.toString();
return StringUtils.join(clzPaths,";");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class ClassFileloaderTest {


static String path1 = "D:\\GitHub\\coding2017\\group01\\1814014897\\zhouhui\\bin";
static String path1 = "D:\\oneces\\GitHub\\coding2017\\group15\\1521_653895972\\out\\production\\1521_653895972";
static String path2 = "C:\temp";


Expand All @@ -31,7 +31,6 @@ public void testClassPath(){
loader.addClassPath(path2);

String clzPath = loader.getClassPath();

Assert.assertEquals(path1+";"+path2,clzPath);

}
Expand All @@ -42,12 +41,12 @@ public void testClassFileLength() throws Exception {
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path1);

String className = "week04.jvm.test.EmployeeV1";
String className = "task4.jvm.test.EmployeeV1";

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

// 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大
Assert.assertEquals(1040, byteCodes.length);
Assert.assertEquals(1038, byteCodes.length);

}

Expand All @@ -56,7 +55,7 @@ public void testClassFileLength() throws Exception {
public void testMagicNumber() throws Exception{
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path1);
String className = "week04.jvm.test.EmployeeV1";
String className = "task4.jvm.test.EmployeeV1";
byte[] byteCodes = loader.readBinaryCode(className);
byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]};

Expand Down
2 changes: 1 addition & 1 deletion group15/1521_653895972/src/task4/jvm/test/EmployeeV1.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package week04.jvm.test;
package task4.jvm.test;

public class EmployeeV1 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public LRUPageFrame(int capacity) {
/**
* 获取缓存中对象
*
* @param key
* @param pageNum
* @return
*/
public void access(int pageNum) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
public class ClassFileloaderTest {


private static final String FULL_QUALIFIED_CLASS_NAME = "com/coderising/jvm/test/EmployeeV1";
private static final String FULL_QUALIFIED_CLASS_NAME = "task5.jvm.test.EmployeeV1";

static String path1 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin";
static String path1 = "D:\\oneces\\GitHub\\coding2017\\group15\\1521_653895972\\out\\production\\1521_653895972";
static String path2 = "C:\temp";

static ClassFile clzFile = null;
static {
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path1);
String className = "com.coderising.jvm.test.EmployeeV1";
String className = "task5.jvm.test.EmployeeV1";

clzFile = loader.loadClass(className);
clzFile.print();
Expand Down Expand Up @@ -56,7 +56,7 @@ public void testClassFileLength() {
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path1);

String className = "com.coderising.jvm.test.EmployeeV1";
String className = "task5.jvm.test.EmployeeV1";

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

Expand All @@ -70,7 +70,7 @@ public void testClassFileLength() {
public void testMagicNumber(){
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path1);
String className = "com.coderising.jvm.test.EmployeeV1";
String className = "task5.jvm.test.EmployeeV1";
byte[] byteCodes = loader.readBinaryCode(className);
byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]};

Expand Down
8 changes: 0 additions & 8 deletions

This file was deleted.

0 comments on commit 08f0ba9

Please sign in to comment.