diff --git a/group13/413007522/lesson05/src/main/java/cn/xl/basic/stack/MyStack.java b/group13/413007522/lesson05/src/main/java/cn/xl/basic/stack/MyStack.java new file mode 100644 index 0000000000..8987537b09 --- /dev/null +++ b/group13/413007522/lesson05/src/main/java/cn/xl/basic/stack/MyStack.java @@ -0,0 +1,124 @@ +package cn.xl.basic.stack; + +import java.util.Arrays; +import java.util.EmptyStackException; + +/** + * Stack是一个后进先出(last in first out,LIFO)的堆栈, + * 在Vector类的基础上扩展5个方法而来 + * @author XIAOLONG + * + */ +public class MyStack { + + private int elementCount; + + private Object[] elementData = new Object[5]; + + /** + * 无参构造方法,创建一个空栈 + * + */ + public MyStack(){ + + } + + + /** + * 元素入栈 + * @param item + * @return 入栈元素 + */ + public synchronized Object push(Object item){ + + ensureCapacity(elementCount+1); + elementData[elementCount] = item; + elementCount ++; + return item; + } + + /** + * 将栈顶元素移除,并返回该元素 + * @return 栈顶元素 + */ + public synchronized Object pop(){ + Object obj; + + obj = peek(); + elementCount --; + elementData[elementCount] = null; + + return obj; + } + + /** + * 查看栈顶元素 + * + * @return 栈顶元素 + * @throws 如果栈为空 , 抛出 EmptyStackException异常 . + */ + public synchronized Object peek(){ + int len = elementCount; + + if(len == 0) + throw new EmptyStackException(); + + return elementData[len - 1]; + + } + + /** + * 检查栈是否为空 + * + * @return True or false + */ + public boolean isEmpty(){ + + return elementCount == 0; + } + + /** + * 查询占栈是否存在某元素 + * @param 查询元素 + * @return 如果元素存在返回元素所在位置(栈顶元素位置为1); + * 如果该元素在栈中有重复,则返回距离栈顶最近的元素位置; + * 如果该元素在栈中不存在,则返回 -1 。 + */ + public synchronized int search(Object o){ + + if(o == null){ + for(int i = elementCount -1;i >= 0; i--){ + if(elementData[i] == null){ + return elementCount - i; + } + } + }else{ + for(int i = elementCount -1;i >= 0; i-- ){ + if(o.equals(elementData[i])){ + return elementCount - i; + } + } + } + + return -1; + } + + /** + * 扩展容量,增加自身容量的一倍 + * @param 当前栈所需最小容量size + */ + private void ensureCapacity(int minCapacity){ + int oldCapacity = elementData.length; + if(minCapacity > oldCapacity){ + int newCapacity = oldCapacity << 1; + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + public static void main(String[] args){ + + + + } + +} diff --git a/group13/413007522/lesson05/src/main/java/cn/xl/basic/stack/StackUtil.java b/group13/413007522/lesson05/src/main/java/cn/xl/basic/stack/StackUtil.java new file mode 100644 index 0000000000..ca686ab19b --- /dev/null +++ b/group13/413007522/lesson05/src/main/java/cn/xl/basic/stack/StackUtil.java @@ -0,0 +1,190 @@ +package cn.xl.basic.stack; + + +public class StackUtil { + + + /** + * 鍋囪鏍堜腑鐨勫厓绱犳槸Integer, 浠庢爤椤跺埌鏍堝簳鏄 : 5,4,3,2,1 璋冪敤璇ユ柟娉曞悗锛 鍏冪礌娆″簭鍙樹负: 1,2,3,4,5 + * 娉ㄦ剰锛氬彧鑳戒娇鐢∕yStack鐨勫熀鏈搷浣滐紝鍗硃ush,pop,peek,isEmpty锛 鍙互浣跨敤鍙﹀涓涓爤鏉ヨ緟鍔 + */ + public static void reverse(MyStack s) { + + if(s == null || s.isEmpty()){ + return ; + } + + MyStack ns = new MyStack(); + Object o = s.pop(); + int count = 0; + while(!s.isEmpty()){ + ns.push(s.pop()); + count ++; + } + s.push(o); + + while(!ns.isEmpty()){ + s.push(ns.pop()); + } + + for(int i = 0; i < count - 1 ;i++){ + o = s.pop(); + for(int j = 0; j < count - 1 - i ;j++){ + ns.push(s.pop()); + } + s.push(o); + while(!ns.isEmpty()){ + s.push(ns.pop()); + } + } + + //缁勫憳鍐欑殑 锛屼笉閿 + /*MyStack a = new MyStack(); + + while (!s.isEmpty()){ + Object o = s.pop(); + int count =0; + while (!a.isEmpty()){ + s.push(a.pop()); + count++; + } + a.push(o); + for (int i = 0; i < count; i++) { + a.push(s.pop()); + } + } + while (!a.isEmpty()){ + s.push(a.pop()); + } + + return s;*/ + } + + /** + * 鍒犻櫎鏍堜腑鐨勬煇涓厓绱 娉ㄦ剰锛氬彧鑳戒娇鐢∕yStack鐨勫熀鏈搷浣滐紝鍗硃ush,pop,peek,isEmpty锛 鍙互浣跨敤鍙﹀涓涓爤鏉ヨ緟鍔 + * + * @param o + */ + public static void remove(MyStack s,Object o) { + + if(s == null || s.isEmpty()){ + return ; + } + + MyStack ms = new MyStack(); + Object obj = null; + while(!s.isEmpty()){ + obj = s.pop(); + if(obj == null){ + if(o == null){ + continue; + }else{ + ms.push(obj); + } + }else{ + if(obj.equals(o)|| obj.toString().equals(o)){ + continue; + }else{ + ms.push(obj); + } + } + } + + while(!ms.isEmpty()){ + s.push(ms.pop()); + } + + } + + /** + * 浠庢爤椤跺彇寰條en涓厓绱, 鍘熸潵鐨勬爤涓厓绱犱繚鎸佷笉鍙 + * 娉ㄦ剰锛氬彧鑳戒娇鐢∕yStack鐨勫熀鏈搷浣滐紝鍗硃ush,pop,peek,isEmpty锛 鍙互浣跨敤鍙﹀涓涓爤鏉ヨ緟鍔 + * @param len + * @return + */ + public static Object[] getTop(MyStack s,int len) { + MyStack ns = new MyStack(); + int count = 0; + Object[] o = new Object[len]; + while(!s.isEmpty()){ + ns.push(s.pop()); + count++; + } + int n = 0; + for(int i = 0; i < count - len;i++){ + if(i < count - len){ + s.push(ns.pop()); + }else{ + o[n] = ns.pop(); + n++; + } + } + + return o; + } + /** + * 瀛楃涓瞫 鍙兘鍖呭惈杩欎簺瀛楃锛 ( ) [ ] { }, a,b,c... x,yz + * 浣跨敤鍫嗘爤妫鏌ュ瓧绗︿覆s涓殑鎷彿鏄笉鏄垚瀵瑰嚭鐜扮殑銆 + * 渚嬪s = "([e{d}f])" , 鍒欒瀛楃涓蹭腑鐨勬嫭鍙锋槸鎴愬鍑虹幇锛 璇ユ柟娉曡繑鍥瀟rue + * 濡傛灉 s = "([b{x]y})", 鍒欒瀛楃涓蹭腑鐨勬嫭鍙蜂笉鏄垚瀵瑰嚭鐜扮殑锛 璇ユ柟娉曡繑鍥瀎alse; + * @param s + * @return + */ + public static boolean isValidPairs(String s){ + + if(s == null || "".equals(s)){ + return true; + } + char[] b = s.toCharArray(); + MyStack ms = new MyStack(); + String str = null; + for(int i = 0; i < b.length ; i++){ + str = String.valueOf(b[i]); + if("(".equals(str)){ + ms.push(b[i]); + }else if(")".equals(str)){ + if(!popObj(ms,"("))return false; + }else if("[".equals(str)){ + ms.push(b[i]); + }else if("]".equals(str)){ + if(!popObj(ms,"["))return false; + }else if("{".equals(str)){ + ms.push(b[i]); + }else if("}".equals(str)){ + if(!popObj(ms,"{"))return false; + } + } + if(ms.isEmpty()){ + return true; + } + return false; + } + + + private static boolean popObj(MyStack s,Object o) { + + MyStack ms = new MyStack(); + Object obj = null; + while(!s.isEmpty()){ + obj = s.pop(); + if(obj.equals(o)||obj.toString().equals(o)){ + while(!ms.isEmpty()){ + s.push(ms.pop()); + } + return true; + }else{ + ms.push(obj); + } + } + return false; + } + + + + public static void main(String[] args){ + + + + } + +} diff --git a/group13/413007522/lesson05/src/main/java/cn/xl/basic/stack/StackUtilTest.java b/group13/413007522/lesson05/src/main/java/cn/xl/basic/stack/StackUtilTest.java new file mode 100644 index 0000000000..c7e4e6a51f --- /dev/null +++ b/group13/413007522/lesson05/src/main/java/cn/xl/basic/stack/StackUtilTest.java @@ -0,0 +1,84 @@ +package cn.xl.basic.stack; + +import org.junit.Test; + +import junit.framework.Assert; + +public class StackUtilTest { + + + @Test + public void reverse(){ + MyStack s = new MyStack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + s.push(6); + s.push(7); + s.push(8); + s.push(9); + s.push(10); + s.push(11); + /*MyStack my = StackUtil.reverse(s); + while(!my.isEmpty()){ + System.out.println(my.pop()); + }*/ + } + + + @Test + public void getTop(){ + MyStack s = new MyStack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + s.push(6); + s.push(7); + s.push(8); + s.push(9); + s.push(10); + s.push(11); + + Object[] o = StackUtil.getTop(s,5); + + Assert.assertEquals(5, o.length); + + /*while(!s.isEmpty()){ + System.out.println(s.pop()); + }*/ + } + + @Test + public void remove(){ + + MyStack s = new MyStack(); + s.push(1); + s.push(2); + s.push(4); + s.push(3); + s.push(4); + s.push(5); + s.push(6); + + + StackUtil.remove(s, 4); + while(!s.isEmpty()){ + System.out.println(s.pop()); + } + + } + + + @Test + public void isValidPairs(){ + + Assert.assertEquals(true, StackUtil.isValidPairs("([e[{{}}df]])")); + + + } + +} diff --git a/group13/413007522/lesson05/src/main/java/cn/xl/jvm/clz/AccessFlag.java b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/clz/AccessFlag.java new file mode 100644 index 0000000000..26d209563c --- /dev/null +++ b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/clz/AccessFlag.java @@ -0,0 +1,25 @@ +package cn.xl.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/group13/413007522/lesson05/src/main/java/cn/xl/jvm/clz/ClassFile.java b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/clz/ClassFile.java new file mode 100644 index 0000000000..384afe2efe --- /dev/null +++ b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/clz/ClassFile.java @@ -0,0 +1,75 @@ +package cn.xl.jvm.clz; + +import cn.xl.jvm.constant.ClassInfo; +import cn.xl.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/group13/413007522/lesson05/src/main/java/cn/xl/jvm/clz/ClassIndex.java b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/clz/ClassIndex.java new file mode 100644 index 0000000000..3f7b12dcdf --- /dev/null +++ b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/clz/ClassIndex.java @@ -0,0 +1,19 @@ +package cn.xl.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/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/ClassInfo.java b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/ClassInfo.java new file mode 100644 index 0000000000..a57c7f31b4 --- /dev/null +++ b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/ClassInfo.java @@ -0,0 +1,24 @@ +package cn.xl.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/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/ConstantInfo.java b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/ConstantInfo.java new file mode 100644 index 0000000000..146329cdf1 --- /dev/null +++ b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/ConstantInfo.java @@ -0,0 +1,29 @@ +package cn.xl.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); + } + +} diff --git a/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/ConstantPool.java b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/ConstantPool.java new file mode 100644 index 0000000000..45c0b54c57 --- /dev/null +++ b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/ConstantPool.java @@ -0,0 +1,29 @@ +package cn.xl.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/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/FieldRefInfo.java b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/FieldRefInfo.java new file mode 100644 index 0000000000..6764edb21d --- /dev/null +++ b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/FieldRefInfo.java @@ -0,0 +1,54 @@ +package cn.xl.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/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/MethodRefInfo.java b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/MethodRefInfo.java new file mode 100644 index 0000000000..0340d77b52 --- /dev/null +++ b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/MethodRefInfo.java @@ -0,0 +1,55 @@ +package cn.xl.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/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/NameAndTypeInfo.java b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/NameAndTypeInfo.java new file mode 100644 index 0000000000..5a13ce1a38 --- /dev/null +++ b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/NameAndTypeInfo.java @@ -0,0 +1,45 @@ +package cn.xl.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/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/NullConstantInfo.java b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/NullConstantInfo.java new file mode 100644 index 0000000000..85b23e1862 --- /dev/null +++ b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/NullConstantInfo.java @@ -0,0 +1,13 @@ +package cn.xl.jvm.constant; + +public class NullConstantInfo extends ConstantInfo { + + public NullConstantInfo(){ + + } + @Override + public int getType() { + return -1; + } + +} diff --git a/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/StringInfo.java b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/StringInfo.java new file mode 100644 index 0000000000..df8c0586f2 --- /dev/null +++ b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/StringInfo.java @@ -0,0 +1,26 @@ +package cn.xl.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/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/UTF8Info.java b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/UTF8Info.java new file mode 100644 index 0000000000..83ba9153cc --- /dev/null +++ b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/constant/UTF8Info.java @@ -0,0 +1,32 @@ +package cn.xl.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/group13/413007522/lesson05/src/main/java/cn/xl/jvm/loader/ByteCodeIterator.java b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/loader/ByteCodeIterator.java new file mode 100644 index 0000000000..41ecfe5411 --- /dev/null +++ b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/loader/ByteCodeIterator.java @@ -0,0 +1,83 @@ +package cn.xl.jvm.loader; + +import java.util.Arrays; + +import cn.xl.jvm.util.Util; + +public class ByteCodeIterator { + + private byte[] bt; + + private int size; + + public int pos = 0; + + public ByteCodeIterator(byte[] b){ + + if(b != null){ + this.bt = b; + this.size = b.length; + } + } + + public boolean hasNext(){ + if(pos < size){ + return true; + }else{ + return false; + } + } + + public Object next(){ + if(bt != null && pos < size){ + return bt[pos++]; + }else{ + return -1; + } + } + + public byte[] nextLenBytes(int len){ + + if(pos+len >= size){ + throw new ArrayIndexOutOfBoundsException(); + } + + byte[] b = Arrays.copyOfRange(bt, pos, pos+len); + pos = pos+len; + + return b; + } + + public int nextU1ToInt(){ + + return Util.byteToInt(new byte[]{bt[pos++]}); + } + + public int nextU2ToInt(){ + + return Util.byteToInt(new byte[]{bt[pos++],bt[pos++]}); + } + + public int nextU4ToInt(){ + + return Util.byteToInt(new byte[]{bt[pos++],bt[pos++],bt[pos++],bt[pos++]}); + } + + + public String nextU4ToHexString(){ + + return Util.byteToHexString(new byte[]{bt[pos++],bt[pos++],bt[pos++],bt[pos++]}); + } + + + public static void main(String[] args){ + + byte[] codes = new byte[]{1,2,3,4,5,6,7}; + + byte[] b = Arrays.copyOfRange(codes, 0, 3); + + for(int i = 0; i < b.length; i++){ + System.out.println(b[i]); + } + } +} diff --git a/group13/413007522/lesson05/src/main/java/cn/xl/jvm/loader/ClassFileLoader.java b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..4c3917a6ab --- /dev/null +++ b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/loader/ClassFileLoader.java @@ -0,0 +1,115 @@ +package cn.xl.jvm.loader; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +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 cn.xl.jvm.clz.ClassFile; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + + public byte[] readBinaryCode(String className) { + + 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; + } + + + public ClassFile loadClass(String className){ + + byte[] codes = readBinaryCode(className); + + ClassFileParser classFileParser = new ClassFileParser(); + ClassFile classFile = classFileParser.parse(codes); + + return classFile; + + } + + + + + private byte[] loadClassFile(String clzFileName){ + + try { + File f = new File(clzFileName); + + return IOUtils.toByteArray(new FileInputStream(f)); + } catch (FileNotFoundException e) { + e.printStackTrace(); + return null; + } catch (IOException e) { + e.printStackTrace(); + return null; + } + + /*InputStream in = null; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try { + // 涓娆¤涓涓瓧鑺 + in = new FileInputStream(file); + int tempbyte; + while ((tempbyte = in.read()) != -1) { + baos.write(tempbyte); + } + in.close(); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + return baos.toByteArray(); */ + + } + + + + public void addClassPath(String path) { + if(this.clzPaths.contains(path)){ + return; + } + clzPaths.add(path); + } + + + + public String getClassPath(){ + /*StringBuffer sbf = new StringBuffer(); + if(clzPaths.size() >= 1){ + sbf.append(clzPaths.get(0)); + } + for(int i = 1; i < clzPaths.size(); i++){ + sbf.append(";"); + sbf.append(clzPaths.get(i)); + } + return sbf.toString(); + */ + + + return StringUtils.join(this.clzPaths, ";"); + + } + + + +} diff --git a/group13/413007522/lesson05/src/main/java/cn/xl/jvm/loader/ClassFileParser.java b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/loader/ClassFileParser.java new file mode 100644 index 0000000000..4f34da4d86 --- /dev/null +++ b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/loader/ClassFileParser.java @@ -0,0 +1,126 @@ +package cn.xl.jvm.loader; + +import java.io.UnsupportedEncodingException; + +import cn.xl.jvm.clz.AccessFlag; +import cn.xl.jvm.clz.ClassFile; +import cn.xl.jvm.clz.ClassIndex; +import cn.xl.jvm.constant.ClassInfo; +import cn.xl.jvm.constant.ConstantPool; +import cn.xl.jvm.constant.FieldRefInfo; +import cn.xl.jvm.constant.MethodRefInfo; +import cn.xl.jvm.constant.NameAndTypeInfo; +import cn.xl.jvm.constant.NullConstantInfo; +import cn.xl.jvm.constant.StringInfo; +import cn.xl.jvm.constant.UTF8Info; + +public class ClassFileParser { + + public ClassFile parse(byte[] codes) { + + ClassFile classFile = new ClassFile(); + + ByteCodeIterator iter = new ByteCodeIterator(codes); + + if(!"cafebabe".equals(iter.nextU4ToHexString())){ + return null; + } + classFile.setMinorVersion(iter.nextU2ToInt()); + classFile.setMajorVersion(iter.nextU2ToInt()); + classFile.setAccessFlag(this.parseAccessFlag(iter)); + classFile.setClassIndex(this.parseClassIndex(iter)); + classFile.setConstPool(this.parseConstantPool(iter)); + + + return classFile; + } + + + + private AccessFlag parseAccessFlag(ByteCodeIterator iter) { + + /*AccessFlag accessFlag = new AccessFlag(iter.nextU2ToInt()); + + return accessFlag;*/ + return null; + } + + private ClassIndex parseClassIndex(ByteCodeIterator iter) { + + return null; + + } + + private ConstantPool parseConstantPool(ByteCodeIterator iter) { + + int constantCount = iter.nextU2ToInt(); + System.out.println("constantPool count锛"+constantCount); + + ConstantPool constantPool = new ConstantPool(); + constantPool.addConstantInfo(new NullConstantInfo()); + + for(int i = 0; i < constantCount; i++){ + int tag = iter.nextU1ToInt(); + if(tag == 7){ + + int utf8Index = iter.nextU2ToInt(); + ClassInfo classInfo = new ClassInfo(constantPool); + classInfo.setUtf8Index(utf8Index); + constantPool.addConstantInfo(classInfo); + }else if(tag == 1){ + + int length = iter.nextU2ToInt(); + UTF8Info utf8Info = new UTF8Info(constantPool); + utf8Info.setLength(length); + byte[] b = iter.nextLenBytes(length); + String strValue = null; + try { + strValue = new String(b,"UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + + utf8Info.setValue(strValue); + constantPool.addConstantInfo(utf8Info); + }else if(tag == 8){ + + int index = iter.nextU2ToInt(); + StringInfo stringInfo = new StringInfo(constantPool); + stringInfo.setIndex(index); + constantPool.addConstantInfo(stringInfo); + }else if(tag == 9){ + + int classInfoindex = iter.nextU2ToInt(); + int nameTypeIndex = iter.nextU2ToInt(); + FieldRefInfo filedRefInfo = new FieldRefInfo(constantPool); + filedRefInfo.setClassInfoIndex(classInfoindex); + filedRefInfo.setNameAndTypeIndex(nameTypeIndex); + constantPool.addConstantInfo(filedRefInfo); + }else if(tag == 10){ + + int classInfoindex = iter.nextU2ToInt(); + int nameTypeIndex = iter.nextU2ToInt(); + MethodRefInfo methodRefInfo = new MethodRefInfo(constantPool); + methodRefInfo.setClassInfoIndex(classInfoindex); + methodRefInfo.setNameAndTypeIndex(nameTypeIndex); + constantPool.addConstantInfo(methodRefInfo); + }else if(tag == 12){ + + int nameIndex = iter.nextU2ToInt(); + int descIndex = iter.nextU2ToInt(); + NameAndTypeInfo nameAndTypeInfo = new NameAndTypeInfo(constantPool); + nameAndTypeInfo.setIndex1(nameIndex); + nameAndTypeInfo.setIndex2(descIndex); + constantPool.addConstantInfo(nameAndTypeInfo); + }else{ + //throw new RuntimeException("the constant Pool tag "+tag+" has not been implement yet锛"); + } + + } + + return constantPool; + } + + + +} diff --git a/group13/413007522/lesson05/src/main/java/cn/xl/jvm/loader/EmployeeV1.java b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/loader/EmployeeV1.java new file mode 100644 index 0000000000..93642d6066 --- /dev/null +++ b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/loader/EmployeeV1.java @@ -0,0 +1,28 @@ +package cn.xl.jvm.loader; + +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(); + + } +} \ No newline at end of file diff --git a/group13/413007522/lesson05/src/main/java/cn/xl/jvm/util/Util.java b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/util/Util.java new file mode 100644 index 0000000000..32e0200220 --- /dev/null +++ b/group13/413007522/lesson05/src/main/java/cn/xl/jvm/util/Util.java @@ -0,0 +1,43 @@ +package cn.xl.jvm.util; + +import java.math.BigDecimal; + +public class Util { + public static int byteToInt(byte[] codes){ + String s1 = byteToHexString(codes); + return Integer.valueOf(s1, 16).intValue(); + } + + public static BigDecimal byteToFloat(byte[] codes){ + String s1 = byteToHexString(codes); + int bits = Integer.valueOf(s1, 16).intValue(); + int s = ((bits >> 31) == 0) ? 1 : -1; + int e = ((bits >> 23) & 0xff); + int m =(e == 0) ?(bits & 0x7fffff) << 1 :(bits & 0x7fffff) | 0x800000; + + double f = Math.pow(2, e-150); + + BigDecimal bs = new BigDecimal(Double.toString(s)); + BigDecimal bm = new BigDecimal(Double.toString(m)); + BigDecimal bf = new BigDecimal(Double.toString(f)); + + return bs.multiply(bm).multiply(bf); + + + + } + + public static String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + 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());*/ + } + + +}