forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request onlyliuxin#61 from Ren650119726/master
102228177 第五次作业
- Loading branch information
Showing
76 changed files
with
3,888 additions
and
54 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> | ||
<classpathentry kind="lib" path="lib/dom4j-1.6.jar"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> | ||
<classpathentry kind="lib" path="lib/dom4j-1.6.1.jar"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
group17/102228177/work3_26/src/com/coderising/jvm/loader/ClassFileLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.coderising.jvm.loader; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
|
||
public class ClassFileLoader { | ||
|
||
private List<String> clzPaths = new ArrayList<String>(); | ||
|
||
public byte[] readBinaryCode(String className) { | ||
String name = ""; | ||
for (int i = 0; i < className.length(); i++) { | ||
if(className.charAt(i)=='.'){ | ||
name += File.separatorChar; | ||
}else{ | ||
name += className.charAt(i); | ||
} | ||
} | ||
File file = new File(getClassPath()+ File.separatorChar +name+".class"); | ||
InputStream in = null; | ||
ByteArrayOutputStream out = null; | ||
try { | ||
in = new FileInputStream(file); | ||
out = new ByteArrayOutputStream(); | ||
byte[] buff = new byte[1024*2]; | ||
int len = 0; | ||
while((len=in.read(buff))!=-1){ | ||
out.write(buff, 0, len); | ||
} | ||
return out.toByteArray(); | ||
} catch (FileNotFoundException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
finally { | ||
if(in!=null){ | ||
try { | ||
in.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
if(out!=null){ | ||
try { | ||
out.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
|
||
|
||
} | ||
|
||
|
||
public void addClassPath(String path) { | ||
clzPaths.add(path); | ||
} | ||
|
||
|
||
|
||
public String getClassPath(){ | ||
StringBuilder sb = new StringBuilder(); | ||
for (String string : clzPaths) { | ||
sb.append(string).append(";"); | ||
} | ||
sb = sb.deleteCharAt(sb.length()-1); | ||
return sb.toString(); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
group17/102228177/work3_26/src/com/coderising/jvm/test/ClassFileloaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.coderising.jvm.test; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.coderising.jvm.loader.ClassFileLoader; | ||
|
||
|
||
|
||
|
||
|
||
public class ClassFileloaderTest { | ||
|
||
|
||
// static String path1 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin"; | ||
static String path1 = "D:\\git\\coding2017\\group17\\102228177\\work3_26\\bin"; | ||
static String path2 = "C:\temp"; | ||
|
||
|
||
|
||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
} | ||
|
||
@Test | ||
public void testClassPath(){ | ||
|
||
ClassFileLoader loader = new ClassFileLoader(); | ||
loader.addClassPath(path1); | ||
loader.addClassPath(path2); | ||
|
||
String clzPath = loader.getClassPath(); | ||
|
||
Assert.assertEquals(path1+";"+path2,clzPath); | ||
|
||
} | ||
|
||
@Test | ||
public void testClassFileLength() { | ||
|
||
ClassFileLoader loader = new ClassFileLoader(); | ||
loader.addClassPath(path1); | ||
|
||
String className = "com.coderising.jvm.test.EmployeeV1"; | ||
|
||
byte[] byteCodes = loader.readBinaryCode(className); | ||
|
||
// 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 | ||
Assert.assertEquals(1056, byteCodes.length); | ||
|
||
} | ||
|
||
|
||
@Test | ||
public void testMagicNumber(){ | ||
ClassFileLoader loader = new ClassFileLoader(); | ||
loader.addClassPath(path1); | ||
String className = "com.coderising.jvm.test.EmployeeV1"; | ||
byte[] byteCodes = loader.readBinaryCode(className); | ||
byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; | ||
|
||
|
||
String acctualValue = this.byteToHexString(codes); | ||
|
||
Assert.assertEquals("cafebabe", acctualValue); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
private String byteToHexString(byte[] codes ){ | ||
StringBuffer buffer = new StringBuffer(); | ||
for(int i=0;i<codes.length;i++){ | ||
byte b = codes[i]; | ||
int value = b & 0xFF; | ||
String strHex = Integer.toHexString(value); | ||
if(strHex.length()< 2){ | ||
strHex = "0" + strHex; | ||
} | ||
buffer.append(strHex); | ||
} | ||
return buffer.toString(); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
group17/102228177/work3_26/src/com/coderising/jvm/test/EmployeeV1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.coderising.jvm.test; | ||
|
||
public class EmployeeV1 { | ||
|
||
|
||
private String name; | ||
private int age; | ||
|
||
public EmployeeV1(String name, int age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
public void setAge(int age){ | ||
this.age = age; | ||
} | ||
public void sayHello() { | ||
System.out.println("Hello , this is class Employee "); | ||
} | ||
public static void main(String[] args){ | ||
EmployeeV1 p = new EmployeeV1("Andy",29); | ||
p.sayHello(); | ||
|
||
} | ||
} |
Oops, something went wrong.