Skip to content

Commit 23c8f56

Browse files
author
SJsunshine
committed
mini jvm之字段方法实现
1 parent b5d315d commit 23c8f56

File tree

82 files changed

+5436
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+5436
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.coderising.jvm.attr;
2+
3+
public abstract class AttributeInfo {
4+
public static final String CODE = "Code";
5+
public static final String CONST_VALUE = "ConstantValue";
6+
public static final String EXCEPTIONS = "Exceptions";
7+
public static final String LINE_NUM_TABLE = "LineNumberTable";
8+
public static final String LOCAL_VAR_TABLE = "LocalVariableTable";
9+
public static final String STACK_MAP_TABLE = "StackMapTable";
10+
int attrNameIndex;
11+
int attrLen ;
12+
public AttributeInfo(int attrNameIndex, int attrLen) {
13+
14+
this.attrNameIndex = attrNameIndex;
15+
this.attrLen = attrLen;
16+
}
17+
18+
19+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package com.coderising.jvm.attr;
2+
3+
import com.coderising.jvm.clz.ClassFile;
4+
import com.coderising.jvm.cmd.ByteCodeCommand;
5+
import com.coderising.jvm.cmd.CommandParser;
6+
import com.coderising.jvm.constant.ConstantPool;
7+
import com.coderising.jvm.loader.ByteCodeIterator;
8+
9+
10+
public class CodeAttr extends AttributeInfo {
11+
private int maxStack ;
12+
private int maxLocals ;
13+
private int codeLen ;
14+
private String code;
15+
public String getCode() {
16+
return code;
17+
}
18+
19+
private ByteCodeCommand[] cmds ;
20+
public ByteCodeCommand[] getCmds() {
21+
return cmds;
22+
}
23+
private LineNumberTable lineNumTable;
24+
private LocalVariableTable localVarTable;
25+
private StackMapTable stackMapTable;
26+
27+
public CodeAttr(int attrNameIndex, int attrLen, int maxStack, int maxLocals, int codeLen,String code ,ByteCodeCommand[] cmds) {
28+
super(attrNameIndex, attrLen);
29+
this.maxStack = maxStack;
30+
this.maxLocals = maxLocals;
31+
this.codeLen = codeLen;
32+
this.code = code;
33+
this.cmds = cmds;
34+
}
35+
36+
public void setLineNumberTable(LineNumberTable t) {
37+
this.lineNumTable = t;
38+
}
39+
40+
public void setLocalVariableTable(LocalVariableTable t) {
41+
this.localVarTable = t;
42+
}
43+
44+
public static CodeAttr parse(ClassFile clzFile, ByteCodeIterator iter){
45+
46+
int attrNameIndex = iter.nextU2ToInt();
47+
int attrLen = iter.nextU4ToInt();
48+
int maxStack = iter.nextU2ToInt();
49+
int maxLocals = iter.nextU2ToInt();
50+
int codeLen = iter.nextU4ToInt();
51+
52+
String code = iter.nextUxToHexString(codeLen);
53+
54+
55+
ByteCodeCommand[] cmds = CommandParser.parse(clzFile,code);
56+
57+
CodeAttr codeAttr = new CodeAttr(attrNameIndex,attrLen, maxStack,maxLocals,codeLen,code,cmds);
58+
59+
int exceptionTableLen = iter.nextU2ToInt();
60+
//TODO 处理exception
61+
if(exceptionTableLen>0){
62+
String exTable = iter.nextUxToHexString(exceptionTableLen);
63+
System.out.println("Encountered exception table , just ignore it :" + exTable);
64+
65+
}
66+
67+
68+
int subAttrCount = iter.nextU2ToInt();
69+
70+
for(int x=1; x<=subAttrCount; x++){
71+
int subAttrIndex = iter.nextU2ToInt();
72+
String subAttrName = clzFile.getConstantPool().getUTF8String(subAttrIndex);
73+
74+
//已经向前移动了U2, 现在退回去。
75+
iter.back(2);
76+
//line item table
77+
if(AttributeInfo.LINE_NUM_TABLE.equalsIgnoreCase(subAttrName)){
78+
79+
LineNumberTable t = LineNumberTable.parse(iter);
80+
codeAttr.setLineNumberTable(t);
81+
}
82+
else if(AttributeInfo.LOCAL_VAR_TABLE.equalsIgnoreCase(subAttrName)){
83+
LocalVariableTable t = LocalVariableTable.parse(iter);
84+
codeAttr.setLocalVariableTable(t);
85+
}
86+
else if (AttributeInfo.STACK_MAP_TABLE.equalsIgnoreCase(subAttrName)){
87+
StackMapTable t = StackMapTable.parse(iter);
88+
codeAttr.setStackMapTable(t);
89+
}
90+
else{
91+
throw new RuntimeException("Need code to process " + subAttrName);
92+
}
93+
94+
95+
}
96+
97+
return codeAttr;
98+
}
99+
100+
101+
public String toString(ConstantPool pool){
102+
StringBuilder buffer = new StringBuilder();
103+
//buffer.append("Code:").append(code).append("\n");
104+
for(int i=0;i<cmds.length;i++){
105+
buffer.append(cmds[i].toString()).append("\n");
106+
}
107+
buffer.append("\n");
108+
buffer.append(this.lineNumTable.toString());
109+
buffer.append(this.localVarTable.toString(pool));
110+
return buffer.toString();
111+
}
112+
private void setStackMapTable(StackMapTable t) {
113+
this.stackMapTable = t;
114+
115+
}
116+
117+
118+
119+
120+
121+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.coderising.jvm.attr;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
7+
import com.coderising.jvm.loader.ByteCodeIterator;
8+
9+
public class LineNumberTable extends AttributeInfo {
10+
List<LineNumberItem> items = new ArrayList<LineNumberItem>();
11+
12+
private static class LineNumberItem{
13+
int startPC;
14+
int lineNum;
15+
public int getStartPC() {
16+
return startPC;
17+
}
18+
public void setStartPC(int startPC) {
19+
this.startPC = startPC;
20+
}
21+
public int getLineNum() {
22+
return lineNum;
23+
}
24+
public void setLineNum(int lineNum) {
25+
this.lineNum = lineNum;
26+
}
27+
}
28+
public void addLineNumberItem(LineNumberItem item){
29+
this.items.add(item);
30+
}
31+
public LineNumberTable(int attrNameIndex, int attrLen) {
32+
super(attrNameIndex, attrLen);
33+
34+
}
35+
36+
public static LineNumberTable parse(ByteCodeIterator iter){
37+
38+
int index = iter.nextU2ToInt();
39+
int len = iter.nextU4ToInt();
40+
41+
LineNumberTable table = new LineNumberTable(index,len);
42+
43+
int itemLen = iter.nextU2ToInt();
44+
45+
for(int i=1; i<=itemLen; i++){
46+
LineNumberItem item = new LineNumberItem();
47+
item.setStartPC(iter.nextU2ToInt());
48+
item.setLineNum(iter.nextU2ToInt());
49+
table.addLineNumberItem(item);
50+
}
51+
return table;
52+
}
53+
54+
public String toString(){
55+
StringBuilder buffer = new StringBuilder();
56+
buffer.append("Line Number Table:\n");
57+
for(LineNumberItem item : items){
58+
buffer.append("startPC:"+item.getStartPC()).append(",");
59+
buffer.append("lineNum:"+item.getLineNum()).append("\n");
60+
}
61+
buffer.append("\n");
62+
return buffer.toString();
63+
64+
}
65+
66+
67+
68+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.coderising.jvm.attr;
2+
3+
public class LocalVariableItem {
4+
private int startPC;
5+
private int length;
6+
private int nameIndex;
7+
private int descIndex;
8+
private int index;
9+
public int getStartPC() {
10+
return startPC;
11+
}
12+
public void setStartPC(int startPC) {
13+
this.startPC = startPC;
14+
}
15+
public int getLength() {
16+
return length;
17+
}
18+
public void setLength(int length) {
19+
this.length = length;
20+
}
21+
public int getNameIndex() {
22+
return nameIndex;
23+
}
24+
public void setNameIndex(int nameIndex) {
25+
this.nameIndex = nameIndex;
26+
}
27+
public int getDescIndex() {
28+
return descIndex;
29+
}
30+
public void setDescIndex(int descIndex) {
31+
this.descIndex = descIndex;
32+
}
33+
public int getIndex() {
34+
return index;
35+
}
36+
public void setIndex(int index) {
37+
this.index = index;
38+
}
39+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.coderising.jvm.attr;
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import com.coderising.jvm.constant.ConstantPool;
8+
9+
import com.coderising.jvm.loader.ByteCodeIterator;
10+
11+
public class LocalVariableTable extends AttributeInfo{
12+
13+
List<LocalVariableItem> items = new ArrayList<LocalVariableItem>();
14+
15+
public LocalVariableTable(int attrNameIndex, int attrLen) {
16+
super(attrNameIndex, attrLen);
17+
}
18+
19+
20+
private void addLocalVariableItem(LocalVariableItem item) {
21+
this.items.add(item);
22+
}
23+
24+
public static LocalVariableTable parse(ByteCodeIterator iter){
25+
26+
int index = iter.nextU2ToInt();
27+
int len = iter.nextU4ToInt();
28+
29+
LocalVariableTable table = new LocalVariableTable(index,len);
30+
31+
int itemLen = iter.nextU2ToInt();
32+
33+
for(int i=1; i<=itemLen; i++){
34+
LocalVariableItem item = new LocalVariableItem();
35+
item.setStartPC(iter.nextU2ToInt());
36+
item.setLength(iter.nextU2ToInt());
37+
item.setNameIndex(iter.nextU2ToInt());
38+
item.setDescIndex(iter.nextU2ToInt());
39+
item.setIndex(iter.nextU2ToInt());
40+
table.addLocalVariableItem(item);
41+
}
42+
return table;
43+
}
44+
45+
46+
public String toString(ConstantPool pool){
47+
StringBuilder buffer = new StringBuilder();
48+
buffer.append("Local Variable Table:\n");
49+
for(LocalVariableItem item : items){
50+
buffer.append("startPC:"+item.getStartPC()).append(",");
51+
buffer.append("name:"+pool.getUTF8String(item.getNameIndex())).append(",");
52+
buffer.append("desc:"+pool.getUTF8String(item.getDescIndex())).append(",");
53+
buffer.append("slotIndex:"+ item.getIndex()).append("\n");
54+
}
55+
buffer.append("\n");
56+
return buffer.toString();
57+
}
58+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.coderising.jvm.attr;
2+
3+
4+
import com.coderising.jvm.loader.ByteCodeIterator;
5+
6+
public class StackMapTable extends AttributeInfo{
7+
8+
private String originalCode;
9+
10+
public StackMapTable(int attrNameIndex, int attrLen) {
11+
super(attrNameIndex, attrLen);
12+
}
13+
14+
public static StackMapTable parse(ByteCodeIterator iter){
15+
int index = iter.nextU2ToInt();
16+
int len = iter.nextU4ToInt();
17+
StackMapTable t = new StackMapTable(index,len);
18+
19+
//后面的StackMapTable太过复杂, 不再处理, 只把原始的代码读进来保存
20+
String code = iter.nextUxToHexString(len);
21+
t.setOriginalCode(code);
22+
23+
return t;
24+
}
25+
26+
private void setOriginalCode(String code) {
27+
this.originalCode = code;
28+
29+
}
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.coderising.jvm.clz;
2+
3+
public class AccessFlag {
4+
private int flagValue;
5+
6+
public AccessFlag(int value) {
7+
this.flagValue = value;
8+
}
9+
10+
public int getFlagValue() {
11+
return flagValue;
12+
}
13+
14+
public void setFlagValue(int flag) {
15+
this.flagValue = flag;
16+
}
17+
18+
public boolean isPublicClass(){
19+
return (this.flagValue & 0x0001) != 0;
20+
}
21+
public boolean isFinalClass(){
22+
return (this.flagValue & 0x0010) != 0;
23+
}
24+
25+
}

0 commit comments

Comments
 (0)