diff --git a/group06/263050006/article.txt b/group06/263050006/article.txt index 2eaa5b792f..f6a5e2ef49 100644 --- a/group06/263050006/article.txt +++ b/group06/263050006/article.txt @@ -1,2 +1,12 @@ -CPU、内存、硬盘、指令以及他们之间的关系 -https://zhuanlan.zhihu.com/p/25442061 \ No newline at end of file +第一次作业博客: +CPU、内存、硬盘、指令以及他们之间的关系 +https://zhuanlan.zhihu.com/p/25442061 +第二次作业博客: +为什么32位处理器只能配4G内存 +https://zhuanlan.zhihu.com/p/26174778 +第三次作业博客: +报文和帧 +https://zhuanlan.zhihu.com/p/26175504 +第四次作业博客: +依赖注入通俗解释 +https://zhuanlan.zhihu.com/p/26176550 diff --git a/group06/263050006/pom.xml b/group06/263050006/pom.xml new file mode 100644 index 0000000000..91c21f2b50 --- /dev/null +++ b/group06/263050006/pom.xml @@ -0,0 +1,63 @@ + + 4.0.0 + com.github.chaoswang + coding2017 + 0.0.1-SNAPSHOT + coding2017 + coding2017 + + coding2017/group06/263050006/src/main/java + coding2017/group06/263050006/src/test/java + + + coding2017/group06/263050006/src/main/java + + **/*.java + + + + + + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + + + + + + + + + junit + junit + 4.12 + test + + + + + org.apache.commons + commons-lang3 + 3.5 + + + + + commons-io + commons-io + 2.5 + + + + + jdom + jdom + 1.1 + + + + + \ No newline at end of file diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/impl/ConnectionImpl.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/impl/ConnectionImpl.java index d3cd269ee1..ee9cd68c56 100644 --- a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/impl/ConnectionImpl.java +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/downloader/impl/ConnectionImpl.java @@ -40,7 +40,8 @@ public byte[] read(int startPos, int endPos) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len = 0; int hasRead = 0; - while((len < currentSectionLength) && ((hasRead = is.read(buffer)) != -1)) { + while((len < currentSectionLength) && ((hasRead = is.read(buffer)) != -1)) { + System.out.println(buffer.length); bos.write(buffer, 0, hasRead); len += hasRead; } diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/ClassFileLoader.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/ClassFileLoader.java deleted file mode 100644 index 1b0b8f1514..0000000000 --- a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/ClassFileLoader.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.github.chaoswang.learning.java.jvm; - -import java.util.ArrayList; -import java.util.List; - -public class ClassFileLoader { - - private List clzPaths = new ArrayList(); - - public byte[] readBinaryCode(String className) { - - return null; - - } - - public void addClassPath(String path) { - - } - - public String getClassPath() { - return null; - } - -} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/attr/AttributeInfo.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/attr/AttributeInfo.java new file mode 100644 index 0000000000..737a9d83db --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/attr/AttributeInfo.java @@ -0,0 +1,19 @@ +package com.github.chaoswang.learning.java.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/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/attr/CodeAttr.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/attr/CodeAttr.java new file mode 100644 index 0000000000..f7157b9d3c --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/attr/CodeAttr.java @@ -0,0 +1,50 @@ +package com.github.chaoswang.learning.java.jvm.attr; + +import com.github.chaoswang.learning.java.jvm.clz.ClassFile; +import com.github.chaoswang.learning.java.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){ + + + return null; + } + private void setStackMapTable(StackMapTable t) { + this.stackMapTable = t; + + } + +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/attr/LineNumberTable.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/attr/LineNumberTable.java new file mode 100644 index 0000000000..8f52a6feac --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/attr/LineNumberTable.java @@ -0,0 +1,45 @@ +package com.github.chaoswang.learning.java.jvm.attr; + +import java.util.ArrayList; +import java.util.List; + +import com.github.chaoswang.learning.java.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) { + + return null; + } +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/attr/LocalVariableItem.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/attr/LocalVariableItem.java new file mode 100644 index 0000000000..4c4443a4d1 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/attr/LocalVariableItem.java @@ -0,0 +1,49 @@ +package com.github.chaoswang.learning.java.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; + } +} \ No newline at end of file diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/attr/LocalVariableTable.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/attr/LocalVariableTable.java new file mode 100644 index 0000000000..c56aee7f26 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/attr/LocalVariableTable.java @@ -0,0 +1,23 @@ +package com.github.chaoswang.learning.java.jvm.attr; + +import java.util.ArrayList; +import java.util.List; + +import com.github.chaoswang.learning.java.jvm.loader.ByteCodeIterator; + +public class LocalVariableTable extends AttributeInfo{ + + List items = new ArrayList(); + + public LocalVariableTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + } + + public static LocalVariableTable parse(ByteCodeIterator iter){ + + return null; + } + private void addLocalVariableItem(LocalVariableItem item) { + this.items.add(item); + } +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/attr/StackMapTable.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/attr/StackMapTable.java new file mode 100644 index 0000000000..3cb32df88b --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/attr/StackMapTable.java @@ -0,0 +1,29 @@ +package com.github.chaoswang.learning.java.jvm.attr; + +import com.github.chaoswang.learning.java.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; + + } +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/clz/AccessFlag.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/clz/AccessFlag.java new file mode 100644 index 0000000000..02c11cc5d7 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/clz/AccessFlag.java @@ -0,0 +1,26 @@ +package com.github.chaoswang.learning.java.jvm.clz; + +public class AccessFlag { + private int flagValue; + + public AccessFlag(int value) { + this.flagValue = value; + } + + public int getFlagValue() { + return flagValue; + } + + public void setFlagValue(int flag) { + this.flagValue = flag; + } + + public boolean isPublicClass() { + return (this.flagValue & 0x0001) != 0; + } + + public boolean isFinalClass() { + return (this.flagValue & 0x0010) != 0; + } + +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/clz/ClassFile.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/clz/ClassFile.java new file mode 100644 index 0000000000..b6c9a423b7 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/clz/ClassFile.java @@ -0,0 +1,114 @@ +package com.github.chaoswang.learning.java.jvm.clz; + +import java.util.ArrayList; +import java.util.List; + +import com.github.chaoswang.learning.java.jvm.constant.ClassInfo; +import com.github.chaoswang.learning.java.jvm.constant.ConstantPool; +import com.github.chaoswang.learning.java.jvm.field.Field; +import com.github.chaoswang.learning.java.jvm.method.Method; + +public class ClassFile { + + //ClassļĴΰ汾 + private int minorVersion; + //Classļ汾,Java SE 6.0Ӧ֧Java SE 5.0ıClassļṹ֮ + private int majorVersion; + + /** + * ClassļгҪ洢 ԼãҪַ + * finalֵijԵijʼֵȵȣҪ洢ͽӿڵȫ޶ƣ + * ֶεԼ Լ + * CONSTANT_Utf8_info,CONSTANT_Float_info,CONSTANT_Double_info + */ + private ConstantPool pool; + + //ʾ߽ӿڵķϢ + private AccessFlag accessFlag; + //ij + private ClassIndex clzIndex; + + //ֶαϢ + private List fields = new ArrayList(); + // + private List methods = new ArrayList(); + + public ClassIndex getClzIndex() { + return clzIndex; + } + + public AccessFlag getAccessFlag() { + return accessFlag; + } + + public void setAccessFlag(AccessFlag accessFlag) { + this.accessFlag = accessFlag; + } + + public ConstantPool getConstantPool() { + return pool; + } + + public int getMinorVersion() { + return minorVersion; + } + + public void setMinorVersion(int minorVersion) { + this.minorVersion = minorVersion; + } + + public int getMajorVersion() { + return majorVersion; + } + + public void setMajorVersion(int majorVersion) { + this.majorVersion = majorVersion; + } + + public void setConstPool(ConstantPool pool) { + this.pool = pool; + + } + + public void setClassIndex(ClassIndex clzIndex) { + this.clzIndex = clzIndex; + } + + public void addField(Field f) { + this.fields.add(f); + } + + public List getFields() { + return this.fields; + } + + public void addMethod(Method m) { + this.methods.add(m); + } + + public List getMethods() { + return methods; + } + + public void print() { + +// if (this.accessFlag.isPublicClass()) { +// System.out.println("Access flag : public "); +// } +// System.out.println("Class Name:" + getClassName()); +// +// System.out.println("Super Class Name:" + getSuperClassName()); + + } + + private String getClassName() { + int thisClassIndex = this.clzIndex.getThisClassIndex(); + ClassInfo thisClass = (ClassInfo) this.getConstantPool().getConstantInfo(thisClassIndex); + return thisClass.getClassName(); + } + + private String getSuperClassName() { + ClassInfo superClass = (ClassInfo) this.getConstantPool().getConstantInfo(this.clzIndex.getSuperClassIndex()); + return superClass.getClassName(); + } +} \ No newline at end of file diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/clz/ClassIndex.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/clz/ClassIndex.java new file mode 100644 index 0000000000..572db1f6d8 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/clz/ClassIndex.java @@ -0,0 +1,22 @@ +package com.github.chaoswang.learning.java.jvm.clz; + +public class ClassIndex { + private int thisClassIndex; + private int superClassIndex; + + public int getThisClassIndex() { + return thisClassIndex; + } + + public void setThisClassIndex(int thisClassIndex) { + this.thisClassIndex = thisClassIndex; + } + + public int getSuperClassIndex() { + return superClassIndex; + } + + public void setSuperClassIndex(int superClassIndex) { + this.superClassIndex = superClassIndex; + } +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/ClassInfo.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/ClassInfo.java new file mode 100644 index 0000000000..f30e75f5df --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/ClassInfo.java @@ -0,0 +1,34 @@ +package com.github.chaoswang.learning.java.jvm.constant; +/** + * CONSTANT_Class_info { + * u1 tag; + * u2 name_index; + * } + */ +public class ClassInfo extends ConstantInfo { + + private int type = ConstantInfo.CLASS_INFO; + private int utf8Index; + + public ClassInfo(ConstantPool pool) { + super(pool); + } + + public int getUtf8Index() { + return utf8Index; + } + + public void setUtf8Index(int utf8Index) { + this.utf8Index = utf8Index; + } + + public int getType() { + return type; + } + + public String getClassName() { + int index = getUtf8Index(); + UTF8Info utf8Info = (UTF8Info) constantPool.getConstantInfo(index); + return utf8Info.getValue(); + } +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/ConstantInfo.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/ConstantInfo.java new file mode 100644 index 0000000000..1884cae795 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/ConstantInfo.java @@ -0,0 +1,32 @@ +package com.github.chaoswang.learning.java.jvm.constant; + +public abstract class ConstantInfo { + public static final int UTF8_INFO = 1; + public static final int FLOAT_INFO = 4; + public static final int CLASS_INFO = 7; + public static final int STRING_INFO = 8; + public static final int FIELD_INFO = 9; + public static final int METHOD_INFO = 10; + public static final int NAME_AND_TYPE_INFO = 12; + + protected ConstantPool constantPool; + + public ConstantInfo() { + + } + + public ConstantInfo(ConstantPool pool) { + this.constantPool = pool; + } + + public abstract int getType(); + + public ConstantPool getConstantPool() { + return constantPool; + } + + public ConstantInfo getConstantInfo(int index) { + return this.constantPool.getConstantInfo(index); + } + +} \ No newline at end of file diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/ConstantPool.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/ConstantPool.java new file mode 100644 index 0000000000..033b2de3da --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/ConstantPool.java @@ -0,0 +1,31 @@ +package com.github.chaoswang.learning.java.jvm.constant; + +import java.util.ArrayList; +import java.util.List; + +public class ConstantPool { + + private List constantInfos = new ArrayList(); + + public ConstantPool() { + + } + + public void addConstantInfo(ConstantInfo info) { + + this.constantInfos.add(info); + + } + + public ConstantInfo getConstantInfo(int index) { + return this.constantInfos.get(index); + } + + public String getUTF8String(int index) { + return ((UTF8Info) this.constantInfos.get(index)).getValue(); + } + + public Object getSize() { + return this.constantInfos.size() - 1; + } +} \ No newline at end of file diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/FieldRefInfo.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/FieldRefInfo.java new file mode 100644 index 0000000000..762f678545 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/FieldRefInfo.java @@ -0,0 +1,61 @@ +package com.github.chaoswang.learning.java.jvm.constant; +/** + * CONSTANT_Fieldref_info { + * u1 tag; + * u2 class_index; + * u2 name_and_type_index; + * } + */ +public class FieldRefInfo extends ConstantInfo{ + + private int type = ConstantInfo.FIELD_INFO; + private int classInfoIndex; + private int nameAndTypeIndex; + + public FieldRefInfo(ConstantPool pool) { + super(pool); + } + public int getType() { + return type; + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } + + public String toString(){ + + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + + return getClassName() +" : "+ typeInfo.getName() + ":" + typeInfo.getTypeInfo() +"]"; + } + + public String getClassName(){ + + ClassInfo classInfo = (ClassInfo) this.getConstantInfo(this.getClassInfoIndex()); + + UTF8Info utf8Info = (UTF8Info)this.getConstantInfo(classInfo.getUtf8Index()); + + return utf8Info.getValue(); + + } + + public String getFieldName(){ + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getName(); + } + + public String getFieldType(){ + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getTypeInfo(); + } +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/MethodRefInfo.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/MethodRefInfo.java new file mode 100644 index 0000000000..f8f30a92cd --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/MethodRefInfo.java @@ -0,0 +1,62 @@ +package com.github.chaoswang.learning.java.jvm.constant; +/** + * CONSTANT_Methodref_info { + * u1 tag; + * u2 class_index; + * u2 name_and_type_index; + */ +public class MethodRefInfo extends ConstantInfo { + + + private int type = ConstantInfo.METHOD_INFO; + private int classInfoIndex; + private int nameAndTypeIndex; + + public MethodRefInfo(ConstantPool pool) { + super(pool); + } + + public int getType() { + return type; + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } + + public String toString() { + + return getClassName() + " : " + this.getMethodName() + " : " + this.getParamAndReturnType(); + } + + public String getClassName() { + ConstantPool pool = this.getConstantPool(); + ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(this.getClassInfoIndex()); + return clzInfo.getClassName(); + } + + public String getMethodName() { + ConstantPool pool = this.getConstantPool(); + NameAndTypeInfo typeInfo = (NameAndTypeInfo) pool.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getName(); + } + + public String getParamAndReturnType() { + ConstantPool pool = this.getConstantPool(); + NameAndTypeInfo typeInfo = (NameAndTypeInfo) pool.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getTypeInfo(); + } + +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/NameAndTypeInfo.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/NameAndTypeInfo.java new file mode 100644 index 0000000000..bd91ec8aac --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/NameAndTypeInfo.java @@ -0,0 +1,54 @@ +package com.github.chaoswang.learning.java.jvm.constant; +/** + * CONSTANT_NameAndType_info { + * u1 tag; + * u2 name_index; + * u2 descriptor_index; + * } + */ +public class NameAndTypeInfo extends ConstantInfo { + + public int type = ConstantInfo.NAME_AND_TYPE_INFO; + private int index1; + private int index2; + + public NameAndTypeInfo(ConstantPool pool) { + super(pool); + } + + public int getIndex1() { + return index1; + } + + public void setIndex1(int index1) { + this.index1 = index1; + } + + public int getIndex2() { + return index2; + } + + public void setIndex2(int index2) { + this.index2 = index2; + } + + public int getType() { + return type; + } + + public String getName() { + ConstantPool pool = this.getConstantPool(); + UTF8Info utf8Info1 = (UTF8Info) pool.getConstantInfo(index1); + return utf8Info1.getValue(); + } + + public String getTypeInfo() { + ConstantPool pool = this.getConstantPool(); + UTF8Info utf8Info2 = (UTF8Info) pool.getConstantInfo(index2); + return utf8Info2.getValue(); + } + + public String toString() { + return "(" + getName() + "," + getTypeInfo() + ")"; + } +} \ No newline at end of file diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/NullConstantInfo.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/NullConstantInfo.java new file mode 100644 index 0000000000..c1cca00185 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/NullConstantInfo.java @@ -0,0 +1,13 @@ +package com.github.chaoswang.learning.java.jvm.constant; + +public class NullConstantInfo extends ConstantInfo { + + public NullConstantInfo(){ + + } + @Override + public int getType() { + return -1; + } + +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/StringInfo.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/StringInfo.java new file mode 100644 index 0000000000..82c4be3245 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/StringInfo.java @@ -0,0 +1,32 @@ +package com.github.chaoswang.learning.java.jvm.constant; +/** + * CONSTANT_String_info { + * u1 tag; + * u2 string_index; + */ +public class StringInfo extends ConstantInfo { + + private int type = ConstantInfo.STRING_INFO; + private int index; + + public StringInfo(ConstantPool pool) { + super(pool); + } + + public int getType() { + return type; + } + + public int getIndex() { + return index; + } + + public void setIndex(int index) { + this.index = index; + } + + public String toString() { + return this.getConstantPool().getUTF8String(index); + } + +} \ No newline at end of file diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/UTF8Info.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/UTF8Info.java new file mode 100644 index 0000000000..5950d094e7 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/constant/UTF8Info.java @@ -0,0 +1,45 @@ +package com.github.chaoswang.learning.java.jvm.constant; + +/** + * CONSTANT_Utf8_info { + * u1 tag; + * u2 length; + * u1 bytes[length]; + * } + */ +public class UTF8Info extends ConstantInfo { + + private int type = ConstantInfo.UTF8_INFO; + private int length; + private String value; + + public UTF8Info(ConstantPool pool) { + super(pool); + } + + public int getLength() { + return length; + } + + public void setLength(int length) { + this.length = length; + } + + public int getType() { + return type; + } + + @Override + public String toString() { + return "UTF8Info [type=" + type + ", length=" + length + ", value=" + value + ")]"; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + +} \ No newline at end of file diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/field/Field.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/field/Field.java new file mode 100644 index 0000000000..c0ef09f88a --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/field/Field.java @@ -0,0 +1,31 @@ +package com.github.chaoswang.learning.java.jvm.field; + +import com.github.chaoswang.learning.java.jvm.constant.ConstantPool; +import com.github.chaoswang.learning.java.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){ + + return null; + } + +} \ No newline at end of file diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/loader/ByteCodeIterator.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/loader/ByteCodeIterator.java new file mode 100644 index 0000000000..8cf2374114 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/loader/ByteCodeIterator.java @@ -0,0 +1,55 @@ +package com.github.chaoswang.learning.java.jvm.loader; + +import java.util.Arrays; + +import com.github.chaoswang.learning.java.jvm.util.Util; + +public class ByteCodeIterator { + byte[] codes; + int pos = 0; + + ByteCodeIterator(byte[] codes) { + this.codes = codes; + } + + public byte[] getBytes(int len) { + if (pos + len >= codes.length) { + throw new ArrayIndexOutOfBoundsException(); + } + + byte[] data = Arrays.copyOfRange(codes, pos, pos + len); + pos += len; + return data; + } + + public int nextU1toInt() { + + return Util.byteToInt(new byte[] { codes[pos++] }); + } + + public int nextU2ToInt() { + return Util.byteToInt(new byte[] { codes[pos++], codes[pos++] }); + } + + public int nextU4ToInt() { + return Util.byteToInt(new byte[] { codes[pos++], codes[pos++], codes[pos++], codes[pos++] }); + } + + public String nextU4ToHexString() { + return Util.byteToHexString((new byte[] { codes[pos++], codes[pos++], codes[pos++], codes[pos++] })); + } + + public String nextUxToHexString(int len) { + byte[] tmp = new byte[len]; + + for (int i = 0; i < len; i++) { + tmp[i] = codes[pos++]; + } + return Util.byteToHexString(tmp).toLowerCase(); + + } + + public void back(int n) { + this.pos -= n; + } +} \ No newline at end of file diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/loader/ClassFileLoader.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..8ac7e8573d --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/loader/ClassFileLoader.java @@ -0,0 +1,116 @@ +package com.github.chaoswang.learning.java.jvm.loader; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; + +import com.github.chaoswang.learning.java.jvm.clz.ClassFile; + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + + // String path = className.replaceAll("\\.", "/"); + // õķ + className = className.replace('.', File.separatorChar) + ".class"; + for (String path : this.clzPaths) { + + String clzFileName = path + File.separatorChar + className; + byte[] codes = loadClassFile(clzFileName); + if (codes != null) { + return codes; + } + } + + return null; + } + + private byte[] loadClassFile(String clzFileName) { + + File f = new File(clzFileName); + + try { + + return IOUtils.toByteArray(new FileInputStream(f)); + + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + public String getClassPath() { + return StringUtils.join(this.clzPaths, ";"); + } + + public ClassFile loadClass(String className) { + byte[] codes = this.readBinaryCode(className); + ClassFileParser parser = new ClassFileParser(); + return parser.parse(codes); + + } + + public void addClassPath(String path) { + // ѾӹIJҪټ + if (clzPaths.contains(path)) { + return; + } + clzPaths.add(path); + } + + // ------------------------------backup------------------------ + public String getClassPath_V1() { + StringBuffer sb = new StringBuffer(); + for (String clzPath : clzPaths) { + sb.append(clzPath).append(";"); + } + String classPath = sb.toString(); + return classPath.substring(0, classPath.length() - 1); + } + + // ȡһ.classļǰ4ֽڣ תʮַDzǣCAFEBABE + private byte[] readClassFile_V1(String filePath) { + BufferedInputStream bis = null; + ByteArrayOutputStream bos = null; + try { + bis = new BufferedInputStream(new FileInputStream(filePath)); + bos = new ByteArrayOutputStream(); + byte[] bbuf = new byte[1024];// ˼ǣܶ1024Ҳܲ1024 + int index = 0; + + while ((index = bis.read(bbuf)) != -1) { + // readڶĩβǰ˷һֱѭʵֻӡһ + System.out.println(bbuf.length); + bos.write(bbuf, 0, index); + } + return bos.toByteArray(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (bis != null) { + try { + bis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (bos != null) { + try { + bos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return null; + } +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/loader/ClassFileParser.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/loader/ClassFileParser.java new file mode 100644 index 0000000000..3b133dba93 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/loader/ClassFileParser.java @@ -0,0 +1,74 @@ +package com.github.chaoswang.learning.java.jvm.loader; + +import com.github.chaoswang.learning.java.jvm.clz.AccessFlag; +import com.github.chaoswang.learning.java.jvm.clz.ClassFile; +import com.github.chaoswang.learning.java.jvm.clz.ClassIndex; +import com.github.chaoswang.learning.java.jvm.constant.ClassInfo; +import com.github.chaoswang.learning.java.jvm.constant.ConstantPool; +import com.github.chaoswang.learning.java.jvm.constant.UTF8Info; + +public class ClassFileParser { + + + public ClassFile parse(byte[] codes) { + ByteCodeIterator codeIterator = new ByteCodeIterator(codes); + ClassFile classFile = new ClassFile(); + //ħ + codeIterator.getBytes(4); + classFile.setMinorVersion(codeIterator.nextU2ToInt()); + classFile.setMajorVersion(codeIterator.nextU2ToInt()); + + return classFile; + } + + + private AccessFlag parseAccessFlag(ByteCodeIterator iter) { + //أصijǶ٣ + AccessFlag accessFlag = new AccessFlag(iter.nextU2ToInt()); + return accessFlag; + } + + private ClassIndex parseClassIndex(ByteCodeIterator iter) { + + return null; + + } + + private ConstantPool parseConstantPool(ByteCodeIterator iter) { + ConstantPool pool = new ConstantPool(); + ClassInfo classInfo = new ClassInfo(pool); + //classInfo tagֵ + iter.nextU1toInt(); + classInfo.setUtf8Index(iter.nextU2ToInt()); + pool.addConstantInfo(classInfo); + + UTF8Info utf8Info = new UTF8Info(pool); + //UTF8Info tagֵ + iter.nextU1toInt(); + //UTF8Info lengthֵ + int length = iter.nextU2ToInt(); + utf8Info.setLength(length); + utf8Info.setValue(iter.nextUxToHexString(length)); + pool.addConstantInfo(utf8Info); + + classInfo = new ClassInfo(pool); + //classInfo tagֵ + iter.nextU1toInt(); + classInfo.setUtf8Index(iter.nextU2ToInt()); + pool.addConstantInfo(classInfo); + + for(int i=0;i<8;i++){ + utf8Info = new UTF8Info(pool); + //UTF8Info tagֵ + iter.nextU1toInt(); + //UTF8Info lengthֵ + length = iter.nextU2ToInt(); + utf8Info.setLength(length); + utf8Info.setValue(iter.nextUxToHexString(length)); + pool.addConstantInfo(utf8Info); + } + + return pool; + } + +} \ No newline at end of file diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/method/Method.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/method/Method.java new file mode 100644 index 0000000000..c55cb5ac25 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/method/Method.java @@ -0,0 +1,51 @@ +package com.github.chaoswang.learning.java.jvm.method; + +import com.github.chaoswang.learning.java.jvm.attr.CodeAttr; +import com.github.chaoswang.learning.java.jvm.clz.ClassFile; +import com.github.chaoswang.learning.java.jvm.loader.ByteCodeIterator; + +public class Method { + private int accessFlag; + private int nameIndex; + private int descriptorIndex; + + private CodeAttr codeAttr; + + private ClassFile clzFile; + + + public ClassFile getClzFile() { + return clzFile; + } + + public int getNameIndex() { + return nameIndex; + } + public int getDescriptorIndex() { + return descriptorIndex; + } + + public CodeAttr getCodeAttr() { + return codeAttr; + } + + public void setCodeAttr(CodeAttr code) { + this.codeAttr = code; + } + + public Method(ClassFile clzFile,int accessFlag, int nameIndex, int descriptorIndex) { + this.clzFile = clzFile; + this.accessFlag = accessFlag; + this.nameIndex = nameIndex; + this.descriptorIndex = descriptorIndex; + } + + + + + + public static Method parse(ClassFile clzFile, ByteCodeIterator iter){ + return null; + + } +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/util/Util.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/util/Util.java new file mode 100644 index 0000000000..f74531ca2d --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/jvm/util/Util.java @@ -0,0 +1,22 @@ +package com.github.chaoswang.learning.java.jvm.util; + +public class Util { + public static int byteToInt(byte[] codes) { + String s1 = byteToHexString(codes); + return Integer.valueOf(s1, 16).intValue(); + } + + public static String byteToHexString(byte[] codes) { + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < codes.length; i++) { + byte b = codes[i]; + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if (strHex.length() < 2) { + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/stack/StackUtil.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/stack/StackUtil.java index d5d7ecc223..7f2c30d1f1 100644 --- a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/stack/StackUtil.java +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/stack/StackUtil.java @@ -1,13 +1,37 @@ package com.github.chaoswang.learning.java.stack; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Stack; + public class StackUtil { + + public static void addToBottom(Stack s, Integer value) { + Stack tmpStack = new Stack(); + while(!s.empty()){ + tmpStack.push(s.pop()); + } + s.push(value); + while(!tmpStack.empty()){ + s.push(tmpStack.pop()); + } + } /** * ջеԪInteger, ջջ : 5,4,3,2,1 ø÷ ԪشΪ: 1,2,3,4,5 * ע⣺ֻʹStackĻpush,pop,peek,isEmpty ʹһջ */ - public static void reverse(Stack s) { - + public static void reverse(Stack s) { + Stack tmpStack = new Stack(); + while(!s.empty()){ + tmpStack.push(s.pop()); + } + while(!tmpStack.empty()){ + StackUtil.addToBottom(s, tmpStack.pop()); + } } /** @@ -15,8 +39,21 @@ public static void reverse(Stack s) { * * @param o */ - public static void remove(Stack s, Object o) { - + public static void remove(Stack s, Object o) { + if(o == null){ + return; + } + Stack tmpStack = new Stack(); + while(!s.empty()){ + Integer poped = s.pop(); + if(o.equals(poped)){ + break; + } + tmpStack.push(poped); + } + while(!tmpStack.empty()){ + s.push(tmpStack.pop()); + } } /** @@ -27,7 +64,21 @@ public static void remove(Stack s, Object o) { * @return */ public static Object[] getTop(Stack s, int len) { - return null; + int stackSize = s.size(); + if(len > stackSize || len <= 0){ + throw new IllegalArgumentException("parameter len illegal"); + } + List ret = new ArrayList(); + Stack tmpStack = new Stack(); + for(int i=0;i map = new HashMap(); + map.put(')', '('); + map.put(']', '['); + map.put('}', '{'); + List bracketList = Arrays.asList('(', ')','[', ']','{', '}'); + Stack tmpStack = new Stack(); + for(char c : s.toCharArray()){ + if(bracketList.contains(c)){ + tmpStack.push(c); + } + } + //ŵĸż϶ɶ + if(tmpStack.size()%2 != 0){ + return false; + } + Stack tmpStack2 = new Stack(); + int tmpSize = tmpStack.size(); + for(int i=0;i", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(10); + Assert.assertEquals("(Ljava/lang/String;I)V", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(11); + Assert.assertEquals("Code", utf8Info.getValue()); + } + + { + MethodRefInfo methodRef = (MethodRefInfo) pool.getConstantInfo(12); + Assert.assertEquals(3, methodRef.getClassInfoIndex()); + Assert.assertEquals(13, methodRef.getNameAndTypeIndex()); + } + + { + NameAndTypeInfo nameAndType = (NameAndTypeInfo) pool.getConstantInfo(13); + Assert.assertEquals(9, nameAndType.getIndex1()); + Assert.assertEquals(14, nameAndType.getIndex2()); + } + // 鼸 + { + MethodRefInfo methodRef = (MethodRefInfo) pool.getConstantInfo(45); + Assert.assertEquals(1, methodRef.getClassInfoIndex()); + Assert.assertEquals(46, methodRef.getNameAndTypeIndex()); + } + + { + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(53); + Assert.assertEquals("EmployeeV1.java", utf8Info.getValue()); + } + } + + @Test + public void testClassIndex() { + + ClassIndex clzIndex = clzFile.getClzIndex(); + ClassInfo thisClassInfo = (ClassInfo) clzFile.getConstantPool().getConstantInfo(clzIndex.getThisClassIndex()); + ClassInfo superClassInfo = (ClassInfo) clzFile.getConstantPool().getConstantInfo(clzIndex.getSuperClassIndex()); + + Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, thisClassInfo.getClassName()); + Assert.assertEquals("java/lang/Object", superClassInfo.getClassName()); + } + + /** + * ǵJVMӦʵֵIJ + */ + @Test + public void testReadFields(){ + + List 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); + } + } \ No newline at end of file diff --git a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/stack/StackUtilTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/stack/StackUtilTest.java new file mode 100644 index 0000000000..cce5434964 --- /dev/null +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/stack/StackUtilTest.java @@ -0,0 +1,76 @@ +package com.github.chaoswang.learning.java.stack; + +import java.util.Stack; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +public class StackUtilTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAddToBottom() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + + StackUtil.addToBottom(s, 0); + + Assert.assertEquals("[0, 1, 2, 3]", s.toString()); + + } + @Test + public void testReverse() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString()); + StackUtil.reverse(s); + Assert.assertEquals("[5, 4, 3, 2, 1]", s.toString()); + } + + @Test + public void testRemove() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + StackUtil.remove(s, 2); + Assert.assertEquals("[1, 3]", s.toString()); + } + + @Test + public void testGetTop() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + { + Object[] values = StackUtil.getTop(s, 3); + Assert.assertEquals(5, values[0]); + Assert.assertEquals(4, values[1]); + Assert.assertEquals(3, values[2]); + } + } + + @Test + public void testIsValidPairs() { + Assert.assertTrue(StackUtil.isValidPairs("([e{d}f])")); + Assert.assertFalse(StackUtil.isValidPairs("([b{x]y})")); + } + +}