forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f3cecb
commit 51a57fb
Showing
12 changed files
with
422 additions
and
25 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...401/src/main/java/com/github/miniyk2012/coding2017/coderising/jvm/attr/AttributeInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.github.miniyk2012.coding2017.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; | ||
} | ||
|
||
|
||
} |
120 changes: 120 additions & 0 deletions
120
...12350401/src/main/java/com/github/miniyk2012/coding2017/coderising/jvm/attr/CodeAttr.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package com.github.miniyk2012.coding2017.coderising.jvm.attr; | ||
|
||
|
||
import com.github.miniyk2012.coding2017.coderising.jvm.clz.ClassFile; | ||
import com.github.miniyk2012.coding2017.coderising.jvm.constant.ConstantPool; | ||
import com.github.miniyk2012.coding2017.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); | ||
|
||
//ByteCodeCommand[] cmds = ByteCodeCommand.parse(clzFile,code); | ||
|
||
CodeAttr codeAttr = new CodeAttr(attrNameIndex,attrLen, maxStack,maxLocals,codeLen,code); | ||
|
||
int exceptionTableLen = iter.nextU2toInt(); | ||
//TODO 处理exception | ||
if(exceptionTableLen>0){ | ||
String exTable = iter.nextUxToHexString(exceptionTableLen); | ||
System.out.println("Encountered exception table , just ignore it :" + exTable); | ||
|
||
} | ||
|
||
|
||
int subAttrCount = iter.nextU2toInt(); | ||
|
||
for(int x=1; x<=subAttrCount; x++){ | ||
int subAttrIndex = iter.nextU2toInt(); | ||
String subAttrName = clzFile.getConstantPool().getUTF8String(subAttrIndex); | ||
|
||
//已经向前移动了U2, 现在退回去。 | ||
iter.skip(-2); | ||
//line item table | ||
if(AttributeInfo.LINE_NUM_TABLE.equalsIgnoreCase(subAttrName)){ | ||
|
||
LineNumberTable t = LineNumberTable.parse(iter); | ||
codeAttr.setLineNumberTable(t); | ||
} | ||
else if(AttributeInfo.LOCAL_VAR_TABLE.equalsIgnoreCase(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 code to process " + subAttrName); | ||
} | ||
|
||
|
||
} | ||
|
||
return codeAttr; | ||
} | ||
|
||
|
||
public String toString(ConstantPool pool){ | ||
StringBuilder buffer = new StringBuilder(); | ||
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("\n"); | ||
buffer.append(this.lineNumTable.toString()); | ||
buffer.append(this.localVarTable.toString(pool)); | ||
return buffer.toString(); | ||
} | ||
private void setStackMapTable(StackMapTable t) { | ||
this.stackMapTable = t; | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
} |
68 changes: 68 additions & 0 deletions
68
...1/src/main/java/com/github/miniyk2012/coding2017/coderising/jvm/attr/LineNumberTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.github.miniyk2012.coding2017.coderising.jvm.attr; | ||
|
||
import com.github.miniyk2012.coding2017.coderising.jvm.loader.ByteCodeIterator; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
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 index = iter.nextU2toInt(); | ||
int len = iter.nextU4toInt(); | ||
|
||
LineNumberTable table = new LineNumberTable(index,len); | ||
|
||
int itemLen = iter.nextU2toInt(); | ||
|
||
for(int i=1; i<=itemLen; 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(); | ||
|
||
} | ||
|
||
|
||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...src/main/java/com/github/miniyk2012/coding2017/coderising/jvm/attr/LocalVariableItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.github.miniyk2012.coding2017.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; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...rc/main/java/com/github/miniyk2012/coding2017/coderising/jvm/attr/LocalVariableTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.github.miniyk2012.coding2017.coderising.jvm.attr; | ||
|
||
|
||
import com.github.miniyk2012.coding2017.coderising.jvm.constant.ConstantPool; | ||
import com.github.miniyk2012.coding2017.coderising.jvm.loader.ByteCodeIterator; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LocalVariableTable extends AttributeInfo{ | ||
|
||
List<LocalVariableItem> items = new ArrayList<LocalVariableItem>(); | ||
|
||
public LocalVariableTable(int attrNameIndex, int attrLen) { | ||
super(attrNameIndex, attrLen); | ||
} | ||
|
||
|
||
private void addLocalVariableItem(LocalVariableItem item) { | ||
this.items.add(item); | ||
} | ||
|
||
public static LocalVariableTable parse(ByteCodeIterator iter){ | ||
|
||
int index = iter.nextU2toInt(); | ||
int len = iter.nextU4toInt(); | ||
|
||
LocalVariableTable table = new LocalVariableTable(index,len); | ||
|
||
int itemLen = iter.nextU2toInt(); | ||
|
||
for(int i=1; i<=itemLen; 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; | ||
} | ||
|
||
|
||
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(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...401/src/main/java/com/github/miniyk2012/coding2017/coderising/jvm/attr/StackMapTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.github.miniyk2012.coding2017.coderising.jvm.attr; | ||
|
||
|
||
import com.github.miniyk2012.coding2017.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; | ||
|
||
} | ||
} |
5 changes: 3 additions & 2 deletions
5
...12350401/src/main/java/com/github/miniyk2012/coding2017/coderising/jvm/clz/ClassFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.