Skip to content

Commit

Permalink
Merge pull request #2 from Mr-ChangK/master
Browse files Browse the repository at this point in the history
week7
  • Loading branch information
vegetableDogBai authored Apr 18, 2017
2 parents 5044710 + ee07771 commit ad4ef46
Show file tree
Hide file tree
Showing 13 changed files with 562 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,27 @@ public class StackUtil {
*/
public static void reverse(Stack s) {
Stack tmp = new Stack();
Stack tmp1 = new Stack();
while(!s.isEmpty())
tmp.push(s.pop());
while(!tmp.isEmpty())
tmp1.push(tmp.pop());
while(!tmp1.isEmpty())
s.push(tmp1.pop());
// Stack tmp1 = new Stack();
// while(!s.isEmpty())
// tmp.push(s.pop());
// while(!tmp.isEmpty())
// tmp1.push(tmp.pop());
// while(!tmp1.isEmpty())
// s.push(tmp1.pop());
/**
* 改正
*/
int len = s.size();
for(int i=0;i<len;i++){
Integer top = (Integer)s.pop();
while(s.size() > i) {
tmp.push(s.pop());
}
s.push(top);
while(tmp.size() > 0){
s.push(tmp.pop());
}
}
}

/**
Expand Down Expand Up @@ -90,9 +104,7 @@ public static boolean isValidPairs(String s){
}
}
}
System.out.println(stack.size());
if(!stack.isEmpty()) return false;
return true;
return stack.size() == 0;
}


Expand Down
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;
}


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

import com.coderising.jvm.clz.ClassFile;
import com.coderising.jvm.constant.ConstantPool;
import com.coderising.jvm.loader.ByteCodeIterator;


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 attrNameIndex = iter.nextU2ToInt();
int attrLen = iter.nextU4ToInt();
int maxStack = iter.nextU2ToInt();
int maxLocals = iter.nextU2ToInt();
int codeLen = iter.nextU4ToInt();
String code = iter.nextUxToHexString(codeLen);
System.out.println(code);
CodeAttr attr = new CodeAttr(attrNameIndex, attrLen, maxStack, maxLocals, codeLen, code);
int expTableLen = iter.nextU2ToInt();
if(expTableLen > 0){
String expTable = iter.nextUxToHexString(expTableLen);
System.out.println("expTable: " + expTable);
// TODO 异常表处理
}
int subAttrCount = iter.nextU2ToInt();
ConstantPool pool = clzFile.getConstantPool();
for (int i = 1; i <= subAttrCount; i++) {
int subAttrNameIndex = iter.nextU2ToInt();
String subAttrName = pool.getUTF8String(subAttrNameIndex);
iter.back(2);
if(AttributeInfo.LINE_NUM_TABLE.equalsIgnoreCase(subAttrName)){
attr.setLineNumberTable(LineNumberTable.parse(iter));
}else if(AttributeInfo.LOCAL_VAR_TABLE.equalsIgnoreCase(subAttrName)){
attr.setLocalVariableTable(LocalVariableTable.parse(iter));
}else if(AttributeInfo.STACK_MAP_TABLE.equalsIgnoreCase(subAttrName)){
attr.setStackMapTable(StackMapTable.parse(iter));
}else{
//TODO
throw new RuntimeException("CodeAttr.parse not implement " + subAttrName);
}
}
return attr;
}
private void setStackMapTable(StackMapTable t) {
this.stackMapTable = t;

}





}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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 attrNameIndex = iter.nextU2ToInt();
int attrLen = iter.nextU4ToInt();
LineNumberTable table = new LineNumberTable(attrNameIndex, attrLen);
int lineNumberTableLen = iter.nextU2ToInt();
for(int i=0;i<lineNumberTableLen;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,40 @@
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 attrNameIndex = iter.nextU2ToInt();
int attrLen = iter.nextU4ToInt();
LocalVariableTable table = new LocalVariableTable(attrNameIndex, attrLen);
int localVarLen = iter.nextU2ToInt();
for(int i=0;i<localVarLen;i++){
LocalVariableItem item = new LocalVariableItem();
item.setStartPC(iter.nextU2ToInt());
item.setLength(iter.nextU2ToInt());
item.setNameIndex(iter.nextU2ToInt());
item.setDescIndex(iter.nextU2ToInt());
item.setIndex(iter.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;

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.coderising.jvm.clz;

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

import com.coderising.jvm.constant.ClassInfo;
import com.coderising.jvm.constant.ConstantPool;
import com.coderising.jvm.field.Field;
import com.coderising.jvm.method.Method;

public class ClassFile {

Expand All @@ -12,7 +17,25 @@ public class ClassFile {
private ClassIndex clzIndex;
private ConstantPool pool;

private List<Field> fields = new ArrayList<>();
private List<Method> methods = new ArrayList<>();


public ConstantPool getPool() {
return pool;
}
public void setPool(ConstantPool pool) {
this.pool = pool;
}
public List<Method> getMethods() {
return methods;
}
public void setMethods(List<Method> methods) {
this.methods = methods;
}
public void setClzIndex(ClassIndex clzIndex) {
this.clzIndex = clzIndex;
}
public ClassIndex getClzIndex() {
return clzIndex;
}
Expand Down Expand Up @@ -72,4 +95,10 @@ private String getSuperClassName(){
ClassInfo superClass = (ClassInfo)this.getConstantPool().getConstantInfo(this.clzIndex.getSuperClassIndex());
return superClass.getClassName();
}
public List<Field> getFields() {
return fields;
}
public void setFields(List<Field> fields) {
this.fields = fields;
}
}
Loading

0 comments on commit ad4ef46

Please sign in to comment.