From b0847da7eb523bf373b08f446b6f88e3dbe98494 Mon Sep 17 00:00:00 2001 From: anxinJ <1028361767@qq.com> Date: Thu, 13 Apr 2017 10:48:40 +0800 Subject: [PATCH 1/2] =?UTF-8?q?StackUtil=E6=94=B9=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit StackUtil改正 --- .../src/com/coding/basic/stack/StackUtil.java | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/group23/1028361767/data-structure/src/com/coding/basic/stack/StackUtil.java b/group23/1028361767/data-structure/src/com/coding/basic/stack/StackUtil.java index bbcc7e05e2..53175a670f 100644 --- a/group23/1028361767/data-structure/src/com/coding/basic/stack/StackUtil.java +++ b/group23/1028361767/data-structure/src/com/coding/basic/stack/StackUtil.java @@ -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 i) { + tmp.push(s.pop()); + } + s.push(top); + while(tmp.size() > 0){ + s.push(tmp.pop()); + } + } } /** @@ -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; } From 65070ef6414dabe3da9cdc971a79fd350387847a Mon Sep 17 00:00:00 2001 From: anxinJ <1028361767@qq.com> Date: Mon, 17 Apr 2017 09:16:31 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=AC=A1jvm=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 第三次jvm作业 --- .../coderising/jvm/attr/AttributeInfo.java | 19 +++++ .../src/com/coderising/jvm/attr/CodeAttr.java | 85 +++++++++++++++++++ .../coderising/jvm/attr/LineNumberTable.java | 51 +++++++++++ .../jvm/attr/LocalVariableItem.java | 39 +++++++++ .../jvm/attr/LocalVariableTable.java | 40 +++++++++ .../coderising/jvm/attr/StackMapTable.java | 30 +++++++ .../src/com/coderising/jvm/clz/ClassFile.java | 29 +++++++ .../src/com/coderising/jvm/field/Field.java | 45 ++++++++++ .../jvm/loader/ByteCodeIterator.java | 13 ++- .../jvm/loader/ClassFileParser.java | 38 ++++++++- .../src/com/coderising/jvm/method/Method.java | 79 +++++++++++++++++ .../jvm/test/ClassFileloaderTest.java | 74 ++++++++++++++++ 12 files changed, 540 insertions(+), 2 deletions(-) create mode 100644 group23/1028361767/mini-jvm/src/com/coderising/jvm/attr/AttributeInfo.java create mode 100644 group23/1028361767/mini-jvm/src/com/coderising/jvm/attr/CodeAttr.java create mode 100644 group23/1028361767/mini-jvm/src/com/coderising/jvm/attr/LineNumberTable.java create mode 100644 group23/1028361767/mini-jvm/src/com/coderising/jvm/attr/LocalVariableItem.java create mode 100644 group23/1028361767/mini-jvm/src/com/coderising/jvm/attr/LocalVariableTable.java create mode 100644 group23/1028361767/mini-jvm/src/com/coderising/jvm/attr/StackMapTable.java create mode 100644 group23/1028361767/mini-jvm/src/com/coderising/jvm/field/Field.java create mode 100644 group23/1028361767/mini-jvm/src/com/coderising/jvm/method/Method.java diff --git a/group23/1028361767/mini-jvm/src/com/coderising/jvm/attr/AttributeInfo.java b/group23/1028361767/mini-jvm/src/com/coderising/jvm/attr/AttributeInfo.java new file mode 100644 index 0000000000..88f60c77f6 --- /dev/null +++ b/group23/1028361767/mini-jvm/src/com/coderising/jvm/attr/AttributeInfo.java @@ -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; + } + + +} diff --git a/group23/1028361767/mini-jvm/src/com/coderising/jvm/attr/CodeAttr.java b/group23/1028361767/mini-jvm/src/com/coderising/jvm/attr/CodeAttr.java new file mode 100644 index 0000000000..56c3bf2b0c --- /dev/null +++ b/group23/1028361767/mini-jvm/src/com/coderising/jvm/attr/CodeAttr.java @@ -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; + + } + + + + + +} diff --git a/group23/1028361767/mini-jvm/src/com/coderising/jvm/attr/LineNumberTable.java b/group23/1028361767/mini-jvm/src/com/coderising/jvm/attr/LineNumberTable.java new file mode 100644 index 0000000000..f83202d6c3 --- /dev/null +++ b/group23/1028361767/mini-jvm/src/com/coderising/jvm/attr/LineNumberTable.java @@ -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 items = new ArrayList(); + + 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 items = new ArrayList(); + + 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 fields = new ArrayList<>(); + private List methods = new ArrayList<>(); + + public ConstantPool getPool() { + return pool; + } + public void setPool(ConstantPool pool) { + this.pool = pool; + } + public List getMethods() { + return methods; + } + public void setMethods(List methods) { + this.methods = methods; + } + public void setClzIndex(ClassIndex clzIndex) { + this.clzIndex = clzIndex; + } public ClassIndex getClzIndex() { return clzIndex; } @@ -72,4 +95,10 @@ private String getSuperClassName(){ ClassInfo superClass = (ClassInfo)this.getConstantPool().getConstantInfo(this.clzIndex.getSuperClassIndex()); return superClass.getClassName(); } + public List getFields() { + return fields; + } + public void setFields(List fields) { + this.fields = fields; + } } diff --git a/group23/1028361767/mini-jvm/src/com/coderising/jvm/field/Field.java b/group23/1028361767/mini-jvm/src/com/coderising/jvm/field/Field.java new file mode 100644 index 0000000000..01a009331c --- /dev/null +++ b/group23/1028361767/mini-jvm/src/com/coderising/jvm/field/Field.java @@ -0,0 +1,45 @@ +package com.coderising.jvm.field; + +import com.coderising.jvm.constant.ConstantPool; +import com.coderising.jvm.constant.UTF8Info; +import com.coderising.jvm.loader.ByteCodeIterator; + + +public class Field { + private int accessFlag; + private int nameIndex; + private int descriptorIndex; + + + + private ConstantPool pool; + + public Field( int accessFlag, int nameIndex, int descriptorIndex,ConstantPool pool) { + + this.accessFlag = accessFlag; + this.nameIndex = nameIndex; + this.descriptorIndex = descriptorIndex; + this.pool = pool; + } + + + + + public static Field parse(ConstantPool pool,ByteCodeIterator iter){ + int accessFlag = iter.nextU2ToInt(); + int nameIndex = iter.nextU2ToInt(); + int descriptorIndex = iter.nextU2ToInt(); + Field field = new Field(accessFlag, nameIndex, descriptorIndex, pool); + int attCount = iter.nextU2ToInt(); + if(attCount > 0){ + //TODO handle field attrs + throw new RuntimeException("Field.attr not implements"); + } + return field; + } + + public String toString() { + return pool.getUTF8String(nameIndex) + ":" + pool.getUTF8String(descriptorIndex); + } + +} diff --git a/group23/1028361767/mini-jvm/src/com/coderising/jvm/loader/ByteCodeIterator.java b/group23/1028361767/mini-jvm/src/com/coderising/jvm/loader/ByteCodeIterator.java index 0fab88f9cb..c0c4598800 100644 --- a/group23/1028361767/mini-jvm/src/com/coderising/jvm/loader/ByteCodeIterator.java +++ b/group23/1028361767/mini-jvm/src/com/coderising/jvm/loader/ByteCodeIterator.java @@ -1,5 +1,7 @@ package com.coderising.jvm.loader; +import java.io.UnsupportedEncodingException; + public class ByteCodeIterator { private byte[] codes; @@ -45,7 +47,16 @@ public String nextUxToString(int len){ for(int i=0;i parseMethod(ByteCodeIterator iter, ClassFile classFile) { + List methods = new ArrayList<>(); + int mthodsCount = iter.nextU2ToInt(); + for(int i=0;i prarseField(ByteCodeIterator iter, ConstantPool pool) { + List fields = new ArrayList<>(); + int fieldsCount = iter.nextU2ToInt(); + for(int i=0;i fields = clzFile.getFields(); + Assert.assertEquals(2, fields.size()); + { + Field f = fields.get(0); + Assert.assertEquals("name:Ljava/lang/String;", f.toString()); + } + { + Field f = fields.get(1); + Assert.assertEquals("age:I", f.toString()); + } + } + @Test + public void testMethods(){ + + List methods = clzFile.getMethods(); + ConstantPool pool = clzFile.getConstantPool(); + + { + Method m = methods.get(0); + assertMethodEquals(pool,m, + "", + "(Ljava/lang/String;I)V", + "2ab7000c2a2bb5000f2a1cb50011b1"); + + } + { + Method m = methods.get(1); + assertMethodEquals(pool,m, + "setName", + "(Ljava/lang/String;)V", + "2a2bb5000fb1"); + + } + { + Method m = methods.get(2); + assertMethodEquals(pool,m, + "setAge", + "(I)V", + "2a1bb50011b1"); + } + { + Method m = methods.get(3); + assertMethodEquals(pool,m, + "sayHello", + "()V", + "b2001c1222b60024b1"); + + } + { + Method m = methods.get(4); + assertMethodEquals(pool,m, + "main", + "([Ljava/lang/String;)V", + "bb000159122b101db7002d4c2bb6002fb1"); + } + } + + private void assertMethodEquals(ConstantPool pool,Method m , String expectedName, String expectedDesc,String expectedCode){ + String methodName = pool.getUTF8String(m.getNameIndex()); + String methodDesc = pool.getUTF8String(m.getDescriptorIndex()); + String code = m.getCodeAttr().getCode(); + Assert.assertEquals(expectedName, methodName); + Assert.assertEquals(expectedDesc, methodDesc); + Assert.assertEquals(expectedCode, code); + } }