From f462045656cd5a61f7a7a4cb33c3e8705ee906ec Mon Sep 17 00:00:00 2001 From: onlyliuxin <14703250@qq.com> Date: Sun, 9 Apr 2017 21:30:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E7=AC=AC=E4=BA=8C=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/coderising/jvm/attr/CodeAttr.java | 65 +------------------ .../coderising/jvm/attr/LineNumberTable.java | 27 +------- .../jvm/attr/LocalVariableTable.java | 31 +-------- .../src/com/coderising/jvm/field/Field.java | 20 +----- .../jvm/loader/ClassFileParser.java | 27 +------- .../src/com/coderising/jvm/method/Method.java | 41 +----------- 6 files changed, 11 insertions(+), 200 deletions(-) diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/attr/CodeAttr.java b/liuxin/mini-jvm/src/com/coderising/jvm/attr/CodeAttr.java index f4cca587cb..b0c67b4b93 100644 --- a/liuxin/mini-jvm/src/com/coderising/jvm/attr/CodeAttr.java +++ b/liuxin/mini-jvm/src/com/coderising/jvm/attr/CodeAttr.java @@ -41,76 +41,15 @@ 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); - - //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.back(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; + return null; } private void setStackMapTable(StackMapTable t) { this.stackMapTable = t; } - public String toString(ConstantPool pool){ - StringBuilder buffer = new StringBuilder(); - buffer.append("Code:").append(code).append("\n"); - /*for(int i=0;i 0){ - throw new RuntimeException("Field Attribute has not been implemented"); - } - - return f; + return null; } } diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileParser.java b/liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileParser.java index 0b272510a5..444ec4c60d 100644 --- a/liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileParser.java +++ b/liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileParser.java @@ -45,10 +45,7 @@ public ClassFile parse(byte[] codes) { parseInterfaces(iter); - parseFileds(clzFile, iter); - - parseMethods(clzFile, iter); - + return clzFile; } @@ -149,26 +146,6 @@ private void parseInterfaces(ByteCodeIterator iter) { // TODO : 如果实现了interface, 这里需要解析 } - private void parseFileds(ClassFile clzFile, ByteCodeIterator iter) { - int fieldCount = iter.nextU2ToInt(); - System.out.println("Field count:" + fieldCount); - for (int i = 1; i <= fieldCount; i++) { - Field f = Field.parse(clzFile.getConstantPool(), iter); - clzFile.addField(f); - - } - - } - - private void parseMethods(ClassFile clzFile, ByteCodeIterator iter) { - - int methodCount = iter.nextU2ToInt(); - - for (int i = 1; i <= methodCount; i++) { - Method m = Method.parse(clzFile, iter); - clzFile.addMethod(m); - } - - } + } diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/method/Method.java b/liuxin/mini-jvm/src/com/coderising/jvm/method/Method.java index 4b30826abe..ba5f7848d1 100644 --- a/liuxin/mini-jvm/src/com/coderising/jvm/method/Method.java +++ b/liuxin/mini-jvm/src/com/coderising/jvm/method/Method.java @@ -48,47 +48,10 @@ public Method(ClassFile clzFile,int accessFlag, int nameIndex, int descriptorInd - public String toString() { - - ConstantPool pool = this.clzFile.getConstantPool(); - StringBuilder buffer = new StringBuilder(); - - String name = ((UTF8Info)pool.getConstantInfo(this.nameIndex)).getValue(); - - String desc = ((UTF8Info)pool.getConstantInfo(this.descriptorIndex)).getValue(); - - buffer.append(name).append(":").append(desc).append("\n"); - - buffer.append(this.codeAttr.toString(pool)); - - return buffer.toString(); - } + public static Method parse(ClassFile clzFile, ByteCodeIterator iter){ - int accessFlag = iter.nextU2ToInt(); - int nameIndex = iter.nextU2ToInt(); - int descIndex = iter.nextU2ToInt(); - int attribCount = iter.nextU2ToInt(); - - - Method m = new Method(clzFile, accessFlag, nameIndex, descIndex); - - for( int j=1; j<= attribCount; j++){ - - int attrNameIndex = iter.nextU2ToInt(); - String attrName = clzFile.getConstantPool().getUTF8String(attrNameIndex); - iter.back(2); - - if(AttributeInfo.CODE.equalsIgnoreCase(attrName)){ - CodeAttr codeAttr = CodeAttr.parse(clzFile, iter); - m.setCodeAttr(codeAttr); - } else{ - throw new RuntimeException("only CODE attribute is implemented , please implement the "+ attrName); - } - - } - //System.out.println("method:"+ m.toString(clzFile.getConstPool())); - return m ; + return null; } }