diff --git a/group06/1378560653/.classpath b/group06/1378560653/.classpath index 036cc56d25..7bd01a193c 100644 --- a/group06/1378560653/.classpath +++ b/group06/1378560653/.classpath @@ -1,8 +1,11 @@ + + + diff --git a/group06/1378560653/article.txt b/group06/1378560653/article.txt index 42b64998ea..5d908b43c6 100644 --- a/group06/1378560653/article.txt +++ b/group06/1378560653/article.txt @@ -2,4 +2,5 @@ �ڶ�ƪ���£�http://blog.csdn.net/raymond120/article/details/58043040 ����ƪ���£�http://blog.csdn.net/raymond120/article/details/60759278 ����ƪ���£�http://blog.csdn.net/raymond120/article/details/61937892 -����ƪ���£�http://blog.csdn.net/raymond120/article/details/68665071 \ No newline at end of file +����ƪ���£�http://blog.csdn.net/raymond120/article/details/68665071 +����ƪ���£�http://blog.csdn.net/raymond120/article/details/68958532 \ No newline at end of file diff --git a/group06/1378560653/mini-jvm/com/coderising/jvm/clz/AccessFlag.java b/group06/1378560653/mini-jvm/com/coderising/jvm/clz/AccessFlag.java new file mode 100644 index 0000000000..faae056835 --- /dev/null +++ b/group06/1378560653/mini-jvm/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/group06/1378560653/mini-jvm/com/coderising/jvm/clz/ClassFile.java b/group06/1378560653/mini-jvm/com/coderising/jvm/clz/ClassFile.java new file mode 100644 index 0000000000..650ca8375d --- /dev/null +++ b/group06/1378560653/mini-jvm/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/group06/1378560653/mini-jvm/com/coderising/jvm/clz/ClassIndex.java b/group06/1378560653/mini-jvm/com/coderising/jvm/clz/ClassIndex.java new file mode 100644 index 0000000000..e424f284b3 --- /dev/null +++ b/group06/1378560653/mini-jvm/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/group06/1378560653/mini-jvm/com/coderising/jvm/constant/ClassInfo.java b/group06/1378560653/mini-jvm/com/coderising/jvm/constant/ClassInfo.java new file mode 100644 index 0000000000..aea9048ea4 --- /dev/null +++ b/group06/1378560653/mini-jvm/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/group06/1378560653/mini-jvm/com/coderising/jvm/constant/ConstantInfo.java b/group06/1378560653/mini-jvm/com/coderising/jvm/constant/ConstantInfo.java new file mode 100644 index 0000000000..466b072244 --- /dev/null +++ b/group06/1378560653/mini-jvm/com/coderising/jvm/constant/ConstantInfo.java @@ -0,0 +1,29 @@ +package com.coderising.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/group06/1378560653/mini-jvm/com/coderising/jvm/constant/ConstantPool.java b/group06/1378560653/mini-jvm/com/coderising/jvm/constant/ConstantPool.java new file mode 100644 index 0000000000..86c0445695 --- /dev/null +++ b/group06/1378560653/mini-jvm/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/group06/1378560653/mini-jvm/com/coderising/jvm/constant/FieldRefInfo.java b/group06/1378560653/mini-jvm/com/coderising/jvm/constant/FieldRefInfo.java new file mode 100644 index 0000000000..65475e194c --- /dev/null +++ b/group06/1378560653/mini-jvm/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/group06/1378560653/mini-jvm/com/coderising/jvm/constant/MethodRefInfo.java b/group06/1378560653/mini-jvm/com/coderising/jvm/constant/MethodRefInfo.java new file mode 100644 index 0000000000..7f05870020 --- /dev/null +++ b/group06/1378560653/mini-jvm/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/group06/1378560653/mini-jvm/com/coderising/jvm/constant/NameAndTypeInfo.java b/group06/1378560653/mini-jvm/com/coderising/jvm/constant/NameAndTypeInfo.java new file mode 100644 index 0000000000..402f9dec86 --- /dev/null +++ b/group06/1378560653/mini-jvm/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/group06/1378560653/mini-jvm/com/coderising/jvm/constant/NullConstantInfo.java b/group06/1378560653/mini-jvm/com/coderising/jvm/constant/NullConstantInfo.java new file mode 100644 index 0000000000..936736016f --- /dev/null +++ b/group06/1378560653/mini-jvm/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/group06/1378560653/mini-jvm/com/coderising/jvm/constant/StringInfo.java b/group06/1378560653/mini-jvm/com/coderising/jvm/constant/StringInfo.java new file mode 100644 index 0000000000..f1f8eb4ed4 --- /dev/null +++ b/group06/1378560653/mini-jvm/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/group06/1378560653/mini-jvm/com/coderising/jvm/constant/UTF8Info.java b/group06/1378560653/mini-jvm/com/coderising/jvm/constant/UTF8Info.java new file mode 100644 index 0000000000..5cac9f04f7 --- /dev/null +++ b/group06/1378560653/mini-jvm/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/group06/1378560653/mini-jvm/com/coderising/jvm/loader/ByteCodeIterator.java b/group06/1378560653/mini-jvm/com/coderising/jvm/loader/ByteCodeIterator.java new file mode 100644 index 0000000000..9293f1706e --- /dev/null +++ b/group06/1378560653/mini-jvm/com/coderising/jvm/loader/ByteCodeIterator.java @@ -0,0 +1,40 @@ +package com.coderising.jvm.loader; + +import java.util.Arrays; + +import com.coderising.jvm.util.Util; + +public class ByteCodeIterator { + byte[] codes; + int pos = 0; + + public ByteCodeIterator(byte[] codes) { + this.codes = codes; + } + + public String nextU4ToHexString() { + return Util.byteToHexString(new byte[]{codes[pos++],codes[pos++],codes[pos++],codes[pos++]}); + } + + 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 byte[] getBytes(int length) { + if(pos + length >= codes.length){ + throw new ArrayIndexOutOfBoundsException(); + } + byte[] data = Arrays.copyOfRange(codes, pos, pos+length); + pos = pos + length; + return data; + } + +} diff --git a/group06/1378560653/mini-jvm/com/coderising/jvm/loader/ClassFileLoader.java b/group06/1378560653/mini-jvm/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..12e7096bc5 --- /dev/null +++ b/group06/1378560653/mini-jvm/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,137 @@ +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; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; + +import com.coderising.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; + + + + } + + 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; + } + + 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", 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/group06/1378560653/src/com/coderising/jvm/test/EmployeeV1.java b/group06/1378560653/mini-jvm/com/coderising/jvm/test/EmployeeV1.java similarity index 100% rename from group06/1378560653/src/com/coderising/jvm/test/EmployeeV1.java rename to group06/1378560653/mini-jvm/com/coderising/jvm/test/EmployeeV1.java diff --git a/group06/1378560653/mini-jvm/com/coderising/jvm/util/Util.java b/group06/1378560653/mini-jvm/com/coderising/jvm/util/Util.java new file mode 100644 index 0000000000..d1e7ec9e64 --- /dev/null +++ b/group06/1378560653/mini-jvm/com/coderising/jvm/util/Util.java @@ -0,0 +1,22 @@ +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;i clzPaths = new ArrayList(); - - public byte[] readBinaryCode(String className) { - String classDirName = className.replace('.', '\\')+".class"; - String clzpath = getClassPath()+"\\"+ classDirName; - try { - FileInputStream clz = new FileInputStream(clzpath); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - int flag = 0; - while((flag = clz.read())!=-1){ - baos.write(flag); - } - clz.close(); - return baos.toByteArray(); - } catch (IOException e) { - e.printStackTrace(); - } - return null; - } - - - public void addClassPath(String path) { - File file = new File(path); - if(file.exists()){ - clzPaths.add(path); - } else { - throw new IllegalArgumentException("路径:"+path+"不存在"); - } - } - - - public String getClassPath(){ - if(clzPaths.isEmpty()){ - return " "; - } - - StringBuffer buf = new StringBuffer(); - for (int i = 0; i < clzPaths.size(); i++) { - buf.append(clzPaths.get(i)); - if(i != (clzPaths.size() - 1)){ - buf.append(";"); - } - } - return buf.toString(); - } - -} \ No newline at end of file diff --git a/group06/1378560653/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group06/1378560653/src/com/coderising/jvm/test/ClassFileloaderTest.java deleted file mode 100644 index 3ace0974f5..0000000000 --- a/group06/1378560653/src/com/coderising/jvm/test/ClassFileloaderTest.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.coderising.jvm.test; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.coderising.jvm.loader.ClassFileLoader; - - -public class ClassFileloaderTest { - - - static String path1 = "H:\\github\\coding2017\\group06\\1378560653\\bin"; - static String path2 = "C:\\Temp"; - - - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testClassPath(){ - - ClassFileLoader loader = new ClassFileLoader(); - loader.addClassPath(path1); - loader.addClassPath(path2); - - String clzPath = loader.getClassPath(); - - Assert.assertEquals(path1+";"+path2,clzPath); - - } - - @Test - public void testClassFileLength() { - - ClassFileLoader loader = new ClassFileLoader(); - loader.addClassPath(path1); - - String className = "com.coderising.jvm.test.EmployeeV1"; - - byte[] byteCodes = loader.readBinaryCode(className); - - // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 - Assert.assertEquals(1056, byteCodes.length); - - } - - - @Test - public void testMagicNumber(){ - ClassFileLoader loader = new ClassFileLoader(); - loader.addClassPath(path1); - String className = "com.coderising.jvm.test.EmployeeV1"; - byte[] byteCodes = loader.readBinaryCode(className); - byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; - - - String acctualValue = this.byteToHexString(codes); - - Assert.assertEquals("cafebabe", acctualValue); - } - - - private String byteToHexString(byte[] codes ){ - StringBuffer buffer = new StringBuffer(); - for(int i=0;i s) { + if(s.isEmpty()){ + return; + } + + Stack temp1 = new Stack<>(); + Stack temp2 = new Stack<>(); + + while(!s.isEmpty()){ + temp1.push(s.pop()); + } + + while(!temp1.isEmpty()){ + temp2.push(temp1.pop()); + } + + while(!temp2.isEmpty()){ + s.push(temp2.pop()); + } + } + + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param o + */ + public static void remove(Stack s,Object o) { + if(s.isEmpty()){ + return; + } + + Stack temp = new Stack<>(); + + while(!s.isEmpty()){ + Object data = s.pop(); + if(o.equals(data)){ + continue; + } + temp.push(data); + } + + while(!temp.isEmpty()){ + s.push(temp.pop()); + } + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param len + * @return + */ + public static Object[] getTop(Stack s,int len) { + if(s.isEmpty()){ + return new Object[0]; + } + + if(len >= s.size()){ + len = s.size(); + } + + Object[] result = new Object[len]; + + Stack temp = new Stack<>(); + int count = 0; + while(!s.isEmpty()){ + Object data = s.pop(); + temp.push(data); + result[count++] = data; + if(count == len){ + break; + } + } + + while(!temp.isEmpty()){ + s.push(temp.pop()); + } + + return result; + } + /** + * 字符串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){ + if(s.length() == 0){ + return false; + } + + Stack temp1 = new Stack<>(); + Stack temp2 = new Stack<>(); + + for(int i = 0, j = s.length()-1; i <= j; i++,j--){ + temp1.push(s.charAt(i)); + temp2.push(s.charAt(j)); + } + + while(!temp1.isEmpty()&&!temp2.isEmpty()){ + char a = temp1.pop(); + char b = temp2.pop(); + if(a == '('){ + if(b != ')'){ + return false; + } + } + if(a == '[') { + if(b != ']'){ + return false; + } + } + if(a == '{'){ + if(b != '}'){ + return false; + } + } + } + return true; + } + + public static String toString(Stack s) { + StringBuffer buffer = new StringBuffer(); + buffer.append("["); + while(!s.isEmpty()){ + buffer.append(s.pop()); + if(s.size()!=0){ + buffer.append(","); + } + } + buffer.append("]"); + return buffer.toString(); + } +} \ No newline at end of file diff --git a/group06/1378560653/src/com/coding/basic/stack/StackUtilTest.java b/group06/1378560653/src/com/coding/basic/stack/StackUtilTest.java new file mode 100644 index 0000000000..dd7a461ac4 --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/stack/StackUtilTest.java @@ -0,0 +1,82 @@ +package com.coding.basic.stack; + +import static org.junit.Assert.*; +import java.util.Stack; +import org.junit.Assert; +import org.junit.Test; + +public class StackUtilTest { + + @Test + public void testReverse() { + Stack s = new Stack<>(); + StackUtil.reverse(s); + Assert.assertEquals("[]", s.toString()); + + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + + StackUtil.reverse(s); + Assert.assertEquals("[1,2,3,4,5]", StackUtil.toString(s)); + } + @Test + public void testRemove() { + Stack s = new Stack<>(); + StackUtil.remove(s,3); + Assert.assertEquals("[]", s.toString()); + { + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + + StackUtil.remove(s,3); + Assert.assertEquals("[5,4,2,1]", StackUtil.toString(s)); + } + { + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + + StackUtil.remove(s,7); + Assert.assertEquals("[5,4,3,2,1]", StackUtil.toString(s)); + } + } + @Test + public void testGetTop() { + Stack s = new Stack<>(); + assertArrayEquals(new Object[0], StackUtil.getTop(s,3)); + + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + + int[] expected = {5,4,3}; + Object[] result = StackUtil.getTop(s,3); + int[] actual = new int[result.length]; + for(int i = 0; i < result.length; i++){ + actual[i] = (int)result[i]; + } + + assertArrayEquals(expected, actual); + } + + @Test + public void testIsValidPairs(){ + String s1 = "([e{d}f])"; + assertTrue(StackUtil.isValidPairs(s1)); + + String s2 = "([b{x]y})"; + assertFalse(StackUtil.isValidPairs(s2)); + } +} + +