diff --git a/group27/383117348/.classpath b/group27/383117348/.classpath
index 840446d6d..a96bb3c42 100644
--- a/group27/383117348/.classpath
+++ b/group27/383117348/.classpath
@@ -6,5 +6,7 @@
+
+
diff --git a/group27/383117348/src/com/coderising/jvm/clz/AccessFlag.java b/group27/383117348/src/com/coderising/jvm/clz/AccessFlag.java
new file mode 100644
index 000000000..cdb8f8859
--- /dev/null
+++ b/group27/383117348/src/com/coderising/jvm/clz/AccessFlag.java
@@ -0,0 +1,25 @@
+package com.coderising.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;
+ }
+
+}
\ No newline at end of file
diff --git a/group27/383117348/src/com/coderising/jvm/clz/ClassFile.java b/group27/383117348/src/com/coderising/jvm/clz/ClassFile.java
new file mode 100644
index 000000000..1b5a8b95a
--- /dev/null
+++ b/group27/383117348/src/com/coderising/jvm/clz/ClassFile.java
@@ -0,0 +1,75 @@
+package com.coderising.jvm.clz;
+
+import com.coderising.jvm.constant.ClassInfo;
+import com.coderising.jvm.constant.ConstantPool;
+
+public class ClassFile {
+
+ private int minorVersion;
+ private int majorVersion;
+
+ private AccessFlag accessFlag;
+ private ClassIndex clzIndex;
+ private ConstantPool pool;
+
+
+ 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 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();
+ }
+}
diff --git a/group27/383117348/src/com/coderising/jvm/clz/ClassIndex.java b/group27/383117348/src/com/coderising/jvm/clz/ClassIndex.java
new file mode 100644
index 000000000..df2298144
--- /dev/null
+++ b/group27/383117348/src/com/coderising/jvm/clz/ClassIndex.java
@@ -0,0 +1,19 @@
+package com.coderising.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;
+ }
+}
\ No newline at end of file
diff --git a/group27/383117348/src/com/coderising/jvm/constant/ClassInfo.java b/group27/383117348/src/com/coderising/jvm/constant/ClassInfo.java
new file mode 100644
index 000000000..e12b3e164
--- /dev/null
+++ b/group27/383117348/src/com/coderising/jvm/constant/ClassInfo.java
@@ -0,0 +1,24 @@
+package com.coderising.jvm.constant;
+
+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/group27/383117348/src/com/coderising/jvm/constant/ConstantInfo.java b/group27/383117348/src/com/coderising/jvm/constant/ConstantInfo.java
new file mode 100644
index 000000000..a3a0b53b7
--- /dev/null
+++ b/group27/383117348/src/com/coderising/jvm/constant/ConstantInfo.java
@@ -0,0 +1,30 @@
+package com.coderising.jvm.constant;
+
+public abstract class ConstantInfo {
+ public static final int UTF8_INFO = 1;
+ public static final int INTEGER_INFO = 3;
+ 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);
+ }
+
+}
diff --git a/group27/383117348/src/com/coderising/jvm/constant/ConstantPool.java b/group27/383117348/src/com/coderising/jvm/constant/ConstantPool.java
new file mode 100644
index 000000000..0e940b78d
--- /dev/null
+++ b/group27/383117348/src/com/coderising/jvm/constant/ConstantPool.java
@@ -0,0 +1,29 @@
+package com.coderising.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;
+ }
+}
diff --git a/group27/383117348/src/com/coderising/jvm/constant/FieldRefInfo.java b/group27/383117348/src/com/coderising/jvm/constant/FieldRefInfo.java
new file mode 100644
index 000000000..7ff9d5fb7
--- /dev/null
+++ b/group27/383117348/src/com/coderising/jvm/constant/FieldRefInfo.java
@@ -0,0 +1,54 @@
+package com.coderising.jvm.constant;
+
+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/group27/383117348/src/com/coderising/jvm/constant/FloatInfo.java b/group27/383117348/src/com/coderising/jvm/constant/FloatInfo.java
new file mode 100644
index 000000000..dc3765720
--- /dev/null
+++ b/group27/383117348/src/com/coderising/jvm/constant/FloatInfo.java
@@ -0,0 +1,26 @@
+package com.coderising.jvm.constant;
+
+
+public class FloatInfo extends ConstantInfo{
+ private int type = ConstantInfo.FLOAT_INFO;
+
+ private float value;
+
+ public FloatInfo(ConstantPool pool){
+ super(pool);
+ }
+
+ @Override
+ public int getType() {
+ // TODO Auto-generated method stub
+ return type;
+ }
+
+ public float getValue() {
+ return value;
+ }
+
+ public void setValue(float value) {
+ this.value = value;
+ }
+}
diff --git a/group27/383117348/src/com/coderising/jvm/constant/IntegerInfo.java b/group27/383117348/src/com/coderising/jvm/constant/IntegerInfo.java
new file mode 100644
index 000000000..06e66c6e0
--- /dev/null
+++ b/group27/383117348/src/com/coderising/jvm/constant/IntegerInfo.java
@@ -0,0 +1,28 @@
+package com.coderising.jvm.constant;
+
+public class IntegerInfo extends ConstantInfo {
+
+ private int type = ConstantInfo.INTEGER_INFO;
+
+ private int value;
+
+ public IntegerInfo(ConstantPool pool) {
+ super(pool);
+ }
+
+ @Override
+ public int getType() {
+
+ return type;
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+ public void setValue(int value) {
+ this.value = value;
+ }
+
+
+}
diff --git a/group27/383117348/src/com/coderising/jvm/constant/MethodRefInfo.java b/group27/383117348/src/com/coderising/jvm/constant/MethodRefInfo.java
new file mode 100644
index 000000000..0feffa65b
--- /dev/null
+++ b/group27/383117348/src/com/coderising/jvm/constant/MethodRefInfo.java
@@ -0,0 +1,55 @@
+package com.coderising.jvm.constant;
+
+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/group27/383117348/src/com/coderising/jvm/constant/NameAndTypeInfo.java b/group27/383117348/src/com/coderising/jvm/constant/NameAndTypeInfo.java
new file mode 100644
index 000000000..dcac7f97c
--- /dev/null
+++ b/group27/383117348/src/com/coderising/jvm/constant/NameAndTypeInfo.java
@@ -0,0 +1,45 @@
+package com.coderising.jvm.constant;
+
+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()+")";
+ }
+}
diff --git a/group27/383117348/src/com/coderising/jvm/constant/NullConstantInfo.java b/group27/383117348/src/com/coderising/jvm/constant/NullConstantInfo.java
new file mode 100644
index 000000000..fa90d110f
--- /dev/null
+++ b/group27/383117348/src/com/coderising/jvm/constant/NullConstantInfo.java
@@ -0,0 +1,13 @@
+package com.coderising.jvm.constant;
+
+public class NullConstantInfo extends ConstantInfo {
+
+ public NullConstantInfo(){
+
+ }
+ @Override
+ public int getType() {
+ return -1;
+ }
+
+}
diff --git a/group27/383117348/src/com/coderising/jvm/constant/StringInfo.java b/group27/383117348/src/com/coderising/jvm/constant/StringInfo.java
new file mode 100644
index 000000000..d01065fd5
--- /dev/null
+++ b/group27/383117348/src/com/coderising/jvm/constant/StringInfo.java
@@ -0,0 +1,26 @@
+package com.coderising.jvm.constant;
+
+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);
+ }
+
+}
diff --git a/group27/383117348/src/com/coderising/jvm/constant/UTF8Info.java b/group27/383117348/src/com/coderising/jvm/constant/UTF8Info.java
new file mode 100644
index 000000000..b7407d146
--- /dev/null
+++ b/group27/383117348/src/com/coderising/jvm/constant/UTF8Info.java
@@ -0,0 +1,32 @@
+package com.coderising.jvm.constant;
+
+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;
+ }
+
+
+
+}
diff --git a/group27/383117348/src/com/coderising/jvm/loader/ByteCodeIterator.java b/group27/383117348/src/com/coderising/jvm/loader/ByteCodeIterator.java
new file mode 100644
index 000000000..2688ae581
--- /dev/null
+++ b/group27/383117348/src/com/coderising/jvm/loader/ByteCodeIterator.java
@@ -0,0 +1,51 @@
+package com.coderising.jvm.loader;
+
+import com.coderising.jvm.util.Util;
+
+public class ByteCodeIterator {
+
+ private byte[] code = null;
+ private int pos = 0;
+
+ public ByteCodeIterator(byte[] code){
+ this.code = code;
+ }
+
+ public int nextU1Int(){
+ return Util.byteToInt(getByteBySize(1));
+ }
+
+ public int nextU2Int(){
+
+ return Util.byteToInt(getByteBySize(2));
+ }
+
+ public String nextU4HexString(){
+ return Util.byteToHexString(getByteBySize(4));
+ }
+
+ public float nextU4Float(){
+ return Util.byteToFloat(getByteBySize(4));
+ }
+
+ public int nextU4Integer(){
+ return Util.byteToInt(getByteBySize(4));
+ }
+
+ public byte[] getByteByLength(int length){
+ if(pos+length>code.length){
+ throw new RuntimeException("长度超出字节数组最大长度");
+ }else{
+ byte[] by = getByteBySize(length);
+ return by;
+ }
+ }
+
+ private byte[] getByteBySize(int size){
+ byte[] by = new byte[size];
+ for(int i=0;i clzPaths = new ArrayList();
-
+
public byte[] readBinaryCode(String className) {
- className = className.replace(".", "\\");
- File file = null;
- for(String classPath : clzPaths){
- file = new File(classPath + "\\" + className + ".class");
- if(file.exists()){
- break;
- }
+
+ 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 void addClassPath(String path) {
+ if(this.clzPaths.contains(path)){
+ return;
}
- if(!file.exists()){
- try {
- throw new ClassNotFoundException();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
+
+ this.clzPaths.add(path);
+
+ }
+
+
+
+ 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);
+
+ }
+
+
+
+ // ------------------------------backup------------------------
+ public String getClassPath_V1(){
+
+ StringBuffer buffer = new StringBuffer();
+ for(int i=0;i 0) {
- if (!clzPaths.contains(path)) {
- clzPaths.add(path);
- }
- }
- }
- public String getClassPath() {
- String paths = "";
- for (String s : clzPaths) {
- paths += s + ";";
- }
- paths = paths.substring(0, paths.length() - 1);
- return paths;
- }
+
-}
+}
\ No newline at end of file
diff --git a/group27/383117348/src/com/coderising/jvm/loader/ClassFileLoader_backup.java b/group27/383117348/src/com/coderising/jvm/loader/ClassFileLoader_backup.java
new file mode 100644
index 000000000..014def27d
--- /dev/null
+++ b/group27/383117348/src/com/coderising/jvm/loader/ClassFileLoader_backup.java
@@ -0,0 +1,77 @@
+package com.coderising.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;
+
+public class ClassFileLoader_backup {
+
+ private List clzPaths = new ArrayList();
+
+ public byte[] readBinaryCode(String className) {
+ className = className.replace(".", "\\");
+ File file = null;
+ for(String classPath : clzPaths){
+ file = new File(classPath + "\\" + className + ".class");
+ if(file.exists()){
+ break;
+ }
+ }
+ if(!file.exists()){
+ try {
+ throw new ClassNotFoundException();
+ } catch (ClassNotFoundException e) {
+ e.printStackTrace();
+ }
+ }
+ ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
+ BufferedInputStream in = null;
+ try {
+ in = new BufferedInputStream(new FileInputStream(file));
+ int buf_size = 1024;
+ byte[] buffer = new byte[buf_size];
+ int len = 0;
+ while (-1 != (len = in.read(buffer, 0, buf_size))) {
+ bos.write(buffer, 0, len);
+ }
+ return bos.toByteArray();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ try {
+ in.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ try {
+ bos.close();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ return null;
+ }
+
+ public void addClassPath(String path) {
+ if (path != null && path.length() > 0) {
+ if (!clzPaths.contains(path)) {
+ clzPaths.add(path);
+ }
+ }
+ }
+
+ public String getClassPath() {
+ String paths = "";
+ for (String s : clzPaths) {
+ paths += s + ";";
+ }
+ paths = paths.substring(0, paths.length() - 1);
+ return paths;
+ }
+
+}
diff --git a/group27/383117348/src/com/coderising/jvm/loader/ClassFileParser.java b/group27/383117348/src/com/coderising/jvm/loader/ClassFileParser.java
new file mode 100644
index 000000000..209ee78d3
--- /dev/null
+++ b/group27/383117348/src/com/coderising/jvm/loader/ClassFileParser.java
@@ -0,0 +1,110 @@
+package com.coderising.jvm.loader;
+
+import java.io.UnsupportedEncodingException;
+
+import com.coderising.jvm.clz.AccessFlag;
+import com.coderising.jvm.clz.ClassFile;
+import com.coderising.jvm.clz.ClassIndex;
+import com.coderising.jvm.constant.ClassInfo;
+import com.coderising.jvm.constant.ConstantPool;
+import com.coderising.jvm.constant.FieldRefInfo;
+import com.coderising.jvm.constant.FloatInfo;
+import com.coderising.jvm.constant.IntegerInfo;
+import com.coderising.jvm.constant.MethodRefInfo;
+import com.coderising.jvm.constant.NameAndTypeInfo;
+import com.coderising.jvm.constant.NullConstantInfo;
+import com.coderising.jvm.constant.StringInfo;
+import com.coderising.jvm.constant.UTF8Info;
+
+public class ClassFileParser {
+
+
+ public ClassFile parse(byte[] codes) {
+ ByteCodeIterator by = new ByteCodeIterator(codes);
+ ClassFile file = new ClassFile();
+ String magicNum = by.nextU4HexString();
+ if(!magicNum.equals("cafebabe")){
+ throw new RuntimeException("文件类型错误");
+ }
+
+ int minVersion = by.nextU2Int();
+ int majorVersion = by.nextU2Int();
+
+ ConstantPool constant = parseConstantPool(by);
+ AccessFlag flag = parseAccessFlag(by);
+ ClassIndex index = parseClassIndex(by);
+
+ file.setMinorVersion(minVersion);
+ file.setMajorVersion(majorVersion);
+ file.setAccessFlag(flag);
+ file.setClassIndex(index);
+ file.setConstPool(constant);
+ return file;
+ }
+
+ private AccessFlag parseAccessFlag(ByteCodeIterator iter) {
+ AccessFlag flag = new AccessFlag(iter.nextU2Int());
+ return flag;
+ }
+
+ private ClassIndex parseClassIndex(ByteCodeIterator iter) {
+ ClassIndex index = new ClassIndex();
+ index.setThisClassIndex(iter.nextU2Int());
+ index.setSuperClassIndex(iter.nextU2Int());
+ return index;
+
+ }
+
+ private ConstantPool parseConstantPool(ByteCodeIterator iter) {
+ int constantCount = iter.nextU2Int();
+ ConstantPool pool = new ConstantPool();
+ pool.addConstantInfo(new NullConstantInfo());
+ for(int i=1;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());
+ }
+
+
+
+}
diff --git a/group27/383117348/src/com/coderising/jvm/test/EmployeeV1.java b/group27/383117348/src/com/coderising/jvm/test/EmployeeV1.java
index d36b122f6..9a36573dd 100644
--- a/group27/383117348/src/com/coderising/jvm/test/EmployeeV1.java
+++ b/group27/383117348/src/com/coderising/jvm/test/EmployeeV1.java
@@ -1,30 +1,28 @@
package com.coderising.jvm.test;
public class EmployeeV1 {
-
+
+
private String name;
- private int age;
-
- public EmployeeV1(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public void sayHello() {
- System.out.println("Hello , this is class Employee ");
- }
-
- public static void main(String[] args) {
- EmployeeV1 p = new EmployeeV1("Andy", 29);
- p.sayHello();
-
- }
+ private int age;
+
+ public EmployeeV1(String name, int age) {
+ this.name = name;
+ this.age = age;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+ public void setAge(int age){
+ this.age = age;
+ }
+ public void sayHello() {
+ System.out.println("Hello , this is class Employee ");
+ }
+ public static void main(String[] args){
+ EmployeeV1 p = new EmployeeV1("Andy",29);
+ p.sayHello();
+
+ }
}
\ No newline at end of file
diff --git a/group27/383117348/src/com/coderising/jvm/util/Util.java b/group27/383117348/src/com/coderising/jvm/util/Util.java
new file mode 100644
index 000000000..0038d0953
--- /dev/null
+++ b/group27/383117348/src/com/coderising/jvm/util/Util.java
@@ -0,0 +1,29 @@
+package com.coderising.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;i0){
+ Stack newStack = new Stack();
+ while(s.size()>0){
+ newStack.push(s.pop());
+ if(newStack.peek().equals(o)){
+ newStack.pop();
+ }
+ }
+ while(newStack.size()>0){
+ s.push(newStack.pop());
+ }
+ }else{
+
+ }
+ }
+
+ /**
+ * 从栈顶取得len个元素, 原来的栈中元素保持不变 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty,
+ * 可以使用另外一个栈来辅助
+ *
+ * @param len
+ * @return
+ */
+ public static Object[] getTop(Stack s, int len) {
+ if(s!=null && s.size()>0 && len>0 && len<=s.size()){
+ Object[] objs = new Object[len];
+ Stack newStack = new Stack();
+ for(int i = 0;i0){
+ s.push(newStack.pop());
+ }
+ return objs;
+ }
+ return null;
+ }
+
+ /**
+ * 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz 使用堆栈检查字符串s中的括号是不是成对出现的。 例如s =
+ * "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true 如果 s = "([b{x]y})",
+ * 则该字符串中的括号不是成对出现的, 该方法返回false;
+ *
+ * @param s
+ * @return
+ */
+ public static boolean isValidPairs(String s) {
+ char[] chars = s.toCharArray();
+ Stack special = new Stack();
+ for(int i =0; i 0){
+ str+=s.pop()+";";
+ }
+ return str;
+ }
+
+}