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
1 change: 1 addition & 0 deletions group02/727171008/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="F:/jdom-2.0.6/jdom-2.0.6.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.HarryHook.coding2017.jvm.attr;

import com.github.HarryHook.coding2017.jvm.clz.ClassFile;
import com.github.HarryHook.coding2017.jvm.cmd.ByteCodeCommand;
import com.github.HarryHook.coding2017.jvm.cmd.CommandParser;
import com.github.HarryHook.coding2017.jvm.constant.ConstantPool;
import com.github.HarryHook.coding2017.jvm.loader.ByteCodeIterator;
import com.sun.org.apache.bcel.internal.generic.NEW;
Expand All @@ -15,22 +17,24 @@ public String getCode() {
return code;
}

// private ByteCodeCommand[] cmds ;
// public ByteCodeCommand[] getCmds() {
// return cmds;
// }
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 */) {
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;
this.cmds = cmds;
}

public void setLineNumberTable(LineNumberTable t) {
Expand All @@ -42,40 +46,44 @@ public void setLocalVariableTable(LocalVariableTable 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 codeAttr = new CodeAttr(attrNameIndex, attrLen, maxStack, maxLocals, codeLen, code);


ByteCodeCommand[] cmds = CommandParser.parse(clzFile, code);
CodeAttr codeAttr = new CodeAttr(attrNameIndex, attrLen, maxStack, maxLocals, codeLen, code, cmds);

int exceptionLength = iter.nextU2ToInt();
if(exceptionLength > 0) {
if (exceptionLength > 0) {
String exceptionTable = iter.nextUxToHexString(exceptionLength);
System.out.println("exception Table has not complemented" + exceptionTable);
}
//解析子属性
// 解析子属性
int subAttrCount = iter.nextU2ToInt();
for(int j=1; j<=subAttrCount; j++) {

for (int j = 1; j <= subAttrCount; j++) {

int subAttrIndex = iter.nextU2ToInt();
String subAttrName = clzFile.getConstantPool().getUTF8String(subAttrIndex);
iter.back(2);
if(AttributeInfo.LINE_NUM_TABLE.equalsIgnoreCase(subAttrName)) {

if (AttributeInfo.LINE_NUM_TABLE.equalsIgnoreCase(subAttrName)) {
LineNumberTable t = LineNumberTable.parse(iter);
codeAttr.setLineNumberTable(t);
} else if(AttributeInfo.LOCAL_VAR_TABLE.equals(subAttrName)) {

} else if (AttributeInfo.LOCAL_VAR_TABLE.equals(subAttrName)) {
LocalVariableTable t = LocalVariableTable.parse(iter);
codeAttr.setLocalVariableTable(t);


} else if (AttributeInfo.STACK_MAP_TABLE.equalsIgnoreCase(subAttrName)) {
StackMapTable t = StackMapTable.parse(iter);
codeAttr.setStackMapTable(t);
} else {
throw new RuntimeException("Need implement" + subAttrName);
}
Expand All @@ -87,14 +95,17 @@ private void setStackMapTable(StackMapTable t) {
this.stackMapTable = t;

}

public String toString(ConstantPool pool) {
StringBuffer buffer = new StringBuffer();
buffer.append("Code:").append(code).append("\n");
for(int i=0;i<cmds.length;i++){
buffer.append(cmds[i].toString(pool)).append("\n");
}
//buffer.append("Code:").append(code).append("\n");
buffer.append("\n");
buffer.append(this.lineNumTable.toString());
//buffer.append(this.localVarTable.toString(pool));
buffer.append(this.localVarTable.toString(pool));
return buffer.toString();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,27 @@ public static LineNumberTable parse(ByteCodeIterator iter) {
int attrLength = iter.nextU4ToInt();
LineNumberTable table = new LineNumberTable(attrNameIndex, attrLength);
int itemLength = iter.nextU2ToInt();
for(int i=1; i<=itemLength; i++) {

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

return table;
}

public String toString() {
StringBuilder buffer = new StringBuilder();
buffer.append("Line Number Table:\n");
for (LineNumberItem item : items) {
buffer.append("startPC:" + item.getStartPC()).append(",");
buffer.append("lineNum:" + item.getLineNum()).append("\n");
}
buffer.append("\n");
return buffer.toString();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,16 @@ public static LocalVariableTable parse(ByteCodeIterator iter) {
private void addLocalVariableItem(LocalVariableItem item) {
this.items.add(item);
}

public String toString(ConstantPool pool){
StringBuilder buffer = new StringBuilder();
buffer.append("Local Variable Table:\n");
for(LocalVariableItem item : items){
buffer.append("startPC:"+item.getStartPC()).append(",");
buffer.append("name:"+pool.getUTF8String(item.getNameIndex())).append(",");
buffer.append("desc:"+pool.getUTF8String(item.getDescIndex())).append(",");
buffer.append("slotIndex:"+ item.getIndex()).append("\n");
}
buffer.append("\n");
return buffer.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.HarryHook.coding2017.jvm.clz;


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

Expand All @@ -18,14 +17,16 @@ public class ClassFile {
private ClassIndex clzIndex;
private ConstantPool pool;
private List<Field> fields = new ArrayList<Field>();
private List<Method> methods = new ArrayList<Method>();;
private List<Method> methods = new ArrayList<Method>();

public ClassIndex getClzIndex() {
return clzIndex;
}

public void setClzIndex(ClassIndex clzIndex) {
this.clzIndex = clzIndex;
}

public AccessFlag getAccessFlag() {
return accessFlag;
}
Expand Down Expand Up @@ -72,32 +73,61 @@ public void print() {

}

private String getClassName() {
public String getClassName() {
int thisClassIndex = clzIndex.getThisClassIndex();
ClassInfo thisClass = (ClassInfo) this.getConstantPool().getConstantInfo(thisClassIndex);
return thisClass.getClassName();
}

private String getSuperClassName() {
public String getSuperClassName() {
ClassInfo superClass = (ClassInfo) this.getConstantPool().getConstantInfo(this.clzIndex.getSuperClassIndex());
return superClass.getClassName();
}

public void addField(Field f) {
fields.add(f);
}

public List<Field> getFields() {
return fields;
}

public void addMethod(Method m) {
methods.add(m);

}

public List<Method> getMethods() {
return methods;
}

public Method getMethod(String methodName, String paramAndReturnType) {

for (Method m : methods) {

int nameIndex = m.getNameIndex();
int descriptionIndex = m.getDescriptorIndex();

String name = this.getConstantPool().getUTF8String(nameIndex);
String desc = this.getConstantPool().getUTF8String(descriptionIndex);
if (name.equals(methodName) && desc.equals(paramAndReturnType)) {
return m;
}
}
return null;
}

public Method getMainMethod() {
for (Method m : methods) {
int nameIndex = m.getNameIndex();
int descIndex = m.getDescriptorIndex();
String name = this.getConstantPool().getUTF8String(nameIndex);
String desc = this.getConstantPool().getUTF8String(descIndex);
if (name.equals("main") && desc.equals("([Ljava/lang/String;)V")) {
return m;
}
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.HarryHook.coding2017.jvm.cmd;

import com.github.HarryHook.coding2017.jvm.clz.ClassFile;
import com.github.HarryHook.coding2017.jvm.constant.ConstantInfo;
import com.github.HarryHook.coding2017.jvm.constant.ConstantPool;


public class BiPushCmd extends OneOperandCmd {

public BiPushCmd(ClassFile clzFile,String opCode) {
super(clzFile,opCode);

}

@Override
public String toString(ConstantPool pool) {

return this.getOffset()+": "+ this.getOpCode()+" " + this.getReadableCodeText() + " " + this.getOperand();
}



}
Loading