Skip to content

Commit

Permalink
Merge pull request onlyliuxin#36 from vegetableDogBai/master
Browse files Browse the repository at this point in the history
week6 jvm字段方法实现
  • Loading branch information
Mr-ChangK authored Apr 17, 2017
2 parents 7e27761 + 5044710 commit ee07771
Show file tree
Hide file tree
Showing 54 changed files with 2,939 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
package com.coderising.jvm.test;

public class EmployeeV1 {
private String name;


private String name;
private int age;

public EmployeeV1(String name, int age) {
this.name = name;
this.age = age;
}
this.age = age;
}

public void setName(String name) {
this.name = name;
}
public void setAge(int age){
this.age = age;

public void setAge(int age) {
this.age = age;
}
public void sayHello() {
System.out.println("Hello , this is class Employee ");

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


public static void main(String[] args) {
EmployeeV1 p = new EmployeeV1("Andy", 29);
p.sayHello();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.coderising.jvm.attr;

public abstract class AttributeInfo {
public static final String CODE = "Code";
public static final String CONST_VALUE = "ConstantValue";
public static final String EXCEPTIONS = "Exceptions";
public static final String LINE_NUM_TABLE = "LineNumberTable";
public static final String LOCAL_VAR_TABLE = "LocalVariableTable";
public static final String STACK_MAP_TABLE = "StackMapTable";
int attrNameIndex;
int attrLen ;
public AttributeInfo(int attrNameIndex, int attrLen) {

this.attrNameIndex = attrNameIndex;
this.attrLen = attrLen;
}


}
126 changes: 126 additions & 0 deletions group23/563253496/week6_jvm/src/com/coderising/jvm/attr/CodeAttr.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package com.coderising.jvm.attr;

import com.coderising.jvm.clz.ClassFile;
import com.coderising.jvm.constant.ConstantPool;
import com.coderising.jvm.loader.ByteCodeIterator;
import com.coderising.jvm.loader.ClassFileLoader;
import sun.text.CodePointIterator;

import java.io.UnsupportedEncodingException;


public class CodeAttr extends AttributeInfo {
private int maxStack;
private int maxLocals;
private int codeLen;
private String code;

public String getCode() {
return code;
}

//private ByteCodeCommand[] cmds ;
//public ByteCodeCommand[] getCmds() {
// return cmds;
//}
private LineNumberTable lineNumTable;
private LocalVariableTable localVarTable;
private StackMapTable stackMapTable;

public CodeAttr(int attrNameIndex, int attrLen, int maxStack, int maxLocals, int codeLen, String code /*ByteCodeCommand[] cmds*/) {
super(attrNameIndex, attrLen);
this.maxStack = maxStack;
this.maxLocals = maxLocals;
this.codeLen = codeLen;
this.code = code;
//this.cmds = cmds;
}

public void setLineNumberTable(LineNumberTable t) {
this.lineNumTable = t;
}

public void setLocalVariableTable(LocalVariableTable t) {
this.localVarTable = t;
}

public static CodeAttr parse(ClassFile clzFile, ByteCodeIterator iter) {

int attribute_name_index = iter.nextU2ToInt();
if (!clzFile.getConstantPool().getUTF8String(attribute_name_index).equalsIgnoreCase(CODE)) {
throw new RuntimeException("CODE属性的attributenameindex解析错误");
}

int attribute_len = iter.nextU4ToInt();
byte[] bytes = iter.getBytes(attribute_len);
ByteCodeIterator codeIter = new ByteCodeIterator(bytes);

int max_stack = codeIter.nextU2ToInt();
int max_locals = codeIter.nextU2ToInt();
int code_len = codeIter.nextU4ToInt();
/*byte[] codes = codeIter.getBytes(code_len);
String code = null;
try {
code = new String(codes, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
*/
String code = codeIter.nextUxToHexString(code_len);
CodeAttr codeAttr = new CodeAttr(attribute_name_index, attribute_len, max_stack, max_locals, code_len, code);

int exception_table_len = codeIter.nextU2ToInt();
if (exception_table_len > 0) {
throw new RuntimeException("方法的code属性中有异常未进行解析");
}

//code中的属性
int attribute_count = codeIter.nextU2ToInt();
/*byte[] bytes1 = codeIter.getBytes(attribute_length);
ByteCodeIterator codeAttrIter = new ByteCodeIterator(bytes1);
while (codeAttrIter.isNotEnd()) {
int attributenameindex = codeAttrIter.nextU2ToInt();
if (clzFile.getConstantPool().getUTF8String(attributenameindex).equalsIgnoreCase(LINE_NUM_TABLE)) {
LineNumberTable table = LineNumberTable.parse(codeAttrIter, attributenameindex);
codeAttr.setLineNumberTable(table);
} else if (clzFile.getConstantPool().getUTF8String(attributenameindex).equalsIgnoreCase(LOCAL_VAR_TABLE)) {
LocalVariableTable table = LocalVariableTable.parse(codeAttrIter,attributenameindex);
codeAttr.setLocalVariableTable(table);
}else if(clzFile.getConstantPool().getUTF8String(attributenameindex).equalsIgnoreCase(STACK_MAP_TABLE)){
codeIter.back(2);
StackMapTable table = StackMapTable.parse(codeIter);
codeAttr.setStackMapTable(table);
}
}*/
for (int i = 0; i < attribute_count; i++) {
int attributenameindex = codeIter.nextU2ToInt();
if (clzFile.getConstantPool().getUTF8String(attributenameindex).equalsIgnoreCase(LINE_NUM_TABLE)) {
LineNumberTable table = LineNumberTable.parse(codeIter, attributenameindex);
codeAttr.setLineNumberTable(table);
} else if (clzFile.getConstantPool().getUTF8String(attributenameindex).equalsIgnoreCase(LOCAL_VAR_TABLE)) {
LocalVariableTable table = LocalVariableTable.parse(codeIter,attributenameindex);
codeAttr.setLocalVariableTable(table);
}else if(clzFile.getConstantPool().getUTF8String(attributenameindex).equalsIgnoreCase(STACK_MAP_TABLE)){
codeIter.back(2);
StackMapTable table = StackMapTable.parse(codeIter);
codeAttr.setStackMapTable(table);
}

}




return codeAttr;
}

private void setStackMapTable(StackMapTable t) {
this.stackMapTable = t;

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.coderising.jvm.attr;

import java.util.ArrayList;
import java.util.List;

import com.coderising.jvm.loader.ByteCodeIterator;

public class LineNumberTable extends AttributeInfo {
List<LineNumberItem> items = new ArrayList<LineNumberItem>();

private static class LineNumberItem {
int startPC;
int lineNum;

public int getStartPC() {
return startPC;
}

public void setStartPC(int startPC) {
this.startPC = startPC;
}

public int getLineNum() {
return lineNum;
}

public void setLineNum(int lineNum) {
this.lineNum = lineNum;
}
}

public void addLineNumberItem(LineNumberItem item) {
this.items.add(item);
}

public LineNumberTable(int attrNameIndex, int attrLen) {
super(attrNameIndex, attrLen);

}

public static LineNumberTable parse(ByteCodeIterator iter, int attribute_name_index) {
int ccc = (iter.codes.length);
long attribute_len = iter.nextU4ToInt();
int line_number_table_len = iter.nextU2ToInt();
LineNumberTable table = new LineNumberTable(attribute_name_index,(int)attribute_len);

/*byte[] bytes = iter.getBytes(line_number_table_len);
ByteCodeIterator lntIter = new ByteCodeIterator(bytes);*/


/*while (lntIter.isNotEnd()) {
//int c = lntIter.codes.length;
LineNumberItem item = new LineNumberItem();
item.setStartPC(lntIter.nextU2ToInt());
item.setLineNum(lntIter.nextU2ToInt());
table.addLineNumberItem(item);
}*/


for (int i = 0; i <line_number_table_len ; i++) {
LineNumberItem item = new LineNumberItem();
item.setStartPC(iter.nextU2ToInt());
item.setLineNum(iter.nextU2ToInt());
table.addLineNumberItem(item);
}
return table;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.coderising.jvm.attr;

public class LocalVariableItem {
private int startPC;
private int length;
private int nameIndex;
private int descIndex;
private int index;
public int getStartPC() {
return startPC;
}
public void setStartPC(int startPC) {
this.startPC = startPC;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getNameIndex() {
return nameIndex;
}
public void setNameIndex(int nameIndex) {
this.nameIndex = nameIndex;
}
public int getDescIndex() {
return descIndex;
}
public void setDescIndex(int descIndex) {
this.descIndex = descIndex;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.coderising.jvm.attr;


import java.util.ArrayList;
import java.util.List;

import com.coderising.jvm.constant.ConstantPool;

import com.coderising.jvm.loader.ByteCodeIterator;

public class LocalVariableTable extends AttributeInfo {

List<LocalVariableItem> items = new ArrayList<LocalVariableItem>();

public LocalVariableTable(int attrNameIndex, int attrLen) {
super(attrNameIndex, attrLen);
}

public static LocalVariableTable parse(ByteCodeIterator iter,int attribute_name_index) {
int attribute_len = iter.nextU2ToInt();

byte[] bytes = iter.getBytes(attribute_len);
ByteCodeIterator lvtIter = new ByteCodeIterator(bytes);

LocalVariableTable table = new LocalVariableTable(attribute_name_index,attribute_len);

while(lvtIter.isNotEnd()){
LocalVariableItem item = new LocalVariableItem();
item.setStartPC(lvtIter.nextU2ToInt());
item.setLength(lvtIter.nextU2ToInt());
item.setNameIndex(lvtIter.nextU2ToInt());
item.setDescIndex(lvtIter.nextU2ToInt());
item.setIndex(lvtIter.nextU2ToInt());
table.addLocalVariableItem(item);
}


return table;
}

private void addLocalVariableItem(LocalVariableItem item) {
this.items.add(item);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.coderising.jvm.attr;


import com.coderising.jvm.loader.ByteCodeIterator;

public class StackMapTable extends AttributeInfo{

private String originalCode;

public StackMapTable(int attrNameIndex, int attrLen) {
super(attrNameIndex, attrLen);
}

public static StackMapTable parse(ByteCodeIterator iter){
int index = iter.nextU2ToInt();
int len = iter.nextU4ToInt();
StackMapTable t = new StackMapTable(index,len);

//后面的StackMapTable太过复杂, 不再处理, 只把原始的代码读进来保存
String code = iter.nextUxToHexString(len);
t.setOriginalCode(code);

return t;
}

private void setOriginalCode(String code) {
this.originalCode = code;

}
}
Loading

0 comments on commit ee07771

Please sign in to comment.