Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.coderising.jvm.loader;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;



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方法
File file = new File(filePath);

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
ByteArrayOutputStream baos = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];
int len = 0;
while((len = bis.read(buffer)) != -1){
baos.write(buffer,0,len);
}
return baos.toByteArray();
}

public void addClassPath(String path) {
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();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
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 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();
}

}
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();

}
}
40 changes: 40 additions & 0 deletions group09/790466157/src/com/coding/basic/BinaryTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.coding.basic;

public class BinaryTree {
private BinaryTreeNode root;

public BinaryTreeNode getRoot(){
return root;
}

public BinaryTreeNode insert(Object o){
BinaryTreeNode node = new BinaryTreeNode(o);
if(root == null){
root = node;
root.setLeft(null);
root.setRight(null);
return root;
}else{
BinaryTreeNode currentNode = root;
BinaryTreeNode parentNode;
while(true){
parentNode = currentNode;

if(((Integer)node.getData()) > ((Integer)currentNode.getData())){
currentNode = currentNode.getRight();
if(currentNode == null){
parentNode.setRight(node);
return node;
}
}else{
currentNode = currentNode.getLeft();
if(currentNode == null){
parentNode.setLeft(node);
return node;
}
}
}
}
}

}
33 changes: 33 additions & 0 deletions group09/790466157/src/com/coding/basic/BinaryTreeNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.coding.basic;

public class BinaryTreeNode {

private Object data;
private BinaryTreeNode left;
private BinaryTreeNode right;

public BinaryTreeNode(Object data){
this.left = null;
this.right = null;
this.data = data;
}

public Object getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public BinaryTreeNode getLeft() {
return left;
}
public void setLeft(BinaryTreeNode left) {
this.left = left;
}
public BinaryTreeNode getRight() {
return right;
}
public void setRight(BinaryTreeNode right) {
this.right = right;
}
}
1 change: 0 additions & 1 deletion group09/790466157/src/com/coding/basic/Iterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
public interface Iterator {
public boolean hasNext();
public Object next();

}
71 changes: 17 additions & 54 deletions group09/790466157/src/com/coding/basic/Queue.java
Original file line number Diff line number Diff line change
@@ -1,68 +1,31 @@
package com.coding.basic;
import java.util.Arrays;

import com.coding.basic.linklist.LinkedList;

public class Queue {
private static final int CAPACITY = 10;
private static int capacity;
private static int front;
private static int tail;
private static Object[] array;

public Queue(){
this.capacity = CAPACITY;
array = new Object[capacity];
front = tail = 0;
}
private LinkedList linkedlist = new LinkedList();

public void enQueue(Object o) throws ExceptionQueueFull {
if (size() == capacity -1)
throw new ExceptionQueueFull("Queue is full");
array[tail] = o;
tail = (tail +1) % capacity;
public void enQueue(Object o){
linkedlist.add(o);
}

public Object deQueue() throws ExceptionQueueEmpty{
Object o;
if (isEmpty())
throw new ExceptionQueueEmpty("Queue is empty");
o = array[front];
front = (front + 1) % capacity;
return o;
public Object deQueue(){
if(!isEmpty()){
return linkedlist.get(0);
}else{
return null;
}
}

public boolean isEmpty(){
return (front == tail);
if(size() > 0){
return false;
}else{
return true;
}
}

public int size(){
if (isEmpty())
return 0;
else
return (capacity + tail - front) % capacity;
}

public class ExceptionQueueEmpty extends Exception {
// Constructor
public ExceptionQueueEmpty() {

}

// Constructor with parameters
public ExceptionQueueEmpty(String mag) {
System.out.println(mag);
}
}

public class ExceptionQueueFull extends Exception {

// Constructor
public ExceptionQueueFull() {

}

// Constructor with parameters
public ExceptionQueueFull(String mag) {
System.out.println(mag);
}
return linkedlist.size();
}
}
Loading