From 680b56b3bc51ea6de6453d81422738e22026cef1 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sun, 9 Apr 2017 18:47:56 +0800 Subject: [PATCH] =?UTF-8?q?stackutil=E5=8F=8Ajvm=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E5=B8=B8=E9=87=8F=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group24/121111914/.classpath | 10 ++ group24/121111914/.project | 17 ++ .../coding2017/basic/{ => stack}/Stack.java | 74 +++++---- .../coding2017/basic/stack/StackUtil.java | 133 ++++++++++++++++ .../coding2017/basic/test/StackTest.java | 2 +- .../coding2017/basic/test/StackUtilTest.java | 73 +++++++++ .../coding2017/minijvm/clz/AccessFlag.java | 37 +++++ .../coding2017/minijvm/clz/ClassFile.java | 75 +++++++++ .../coding2017/minijvm/clz/ClassIndex.java | 21 +++ .../minijvm/constant/ClassInfo.java | 26 +++ .../minijvm/constant/ConstantInfo.java | 29 ++++ .../minijvm/constant/ConstantPool.java | 29 ++++ .../minijvm/constant/FieldRefInfo.java | 54 +++++++ .../minijvm/constant/MethodRefInfo.java | 55 +++++++ .../minijvm/constant/NameAndTypeInfo.java | 45 ++++++ .../minijvm/constant/NullConstantInfo.java | 13 ++ .../minijvm/constant/StringInfo.java | 26 +++ .../coding2017/minijvm/constant/UTF8Info.java | 32 ++++ .../minijvm/loader/ByteCodeIterator.java | 28 ++++ .../minijvm/loader/ClassFileLoader.java | 141 ++++++++++++---- .../minijvm/loader/ClassFileLoader1.java | 125 +++++++++++++++ .../minijvm/loader/ClassFileParser.java | 149 +++++++++++++++++ .../minijvm/test/ClassFileloaderTest.java | 150 +++++++++++++++--- .../ipk2015/coding2017/minijvm/util/Util.java | 26 +++ 24 files changed, 1283 insertions(+), 87 deletions(-) create mode 100644 group24/121111914/.classpath create mode 100644 group24/121111914/.project rename group24/121111914/src/com/github/ipk2015/coding2017/basic/{ => stack}/Stack.java (60%) create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/basic/stack/StackUtil.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/basic/test/StackUtilTest.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/minijvm/clz/AccessFlag.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/minijvm/clz/ClassFile.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/minijvm/clz/ClassIndex.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/ClassInfo.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/ConstantInfo.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/ConstantPool.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/FieldRefInfo.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/MethodRefInfo.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/NameAndTypeInfo.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/NullConstantInfo.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/StringInfo.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/UTF8Info.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ByteCodeIterator.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ClassFileLoader1.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ClassFileParser.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/minijvm/util/Util.java diff --git a/group24/121111914/.classpath b/group24/121111914/.classpath new file mode 100644 index 0000000000..3e9442280c --- /dev/null +++ b/group24/121111914/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/group24/121111914/.project b/group24/121111914/.project new file mode 100644 index 0000000000..5fb6378a67 --- /dev/null +++ b/group24/121111914/.project @@ -0,0 +1,17 @@ + + + JavaImp2017 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/Stack.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/stack/Stack.java similarity index 60% rename from group24/121111914/src/com/github/ipk2015/coding2017/basic/Stack.java rename to group24/121111914/src/com/github/ipk2015/coding2017/basic/stack/Stack.java index 4dae60e12b..ddb9b62dda 100644 --- a/group24/121111914/src/com/github/ipk2015/coding2017/basic/Stack.java +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/stack/Stack.java @@ -1,32 +1,42 @@ -package com.github.ipk2015.coding2017.basic; - -import java.util.EmptyStackException; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - elementData.add(o); - } - - public Object pop(){ - if(isEmpty()){ - throw new EmptyStackException(); - } - Object data=elementData.remove(size()-1); - return data; - } - - public Object peek(){ - if(isEmpty()){ - throw new EmptyStackException(); - } - return elementData.get(size()-1); - } - public boolean isEmpty(){ - return size()==0; - } - public int size(){ - return elementData.size(); - } -} +package com.github.ipk2015.coding2017.basic.stack; + +import java.util.EmptyStackException; + +import com.github.ipk2015.coding2017.basic.ArrayList; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(isEmpty()){ + throw new EmptyStackException(); + } + Object data=elementData.remove(size()-1); + return data; + } + + public Object peek(){ + if(isEmpty()){ + throw new EmptyStackException(); + } + return elementData.get(size()-1); + } + public boolean isEmpty(){ + return size()==0; + } + public int size(){ + return elementData.size(); + } + public String toString(){ + StringBuffer buffer=new StringBuffer(); + int size=elementData.size(); + for(int i=0;is.size()){ + throw new RuntimeException("len超出范围"); + } + Stack tempStack=new Stack(); + for(int i=0;i0){ + stack.push(flag); + }else if(flag<0){ + if(stack.size()==0){ + return false; + } + Integer peek = (Integer)stack.peek(); + if(peek+flag==0){ + stack.pop(); + }else{ + return false; + } + } + } + if(stack.size()>0){ + return false; + } + return true; + } + private static int switchChar(char c){ + int result=0; + switch(c){ + case '(': + result=1; + break; + case ')': + result=-1; + break; + case '[': + result=2; + break; + case ']': + result=-2; + break; + case '{': + result=3; + break; + case '}': + result=-3; + break; + default: + break; + } + return result; + } + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/StackTest.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/StackTest.java index 7cc10b77df..dbb28a9df2 100644 --- a/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/StackTest.java +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/StackTest.java @@ -5,7 +5,7 @@ import org.junit.Before; import org.junit.Test; -import com.github.ipk2015.coding2017.basic.Stack; +import com.github.ipk2015.coding2017.basic.stack.Stack; public class StackTest { Stack stack; diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/StackUtilTest.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/StackUtilTest.java new file mode 100644 index 0000000000..e1a2b7e42c --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/StackUtilTest.java @@ -0,0 +1,73 @@ +package com.github.ipk2015.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.ipk2015.coding2017.basic.stack.Stack; +import com.github.ipk2015.coding2017.basic.stack.StackUtil; + +public class StackUtilTest { + Stack stack=null; + @Before + public void setUp() throws Exception { + stack=new Stack(); + } + + @Test + public void testReverse() { + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + StackUtil.reverse(stack); + assertEquals("4,3,2,1,", stack.toString()); + } + + @Test + public void testRemove() { + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + StackUtil.remove(stack, 2); + assertEquals("1,3,4,", stack.toString()); + StackUtil.remove(stack, 5); + assertEquals("1,3,4,", stack.toString()); + } + + @Test + public void testGetTop() { + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + Object[] array=StackUtil.getTop(stack, 3); + assertEquals("1,2,3,4,", stack.toString()); + StringBuffer buffer=new StringBuffer(); + for(int i=0;i 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/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/FieldRefInfo.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/FieldRefInfo.java new file mode 100644 index 0000000000..b74fb427ba --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/FieldRefInfo.java @@ -0,0 +1,54 @@ +package com.github.ipk2015.coding2017.minijvm.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/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/MethodRefInfo.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/MethodRefInfo.java new file mode 100644 index 0000000000..cc5352db6e --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/MethodRefInfo.java @@ -0,0 +1,55 @@ +package com.github.ipk2015.coding2017.minijvm.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/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/NameAndTypeInfo.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/NameAndTypeInfo.java new file mode 100644 index 0000000000..d1cd005111 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/NameAndTypeInfo.java @@ -0,0 +1,45 @@ +package com.github.ipk2015.coding2017.minijvm.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/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/NullConstantInfo.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/NullConstantInfo.java new file mode 100644 index 0000000000..38eef91f32 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/NullConstantInfo.java @@ -0,0 +1,13 @@ +package com.github.ipk2015.coding2017.minijvm.constant; + +public class NullConstantInfo extends ConstantInfo { + + public NullConstantInfo(){ + + } + @Override + public int getType() { + return -1; + } + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/StringInfo.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/StringInfo.java new file mode 100644 index 0000000000..8f23231e72 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/StringInfo.java @@ -0,0 +1,26 @@ +package com.github.ipk2015.coding2017.minijvm.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/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/UTF8Info.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/UTF8Info.java new file mode 100644 index 0000000000..d94a267bbc --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/constant/UTF8Info.java @@ -0,0 +1,32 @@ +package com.github.ipk2015.coding2017.minijvm.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/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ByteCodeIterator.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ByteCodeIterator.java new file mode 100644 index 0000000000..85e970c64a --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ByteCodeIterator.java @@ -0,0 +1,28 @@ +package com.github.ipk2015.coding2017.minijvm.loader; + +import java.util.Arrays; + +import com.github.ipk2015.coding2017.minijvm.util.Util; + +public class ByteCodeIterator { + private byte[] byteArray; + int pos=0; + + public ByteCodeIterator(byte[] codes){ + this.byteArray=codes; + } + + public int nextUNToInt(int n){ + return Util.byteToInt(nextUNToArray(n)); + } + + public String nextUNToHexString(int n){ + return Util.byteToHexString(nextUNToArray(n)); + } + + public byte[] nextUNToArray(int n){ + byte[] bytes=Arrays.copyOfRange(byteArray, pos, pos+n); + pos=pos+n; + return bytes; + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ClassFileLoader.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ClassFileLoader.java index ab7f54a796..2f854174dd 100644 --- a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ClassFileLoader.java +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ClassFileLoader.java @@ -6,67 +6,138 @@ import java.io.ByteArrayOutputStream; 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 com.github.ipk2015.coding2017.minijvm.clz.ClassFile; + + + + + public class ClassFileLoader { private List clzPaths = new ArrayList(); - public byte[] readBinaryCode(String className) throws IOException { - className=getCompleteClassName(className); - File file=null; - for(String path:clzPaths){ - file=new File(path+"\\"+className); - if(file.exists()){ - break; - } + 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; + } } - if(null==file){ - throw new FileNotFoundException(className); - } - ByteArrayOutputStream bos=new ByteArrayOutputStream((int)file.length()); - BufferedInputStream in=new BufferedInputStream(new FileInputStream(file)); - int size=1024; - byte[] buffer=new byte[size]; - int length=0; - while((length=in.read(buffer, 0, size))!=-1){ - bos.write(buffer,0,length); + + 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; } - return bos.toByteArray(); } + public void addClassPath(String path) { - clzPaths.add(path); + if(this.clzPaths.contains(path)){ + return; + } + + this.clzPaths.add(path); + } public String getClassPath(){ - StringBuffer buffer=new StringBuffer(); - for(String path:clzPaths){ - buffer.append(path+";"); - } - buffer.deleteCharAt(buffer.length()-1); - return buffer.toString(); + return StringUtils.join(this.clzPaths,";"); } - private String getCompleteClassName(String name){ - if(!name.endsWith(".class")){ - name=name+".class"; + 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-1){ - name=name.substring(pointPos+1); + return buffer.toString(); + } + + private byte[] loadClassFile_V1(String clzFileName) { + + BufferedInputStream bis = null; + + try { + + File f = new File(clzFileName); + + + bis = new BufferedInputStream(new FileInputStream(f)); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + + byte[] buffer = new byte[1024]; + int length = -1; + + while((length = bis.read(buffer)) != -1){ + bos.write(buffer, 0, length); + } + + byte [] codes = bos.toByteArray(); + + return codes; + + } catch(IOException e){ + e.printStackTrace(); + + } finally{ + if(bis != null){ + try { + bis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } - return name; + return null; + } + -} +} \ No newline at end of file diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ClassFileLoader1.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ClassFileLoader1.java new file mode 100644 index 0000000000..6f5e68c4a3 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/loader/ClassFileLoader1.java @@ -0,0 +1,125 @@ +package com.github.ipk2015.coding2017.minijvm.loader; + + + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + + + +public class ClassFileLoader1 { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) throws IOException { + className=getCompleteClassName(className); + File file=null; + for(String path:clzPaths){ + file=new File(path+"\\"+className); + if(file.exists()){ + break; + } + } + if(null==file){ + throw new FileNotFoundException(className); + } + ByteArrayOutputStream bos=new ByteArrayOutputStream((int)file.length()); + BufferedInputStream in=new BufferedInputStream(new FileInputStream(file)); + int size=1024; + byte[] buffer=new byte[size]; + int length=0; + while((length=in.read(buffer, 0, size))!=-1){ + bos.write(buffer,0,length); + } + return bos.toByteArray(); + } + + + public void addClassPath(String path) { + clzPaths.add(path); + } + + + + public String getClassPath(){ + StringBuffer buffer=new StringBuffer(); + for(String path:clzPaths){ + buffer.append(path+";"); + } + buffer.deleteCharAt(buffer.length()-1); + return buffer.toString(); + } + + private String getCompleteClassName(String name){ + if(!name.endsWith(".class")){ + name=name+".class"; + } + int pointPos=name.lastIndexOf(".", name.length()-7); + if(pointPos>-1){ + name=name.substring(pointPos+1); + } + return name; + } + // ------------------------------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/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/util/Util.java b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/util/Util.java new file mode 100644 index 0000000000..b8eaef98ef --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/minijvm/util/Util.java @@ -0,0 +1,26 @@ +package com.github.ipk2015.coding2017.minijvm.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