-
Notifications
You must be signed in to change notification settings - Fork 641
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 #42 from Jiandan1357/master
5week
- Loading branch information
Showing
19 changed files
with
695 additions
and
152 deletions.
There are no files selected for viewing
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,17 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto | ||
|
||
# Custom for Visual Studio | ||
*.cs diff=csharp | ||
|
||
# Standard to msysgit | ||
*.doc diff=astextplain | ||
*.DOC diff=astextplain | ||
*.docx diff=astextplain | ||
*.DOCX diff=astextplain | ||
*.dot diff=astextplain | ||
*.DOT diff=astextplain | ||
*.pdf diff=astextplain | ||
*.PDF diff=astextplain | ||
*.rtf diff=astextplain | ||
*.RTF diff=astextplain |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
group23/601689050/4weekLRU&JVM/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,65 @@ | ||
package loader; | ||
import org.apache.commons.io.IOUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.io.*; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
/** | ||
* Created by Lxx on 2017/4/23. | ||
*/ | ||
public class ClassFileLoader { | ||
private List<String> clzPaths = new ArrayList<String>(); | ||
|
||
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){ | ||
return codes; | ||
} | ||
} | ||
|
||
return null; | ||
|
||
|
||
|
||
} | ||
|
||
private byte[] loadClassFile(String clzFileName) { | ||
|
||
File f = new File(clzFileName); | ||
|
||
try { | ||
|
||
return IOUtils.toByteArray(new FileInputStream(f)); | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
|
||
|
||
|
||
public void addClassPath(String path) { | ||
if(this.clzPaths.contains(path)){ | ||
return; | ||
} | ||
|
||
this.clzPaths.add(path); | ||
|
||
} | ||
|
||
|
||
|
||
public String getClassPath(){ | ||
return StringUtils.join(this.clzPaths,";"); | ||
} | ||
|
||
|
||
} |
86 changes: 86 additions & 0 deletions
86
group23/601689050/4weekLRU&JVM/JVM/test/loader/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,86 @@ | ||
package test.loader; | ||
|
||
import loader.ClassFileLoader; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class ClassFileLoaderTest { | ||
static String path1 = "D:\\JavaProject\\4week_minijvm\\out\\production\\4week_minijvm"; | ||
static String path2 = "D:\\temp"; | ||
|
||
|
||
|
||
@Before | ||
public void before() throws Exception { | ||
} | ||
|
||
@After | ||
public void after() throws Exception { | ||
} | ||
|
||
/** | ||
* | ||
* Method: readBinaryCode(String className) | ||
* | ||
*/ | ||
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 = "test.loader.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 = "test.loader.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(); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...23/601689050/4LRU&JVM/JVM/EmployeeV1.java → ...ekLRU&JVM/JVM/test/loader/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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.test; | ||
package test.loader; | ||
|
||
public class EmployeeV1 { | ||
|
||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.