-
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 branch 'master' of https://github.com/MrGPanPan/coding2017
- Loading branch information
Showing
34 changed files
with
1,364 additions
and
49 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
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,13 @@ | ||
apply plugin: 'java' | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
compile("commons-io:commons-io:2.4") | ||
//compile("commons-lang:commons-lang:2.6") | ||
compile("org.apache.commons:commons-lang3:3.4") | ||
testCompile("junit:junit:4.12") | ||
} | ||
|
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,16 @@ | ||
## 讲课内容: | ||
- 17-03-27:JVM第一周 | ||
- 17-03-29:JVM之classLoader | ||
|
||
## 第六周作业(3-27 至 04-02) | ||
- 完成对一个.class文件的读取和对.class文件开头四个字节的魔数的判断需要实现ClassLoader.java | ||
- 实现LRU算法 | ||
- 一篇文章 | ||
|
||
## 完成情况: | ||
- ClassLoader.java已完 | ||
- LRU已完 | ||
- [文章](http://www.jianshu.com/p/02a8b4ee4596) | ||
|
||
## 我的收获: | ||
使用开源工具读取文件,并利用工作类进行字符转换.LRU算法(双链表操作),但是更重要的抽象化思想. |
117 changes: 117 additions & 0 deletions
117
group11/1178243325/week06/src/main/java/com/sprint/basic/LRUPageFrame.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,117 @@ | ||
package com.sprint.basic; | ||
|
||
public class LRUPageFrame { | ||
|
||
/** | ||
* 用双向链表实现LRU算法 | ||
*/ | ||
|
||
private static class Node { | ||
Node prev; | ||
Node next; | ||
int pageNum; | ||
|
||
Node() { | ||
|
||
} | ||
} | ||
|
||
private int capacity; | ||
private int currentSize; | ||
private Node first; | ||
private Node last; | ||
|
||
public LRUPageFrame(int capacity) { | ||
this.currentSize = 0; | ||
this.capacity = capacity; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public void access(int pageNum) { | ||
Node node = find(pageNum); | ||
if (node != null) { | ||
moveExistingNodeToHead(node); | ||
} else { | ||
node = new Node(); | ||
node.pageNum = pageNum; | ||
if (currentSize >= capacity) { | ||
removeLast(); | ||
} | ||
addNewNodeToHead(node); | ||
} | ||
} | ||
|
||
private Node find(int pageNum) { | ||
Node node = first; | ||
while (node != null) { | ||
if (node.pageNum == pageNum) { | ||
return node; | ||
} | ||
node = node.next; | ||
} | ||
return null; | ||
} | ||
|
||
private void moveExistingNodeToHead(Node node) { | ||
if (node == first) { | ||
return; | ||
} else if (node == last) { | ||
Node prevNode = node.prev; | ||
prevNode.next = null; | ||
last.prev = null; | ||
last = prevNode; | ||
} else { | ||
Node prevNode = node.prev; | ||
prevNode.next = node.next; | ||
Node nextNode = node.next; | ||
nextNode.prev = prevNode; | ||
} | ||
|
||
node.prev = null; | ||
node.next = first; | ||
first.prev = node; | ||
first = node; | ||
} | ||
|
||
private void removeLast() { | ||
Node prev = last.prev; | ||
prev.next = null; | ||
last.prev = null; | ||
last = prev; | ||
this.currentSize--; | ||
} | ||
|
||
private void addNewNodeToHead(Node node) { | ||
if (isEmpty()) { | ||
node.prev = null; | ||
node.next = null; | ||
first = node; | ||
last = node; | ||
} else { | ||
node.prev = null; | ||
node.next = first; | ||
first.prev = node; | ||
first = node; | ||
} | ||
this.currentSize++; | ||
} | ||
|
||
private boolean isEmpty() { | ||
return (first == null) && (last == null); | ||
} | ||
@Override | ||
public String toString() { | ||
StringBuilder buffer = new StringBuilder(); | ||
Node node = first; | ||
while (node != null) { | ||
buffer.append(node.pageNum); | ||
node = node.next; | ||
if (node != null) { | ||
buffer.append(","); | ||
} | ||
} | ||
return buffer.toString(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/ClassFile.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,9 @@ | ||
package com.sprint.jvm.clz; | ||
|
||
public class ClassFile { | ||
private final String cafebabe = "cafebabe"; | ||
|
||
public String getCafebabe() { | ||
return cafebabe; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ByteCodeIterator.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,17 @@ | ||
package com.sprint.jvm.loader; | ||
|
||
import com.sprint.jvm.util.Util; | ||
import java.util.Arrays; | ||
public class ByteCodeIterator { | ||
byte[] codes; | ||
int pos = 0; | ||
ByteCodeIterator(byte[] codes) { | ||
this.codes = codes; | ||
} | ||
|
||
public String nextU4ToHexString() { | ||
return Util.byteToHexString(new byte[] {codes[pos++], codes[pos++], codes[pos++], codes[pos++]}); | ||
} | ||
|
||
|
||
} |
49 changes: 49 additions & 0 deletions
49
group11/1178243325/week06/src/main/java/com/sprint/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,49 @@ | ||
package com.sprint.jvm.loader; | ||
|
||
import com.sprint.jvm.clz.ClassFile; | ||
import java.io.IOException; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
import org.apache.commons.io.IOUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
public class ClassFileLoader { | ||
List<String> clzPaths = new ArrayList<>(); | ||
|
||
public ClassFile loadClass(String className) { | ||
byte[] codes = this.readBinaryCode(className); | ||
ClassFileParser parser = new ClassFileParser(); | ||
return parser.parse(codes); | ||
} | ||
|
||
public void addClassPath(String clzPath) { | ||
if (this.clzPaths.contains(clzPath)) { | ||
return; | ||
} | ||
this.clzPaths.add(clzPath); | ||
} | ||
|
||
private byte[] readBinaryCode(String className) { | ||
className = className.replace('.', File.separatorChar) + ".class"; | ||
for (String path : 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; | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ClassFileParser.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,14 @@ | ||
package com.sprint.jvm.loader; | ||
|
||
import com.sprint.jvm.clz.ClassFile; | ||
public class ClassFileParser { | ||
public ClassFile parse(byte[] codes) { | ||
ClassFile clzFile = new ClassFile(); | ||
ByteCodeIterator iter = new ByteCodeIterator(codes); | ||
String magicNumber = iter.nextU4ToHexString(); | ||
if (!"cafebabe".equals(magicNumber)) { | ||
return null; | ||
} | ||
return clzFile; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
group11/1178243325/week06/src/main/java/com/sprint/jvm/util/Util.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,17 @@ | ||
package com.sprint.jvm.util; | ||
|
||
public class Util { | ||
public static 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(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
group11/1178243325/week06/src/test/java/com/sprint/basic/LRUPageFrameTest.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,33 @@ | ||
package com.sprint.basic; | ||
import org.junit.Assert; | ||
|
||
import org.junit.Test; | ||
|
||
|
||
public class LRUPageFrameTest { | ||
|
||
@Test | ||
public void testAccess() { | ||
LRUPageFrame frame = new LRUPageFrame(3); | ||
frame.access(7); | ||
frame.access(0); | ||
frame.access(1); | ||
Assert.assertEquals("1,0,7", frame.toString()); | ||
frame.access(2); | ||
Assert.assertEquals("2,1,0", frame.toString()); | ||
frame.access(0); | ||
Assert.assertEquals("0,2,1", frame.toString()); | ||
frame.access(0); | ||
Assert.assertEquals("0,2,1", frame.toString()); | ||
frame.access(3); | ||
Assert.assertEquals("3,0,2", frame.toString()); | ||
frame.access(0); | ||
Assert.assertEquals("0,3,2", frame.toString()); | ||
frame.access(4); | ||
Assert.assertEquals("4,0,3", frame.toString()); | ||
frame.access(5); | ||
Assert.assertEquals("5,4,0", frame.toString()); | ||
|
||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
group11/1178243325/week06/src/test/java/com/sprint/jvm/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,23 @@ | ||
package com.sprint.jvm.loader; | ||
|
||
import com.sprint.jvm.clz.ClassFile; | ||
import org.junit.Test; | ||
import org.junit.Assert; | ||
public class ClassFileLoaderTest { | ||
|
||
private static final String FULL_QUALTFIED_CLASS_NAME = "com/sprint/jvm/EmployeeV1"; | ||
|
||
static String path1 = "/home/sprint/java/code/coding2017/group11/1178243325/week06/build/classes/test"; | ||
static ClassFile clzFile = null; | ||
static { | ||
ClassFileLoader loader = new ClassFileLoader(); | ||
loader.addClassPath(path1); | ||
String className = "com.sprint.jvm.loader.EmployeeV1"; | ||
clzFile = loader.loadClass(className); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
Assert.assertEquals("cafebabe", clzFile.getCafebabe()); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
group11/1178243325/week06/src/test/java/com/sprint/jvm/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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.sprint.jvm.loader; | ||
|
||
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", 20); | ||
p.sayHello(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
group11/996108220/src/com/coderising/jvm/clz/AccessFlag.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,25 @@ | ||
package com.coderising.jvm.clz; | ||
|
||
public class AccessFlag { | ||
private int flagValue; | ||
|
||
public AccessFlag(int value) { | ||
this.flagValue = value; | ||
} | ||
|
||
public int getFlagValue() { | ||
return flagValue; | ||
} | ||
|
||
public void setFlagValue(int flag) { | ||
this.flagValue = flag; | ||
} | ||
|
||
public boolean isPublicClass(){ | ||
return (this.flagValue & 0x0001) != 0; | ||
} | ||
public boolean isFinalClass(){ | ||
return (this.flagValue & 0x0010) != 0; | ||
} | ||
|
||
} |
Oops, something went wrong.