From 4c61b24c65f4c10d0ec206108ee2bfb8b94306a6 Mon Sep 17 00:00:00 2001 From: x_zhaohu Date: Tue, 7 Mar 2017 21:41:52 +0800 Subject: [PATCH 01/41] xiaoti --- .../DataStructure/src/main/java/com/Main.java | 57 +------ .../basic/.LinkedList.java.swp} | Bin 12288 -> 16384 bytes .../java/com/coding/basic/LinkedList.java | 156 +++++++++++++++++- 3 files changed, 164 insertions(+), 49 deletions(-) rename group11/1178243325/DataStructure/src/main/java/com/{coderising/litestruts/.Struts.java.swp => coding/basic/.LinkedList.java.swp} (57%) diff --git a/group11/1178243325/DataStructure/src/main/java/com/Main.java b/group11/1178243325/DataStructure/src/main/java/com/Main.java index f5e5a36ebd..8d49de33c4 100644 --- a/group11/1178243325/DataStructure/src/main/java/com/Main.java +++ b/group11/1178243325/DataStructure/src/main/java/com/Main.java @@ -1,56 +1,17 @@ package com; -import java.util.*; -import com.coderising.litestruts.*; -import com.coderising.array.*; +import com.coding.basic.LinkedList; +import com.coding.basic.Iterator; public class Main { public static void main(String[] args) { - int[] array = {1, 2, 3, 4, 5}; - System.out.print("reverseArray测试:"); - ArrayUtil.reverseArray(array); - for (int i : array) - System.out.print(i + " "); - System.out.print("\nremoveZero测试:"); + LinkedList list = new LinkedList(); - int[] oldArray = {1, 3, 4, 5, 0, 0, 8 , 0, 9}; - oldArray = ArrayUtil.removeZero(oldArray); - for (int i : oldArray) { - System.out.print(i + " "); + for(int i = 0; i < 10; i++) + list.add(i); + list.remove(2, 5); + Iterator iter = list.iterator(); + while(iter.hasNext()) { + System.out.println(iter.next()); } - - System.out.print("\nmerge测试:"); - int[] a1 = {3, 5,8}; - int[] a2 = {4, 5, 6,7}; - int[] arrays = ArrayUtil.merge(a1, a2); - for (int i : arrays) - System.out.print(i + " "); - - System.out.print("\ngrow测试:"); - - int[] growArray = ArrayUtil.grow(a1, 5); - for (int i : growArray) - System.out.print(i + " "); - - System.out.print("\nfibonacci测试"); - int[] fArray = ArrayUtil.fibonacci(1); - System.out.print(fArray); - System.out.println(); - fArray = ArrayUtil.fibonacci(15); - for (int i : fArray) - System.out.print(i + " "); - System.out.print("\ngetPrimes测试:"); - int[] primesArray = ArrayUtil.getPrimes(23); - for (int i : primesArray) - System.out.print(i + " "); - System.out.print("\ngetPerfectNumbers测试:"); - int[] pArray = ArrayUtil.getPerfectNumbers(100); - for (int i : pArray) - System.out.print(i + " "); - System.out.print("\njoin测试:"); - int[] jArray = new int[]{2, 3, 8}; - System.out.print(ArrayUtil.join(jArray, "-")); - Map map = new HashMap<>(); - Struts.runAction("login", map); - } } diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/.Struts.java.swp b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/.LinkedList.java.swp similarity index 57% rename from group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/.Struts.java.swp rename to group11/1178243325/DataStructure/src/main/java/com/coding/basic/.LinkedList.java.swp index 1f45a5f25e9bf8dcf03addb1f9a3a0a4c6fe0a2f..7ea2e295d458709c233e753f5a1bc442a43cead0 100644 GIT binary patch literal 16384 zcmeI3X>1(j8OH}o8@67wmxVn7)tU2c7(n$t?R~W{|?lboveCT?kqrot;4H+lpO2UW# zebjtU>WpQNSWB$3M7qY74<}VuQ68UMR+mXPHpJs)@%ZE^6_X}Tthljky5(B;x|zm= z+nBM-vYA9#z2&5W;`M&%vT7^qB+6zxsl|59Y$xlMO8p<+<0v&!;8UhRqgz`tWwbG| zB3>>d|Js-v%&We1-=|E3Xt`|4;#|#xSm; zUw@SV0MGw}{eS+8hT*}humw7y3F=`!+yvv{n=lqGgTIY7j0^B*_&NLt9)Vi84X%U> zmm9`A@HX^99$tr|@GLBaYIqRl!%UbC)8Ol1z-wPHjMH!mcEeg&1FIncw}1iff8H>D z4JTkTtOpk!g)}UH*)R)c!dSQtu7zvh!ewXxZ^1cu9ge~wcn;d35i;;7d(fi@JDg0`EgDxzTC6sm zF^hN!#&pc7W_cxto6SVJG3Az3@MFS+apvPvg=r_Vw)*;?)HHR3s+?N0NHt)7!>llG zpi$j~^g*AWYG$3sY{J-)SXli^e_9@XYaL@?m=WSLFBXO7d+YFcJf)Zy1*CB9%zDb7 zKec06d9&CYv9KwX>xOmR-A%dfAA4<^`!}8Gd*+}tvbN1@Ti3txpx3g-7-|bARbwxk zFhS|>+Vu^kHM|Ez$J5Cgc_V0nN?%w=CPn2+*~?rxxpa|}w2KO-M8i~LGO1;h(-c@_ zTQ!nbRmJb}O4QP(3%4G$D1w_MrfxWBPkQy=bwc@*43etiQpH@}g;IkEb!}etBX+`F zurL@lNhy1&>7SM!i3CG@V2Pc{+SooeV0&#otvrS`Co0>E;m!W0HNB^gV0LfAHc2w$ zy zq}UwP=a@8DI3s2aYqRH8AHu~Y6G zUj3_e5@?Z54b(p-f4Iv#`byvP&*YCkli#&AclJoW^A)e7HGg>Dn1ObJ>Sc0^8q$nE z6@1aflaznNVz& zsA92!R~=`$cE)nk8God;PRdH+GF&SB5iuEjZ=njVVq(~kFKQngUZ3gIHze(P%#<~Q zHq>O6&iLZVYBap0x^DWWD3o*x-Bg}FP=~&16-gIM;$q&xM%2l(Ex8v_1tk3j8Xph#EzTGeZX;ixx7d$?*vfKrfyCWGZ+|sP|$Y zJ!}2D7=HkAP}e}opl?$Or-IuQbL?2T$(*Vsl(kzXe{lcFkGUd-U!i{+6I@wT(tsxf zJ@h9oWyL(YO9jUmxF#=^;sw`YcB|_2U59)vN*9}hi(v!&S@)=rGSpBXi>Imi^ns2R zOzQz3<7Ou`S@m2Uv?Rztd^atW9#%qmx4K!3#YB3BDvO{JM{Y5T^^K-nMf|!3)i{vgQS{|C?kb#NEl0+lcUMuF`2`{9@HDjbKy zAp8IQun+cv1x3)$9{yJ#`~8j32CeV_+z+$hPM86*_rD!(gPWle-ePZm4i3P6*auzE z37eoD+F(7b0}CF8h42tO0QbXuxDW1udGH>4{`2r8EQe*V6z+j}AhCkG;ZBfv!3>xV zw?Gw0tRM&Vkc5Ts5X^zuFca>8>)^|94O|UZ!WA$YE{98D6s%#1TMZY(Mes*#^d7te zZ^H?A3HHN2coxKtPs1MA4ZC0`?11gC4WhqDfk=T!fk=T!f&W_tmivElv&KzkaC^;d z7WXs3{Z(iKD%)O(ekD|#BAg&`OLdzr;ZPOYy0yW|SoNkP=o7u|$NKg^m4ALkZ|Bqb zmX7?EV><3N?fj`$Z}kap^~&BC_YxxWPH!S~snfiTtx`d*`>;+XteNZS>fgDBGP&+f z0-e)HlVGPREy0X4yNRy$H*NHu-NR3>XG6Yyqt~)k_rZIrx$nrCAuV_tR^&GylXmj0 z&EAn6ROh$8yIDfEx?>+u@A*?LL{4(uP4p$)6*}Q{tjcw__3rE@!}(Kd>46T2^F_(c z(Z2@`hnK%?rRrpl>6-OUs#p{^>#b!YYRh>&EMIF1=-+)uH+yTg)2aUFw&gdsFreNe zTk|h$7Nu3RSa&wRwn-$Sr(FkfJ?#`nQ}5UY`ED&MsLBxg*6-6ryu;hPT?hNlZqM(1 zIg~*LCo%AskM}<9hcm-sh>9Z_Mf&r~2}up&*16NWq=^*=drxl4?^*TxrWMruQDmFm z*g&A3*x!6B*=T16V)MFVBaK>HMn=)KBX@ecwljmxoj&WG*_z+fCY@37`re+i`KAMd zL{m;7Wl;||8Eof*6k$*M#01GUR=XK1;T8?-jMOw#KBNpa(iGJe%$;l>(r3D@eFnP+ zbFsOH$y_kM`3V|-u?+*BT4Xqw8WM2y?np2kb6i1$w_sk%v>*3SaMB1#@IQB6q||AtL= z`Jgr)63GX3@<^aOu8R*dfVW|b*WA;;;)l7OoobO{)&}d8_F3)XPs!R9Zr5KtaZj&x zwb$_+v(sy9F06=*G{5;pR=!|@NjOD?RMbZ-X{YMkMI|a!Cf5deY**y?p#~jVF^hC2 zvnqIm7Jf?Sml^R<-(p1JQQu-7_30}3&XYcRpkiI-)WSnK)fsJe37qI;qFxf2@LL}e z0}HRFIy|A5QJuT6l8Vg(A5cS8GWutnz!<%U4mt8*P0e@jRFijLJ*$@gOwMn=kZwv+ R{)L12RT~^;Qn}7={2RR2tVI9- literal 12288 zcmeHN-)|H}93MFDF=6Z>+URINc933 z+R}=_wrHyrG^inRZ8c&`TX<3b27eI~>Fym52A@oP($DP7^{!2+z7l4W&+gvN_t$*B z-|tMCPFMZDwsyMRXd<{a5mM8((7EAtyl*4KcfGX3_`urN|CM`F2qq7jhs~sw>$dqV z?esL%r?w}1yjQpr?m`;LaD#>R$i$rnv#?qObFvB0yFzGo$~X4)yMXJZpy+J}W? zCe3UgqRdZn4dxr%$=YG7QN_T$!$83HKeJ;aX>3T<^GQFcZ=suOI_{nMtC=bW6a$I@ z#eiZ!F`yVw3@8Q^1BAKj$XnpyKFLL^e6CyZlt0x&F`yVw3@8Q^1BwB~fMP%~pcqgL zCu#1&`UHzarN>W;Wy2*agffTPLRnoi!azRF@QUyHO)5S1Alv%X&u_@ znU3Yn>?Jhk8n|qzZkyFUNb#2Yct#P6E1)C zV@pfJFZ*udpzn5g8S#A|)F{`bJE?;^|1F$rIL?OyepE4KRg@i5+ z{uxKJ8NH607LV{1c(0?L?FShqT!=6XH(&`pYx}<0V?$cL36XZwZ4vJ3sCc$gp(exH zW%>tJ5#o{Z$;(isP3R=Vms-TyE?gRE=Wy<~v(1A|=q|$M za(b;G7OFfU|E1ujRSc>SV&2Nb3Ha$CVRRiRQmW1hSA;j6t_t{HRLb?rh(E z$J&qWFz*etDH=S!wkC;u;|(7TUkm%Dc%Oxnv!&?^#j%rUvGDS_sPN6=%;lT;;}B{x zXmoxy8Y=kWOf5PLV-Te+qTpKTt+$7Y3x&GHxr^bM0a-B+C|w^8$0x%4{O$QcN{bf? zVPPzM@6zH-esSg!9L1mEVf9Gjri<$d^htrKuCe;V-y;G%x@M z;n~4(dLqhy9xi+m^}Q=R$VR1gbn)8h@U!Fe*iz3wOQ~M`U@lx3fpj?VvEBp;uZ~7dXWg8~C_lJvWK5^W@a+8=REZ%Lx8`L60p9~< gnXfW}7_cX~*vPTZQ7e}<_<4JASl*}{mTxBi0@A~$y#N3J diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/LinkedList.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/LinkedList.java index d82349089b..5717f2226d 100644 --- a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/LinkedList.java +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/LinkedList.java @@ -60,7 +60,6 @@ public int size(){ } public void addFirst(Object o){ - //就是这么硬! add(0, o); } @@ -132,4 +131,159 @@ Object getNext() { return next; } } + + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + Object[] oldData = new Object[size]; + Node temp = head; + int index = 1; + while(temp.next != null) { + temp = temp.next; + oldData[size - index] = temp.data; + index++; + } + + index = 0; + temp = head; + while(temp.next != null) { + temp = temp.next; + temp.data = oldData[index]; + index++; + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + + public void removeFirstHalf(){ + int count = size; + if (count % 2 != 0) { + for (int i = 0; i <= count/2; i++) { + removeFirst(); + } + } else { + for (int i = 0; i < count/2; i++) { + removeFirst(); + } + } + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + + public void remove(int i, int length){ + if (i < 0 || length < 0) { + return; + } + if (i == 0) { + for (int k = 0; k < length; k++) + removeFirst(); + } else { + while (length > 0) { + remove(i-1); + length--; + } + } + } + + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + + public static int[] getElements(LinkedList list){ + + return null; + + } + + + + /** + + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + + * 从当前链表中中删除在list中出现的元素 + + + + * @param list + + */ + + + + public void subtract(LinkedList list){ + + + + } + + + + /** + + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + + */ + + public void removeDuplicateValues(){ + + + + } + + + + /** + + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + + * @param min + + * @param max + + */ + + public void removeRange(int min, int max){ + + + + } + + + + /** + + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + + * @param list + + */ + + public LinkedList intersection( LinkedList list){ + + return null; + + } } From b7c11cec17f199b79802cfcefa4a3a789f945a40 Mon Sep 17 00:00:00 2001 From: x_zhaohu Date: Thu, 23 Mar 2017 23:32:15 +0800 Subject: [PATCH 02/41] =?UTF-8?q?=E5=B7=A5=E4=BD=9C=E6=B5=81=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group11/1178243325/week01/build.gradle | 9 + group11/1178243325/week01/readme.md | 15 ++ .../main/java/com/sprint/basic/Iterator.java | 6 + .../ConcurrentModificationException.java | 9 + .../basic/exception/EmptyQueueException.java | 8 + .../basic/exception/EmptyStackException.java | 8 + .../java/com/sprint/basic/list/ArrayList.java | 99 +++++++++++ .../com/sprint/basic/list/LinkedList.java | 160 ++++++++++++++++++ .../main/java/com/sprint/basic/list/List.java | 10 ++ .../java/com/sprint/basic/queue/Queue.java | 32 ++++ .../java/com/sprint/basic/stack/Stack.java | 40 +++++ .../com/sprint/basic/tree/BinaryTreeNode.java | 122 +++++++++++++ .../com/sprint/basic/list/ArrayListTest.java | 56 ++++++ .../com/sprint/basic/list/LinkedListTest.java | 56 ++++++ .../com/sprint/basic/queue/QueueTest.java | 20 +++ .../com/sprint/basic/stack/StackTest.java | 23 +++ .../sprint/basic/tree/BinaryTreeNodeTest.java | 54 ++++++ 17 files changed, 727 insertions(+) create mode 100644 group11/1178243325/week01/build.gradle create mode 100644 group11/1178243325/week01/readme.md create mode 100644 group11/1178243325/week01/src/main/java/com/sprint/basic/Iterator.java create mode 100644 group11/1178243325/week01/src/main/java/com/sprint/basic/exception/ConcurrentModificationException.java create mode 100644 group11/1178243325/week01/src/main/java/com/sprint/basic/exception/EmptyQueueException.java create mode 100644 group11/1178243325/week01/src/main/java/com/sprint/basic/exception/EmptyStackException.java create mode 100644 group11/1178243325/week01/src/main/java/com/sprint/basic/list/ArrayList.java create mode 100644 group11/1178243325/week01/src/main/java/com/sprint/basic/list/LinkedList.java create mode 100644 group11/1178243325/week01/src/main/java/com/sprint/basic/list/List.java create mode 100644 group11/1178243325/week01/src/main/java/com/sprint/basic/queue/Queue.java create mode 100644 group11/1178243325/week01/src/main/java/com/sprint/basic/stack/Stack.java create mode 100644 group11/1178243325/week01/src/main/java/com/sprint/basic/tree/BinaryTreeNode.java create mode 100644 group11/1178243325/week01/src/test/java/com/sprint/basic/list/ArrayListTest.java create mode 100644 group11/1178243325/week01/src/test/java/com/sprint/basic/list/LinkedListTest.java create mode 100644 group11/1178243325/week01/src/test/java/com/sprint/basic/queue/QueueTest.java create mode 100644 group11/1178243325/week01/src/test/java/com/sprint/basic/stack/StackTest.java create mode 100644 group11/1178243325/week01/src/test/java/com/sprint/basic/tree/BinaryTreeNodeTest.java diff --git a/group11/1178243325/week01/build.gradle b/group11/1178243325/week01/build.gradle new file mode 100644 index 0000000000..50d1380b3f --- /dev/null +++ b/group11/1178243325/week01/build.gradle @@ -0,0 +1,9 @@ +apply plugin: 'java' + +repositories { + mavenCentral(); +} + +dependencies { + testCompile("junit:junit:4.12") +} diff --git a/group11/1178243325/week01/readme.md b/group11/1178243325/week01/readme.md new file mode 100644 index 0000000000..61c31c1c9c --- /dev/null +++ b/group11/1178243325/week01/readme.md @@ -0,0 +1,15 @@ +####讲课内容: +- 17-2-15: 社群kickoff +- 17-2-19:讲解Java自测题和基本数据结构 +- 17-2-22:计算机组成原理和计算机编程语言 +- 17-2-26:程序的机器级表示 + +####第一周作业(2-15 至 2-26) +- 实现各种基本数据结构(ArrayList, Stack, LinkedList, Queue, Tree, Iterator) +- 写一篇介绍CPU,内存,硬盘,指令以及他们之间的关系 + +####完成情况: +- 除了Tree未完成,其余都基本实现 +- 文章地址 + +####我的收获: diff --git a/group11/1178243325/week01/src/main/java/com/sprint/basic/Iterator.java b/group11/1178243325/week01/src/main/java/com/sprint/basic/Iterator.java new file mode 100644 index 0000000000..1e73a2a4b9 --- /dev/null +++ b/group11/1178243325/week01/src/main/java/com/sprint/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.sprint.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group11/1178243325/week01/src/main/java/com/sprint/basic/exception/ConcurrentModificationException.java b/group11/1178243325/week01/src/main/java/com/sprint/basic/exception/ConcurrentModificationException.java new file mode 100644 index 0000000000..c91c388bbd --- /dev/null +++ b/group11/1178243325/week01/src/main/java/com/sprint/basic/exception/ConcurrentModificationException.java @@ -0,0 +1,9 @@ +package com.sprint.basic.exception; + +public class ConcurrentModificationException extends RuntimeException { + public ConcurrentModificationException() {} + public ConcurrentModificationException(String msg) { + super(msg); + } +} + diff --git a/group11/1178243325/week01/src/main/java/com/sprint/basic/exception/EmptyQueueException.java b/group11/1178243325/week01/src/main/java/com/sprint/basic/exception/EmptyQueueException.java new file mode 100644 index 0000000000..ddf89ac120 --- /dev/null +++ b/group11/1178243325/week01/src/main/java/com/sprint/basic/exception/EmptyQueueException.java @@ -0,0 +1,8 @@ +package com.sprint.basic.exception; + +public class EmptyQueueException extends RuntimeException { + public EmptyQueueException() {} + public EmptyQueueException(String msg) { + super(msg); + } +} diff --git a/group11/1178243325/week01/src/main/java/com/sprint/basic/exception/EmptyStackException.java b/group11/1178243325/week01/src/main/java/com/sprint/basic/exception/EmptyStackException.java new file mode 100644 index 0000000000..d654c7cd16 --- /dev/null +++ b/group11/1178243325/week01/src/main/java/com/sprint/basic/exception/EmptyStackException.java @@ -0,0 +1,8 @@ +package com.sprint.basic.exception; + +public class EmptyStackException extends RuntimeException { + public EmptyStackException() {} + public EmptyStackException(String msg) { + super(msg); + } +} diff --git a/group11/1178243325/week01/src/main/java/com/sprint/basic/list/ArrayList.java b/group11/1178243325/week01/src/main/java/com/sprint/basic/list/ArrayList.java new file mode 100644 index 0000000000..fb64e93f36 --- /dev/null +++ b/group11/1178243325/week01/src/main/java/com/sprint/basic/list/ArrayList.java @@ -0,0 +1,99 @@ +package com.sprint.basic.list; + +import com.sprint.basic.exception.ConcurrentModificationException; +import com.sprint.basic.Iterator; +public class ArrayList implements List { + + private int size; + private Object[] elementData; + + public ArrayList () { + size = 0; + elementData = new Object[100]; + } + + public boolean add(Object o) { + add(size(), o); + return true; + } + + public boolean add(int index, Object o){ + if (size() == elementData.length) + ensureCapacity( size() * 2 + 1); + if (index > size() || index < 0) { //index == size时相当于在尾后插入 + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + for (int i = size; i > index; i--) { + elementData[i] = elementData[i-1]; + } + elementData[index] = o; + size++; + return true; + } + + private void ensureCapacity(int newCapacity) { + if (newCapacity < size()) + return; + Object[] old = elementData; + elementData = new Object[newCapacity]; + for (int i = 0; i < size(); i++) { + elementData[i] = old[i]; + } + } + + public Object get(int index){ + if (index >= size() || index < 0) { //获取时,index==size()越界 + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + return elementData[index]; + } + + private String outOfBoundsMsg(int index) { + return "Index:" + index + ", Size:" + size; + } + + public Object remove(int index){ + if (index >= size() || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + Object old = elementData[index]; + for (int i = index; i < size(); i++) { + elementData[i] = elementData[i+1]; + } + size--; + return old; + } + + /*获取表内容量*/ + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + public class ArrayListIterator implements Iterator { + private final int ONLY_CAPACITY = size; + private int index; + public ArrayListIterator() { + index = 0; + } + + @Override + public boolean hasNext() { + if (ONLY_CAPACITY != size) + throw new ConcurrentModificationException("此对象没有进行修改同步"); + return index != size; + } + + @Override + public Object next() { + if (ONLY_CAPACITY != size) + throw new ConcurrentModificationException("此对象没有进行修改同步"); + if (index >= ONLY_CAPACITY) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + return elementData[index++]; + } + } +} diff --git a/group11/1178243325/week01/src/main/java/com/sprint/basic/list/LinkedList.java b/group11/1178243325/week01/src/main/java/com/sprint/basic/list/LinkedList.java new file mode 100644 index 0000000000..503f41f65b --- /dev/null +++ b/group11/1178243325/week01/src/main/java/com/sprint/basic/list/LinkedList.java @@ -0,0 +1,160 @@ +package com.sprint.basic.list; + +import com.sprint.basic.exception.ConcurrentModificationException; +import com.sprint.basic.Iterator; +import java.util.Objects; +public class LinkedList implements List { + + private Node head; + private int size; + public LinkedList() { + head = new Node(null, null); + size = 0; + } + + public boolean add(Object o) { + add(size, o); + return true; + } + + public boolean add(int index , Object o) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + Node frontNode = getNode(index-1); + Node newNode = new Node(o, frontNode.next); + frontNode.next = newNode; + size++; + return true; + } + + /*getNode getPreNodeByElement getNextNodeByElement的效率低些*/ + private Node getNode(int index) { + Node node = head; + int i = 0; + while(node.next != null && i <= index) { + node = node.next; + i++; + } + return node; + } + + private Node getPreNodeByElement(Object obj) { + if (obj != null) { + for (int i = 0; i < size(); i++) { + if (getNode(i).data == obj) { + return getNode(i-1); + } + } + } + return null; + } + + private Node getNextNodeByElement(Object obj) { + if (obj != null) { + for (int i = 0; i < size(); i++) { + if (getNode(i).data == obj) { + return getNode(i+1); + } + } + } + return null; + } + + public Object get(int index){ + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + Node node = getNode(index); + return node.data; + } + + public Object remove(int index){ + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + Node frontNode = getNode(index-1); + Node oldNode = getNode(index); + frontNode.next = oldNode.next; + size--; + return oldNode.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + add(0, o); + } + + public void addLast(Object o){ + add(size, o); + } + + public Object removeFirst(){ + return remove(0); + } + + public Object removeLast(){ + return remove(size-1); + } + + public Iterator iterator(){ + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + int index; + final int capacity = size; + LinkedListIterator() { + index = 0; + } + @Override + public boolean hasNext() { + if (capacity != size) + throw new ConcurrentModificationException("此对象没有修改同步"); + return index < capacity; + } + + @Override + public Object next() { + if (capacity != size) + throw new ConcurrentModificationException("此对象没有修改同步"); + if (index >= capacity) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + return get(index++); + } + } + + private String outOfBoundsMsg(int index) { + return "index:" + index + ", size:" + size; + } + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + + void setData(Object data) { + this.data = data; + } + + Object getData() { + return data; + } + + void setNext(Node next) { + this.next = next; + } + + Object getNext() { + return next; + } + } +} diff --git a/group11/1178243325/week01/src/main/java/com/sprint/basic/list/List.java b/group11/1178243325/week01/src/main/java/com/sprint/basic/list/List.java new file mode 100644 index 0000000000..0e90471a48 --- /dev/null +++ b/group11/1178243325/week01/src/main/java/com/sprint/basic/list/List.java @@ -0,0 +1,10 @@ +package com.sprint.basic.list; +import com.sprint.basic.Iterator; +public interface List { + public boolean add(Object o); + public boolean add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); + public Iterator iterator(); +} diff --git a/group11/1178243325/week01/src/main/java/com/sprint/basic/queue/Queue.java b/group11/1178243325/week01/src/main/java/com/sprint/basic/queue/Queue.java new file mode 100644 index 0000000000..47f7b98d96 --- /dev/null +++ b/group11/1178243325/week01/src/main/java/com/sprint/basic/queue/Queue.java @@ -0,0 +1,32 @@ +package com.sprint.basic.queue; +import com.sprint.basic.exception.EmptyQueueException; +import com.sprint.basic.list.LinkedList; +public class Queue { + + private LinkedList elementData; + + public Queue() { + elementData = new LinkedList(); + } + + public boolean enQueue(Object o){ + elementData.addLast(o); + return true; + } + + public Object deQueue(){ + if (isEmpty()) { + throw new EmptyQueueException("队空"); + } + Object result = elementData.removeFirst(); + return result; + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group11/1178243325/week01/src/main/java/com/sprint/basic/stack/Stack.java b/group11/1178243325/week01/src/main/java/com/sprint/basic/stack/Stack.java new file mode 100644 index 0000000000..e399dcb850 --- /dev/null +++ b/group11/1178243325/week01/src/main/java/com/sprint/basic/stack/Stack.java @@ -0,0 +1,40 @@ +package com.sprint.basic.stack; + +import com.sprint.basic.exception.EmptyStackException; +import com.sprint.basic.list.ArrayList; +public class Stack { + + private ArrayList elementData; + public Stack() { + elementData = new ArrayList(); + } + + public boolean push(Object o){ + elementData.add(o); + return true; + } + + public Object pop(){ + if (isEmpty()) { + throw new EmptyStackException("栈空"); + } + Object result = elementData.get(size()-1); + elementData.remove(size()-1); + return result; + } + + public Object peek(){ + if (isEmpty()) { + throw new EmptyStackException("栈空"); + } + return elementData.get(0); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group11/1178243325/week01/src/main/java/com/sprint/basic/tree/BinaryTreeNode.java b/group11/1178243325/week01/src/main/java/com/sprint/basic/tree/BinaryTreeNode.java new file mode 100644 index 0000000000..efaf261521 --- /dev/null +++ b/group11/1178243325/week01/src/main/java/com/sprint/basic/tree/BinaryTreeNode.java @@ -0,0 +1,122 @@ +package com.sprint.basic.tree; + +public class BinaryTreeNode { + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private int size; + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(T data) { + if (this.data == null) { + this.data = data; + return this; + } + int compareResult = this.data.compareTo(data); + if (compareResult > 0) { + if (this.left == null) { + this.left = new BinaryTreeNode(); + this.left.data = data; + return this.left; + } else { + return this.left.insert(data); + } + } else if (compareResult < 0) { + if (this.right == null) { + this.right = new BinaryTreeNode(); + this.right.data = data; + return this.right; + } else { + return this.right.insert(data); + } + } else { + return this; + } + } + + /*没看懂*/ + public BinaryTreeNode delete(T data) { + BinaryTreeNode treeNode = search(data); + if (treeNode == null) { + return null; + } + int compareResult = this.data.compareTo(data); + if (compareResult > 0) { + return this.left.delete(data); + } else if (compareResult < 0) { + return this.right.delete(data); + } else { + if (treeNode.right == null) { + if (this.left == null) { + this.data = null; + } else { + this.left = this; + } + } else { + this.data = (T) this.right.findMin().data; + + this.right.delete(this.data); + } + } + + return this; + } + + private BinaryTreeNode findMin() { + if (this.data == null) { + return null; + } + if (this.left == null) { + return this; + } + return this.left.findMin(); + } + + public BinaryTreeNode search(T data) { + if (this.data == null) { + return null; + } + int compareResult = this.data.compareTo(data); + if (compareResult > 0) { + if (this.left == null) { + return null; + } else { + return this.left.search(data); + } + } else if (compareResult < 0) { + if (this.right == null) { + return null; + } else { + return this.right.search(data); + } + } else { + return this; + } + } + + +} diff --git a/group11/1178243325/week01/src/test/java/com/sprint/basic/list/ArrayListTest.java b/group11/1178243325/week01/src/test/java/com/sprint/basic/list/ArrayListTest.java new file mode 100644 index 0000000000..63936c288c --- /dev/null +++ b/group11/1178243325/week01/src/test/java/com/sprint/basic/list/ArrayListTest.java @@ -0,0 +1,56 @@ +package com.sprint.basic.list; + +import com.sprint.basic.Iterator; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ArrayListTest { + private List list; + + @Before + public void init() { + list = new ArrayList(); + } + + @Test + public void add() { + for (int i = 0; i < 5; i++) { + list.add(i); + } + /*Assert.assertTrue(args): if (args != true) to failed*/ + System.out.println(list); + Assert.assertTrue(list.add(5)); + Assert.assertEquals(6, list.size()); + Assert.assertTrue(list.add(3, 10)); + Assert.assertEquals(7, list.size()); + + } + + @Test + public void remove() { + add(); + Assert.assertEquals(5, list.remove(6)); + Assert.assertEquals(6, list.size()); + } + + @Test + public void get() { + add(); + Assert.assertEquals(5, list.get(6)); + } + + @Test + public void testIterator() { + for (int i = 0; i < 10; i++) { + Assert.assertTrue(list.add(i)); + } + Iterator iter = list.iterator(); + int count = 0; + while(iter.hasNext()) { + Assert.assertEquals(count, iter.next()); + count++; + } + } + +} diff --git a/group11/1178243325/week01/src/test/java/com/sprint/basic/list/LinkedListTest.java b/group11/1178243325/week01/src/test/java/com/sprint/basic/list/LinkedListTest.java new file mode 100644 index 0000000000..c5ab12aa4e --- /dev/null +++ b/group11/1178243325/week01/src/test/java/com/sprint/basic/list/LinkedListTest.java @@ -0,0 +1,56 @@ +package com.sprint.basic.list; + +import com.sprint.basic.Iterator; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class LinkedListTest { + private List list; + + @Before + public void init() { + list = new LinkedList(); + } + + @Test + public void add() { + for (int i = 0; i < 5; i++) { + list.add(i); + } + /*Assert.assertTrue(args): if (args != true) to failed*/ + System.out.println(list); + Assert.assertTrue(list.add(5)); + Assert.assertEquals(6, list.size()); + Assert.assertTrue(list.add(3, 10)); + Assert.assertEquals(7, list.size()); + + } + + @Test + public void remove() { + add(); + Assert.assertEquals(5, list.remove(6)); + Assert.assertEquals(6, list.size()); + } + + @Test + public void get() { + add(); + Assert.assertEquals(5, list.get(6)); + } + + @Test + public void testIterator() { + for (int i = 0; i < 10; i++) { + Assert.assertTrue(list.add(i)); + } + Iterator iter = list.iterator(); + int count = 0; + while(iter.hasNext()) { + Assert.assertEquals(count, iter.next()); + count++; + } + } + +} diff --git a/group11/1178243325/week01/src/test/java/com/sprint/basic/queue/QueueTest.java b/group11/1178243325/week01/src/test/java/com/sprint/basic/queue/QueueTest.java new file mode 100644 index 0000000000..b7cfe4b32f --- /dev/null +++ b/group11/1178243325/week01/src/test/java/com/sprint/basic/queue/QueueTest.java @@ -0,0 +1,20 @@ +package com.sprint.basic.queue; + +import org.junit.Assert; +import org.junit.Test; +public class QueueTest { + + private Queue queue = new Queue(); + + @Test + public void testQueueApi() { + Assert.assertTrue(queue.enQueue(1)); + Assert.assertTrue(queue.enQueue(2)); + Assert.assertFalse(queue.isEmpty()); + Assert.assertEquals(2, queue.size()); + Assert.assertEquals(1, queue.deQueue()); + Assert.assertEquals(2, queue.deQueue()); + Assert.assertEquals(0, queue.size()); + Assert.assertTrue(queue.isEmpty()); + } +} diff --git a/group11/1178243325/week01/src/test/java/com/sprint/basic/stack/StackTest.java b/group11/1178243325/week01/src/test/java/com/sprint/basic/stack/StackTest.java new file mode 100644 index 0000000000..e267c59971 --- /dev/null +++ b/group11/1178243325/week01/src/test/java/com/sprint/basic/stack/StackTest.java @@ -0,0 +1,23 @@ +package com.sprint.basic.stack; + +import org.junit.Assert; +import org.junit.Test; + +public class StackTest { + + private Stack stack = new Stack(); + + @Test + public void testStack() { + Assert.assertTrue(stack.push(1)); + Assert.assertEquals(1, stack.pop()); + Assert.assertTrue(stack.push(2)); + Assert.assertTrue(stack.push(3)); + Assert.assertTrue(stack.push(4)); + Assert.assertEquals(4, stack.pop()); + Assert.assertEquals(2, stack.peek()); + Assert.assertEquals(2, stack.size()); + Assert.assertFalse(stack.isEmpty()); + } + +} diff --git a/group11/1178243325/week01/src/test/java/com/sprint/basic/tree/BinaryTreeNodeTest.java b/group11/1178243325/week01/src/test/java/com/sprint/basic/tree/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..d9a27ab211 --- /dev/null +++ b/group11/1178243325/week01/src/test/java/com/sprint/basic/tree/BinaryTreeNodeTest.java @@ -0,0 +1,54 @@ +package com.sprint.basic.tree; + +import org.junit.Assert; +/*参考程序*/ +public class BinaryTreeNodeTest { + + private BinaryTreeNode treeNode; + + @org.junit.Before + public void setUp() throws Exception { + treeNode = new BinaryTreeNode<>(); + treeNode.insert(5); + treeNode.insert(3); + treeNode.insert(7); + treeNode.insert(1); + treeNode.insert(4); + treeNode.insert(2); + treeNode.insert(8); + treeNode.insert(6); + } + + @org.junit.Test + public void insert() { + Assert.assertEquals(treeNode.getData().intValue(), 5); + Assert.assertEquals(treeNode.getLeft().getData(), 3); + Assert.assertEquals(treeNode.getRight().getData(), 7); + Assert.assertEquals(treeNode.getLeft().getLeft().getData(), 1); + Assert.assertEquals(treeNode.getLeft().getRight().getData(), 4); + Assert.assertEquals(treeNode.getLeft().getLeft().getRight().getData(), 2); + Assert.assertEquals(treeNode.getRight().getRight().getData(), 8); + Assert.assertEquals(treeNode.getRight().getLeft().getData(), 6); + } + + @org.junit.Test + public void delete() throws Exception { + treeNode.delete(3); + for (int i = 1; i < 9; i++) { + if (i != 3) { + Assert.assertNotNull(treeNode.search(i)); + } else { + Assert.assertNull(treeNode.search(i)); + } + } + } + + @org.junit.Test + public void search() throws Exception { + for (int i = 1; i < 9; i++) { + Assert.assertNotNull(treeNode.search(i)); + } + Assert.assertNull(treeNode.search(0)); + Assert.assertNull(treeNode.search(9)); + } +} From dfa576a2a857e35729c018e07a12cfdbe668fb5a Mon Sep 17 00:00:00 2001 From: x_zhaohu Date: Fri, 24 Mar 2017 08:47:05 +0800 Subject: [PATCH 03/41] update format --- group11/1178243325/week01/readme.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/group11/1178243325/week01/readme.md b/group11/1178243325/week01/readme.md index 61c31c1c9c..8d0e6f12a2 100644 --- a/group11/1178243325/week01/readme.md +++ b/group11/1178243325/week01/readme.md @@ -1,15 +1,15 @@ -####讲课内容: +## 讲课内容: - 17-2-15: 社群kickoff - 17-2-19:讲解Java自测题和基本数据结构 - 17-2-22:计算机组成原理和计算机编程语言 - 17-2-26:程序的机器级表示 -####第一周作业(2-15 至 2-26) +## 第一周作业(2-15 至 2-26) - 实现各种基本数据结构(ArrayList, Stack, LinkedList, Queue, Tree, Iterator) - 写一篇介绍CPU,内存,硬盘,指令以及他们之间的关系 -####完成情况: +## 完成情况: - 除了Tree未完成,其余都基本实现 - 文章地址 -####我的收获: +## 我的收获: From 12b5c8d5adb4b54a66fe0fb3748c239340e9cbfa Mon Sep 17 00:00:00 2001 From: James <1310368322@qq.com> Date: Fri, 24 Mar 2017 12:18:57 +0800 Subject: [PATCH 04/41] Third_Homework --- .../BaseDataStructure}/ArrayList.java | 0 .../BaseDataStructure}/LinkedList.java | 0 .../BaseDataStructure}/ArrayUtil.java | 0 .../BaseDataStructure/LinkedList.java | 162 ++++++++++++++++++ .../Download/DownloadThread.java | 0 .../Download/FileDownloader.java | 0 .../Download/api/Connection.java | 0 .../Download/api/ConnectionException.java | 0 .../Download/api/ConnectionManager.java | 0 .../Download/api/DownloadListener.java | 0 .../Download/impl/ConnectionImpl.java | 0 .../Download/impl/ConnectionManagerImpl.java | 0 .../BaseDataStructure}/ArrayUtilTest.java | 0 .../BaseDataStructure/TestLinkedList.java | 25 +++ .../Download}/FileDownloaderTest.java | 0 15 files changed, 187 insertions(+) rename group11/1310368322/src/{ => FirstHomework/BaseDataStructure}/ArrayList.java (100%) rename group11/1310368322/src/{ => FirstHomework/BaseDataStructure}/LinkedList.java (100%) rename group11/1310368322/src/{ => SecondHomework/BaseDataStructure}/ArrayUtil.java (100%) create mode 100644 group11/1310368322/src/ThirdHomework/BaseDataStructure/LinkedList.java rename group11/1310368322/src/{ => ThirdHomework}/Download/DownloadThread.java (100%) rename group11/1310368322/src/{ => ThirdHomework}/Download/FileDownloader.java (100%) rename group11/1310368322/src/{ => ThirdHomework}/Download/api/Connection.java (100%) rename group11/1310368322/src/{ => ThirdHomework}/Download/api/ConnectionException.java (100%) rename group11/1310368322/src/{ => ThirdHomework}/Download/api/ConnectionManager.java (100%) rename group11/1310368322/src/{ => ThirdHomework}/Download/api/DownloadListener.java (100%) rename group11/1310368322/src/{ => ThirdHomework}/Download/impl/ConnectionImpl.java (100%) rename group11/1310368322/src/{ => ThirdHomework}/Download/impl/ConnectionManagerImpl.java (100%) rename group11/1310368322/test/{ => SecondHomwork/BaseDataStructure}/ArrayUtilTest.java (100%) create mode 100644 group11/1310368322/test/ThirdHomework/BaseDataStructure/TestLinkedList.java rename group11/1310368322/test/{ => ThirdHomework/Download}/FileDownloaderTest.java (100%) diff --git a/group11/1310368322/src/ArrayList.java b/group11/1310368322/src/FirstHomework/BaseDataStructure/ArrayList.java similarity index 100% rename from group11/1310368322/src/ArrayList.java rename to group11/1310368322/src/FirstHomework/BaseDataStructure/ArrayList.java diff --git a/group11/1310368322/src/LinkedList.java b/group11/1310368322/src/FirstHomework/BaseDataStructure/LinkedList.java similarity index 100% rename from group11/1310368322/src/LinkedList.java rename to group11/1310368322/src/FirstHomework/BaseDataStructure/LinkedList.java diff --git a/group11/1310368322/src/ArrayUtil.java b/group11/1310368322/src/SecondHomework/BaseDataStructure/ArrayUtil.java similarity index 100% rename from group11/1310368322/src/ArrayUtil.java rename to group11/1310368322/src/SecondHomework/BaseDataStructure/ArrayUtil.java diff --git a/group11/1310368322/src/ThirdHomework/BaseDataStructure/LinkedList.java b/group11/1310368322/src/ThirdHomework/BaseDataStructure/LinkedList.java new file mode 100644 index 0000000000..5ea6a988be --- /dev/null +++ b/group11/1310368322/src/ThirdHomework/BaseDataStructure/LinkedList.java @@ -0,0 +1,162 @@ +package DataStructure_3; + +import java.util.Stack; + +import DataStructure_1.LinkedList.Node; + +public class LinkedList { + private Node head; + static int size = 0; + public void add(Object o){ + if(null == head){ + head = new Node(); + head.data = o; + head.next = null; + }else{ + Node p = head; + while(null != p.next){ + p = p.next; + } + Node newNode = new Node(); + newNode.data = o; + p.next = newNode; + newNode.next =null; + } + size++; + } + public int size(){ + return size; + } + public void add(int index,Object o){ + if(index < 0){ + throw new RuntimeException("±겻Ϊ"); + } + if(index == 0){ + addFirst(o); + size++; + return; + } + if(index > size){ + throw new RuntimeException(""); + } + int i = 0; + Node p = head; + Node q = null; + + while(i!=index){ + q = p; + p = p.next; + i++; + } + Node r = new Node(); + r.data = o; + r.next =null; + q.next = r; + r.next = p; + size++; + return; + } + + public Object get(int index){ + int i = 0; + Node p = head; + while(i != index){ + p = p.next; + i++; + } + return p.data; + } + public Object remove(int index){ + if(index < 0){ + throw new RuntimeException("±겻Ϊ"); + } + if(index == 1){ + size--; + return head.data; + } + int i = 0; + Node p = head; + Node q = null; + while(i != index){ + q = p; + p = p.next; + i++; + } + q.next = p.next; + size--; + return p.data; + } + public void addFirst(Object o){ + Node p = new Node(); + p.next = head; + p.data = o; + head = p; + size++; + } + public Object removeFirst(){ + head = head.next; + size--; + return null; + } + public static class Node{ + Object data; + Node next; + } + + /** + * Ѹ + * 3->7->10 úΪ 10->7->3 + */ + public void reverse(){ + if(null == head || null == head.next){ + return; + } + Stack s = new Stack(); + Node curNode = head; + while(curNode != null){ + s.push(curNode); + Node nextNode = curNode.next; + curNode.next = null; // Ͽ + curNode = nextNode; + } + + head = s.pop(); + curNode = head; + while(!s.isEmpty()){ + Node nextNode = s.pop(); + curNode.next = nextNode; + curNode = nextNode; + } + + } + + public String toString(){ + StringBuffer buffer = new StringBuffer(); + buffer.append("["); + Node node = head; + while(node != null){ + buffer.append(node.data); + if(node.next != null){ + buffer.append(","); + } + node = node.next; + } + buffer.append("]"); + return buffer.toString(); + } + + + + + + + + + + + + + + + +} diff --git a/group11/1310368322/src/Download/DownloadThread.java b/group11/1310368322/src/ThirdHomework/Download/DownloadThread.java similarity index 100% rename from group11/1310368322/src/Download/DownloadThread.java rename to group11/1310368322/src/ThirdHomework/Download/DownloadThread.java diff --git a/group11/1310368322/src/Download/FileDownloader.java b/group11/1310368322/src/ThirdHomework/Download/FileDownloader.java similarity index 100% rename from group11/1310368322/src/Download/FileDownloader.java rename to group11/1310368322/src/ThirdHomework/Download/FileDownloader.java diff --git a/group11/1310368322/src/Download/api/Connection.java b/group11/1310368322/src/ThirdHomework/Download/api/Connection.java similarity index 100% rename from group11/1310368322/src/Download/api/Connection.java rename to group11/1310368322/src/ThirdHomework/Download/api/Connection.java diff --git a/group11/1310368322/src/Download/api/ConnectionException.java b/group11/1310368322/src/ThirdHomework/Download/api/ConnectionException.java similarity index 100% rename from group11/1310368322/src/Download/api/ConnectionException.java rename to group11/1310368322/src/ThirdHomework/Download/api/ConnectionException.java diff --git a/group11/1310368322/src/Download/api/ConnectionManager.java b/group11/1310368322/src/ThirdHomework/Download/api/ConnectionManager.java similarity index 100% rename from group11/1310368322/src/Download/api/ConnectionManager.java rename to group11/1310368322/src/ThirdHomework/Download/api/ConnectionManager.java diff --git a/group11/1310368322/src/Download/api/DownloadListener.java b/group11/1310368322/src/ThirdHomework/Download/api/DownloadListener.java similarity index 100% rename from group11/1310368322/src/Download/api/DownloadListener.java rename to group11/1310368322/src/ThirdHomework/Download/api/DownloadListener.java diff --git a/group11/1310368322/src/Download/impl/ConnectionImpl.java b/group11/1310368322/src/ThirdHomework/Download/impl/ConnectionImpl.java similarity index 100% rename from group11/1310368322/src/Download/impl/ConnectionImpl.java rename to group11/1310368322/src/ThirdHomework/Download/impl/ConnectionImpl.java diff --git a/group11/1310368322/src/Download/impl/ConnectionManagerImpl.java b/group11/1310368322/src/ThirdHomework/Download/impl/ConnectionManagerImpl.java similarity index 100% rename from group11/1310368322/src/Download/impl/ConnectionManagerImpl.java rename to group11/1310368322/src/ThirdHomework/Download/impl/ConnectionManagerImpl.java diff --git a/group11/1310368322/test/ArrayUtilTest.java b/group11/1310368322/test/SecondHomwork/BaseDataStructure/ArrayUtilTest.java similarity index 100% rename from group11/1310368322/test/ArrayUtilTest.java rename to group11/1310368322/test/SecondHomwork/BaseDataStructure/ArrayUtilTest.java diff --git a/group11/1310368322/test/ThirdHomework/BaseDataStructure/TestLinkedList.java b/group11/1310368322/test/ThirdHomework/BaseDataStructure/TestLinkedList.java new file mode 100644 index 0000000000..343f9d6f39 --- /dev/null +++ b/group11/1310368322/test/ThirdHomework/BaseDataStructure/TestLinkedList.java @@ -0,0 +1,25 @@ +package DataStructure_3; + +import static org.junit.Assert.*; +import org.junit.Assert; +import org.junit.Test; + +public class TestLinkedList { + + @Test + public void test() { + LinkedList L = new LinkedList(); + Assert.assertEquals("[]", L.toString()); + + L.add(1); + L.reverse(); + Assert.assertEquals("[1]", L.toString()); + + L.add(2); + L.add(3); + L.add(4); + L.reverse(); + Assert.assertEquals("[4,3,2,1]",L.toString()); + } + +} diff --git a/group11/1310368322/test/FileDownloaderTest.java b/group11/1310368322/test/ThirdHomework/Download/FileDownloaderTest.java similarity index 100% rename from group11/1310368322/test/FileDownloaderTest.java rename to group11/1310368322/test/ThirdHomework/Download/FileDownloaderTest.java From 63b87fa8b58cccb78ea661463c374ea200e3c0f3 Mon Sep 17 00:00:00 2001 From: James <1310368322@qq.com> Date: Fri, 24 Mar 2017 12:24:38 +0800 Subject: [PATCH 05/41] remove --- .../BaseDataStructure/ArrayList.java | 98 ----------- .../BaseDataStructure/LinkedList.java | 113 ------------ .../BaseDataStructure/ArrayUtil.java | 83 --------- .../BaseDataStructure/LinkedList.java | 162 ------------------ .../Download/DownloadThread.java | 39 ----- .../Download/FileDownloader.java | 114 ------------ .../Download/api/Connection.java | 22 --- .../Download/api/ConnectionException.java | 7 - .../Download/api/ConnectionManager.java | 10 -- .../Download/api/DownloadListener.java | 5 - .../Download/impl/ConnectionImpl.java | 73 -------- .../Download/impl/ConnectionManagerImpl.java | 16 -- 12 files changed, 742 deletions(-) delete mode 100644 group11/1310368322/src/FirstHomework/BaseDataStructure/ArrayList.java delete mode 100644 group11/1310368322/src/FirstHomework/BaseDataStructure/LinkedList.java delete mode 100644 group11/1310368322/src/SecondHomework/BaseDataStructure/ArrayUtil.java delete mode 100644 group11/1310368322/src/ThirdHomework/BaseDataStructure/LinkedList.java delete mode 100644 group11/1310368322/src/ThirdHomework/Download/DownloadThread.java delete mode 100644 group11/1310368322/src/ThirdHomework/Download/FileDownloader.java delete mode 100644 group11/1310368322/src/ThirdHomework/Download/api/Connection.java delete mode 100644 group11/1310368322/src/ThirdHomework/Download/api/ConnectionException.java delete mode 100644 group11/1310368322/src/ThirdHomework/Download/api/ConnectionManager.java delete mode 100644 group11/1310368322/src/ThirdHomework/Download/api/DownloadListener.java delete mode 100644 group11/1310368322/src/ThirdHomework/Download/impl/ConnectionImpl.java delete mode 100644 group11/1310368322/src/ThirdHomework/Download/impl/ConnectionManagerImpl.java diff --git a/group11/1310368322/src/FirstHomework/BaseDataStructure/ArrayList.java b/group11/1310368322/src/FirstHomework/BaseDataStructure/ArrayList.java deleted file mode 100644 index b5024f1dfb..0000000000 --- a/group11/1310368322/src/FirstHomework/BaseDataStructure/ArrayList.java +++ /dev/null @@ -1,98 +0,0 @@ -package Day_2017_2_26_FirstHomework; - -public class ArrayList { - - private static final int DEFAULT_SIZE = 10; - private static final int MAX_VALUE = 2147483647; - private Object[] elementData = new Object[DEFAULT_SIZE]; - private Exception Exception; - private int size = 0; - - public ArrayList(){ - this(DEFAULT_SIZE); - } - public ArrayList(int defaultSize) { - rangCheckForConstructor(defaultSize); - elementData = new Object[defaultSize]; - } - - - private void rangCheckForConstructor(int defaultSize) { - if(defaultSize<0 || defaultSize>MAX_VALUE){ - throw new IndexOutOfBoundsException("ֵ"); - } - - } - - public void add(Object o){ - ensureCapacity(); - for(int i = 0; i < elementData.length; i++){ - if(null == elementData[i]){ - elementData[i] = o; - break; - } - } - size++; - } - private void ensureCapacity() { - if(size>elementData.length){ - elementData = ArrayList.grow(elementData, 10); - } - } - public void add(int index, Object o){ - rangeCheckForAdd(index); - ensureCapacity(); - int k = -1; - for(int i = index; i < elementData.length; i++){ - if(null==elementData[i]){ - k = i-1; - break; - } - } - for(int i = k; i >= index;i--){ - elementData[i+1] = elementData[i]; - } - elementData[index] = o; - size++; - } - private void rangeCheckForAdd(int index) { - if(index < 0 || index > this.size){// add Ԫֻ [0,size](ԸsizeλòԪأԸsizeԪ) - throw new IndexOutOfBoundsException("±Խ"); - } - - } - public Object get(int index){ - return elementData[index]; - } - - public Object remove(int index){ - while(true){ - elementData[index] = elementData[index+++1]; - if(elementData[index]==null){ - break; - } - } - size--; - return null; - } - public int size(){ - return -1; - } - public void getElementData(){ - for(int i = 0; i < elementData.length; i++){ - System.out.println(elementData[i]); - - } - } - public static Object[] grow(Object[] elementData2, int size){ - Object []target = new Object[elementData2.length+size]; - System.arraycopy(elementData2, 0, target, 0, elementData2.length); - return target; - } - - public static void main(String[] args) { - ArrayList a = new ArrayList(); - a.getElementData(); - System.out.println(a.size); - } -} diff --git a/group11/1310368322/src/FirstHomework/BaseDataStructure/LinkedList.java b/group11/1310368322/src/FirstHomework/BaseDataStructure/LinkedList.java deleted file mode 100644 index 488f2a22a6..0000000000 --- a/group11/1310368322/src/FirstHomework/BaseDataStructure/LinkedList.java +++ /dev/null @@ -1,113 +0,0 @@ -package Day_2017_2_26_FirstHomework; - -public class LinkedList{ - private Node head; - static int size = 0; - public void add(Object o){ - if(null == head){ - head = new Node(); - head.data = o; - head.next = null; - }else{ - Node p = head; - while(null != p.next){ - p = p.next; - } - Node newNode = new Node(); - newNode.data = o; - p.next = newNode; - newNode.next =null; - } - size++; - } - public int size(){ - return size; - } - public void add(int index,Object o){ - if(index < 0){ - throw new RuntimeException("±겻Ϊ"); - } - if(index == 0){ - addFirst(o); - size++; - return; - } - if(index > size){ - throw new RuntimeException(""); - } - int i = 0; - Node p = head; - Node q = null; - - while(i!=index){ - q = p; - p = p.next; - i++; - } - Node r = new Node(); - r.data = o; - r.next =null; - q.next = r; - r.next = p; - size++; - return; - } - - public Object get(int index){ - int i = 0; - Node p = head; - while(i != index){ - p = p.next; - i++; - } - return p.data; - } - public Object remove(int index){ - if(index < 0){ - throw new RuntimeException("±겻Ϊ"); - } - if(index == 1){ - size--; - return head.data; - } - int i = 0; - Node p = head; - Node q = null; - while(i != index){ - q = p; - p = p.next; - i++; - } - q.next = p.next; - size--; - return p.data; - } - public void addFirst(Object o){ - Node p = new Node(); - p.next = head; - p.data = o; - head = p; - size++; - } - public Object removeFirst(){ - head = head.next; - size--; - return null; - } - public static class Node{ - Object data; - Node next; - } - - public static void main(String[] args) { - LinkedList linkedList = new LinkedList(); - linkedList.add("a"); - linkedList.add("b"); - linkedList.add("c"); - linkedList.add("d"); - linkedList.add(5, "f"); - System.out.println(linkedList.get(5)); - System.out.println(linkedList.size()); - } - -} \ No newline at end of file diff --git a/group11/1310368322/src/SecondHomework/BaseDataStructure/ArrayUtil.java b/group11/1310368322/src/SecondHomework/BaseDataStructure/ArrayUtil.java deleted file mode 100644 index 81bc431679..0000000000 --- a/group11/1310368322/src/SecondHomework/BaseDataStructure/ArrayUtil.java +++ /dev/null @@ -1,83 +0,0 @@ -package day_2017_2_26_SecondHomework; - -import java.util.Arrays; - -import javax.management.RuntimeErrorException; - -public class ArrayUtil { - - /* * - * һ a Ըֵû - * 磺 a = [7, 9, 30, 3], ûΪ [3, 30, 9, 7] - * */ - - /*public ArrayUtil(int[] a2) { - this.a = a2; - }*/ - public void reverseArray(int [] a){ - if(null == a){ - System.out.println("ָ----"); - return; - } - int temp; - int last = a.length-1; - for (int i = 0; i < a.length/2; i++) { - temp = a[i]; - a[i] = a[last]; - a[last--] = temp; - } - } - public void print(int [] a){ - if(null == a){ - System.out.println("ָ----"); - return; - } - for (int i = 0; i < a.length; i++) { - System.out.print(a[i] + " "); - } - System.out.println(); - } - - /* * - * µһ飬 int oldArr[] = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * ҪеֵΪ 0 ȥΪ 0 ֵһµ飬ɵΪ - * {1,3,4,5,6,6,5,4,7,6,7,5} - * @param oldArray - * @return - */ - public int [] removeZero(int [] oldArray){ - if(null == oldArray){ - return null; - } - int count = 0; - int oldArrayLength = oldArray.length; - for(int i = 0; i < oldArrayLength;){ - if(oldArray[i]==0){ - for(int j = i; j < oldArrayLength -1; j++){ - oldArray[j] = oldArray[j+1]; - } - oldArrayLength--; - count++; - }else{ - i++; - } - } - int [] target = new int[oldArray.length-count]; - System.arraycopy(oldArray, 0, target, 0, oldArray.length-count); - return target; - } - - - - - - - - - - - - - - -} diff --git a/group11/1310368322/src/ThirdHomework/BaseDataStructure/LinkedList.java b/group11/1310368322/src/ThirdHomework/BaseDataStructure/LinkedList.java deleted file mode 100644 index 5ea6a988be..0000000000 --- a/group11/1310368322/src/ThirdHomework/BaseDataStructure/LinkedList.java +++ /dev/null @@ -1,162 +0,0 @@ -package DataStructure_3; - -import java.util.Stack; - -import DataStructure_1.LinkedList.Node; - -public class LinkedList { - private Node head; - static int size = 0; - public void add(Object o){ - if(null == head){ - head = new Node(); - head.data = o; - head.next = null; - }else{ - Node p = head; - while(null != p.next){ - p = p.next; - } - Node newNode = new Node(); - newNode.data = o; - p.next = newNode; - newNode.next =null; - } - size++; - } - public int size(){ - return size; - } - public void add(int index,Object o){ - if(index < 0){ - throw new RuntimeException("±겻Ϊ"); - } - if(index == 0){ - addFirst(o); - size++; - return; - } - if(index > size){ - throw new RuntimeException(""); - } - int i = 0; - Node p = head; - Node q = null; - - while(i!=index){ - q = p; - p = p.next; - i++; - } - Node r = new Node(); - r.data = o; - r.next =null; - q.next = r; - r.next = p; - size++; - return; - } - - public Object get(int index){ - int i = 0; - Node p = head; - while(i != index){ - p = p.next; - i++; - } - return p.data; - } - public Object remove(int index){ - if(index < 0){ - throw new RuntimeException("±겻Ϊ"); - } - if(index == 1){ - size--; - return head.data; - } - int i = 0; - Node p = head; - Node q = null; - while(i != index){ - q = p; - p = p.next; - i++; - } - q.next = p.next; - size--; - return p.data; - } - public void addFirst(Object o){ - Node p = new Node(); - p.next = head; - p.data = o; - head = p; - size++; - } - public Object removeFirst(){ - head = head.next; - size--; - return null; - } - public static class Node{ - Object data; - Node next; - } - - /** - * Ѹ - * 3->7->10 úΪ 10->7->3 - */ - public void reverse(){ - if(null == head || null == head.next){ - return; - } - Stack s = new Stack(); - Node curNode = head; - while(curNode != null){ - s.push(curNode); - Node nextNode = curNode.next; - curNode.next = null; // Ͽ - curNode = nextNode; - } - - head = s.pop(); - curNode = head; - while(!s.isEmpty()){ - Node nextNode = s.pop(); - curNode.next = nextNode; - curNode = nextNode; - } - - } - - public String toString(){ - StringBuffer buffer = new StringBuffer(); - buffer.append("["); - Node node = head; - while(node != null){ - buffer.append(node.data); - if(node.next != null){ - buffer.append(","); - } - node = node.next; - } - buffer.append("]"); - return buffer.toString(); - } - - - - - - - - - - - - - - - -} diff --git a/group11/1310368322/src/ThirdHomework/Download/DownloadThread.java b/group11/1310368322/src/ThirdHomework/Download/DownloadThread.java deleted file mode 100644 index af248b72b2..0000000000 --- a/group11/1310368322/src/ThirdHomework/Download/DownloadThread.java +++ /dev/null @@ -1,39 +0,0 @@ -package day_2017_3_8_ThreadHomework; - -import java.io.RandomAccessFile; -import java.util.concurrent.CyclicBarrier; - -import com.coderising.download.api.Connection; - -public class DownloadThread extends Thread{ - - Connection conn; - int startPos; - int endPos; - CyclicBarrier barrier; - String localFile; - public DownloadThread(Connection conn, int startPos, int endPos,String localFile,CyclicBarrier barrier){ - this.conn = conn; - this.startPos = startPos; - this.endPos = endPos; - this.localFile = localFile; - this.barrier = barrier; - } - public void run(){ - try { - System.out.println("Begin to read [" + startPos + "-" + endPos + "]"); - byte [] data = conn.read(startPos, endPos); - System.out.println("һȡļĶ"); - RandomAccessFile file = new RandomAccessFile(localFile,"rw"); - file.seek(startPos); - System.out.println("Ҫд"); - file.write(data); - file.close(); - conn.close(); - System.out.println(this.currentThread().getName()+"once over"); - barrier.await(); - } catch (Exception e) { - // TODO: handle exception - } - } -} diff --git a/group11/1310368322/src/ThirdHomework/Download/FileDownloader.java b/group11/1310368322/src/ThirdHomework/Download/FileDownloader.java deleted file mode 100644 index 445ede2e3d..0000000000 --- a/group11/1310368322/src/ThirdHomework/Download/FileDownloader.java +++ /dev/null @@ -1,114 +0,0 @@ -package day_2017_3_8_ThreadHomework; - -import java.io.IOException; -import java.io.RandomAccessFile; -import java.util.concurrent.CyclicBarrier; - -import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionManager; -import com.coderising.download.api.DownloadListener; - -public class FileDownloader { - private String url; - private String localFile; - DownloadListener listener; - ConnectionManager cm; - - private static final int DOWNLOAD_THREAD_NUM = 3; - public FileDownloader(String _url,String localFile){ - this.url = _url; - this.localFile = localFile; - } - - public void execute(){ - // ʵĴ룬 ע⣺ Ҫö߳ʵ - // ӿڣҪд⼸ӿڵʵִ - // 1 ConnectionManager ԴһӣͨConnectionԶȡеһΣStartPos,endPosָ - // 2DownloadListener, Ƕ߳أĿͻ˲֪ʲôʱҪʵ̶ִֵ߳Ժ󣬵listenernotifiedFinishedͻ˾յ֪ͨ - // ʵ˼· - // 1. ҪConnectionManager open ӣȻͨ Connection.getContentLengthļij - // 2. 3߳أעÿ߳ҪȵConnectionManageropen - // Ȼ read read жȡļĿʼλúͽλõIJֵbyte[] - // 3. byte д뵽ļ - // 4.е̶߳ԺҪ listener notifiedFinished - - // Ĵʵ룬Ҳ˵ֻһ̣߳Ҫɶ̵߳ - CyclicBarrier barrier = new CyclicBarrier(DOWNLOAD_THREAD_NUM,new Runnable() {// еThread awaitʱִк barrierAction,ú߳ - @Override - public void run() { - listener.notifyFinished(); - } - }); - - Connection conn = null; - try { - conn = cm.open(this.url); - int length = conn.getContentLength();// õҪļij - createPlaceHolderFile(this.localFile,length);//ռλ - System.out.println("ռλ"); - int [][] ranges = allocateDownloadRange(DOWNLOAD_THREAD_NUM,length);// ÿ̷߳俪ʼλúͽλ - // ʼļ - System.out.println("ʼļ"); - for(int i = 0; i < DOWNLOAD_THREAD_NUM; i++){ - DownloadThread thread = new DownloadThread( - cm.open(url), - ranges[i][0], - ranges[i][1], - localFile, - barrier); - thread.start(); - System.out.println("" + (i+1) + "߳Ѿ"); - } - - } catch (Exception e) { - e.printStackTrace(); - }finally{ - System.out.println("ر"); - if(conn != null){ - conn.close(); - System.out.println("رӳɹ"); - } - } - } - - public void setListener(DownloadListener listener){ - this.listener = listener; - } - public void setConnectionManager(ConnectionManager ucm){ - this.cm = ucm; - } - public DownloadListener getListener(){ - return this.listener; - } - private void createPlaceHolderFile(String fileName,int contentLen) throws IOException{ - RandomAccessFile file = new RandomAccessFile(fileName,"rw"); - for(int i = 0; i < contentLen; i++){ - file.write(0); - } - file.close(); - } - /** - * ߳ļȣһά飬ÿ߳صĿʼλúͽλ - * @param threadNum - * @param contentLen - * @return - */ - private int [][] allocateDownloadRange(int threadNum, int contentLen){ - int [][] ranges = new int[threadNum][2];// öάÿ̵߳Ŀʼλúͽλ - - int eachThreadSize = contentLen / threadNum;// ÿ߳ҪصļС - int left = contentLen % threadNum;// ʣµĹһ߳ - - for(int i = 0; i totalLen){ - byte[] data = baos.toByteArray(); - return Arrays.copyOf(data, totalLen); - } - return baos.toByteArray(); - } - - @Override - public int getContentLength() { - URLConnection con; - try { - con = url.openConnection(); - return con.getContentLength(); - } catch (Exception e) { - e.printStackTrace(); - } - return -1; - } - - @Override - public void close() { - - } - -} diff --git a/group11/1310368322/src/ThirdHomework/Download/impl/ConnectionManagerImpl.java b/group11/1310368322/src/ThirdHomework/Download/impl/ConnectionManagerImpl.java deleted file mode 100644 index ff92aa77fe..0000000000 --- a/group11/1310368322/src/ThirdHomework/Download/impl/ConnectionManagerImpl.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.coderising.download.impl; - -import java.net.URL; - -import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; -import com.coderising.download.api.ConnectionManager; - -public class ConnectionManagerImpl implements ConnectionManager{ - - @Override - public Connection open(String url) throws ConnectionException { - return new ConnectionImpl(url); - } - -} From 27f7a294865b93a64f1663bb95c003b50fde3fab Mon Sep 17 00:00:00 2001 From: James <1310368322@qq.com> Date: Fri, 24 Mar 2017 12:28:19 +0800 Subject: [PATCH 06/41] Third_Homework --- .../BaseDataStructure/ArrayList.java | 98 +++++++++++ .../BaseDataStructure/LinkedList.java | 113 ++++++++++++ .../BaseDataStructure/ArrayUtil.java | 83 +++++++++ .../BaseDataStructure/LinkedList.java | 162 ++++++++++++++++++ .../Download/DownloadThread.java | 39 +++++ .../Download/FileDownloader.java | 114 ++++++++++++ .../Download/api/Connection.java | 22 +++ .../Download/api/ConnectionException.java | 7 + .../Download/api/ConnectionManager.java | 10 ++ .../Download/api/DownloadListener.java | 5 + .../Download/impl/ConnectionImpl.java | 73 ++++++++ .../Download/impl/ConnectionManagerImpl.java | 16 ++ 12 files changed, 742 insertions(+) create mode 100644 group11/1310368322/src/FirstHomework/BaseDataStructure/ArrayList.java create mode 100644 group11/1310368322/src/FirstHomework/BaseDataStructure/LinkedList.java create mode 100644 group11/1310368322/src/SecondHomework/BaseDataStructure/ArrayUtil.java create mode 100644 group11/1310368322/src/ThirdHomework/BaseDataStructure/LinkedList.java create mode 100644 group11/1310368322/src/ThirdHomework/Download/DownloadThread.java create mode 100644 group11/1310368322/src/ThirdHomework/Download/FileDownloader.java create mode 100644 group11/1310368322/src/ThirdHomework/Download/api/Connection.java create mode 100644 group11/1310368322/src/ThirdHomework/Download/api/ConnectionException.java create mode 100644 group11/1310368322/src/ThirdHomework/Download/api/ConnectionManager.java create mode 100644 group11/1310368322/src/ThirdHomework/Download/api/DownloadListener.java create mode 100644 group11/1310368322/src/ThirdHomework/Download/impl/ConnectionImpl.java create mode 100644 group11/1310368322/src/ThirdHomework/Download/impl/ConnectionManagerImpl.java diff --git a/group11/1310368322/src/FirstHomework/BaseDataStructure/ArrayList.java b/group11/1310368322/src/FirstHomework/BaseDataStructure/ArrayList.java new file mode 100644 index 0000000000..b5024f1dfb --- /dev/null +++ b/group11/1310368322/src/FirstHomework/BaseDataStructure/ArrayList.java @@ -0,0 +1,98 @@ +package Day_2017_2_26_FirstHomework; + +public class ArrayList { + + private static final int DEFAULT_SIZE = 10; + private static final int MAX_VALUE = 2147483647; + private Object[] elementData = new Object[DEFAULT_SIZE]; + private Exception Exception; + private int size = 0; + + public ArrayList(){ + this(DEFAULT_SIZE); + } + public ArrayList(int defaultSize) { + rangCheckForConstructor(defaultSize); + elementData = new Object[defaultSize]; + } + + + private void rangCheckForConstructor(int defaultSize) { + if(defaultSize<0 || defaultSize>MAX_VALUE){ + throw new IndexOutOfBoundsException("ֵ"); + } + + } + + public void add(Object o){ + ensureCapacity(); + for(int i = 0; i < elementData.length; i++){ + if(null == elementData[i]){ + elementData[i] = o; + break; + } + } + size++; + } + private void ensureCapacity() { + if(size>elementData.length){ + elementData = ArrayList.grow(elementData, 10); + } + } + public void add(int index, Object o){ + rangeCheckForAdd(index); + ensureCapacity(); + int k = -1; + for(int i = index; i < elementData.length; i++){ + if(null==elementData[i]){ + k = i-1; + break; + } + } + for(int i = k; i >= index;i--){ + elementData[i+1] = elementData[i]; + } + elementData[index] = o; + size++; + } + private void rangeCheckForAdd(int index) { + if(index < 0 || index > this.size){// add Ԫֻ [0,size](ԸsizeλòԪأԸsizeԪ) + throw new IndexOutOfBoundsException("±Խ"); + } + + } + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + while(true){ + elementData[index] = elementData[index+++1]; + if(elementData[index]==null){ + break; + } + } + size--; + return null; + } + public int size(){ + return -1; + } + public void getElementData(){ + for(int i = 0; i < elementData.length; i++){ + System.out.println(elementData[i]); + + } + } + public static Object[] grow(Object[] elementData2, int size){ + Object []target = new Object[elementData2.length+size]; + System.arraycopy(elementData2, 0, target, 0, elementData2.length); + return target; + } + + public static void main(String[] args) { + ArrayList a = new ArrayList(); + a.getElementData(); + System.out.println(a.size); + } +} diff --git a/group11/1310368322/src/FirstHomework/BaseDataStructure/LinkedList.java b/group11/1310368322/src/FirstHomework/BaseDataStructure/LinkedList.java new file mode 100644 index 0000000000..488f2a22a6 --- /dev/null +++ b/group11/1310368322/src/FirstHomework/BaseDataStructure/LinkedList.java @@ -0,0 +1,113 @@ +package Day_2017_2_26_FirstHomework; + +public class LinkedList{ + private Node head; + static int size = 0; + public void add(Object o){ + if(null == head){ + head = new Node(); + head.data = o; + head.next = null; + }else{ + Node p = head; + while(null != p.next){ + p = p.next; + } + Node newNode = new Node(); + newNode.data = o; + p.next = newNode; + newNode.next =null; + } + size++; + } + public int size(){ + return size; + } + public void add(int index,Object o){ + if(index < 0){ + throw new RuntimeException("±겻Ϊ"); + } + if(index == 0){ + addFirst(o); + size++; + return; + } + if(index > size){ + throw new RuntimeException(""); + } + int i = 0; + Node p = head; + Node q = null; + + while(i!=index){ + q = p; + p = p.next; + i++; + } + Node r = new Node(); + r.data = o; + r.next =null; + q.next = r; + r.next = p; + size++; + return; + } + + public Object get(int index){ + int i = 0; + Node p = head; + while(i != index){ + p = p.next; + i++; + } + return p.data; + } + public Object remove(int index){ + if(index < 0){ + throw new RuntimeException("±겻Ϊ"); + } + if(index == 1){ + size--; + return head.data; + } + int i = 0; + Node p = head; + Node q = null; + while(i != index){ + q = p; + p = p.next; + i++; + } + q.next = p.next; + size--; + return p.data; + } + public void addFirst(Object o){ + Node p = new Node(); + p.next = head; + p.data = o; + head = p; + size++; + } + public Object removeFirst(){ + head = head.next; + size--; + return null; + } + public static class Node{ + Object data; + Node next; + } + + public static void main(String[] args) { + LinkedList linkedList = new LinkedList(); + linkedList.add("a"); + linkedList.add("b"); + linkedList.add("c"); + linkedList.add("d"); + linkedList.add(5, "f"); + System.out.println(linkedList.get(5)); + System.out.println(linkedList.size()); + } + +} \ No newline at end of file diff --git a/group11/1310368322/src/SecondHomework/BaseDataStructure/ArrayUtil.java b/group11/1310368322/src/SecondHomework/BaseDataStructure/ArrayUtil.java new file mode 100644 index 0000000000..81bc431679 --- /dev/null +++ b/group11/1310368322/src/SecondHomework/BaseDataStructure/ArrayUtil.java @@ -0,0 +1,83 @@ +package day_2017_2_26_SecondHomework; + +import java.util.Arrays; + +import javax.management.RuntimeErrorException; + +public class ArrayUtil { + + /* * + * һ a Ըֵû + * 磺 a = [7, 9, 30, 3], ûΪ [3, 30, 9, 7] + * */ + + /*public ArrayUtil(int[] a2) { + this.a = a2; + }*/ + public void reverseArray(int [] a){ + if(null == a){ + System.out.println("ָ----"); + return; + } + int temp; + int last = a.length-1; + for (int i = 0; i < a.length/2; i++) { + temp = a[i]; + a[i] = a[last]; + a[last--] = temp; + } + } + public void print(int [] a){ + if(null == a){ + System.out.println("ָ----"); + return; + } + for (int i = 0; i < a.length; i++) { + System.out.print(a[i] + " "); + } + System.out.println(); + } + + /* * + * µһ飬 int oldArr[] = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * ҪеֵΪ 0 ȥΪ 0 ֵһµ飬ɵΪ + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + public int [] removeZero(int [] oldArray){ + if(null == oldArray){ + return null; + } + int count = 0; + int oldArrayLength = oldArray.length; + for(int i = 0; i < oldArrayLength;){ + if(oldArray[i]==0){ + for(int j = i; j < oldArrayLength -1; j++){ + oldArray[j] = oldArray[j+1]; + } + oldArrayLength--; + count++; + }else{ + i++; + } + } + int [] target = new int[oldArray.length-count]; + System.arraycopy(oldArray, 0, target, 0, oldArray.length-count); + return target; + } + + + + + + + + + + + + + + +} diff --git a/group11/1310368322/src/ThirdHomework/BaseDataStructure/LinkedList.java b/group11/1310368322/src/ThirdHomework/BaseDataStructure/LinkedList.java new file mode 100644 index 0000000000..5ea6a988be --- /dev/null +++ b/group11/1310368322/src/ThirdHomework/BaseDataStructure/LinkedList.java @@ -0,0 +1,162 @@ +package DataStructure_3; + +import java.util.Stack; + +import DataStructure_1.LinkedList.Node; + +public class LinkedList { + private Node head; + static int size = 0; + public void add(Object o){ + if(null == head){ + head = new Node(); + head.data = o; + head.next = null; + }else{ + Node p = head; + while(null != p.next){ + p = p.next; + } + Node newNode = new Node(); + newNode.data = o; + p.next = newNode; + newNode.next =null; + } + size++; + } + public int size(){ + return size; + } + public void add(int index,Object o){ + if(index < 0){ + throw new RuntimeException("±겻Ϊ"); + } + if(index == 0){ + addFirst(o); + size++; + return; + } + if(index > size){ + throw new RuntimeException(""); + } + int i = 0; + Node p = head; + Node q = null; + + while(i!=index){ + q = p; + p = p.next; + i++; + } + Node r = new Node(); + r.data = o; + r.next =null; + q.next = r; + r.next = p; + size++; + return; + } + + public Object get(int index){ + int i = 0; + Node p = head; + while(i != index){ + p = p.next; + i++; + } + return p.data; + } + public Object remove(int index){ + if(index < 0){ + throw new RuntimeException("±겻Ϊ"); + } + if(index == 1){ + size--; + return head.data; + } + int i = 0; + Node p = head; + Node q = null; + while(i != index){ + q = p; + p = p.next; + i++; + } + q.next = p.next; + size--; + return p.data; + } + public void addFirst(Object o){ + Node p = new Node(); + p.next = head; + p.data = o; + head = p; + size++; + } + public Object removeFirst(){ + head = head.next; + size--; + return null; + } + public static class Node{ + Object data; + Node next; + } + + /** + * Ѹ + * 3->7->10 úΪ 10->7->3 + */ + public void reverse(){ + if(null == head || null == head.next){ + return; + } + Stack s = new Stack(); + Node curNode = head; + while(curNode != null){ + s.push(curNode); + Node nextNode = curNode.next; + curNode.next = null; // Ͽ + curNode = nextNode; + } + + head = s.pop(); + curNode = head; + while(!s.isEmpty()){ + Node nextNode = s.pop(); + curNode.next = nextNode; + curNode = nextNode; + } + + } + + public String toString(){ + StringBuffer buffer = new StringBuffer(); + buffer.append("["); + Node node = head; + while(node != null){ + buffer.append(node.data); + if(node.next != null){ + buffer.append(","); + } + node = node.next; + } + buffer.append("]"); + return buffer.toString(); + } + + + + + + + + + + + + + + + +} diff --git a/group11/1310368322/src/ThirdHomework/Download/DownloadThread.java b/group11/1310368322/src/ThirdHomework/Download/DownloadThread.java new file mode 100644 index 0000000000..af248b72b2 --- /dev/null +++ b/group11/1310368322/src/ThirdHomework/Download/DownloadThread.java @@ -0,0 +1,39 @@ +package day_2017_3_8_ThreadHomework; + +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + CyclicBarrier barrier; + String localFile; + public DownloadThread(Connection conn, int startPos, int endPos,String localFile,CyclicBarrier barrier){ + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.localFile = localFile; + this.barrier = barrier; + } + public void run(){ + try { + System.out.println("Begin to read [" + startPos + "-" + endPos + "]"); + byte [] data = conn.read(startPos, endPos); + System.out.println("һȡļĶ"); + RandomAccessFile file = new RandomAccessFile(localFile,"rw"); + file.seek(startPos); + System.out.println("Ҫд"); + file.write(data); + file.close(); + conn.close(); + System.out.println(this.currentThread().getName()+"once over"); + barrier.await(); + } catch (Exception e) { + // TODO: handle exception + } + } +} diff --git a/group11/1310368322/src/ThirdHomework/Download/FileDownloader.java b/group11/1310368322/src/ThirdHomework/Download/FileDownloader.java new file mode 100644 index 0000000000..445ede2e3d --- /dev/null +++ b/group11/1310368322/src/ThirdHomework/Download/FileDownloader.java @@ -0,0 +1,114 @@ +package day_2017_3_8_ThreadHomework; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + +public class FileDownloader { + private String url; + private String localFile; + DownloadListener listener; + ConnectionManager cm; + + private static final int DOWNLOAD_THREAD_NUM = 3; + public FileDownloader(String _url,String localFile){ + this.url = _url; + this.localFile = localFile; + } + + public void execute(){ + // ʵĴ룬 ע⣺ Ҫö߳ʵ + // ӿڣҪд⼸ӿڵʵִ + // 1 ConnectionManager ԴһӣͨConnectionԶȡеһΣStartPos,endPosָ + // 2DownloadListener, Ƕ߳أĿͻ˲֪ʲôʱҪʵ̶ִֵ߳Ժ󣬵listenernotifiedFinishedͻ˾յ֪ͨ + // ʵ˼· + // 1. ҪConnectionManager open ӣȻͨ Connection.getContentLengthļij + // 2. 3߳أעÿ߳ҪȵConnectionManageropen + // Ȼ read read жȡļĿʼλúͽλõIJֵbyte[] + // 3. byte д뵽ļ + // 4.е̶߳ԺҪ listener notifiedFinished + + // Ĵʵ룬Ҳ˵ֻһ̣߳Ҫɶ̵߳ + CyclicBarrier barrier = new CyclicBarrier(DOWNLOAD_THREAD_NUM,new Runnable() {// еThread awaitʱִк barrierAction,ú߳ + @Override + public void run() { + listener.notifyFinished(); + } + }); + + Connection conn = null; + try { + conn = cm.open(this.url); + int length = conn.getContentLength();// õҪļij + createPlaceHolderFile(this.localFile,length);//ռλ + System.out.println("ռλ"); + int [][] ranges = allocateDownloadRange(DOWNLOAD_THREAD_NUM,length);// ÿ̷߳俪ʼλúͽλ + // ʼļ + System.out.println("ʼļ"); + for(int i = 0; i < DOWNLOAD_THREAD_NUM; i++){ + DownloadThread thread = new DownloadThread( + cm.open(url), + ranges[i][0], + ranges[i][1], + localFile, + barrier); + thread.start(); + System.out.println("" + (i+1) + "߳Ѿ"); + } + + } catch (Exception e) { + e.printStackTrace(); + }finally{ + System.out.println("ر"); + if(conn != null){ + conn.close(); + System.out.println("رӳɹ"); + } + } + } + + public void setListener(DownloadListener listener){ + this.listener = listener; + } + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + public DownloadListener getListener(){ + return this.listener; + } + private void createPlaceHolderFile(String fileName,int contentLen) throws IOException{ + RandomAccessFile file = new RandomAccessFile(fileName,"rw"); + for(int i = 0; i < contentLen; i++){ + file.write(0); + } + file.close(); + } + /** + * ߳ļȣһά飬ÿ߳صĿʼλúͽλ + * @param threadNum + * @param contentLen + * @return + */ + private int [][] allocateDownloadRange(int threadNum, int contentLen){ + int [][] ranges = new int[threadNum][2];// öάÿ̵߳Ŀʼλúͽλ + + int eachThreadSize = contentLen / threadNum;// ÿ߳ҪصļС + int left = contentLen % threadNum;// ʣµĹһ߳ + + for(int i = 0; i totalLen){ + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + return baos.toByteArray(); + } + + @Override + public int getContentLength() { + URLConnection con; + try { + con = url.openConnection(); + return con.getContentLength(); + } catch (Exception e) { + e.printStackTrace(); + } + return -1; + } + + @Override + public void close() { + + } + +} diff --git a/group11/1310368322/src/ThirdHomework/Download/impl/ConnectionManagerImpl.java b/group11/1310368322/src/ThirdHomework/Download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..ff92aa77fe --- /dev/null +++ b/group11/1310368322/src/ThirdHomework/Download/impl/ConnectionManagerImpl.java @@ -0,0 +1,16 @@ +package com.coderising.download.impl; + +import java.net.URL; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager{ + + @Override + public Connection open(String url) throws ConnectionException { + return new ConnectionImpl(url); + } + +} From 6306c3c32788790761c53d691fe04b315c9aa1ca Mon Sep 17 00:00:00 2001 From: x_zhaohu Date: Sat, 25 Mar 2017 19:45:40 +0800 Subject: [PATCH 07/41] =?UTF-8?q?week01=20=E6=95=B4=E7=90=86=E5=AE=8C?= =?UTF-8?q?=E6=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group11/1178243325/week01/.readme.md.swp | Bin 0 -> 12288 bytes group11/1178243325/week01/readme.md | 5 +++-- 2 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 group11/1178243325/week01/.readme.md.swp diff --git a/group11/1178243325/week01/.readme.md.swp b/group11/1178243325/week01/.readme.md.swp new file mode 100644 index 0000000000000000000000000000000000000000..44d97db14f1c5235f5e0a65a0bdaee85c90f52de GIT binary patch literal 12288 zcmeI&OHUI~6ae6h3*CqsUAQx&Y$Tzb&J=7hQA1o93?>@IkQhzIb_%7Tt;5vDjgnZ} zYRk(9k6J*qhDJ(hKm^*t4{+(iU(h>uSeP#S1D>IPMgnnXFlUl8uY2y?bHCe7JH2nY z-F%I+nd=E`#|XL7_O0zp=P5FCn2>J2;`XXW!`|E9<~^ky@HTVf&W2^e(xOs^O6hHwK zKmim$0Te(16hHwKKmim$fqf_-!CUpFYC^^#jQ9Ur-~YcKCFCo_ID`Y@BE$uVlMt&% z2vHy&K-`141aS!BAjFTYXYE4^EExq*00mG01yBG5Pyhu`00sV;fQh@K6=(EZRL@Sa z?1+}kvqDIV4$}TfHW;Qa;;cB#(lLE*R!{fbJ=>wG{_{K^3 zYM~#U%xlSv_ByBaWm(~+HW8vr^E4ZWGC57IM|M_j-3Z75xsJP~$gpo#WkphbO1b#V zD4b<7P$eBomsXRd{)yp>d< Date: Sun, 26 Mar 2017 19:57:28 +0800 Subject: [PATCH 08/41] =?UTF-8?q?week02=E6=95=B4=E7=90=86=E5=AE=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group11/1178243325/week02/build.gradle | 12 + group11/1178243325/week02/readme.md | 23 ++ .../main/java/com/sprint/array/ArrayUtil.java | 229 ++++++++++++++++++ .../com/sprint/litestruts/Configuration.java | 86 +++++++ .../litestruts/ConfigurationException.java | 18 ++ .../com/sprint/litestruts/LoginAction.java | 40 +++ .../com/sprint/litestruts/ReflectionUtil.java | 70 ++++++ .../litestruts/ReflectionUtilException.java | 20 ++ .../java/com/sprint/litestruts/Struts.java | 53 ++++ .../main/java/com/sprint/litestruts/View.java | 24 ++ .../week02/src/main/resources/struts.xml | 12 + .../java/com/sprint/array/ArrayUtilTest.java | 76 ++++++ .../sprint/litestruts/ConfigurationTest.java | 31 +++ .../sprint/litestruts/ReflectionUtilTest.java | 92 +++++++ .../com/sprint/litestruts/StrutsTest.java | 32 +++ 15 files changed, 818 insertions(+) create mode 100644 group11/1178243325/week02/build.gradle create mode 100644 group11/1178243325/week02/readme.md create mode 100644 group11/1178243325/week02/src/main/java/com/sprint/array/ArrayUtil.java create mode 100644 group11/1178243325/week02/src/main/java/com/sprint/litestruts/Configuration.java create mode 100644 group11/1178243325/week02/src/main/java/com/sprint/litestruts/ConfigurationException.java create mode 100644 group11/1178243325/week02/src/main/java/com/sprint/litestruts/LoginAction.java create mode 100644 group11/1178243325/week02/src/main/java/com/sprint/litestruts/ReflectionUtil.java create mode 100644 group11/1178243325/week02/src/main/java/com/sprint/litestruts/ReflectionUtilException.java create mode 100644 group11/1178243325/week02/src/main/java/com/sprint/litestruts/Struts.java create mode 100644 group11/1178243325/week02/src/main/java/com/sprint/litestruts/View.java create mode 100644 group11/1178243325/week02/src/main/resources/struts.xml create mode 100644 group11/1178243325/week02/src/test/java/com/sprint/array/ArrayUtilTest.java create mode 100644 group11/1178243325/week02/src/test/java/com/sprint/litestruts/ConfigurationTest.java create mode 100644 group11/1178243325/week02/src/test/java/com/sprint/litestruts/ReflectionUtilTest.java create mode 100644 group11/1178243325/week02/src/test/java/com/sprint/litestruts/StrutsTest.java diff --git a/group11/1178243325/week02/build.gradle b/group11/1178243325/week02/build.gradle new file mode 100644 index 0000000000..f6ebbb892e --- /dev/null +++ b/group11/1178243325/week02/build.gradle @@ -0,0 +1,12 @@ +apply plugin: 'java' + + +repositories { + maven { url "http://maven.aliyun.com/nexus/content/groups/public" } + mavenCentral() +} + +dependencies { + compile("org.jdom:jdom2:2.0.5") + testCompile("junit:junit:4.12") +} diff --git a/group11/1178243325/week02/readme.md b/group11/1178243325/week02/readme.md new file mode 100644 index 0000000000..a4f9adc15a --- /dev/null +++ b/group11/1178243325/week02/readme.md @@ -0,0 +1,23 @@ +## 讲课内容: +- 17-03-01:第一周作业讲评 +- 17-03-05:漫谈进程和线程和布置第三周作业 + +## 第二周作业(2-27 至 3-5) +- 实现第一个大作业:读取struts.xml,执行Action +- 5道数据结构习题 + +## 完成情况: +- struts大作业完成 +- 数据结构习题完成 + +## 我的收获: +- TDD +- 操作系统抽象概念 +- 进程在虚拟存储器表示 +- 进程调度 +- 进程同步 +- 线程 + + +![99.jpg](http://upload-images.jianshu.io/upload_images/2031765-d3740acf4d284e93.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) + diff --git a/group11/1178243325/week02/src/main/java/com/sprint/array/ArrayUtil.java b/group11/1178243325/week02/src/main/java/com/sprint/array/ArrayUtil.java new file mode 100644 index 0000000000..01d7d5f0da --- /dev/null +++ b/group11/1178243325/week02/src/main/java/com/sprint/array/ArrayUtil.java @@ -0,0 +1,229 @@ +package com.sprint.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public static void reverseArray(int[] origin){ + if (origin == null) { + return; + } + + int length = origin.length; + int[] temp = new int[length]; + for (int i = 0; i < length; i++) + temp[i] = origin[i]; + for (int i = length - 1, j = 0; i >= 0 && j < length; i--, j++) + origin[j] = temp[i]; + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray){ + if (oldArray == null) { + return new int[0]; + } + + int zeroCount = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) + zeroCount++; + } + int[] newArray = new int[oldArray.length-zeroCount]; + for (int i = 0, j = 0; i < oldArray.length && j < newArray.length; i++) { + if (oldArray[i] != 0) { + newArray[j] = oldArray[i]; + j++; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2){ + if (array1 == null && array2 == null) + return new int[0]; + int index1 = 0, index2 = 0; + int[] array3 = new int[array1.length + array2.length]; + int index = 0; + while (index1 != array1.length && index2 != array2.length) { + if (array1[index1] < array2[index2]) { + array3[index++] = array1[index1++]; + } else if (array1[index1] > array2[index2]) { + array3[index++] = array2[index2++]; + } else if (array1[index1] == array2[index2]){ + array3[index++] = array1[index1++]; + index2++; + } + } + + if (index1 == array1.length && index2 != array2.length) { + for (int i = index2; i < array2.length; i++) + array3[index++] = array2[i]; + } else if (index2 == array2.length && index1 != array1.length) { + for (int i = index1; i < array1.length; i++) { + array3[index++] = array1[i]; + } + } + + int[] newArray = new int[index]; + for (int i = 0; i < newArray.length; i++) + newArray[i] = array3[i]; + return newArray; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + if (size <= 0) + return new int[0]; + int[] newArray = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if (max < 1) + return new int[0]; + if (max == 1) + return new int[0]; + int[] array = new int[max]; + int i = 0; + int value = fibonaccis(i+1); + while ( value < max) { + array[i++] = value; + value = fibonaccis(i+1); + } + int[] newArray = new int[i]; + for (int j = 0; j < newArray.length; j++) { + newArray[j] = array[j]; + } + return newArray; + } + + private static int fibonaccis(int n) { + if (n <=0) + return 0; + if (n == 1 || n ==2 ) + return 1; + return fibonaccis(n-1)+fibonaccis(n-2); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + if (max <= 1) { + return new int[0]; + } + int[] array = new int[max]; + int index = 0; + for (int i = 2; i < max; i++) { + if (i == 2 || i == 3 || i == 5 || i == 7) + array[index++] = i; + if (i%2 !=0 && i%3 != 0 && i%5 != 0 && i%7 != 0) + array[index++] = i; + } + int[] newArray = new int[index]; + for (int i = 0; i < newArray.length; i++) { + newArray[i] = array[i]; + } + + return newArray; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max){ + if (max <= 0) + return new int[0]; + int[] array = new int[max]; + int index = 0; + for (int i = 1; i < max; i++) { + if (isPerfectNumber(i)) + array[index++] = i; + } + + int[] newArray = new int[index]; + for (int i = 0; i < newArray.length; i++) + newArray[i] = array[i]; + + return newArray; + } + + private static boolean isPerfectNumber(int n) { + int sum = 0; + int i = 1; + while (i < n) { + if (n%i == 0) + sum += i; + i++; + } + if (sum == n) + return true; + return false; + } + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + if (array == null) + return null; + StringBuilder str = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + if (i == array.length-1) + str.append(array[i]); + else + str.append(array[i] + seperator); + } + return str.toString(); + } + + +} diff --git a/group11/1178243325/week02/src/main/java/com/sprint/litestruts/Configuration.java b/group11/1178243325/week02/src/main/java/com/sprint/litestruts/Configuration.java new file mode 100644 index 0000000000..0f10458fc3 --- /dev/null +++ b/group11/1178243325/week02/src/main/java/com/sprint/litestruts/Configuration.java @@ -0,0 +1,86 @@ +package com.sprint.litestruts; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + + +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.input.SAXBuilder; +public class Configuration { + Map actions = new HashMap<>(); + public Configuration(String fileName) { + InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileName); + parseXML(is); + try { + is.close(); + } catch (IOException e) { + throw new ConfigurationException(e); + } + } + + private void parseXML(InputStream is) { + SAXBuilder builder = new SAXBuilder(); + try { + Document doc = builder.build(is); + Element root = doc.getRootElement(); + for (Element actionElement : root.getChildren("action")) { + String actionName = actionElement.getAttributeValue("name"); + String clzName = actionElement.getAttributeValue("class"); + + ActionConfig ac = new ActionConfig(actionName, clzName); + for (Element resultElement : actionElement.getChildren("result")) { + String resultName = resultElement.getAttributeValue("name"); + String viewName = resultElement.getText().trim(); + ac.addViewResult(resultName, viewName); + } + this.actions.put(actionName, ac); + } + } catch (JDOMException e) { + throw new ConfigurationException(e); + } catch (IOException e) { + throw new ConfigurationException(e); + } + } + + public String getClassName(String actionName) { + ActionConfig ac = this.actions.get(actionName); + if(ac == null) { + return null; + } + return ac.getClassName(); + } + + public String getResultView(String actionName, String resultName) { + ActionConfig ac = this.actions.get(actionName); + if (ac == null) { + return null; + } + return ac.getViewName(resultName); + } + private static class ActionConfig { + String name; + String clzName; + Map viewResult = new HashMap<>(); + + public ActionConfig(String actionName, String clzName) { + this.name = actionName; + this.clzName = clzName; + } + + public String getClassName() { + return clzName; + } + + public void addViewResult(String name, String viewName) { + viewResult.put(name, viewName); + } + + public String getViewName(String resultName) { + return viewResult.get(resultName); + } + } +} diff --git a/group11/1178243325/week02/src/main/java/com/sprint/litestruts/ConfigurationException.java b/group11/1178243325/week02/src/main/java/com/sprint/litestruts/ConfigurationException.java new file mode 100644 index 0000000000..25c6784f43 --- /dev/null +++ b/group11/1178243325/week02/src/main/java/com/sprint/litestruts/ConfigurationException.java @@ -0,0 +1,18 @@ +package com.sprint.litestruts; + +import java.io.IOException; +import org.jdom2.JDOMException; +public class ConfigurationException extends RuntimeException { + + public ConfigurationException(String msg) { + super(msg); + } + + public ConfigurationException(JDOMException e) { + super(e); + } + + public ConfigurationException(IOException e) { + super(e); + } +} diff --git a/group11/1178243325/week02/src/main/java/com/sprint/litestruts/LoginAction.java b/group11/1178243325/week02/src/main/java/com/sprint/litestruts/LoginAction.java new file mode 100644 index 0000000000..4b4f2b8e38 --- /dev/null +++ b/group11/1178243325/week02/src/main/java/com/sprint/litestruts/LoginAction.java @@ -0,0 +1,40 @@ +package com.sprint.litestruts; + +/** + * 这是一个展示业务逻辑的类,其中用户名是硬编码 + * @author xingzhaohu + */ + +public class LoginAction { + private String name; + private String password; + private String message; + + public String execute() { + if ("test".equals(name) && "1234".equals(password)) { + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + public void setName(String name) { + this.name = name; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String getMessage() { + return message; + } +} diff --git a/group11/1178243325/week02/src/main/java/com/sprint/litestruts/ReflectionUtil.java b/group11/1178243325/week02/src/main/java/com/sprint/litestruts/ReflectionUtil.java new file mode 100644 index 0000000000..822355655b --- /dev/null +++ b/group11/1178243325/week02/src/main/java/com/sprint/litestruts/ReflectionUtil.java @@ -0,0 +1,70 @@ +package com.sprint.litestruts; + +import java.lang.reflect.Method; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +public class ReflectionUtil { + + public static List getSetterMethods(Class clz) { + return getMethods(clz, "set"); + } + + public static List getGetterMethods(Class clz) { + return getMethods(clz, "get"); + } + + private static List getMethods(Class clz, String startWithName) { + List methods = new ArrayList<>(); + for (Method m : clz.getDeclaredMethods()) { + if (m.getName().startsWith(startWithName)) { + methods.add(m); + } + } + return methods; + } + + public static void setParameters(Object o, Map params) { + List methods = getSetterMethods(o.getClass()); + for (String name : params.keySet()) { + String methodName = "set" + name; + for (Method m : methods) { + if (m.getName().equalsIgnoreCase(methodName)) { + try { + m.invoke(o, params.get(name)); + } catch (IllegalAccessException e) { + throw new ReflectionUtilException(e); + } catch (IllegalArgumentException e) { + throw new ReflectionUtilException(e); + } catch (InvocationTargetException e) { + throw new ReflectionUtilException(e); + } + } + } + } + + } + + public static Map getParameterMap(Object o) { + Map params = new HashMap<>(); + List methods = getGetterMethods(o.getClass()); + for (Method m : methods) { + String methodName = m.getName(); + String name = methodName.replaceFirst("get", "").toLowerCase(); + try { + Object value = m.invoke(o); + params.put(name, value); + } catch (IllegalAccessException e) { + throw new ReflectionUtilException(e); + } catch (IllegalArgumentException e) { + throw new ReflectionUtilException(e); + } catch (InvocationTargetException e) { + throw new ReflectionUtilException(e); + } + } + return params; + } + +} diff --git a/group11/1178243325/week02/src/main/java/com/sprint/litestruts/ReflectionUtilException.java b/group11/1178243325/week02/src/main/java/com/sprint/litestruts/ReflectionUtilException.java new file mode 100644 index 0000000000..65cbfdf322 --- /dev/null +++ b/group11/1178243325/week02/src/main/java/com/sprint/litestruts/ReflectionUtilException.java @@ -0,0 +1,20 @@ +package com.sprint.litestruts; + +import java.lang.reflect.InvocationTargetException; +public class ReflectionUtilException extends RuntimeException { + + public ReflectionUtilException(String msg) { + super(msg); + } + public ReflectionUtilException(IllegalAccessException e) { + super(e); + } + + public ReflectionUtilException(IllegalArgumentException e) { + super(e); + } + + public ReflectionUtilException(InvocationTargetException e) { + super(e); + } +} diff --git a/group11/1178243325/week02/src/main/java/com/sprint/litestruts/Struts.java b/group11/1178243325/week02/src/main/java/com/sprint/litestruts/Struts.java new file mode 100644 index 0000000000..bff4dec33b --- /dev/null +++ b/group11/1178243325/week02/src/main/java/com/sprint/litestruts/Struts.java @@ -0,0 +1,53 @@ +package com.sprint.litestruts; + +import java.lang.reflect.Method; +import java.util.Map; +public class Struts { + private final static Configuration cfg = new Configuration("struts.xml"); + public static View runAction(String actionName, Map parameters) { + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + String clzName = cfg.getClassName(actionName); + if (clzName == null) { + return null; + } + + try { + Class clz = Class.forName(clzName); + Object action = clz.newInstance(); + + ReflectionUtil.setParameters(action, parameters); + + Method m = clz.getDeclaredMethod("execute"); + String resultName = (String)m.invoke(action); + + Map params = ReflectionUtil.getParameterMap(action); + String resultView = cfg.getResultView(actionName, resultName); + View view = new View(); + view.setParameters(params); + view.setJsp(resultView); + return view; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } +} diff --git a/group11/1178243325/week02/src/main/java/com/sprint/litestruts/View.java b/group11/1178243325/week02/src/main/java/com/sprint/litestruts/View.java new file mode 100644 index 0000000000..fb380de515 --- /dev/null +++ b/group11/1178243325/week02/src/main/java/com/sprint/litestruts/View.java @@ -0,0 +1,24 @@ +package com.sprint.litestruts; + +import java.util.Map; +public class View { + private String jsp; + private Map parameters; + + public void setJsp(String jsp) { + this.jsp = jsp; + } + + public String getJsp() { + return jsp; + } + + public void setParameters(Map parameters) { + this.parameters = parameters; + } + + public Map getParameters() { + return parameters; + } + +} diff --git a/group11/1178243325/week02/src/main/resources/struts.xml b/group11/1178243325/week02/src/main/resources/struts.xml new file mode 100644 index 0000000000..c82c7d1574 --- /dev/null +++ b/group11/1178243325/week02/src/main/resources/struts.xml @@ -0,0 +1,12 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + + diff --git a/group11/1178243325/week02/src/test/java/com/sprint/array/ArrayUtilTest.java b/group11/1178243325/week02/src/test/java/com/sprint/array/ArrayUtilTest.java new file mode 100644 index 0000000000..9cba21f7cf --- /dev/null +++ b/group11/1178243325/week02/src/test/java/com/sprint/array/ArrayUtilTest.java @@ -0,0 +1,76 @@ +package com.sprint.array; + +import java.util.Arrays; +import org.junit.Assert; +import org.junit.Test; +public class ArrayUtilTest { + + @Test + public void testReverseArray() { + int[] a = new int[]{7, 9, 30, 3}; + int[] expected = new int[]{3, 30, 9, 7}; + ArrayUtil.reverseArray(a); + Assert.assertArrayEquals(a, expected); + a = new int[]{7, 9, 30, 3, 4}; + expected = new int[]{4, 3, 30, 9, 7}; + ArrayUtil.reverseArray(a); + Assert.assertArrayEquals(a, expected); + } + + @Test + public void testRemoveZero() { + int[] oldArr = new int[]{1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] expected = new int[]{1,3,4,5,6,6,5,4,7,6,7,5}; + Assert.assertArrayEquals(ArrayUtil.removeZero(oldArr), expected); + oldArr = new int[]{1, 0, 2, 0, 3, 0}; + expected = new int[]{1, 2, 3}; + Assert.assertArrayEquals(ArrayUtil.removeZero(oldArr), expected); + } + + @Test + public void testMerge() { + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + int[] a3 = {3, 4, 5, 6, 7, 8}; + Assert.assertArrayEquals(ArrayUtil.merge(a1, a2), a3); + } + + @Test + public void testGrow() { + int[] oldArray = new int[]{2, 3, 6}; + int[] expected = new int[]{2, 3, 6, 0, 0, 0}; + Assert.assertArrayEquals(ArrayUtil.grow(oldArray, 3), expected); + } + + @Test + public void testFibonacci() { + int[] expected = new int[]{1, 1, 2, 3, 5, 8, 13}; + Assert.assertArrayEquals(ArrayUtil.fibonacci(15), expected); + expected = new int[0]; + Assert.assertArrayEquals(ArrayUtil.fibonacci(1), expected); + /*GET 新技能: [] == new int[0]*/ + System.out.println(Arrays.toString(expected)); + } + + @Test + public void testGetPrimes() { + int[] expected = new int[]{2,3,5,7,11,13,17,19}; + Assert.assertArrayEquals(ArrayUtil.getPrimes(23), expected); + } + + @Test + public void testGetPerfectNumbers() { + int[] result = new int[]{6}; + int length = ArrayUtil.getPerfectNumbers(7).length; + System.out.println(length); + Assert.assertArrayEquals(ArrayUtil.getPerfectNumbers(7), result); + } + + @Test + public void tetJoin() { + String result = "3-8-9"; + int[] array = {3, 8, 9}; + Assert.assertEquals(ArrayUtil.join(array, "-"), result); + } + +} diff --git a/group11/1178243325/week02/src/test/java/com/sprint/litestruts/ConfigurationTest.java b/group11/1178243325/week02/src/test/java/com/sprint/litestruts/ConfigurationTest.java new file mode 100644 index 0000000000..f4b5f91668 --- /dev/null +++ b/group11/1178243325/week02/src/test/java/com/sprint/litestruts/ConfigurationTest.java @@ -0,0 +1,31 @@ +package com.sprint.litestruts; + +import org.junit.Test; +import org.junit.Assert; +public class ConfigurationTest { + Configuration cfg = new Configuration("struts.xml"); + + @Test + public void testGetClassName() { + String clzName = cfg.getClassName("login"); + Assert.assertEquals("com.sprint.litestruts.LoginAction", clzName); + clzName = cfg.getClassName("logout"); + Assert.assertEquals("com.sprint.litestruts.LoginAction", clzName); + } + + @Test + public void testGetResultView() { + String jsp = cfg.getResultView("login", "success"); + Assert.assertEquals("/jsp/homepage.jsp", jsp); + + jsp = cfg.getResultView("login", "fail"); + Assert.assertEquals("/jsp/showLogin.jsp", jsp); + + jsp = cfg.getResultView("logout", "success"); + Assert.assertEquals("/jsp/welcome.jsp", jsp); + + jsp = cfg.getResultView("logout", "error"); + Assert.assertEquals("/jsp/error.jsp", jsp); + } + +} diff --git a/group11/1178243325/week02/src/test/java/com/sprint/litestruts/ReflectionUtilTest.java b/group11/1178243325/week02/src/test/java/com/sprint/litestruts/ReflectionUtilTest.java new file mode 100644 index 0000000000..c6fb04bda6 --- /dev/null +++ b/group11/1178243325/week02/src/test/java/com/sprint/litestruts/ReflectionUtilTest.java @@ -0,0 +1,92 @@ +package com.sprint.litestruts; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import java.util.HashSet; +import java.util.HashMap; +import java.util.Set; +import java.util.Map; + + +import org.junit.Test; +import org.junit.Assert; +public class ReflectionUtilTest { + + @Test + public void testGetSetterMethod() throws Exception{ + String name = "com.sprint.litestruts.LoginAction"; + Class clz = Class.forName(name); + List methods = ReflectionUtil.getSetterMethods(clz); + + Assert.assertEquals(2, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("setName"); + expectedNames.add("setPassword"); + + Set acctualNames = new HashSet<>(); + for (Method m : methods) { + acctualNames.add(m.getName()); + } + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + } + + @Test + public void testGetGetterMethod() throws Exception { + String name = "com.sprint.litestruts.LoginAction"; + Class clz = Class.forName(name); + List methods = ReflectionUtil.getGetterMethods(clz); + + Assert.assertEquals(3, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("getName"); + expectedNames.add("getPassword"); + expectedNames.add("getMessage"); + + Set acctualNames = new HashSet<>(); + for (Method m : methods) { + acctualNames.add(m.getName()); + } + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + } + + @Test + public void testSetParameters() throws Exception { + String name = "com.sprint.litestruts.LoginAction"; + Class clz = Class.forName(name); + Object o = clz.newInstance(); + + Map params = new HashMap<>(); + params.put("name", "test"); + params.put("password", "1234"); + + ReflectionUtil.setParameters(o, params); + Field f = clz.getDeclaredField("name"); + f.setAccessible(true); + Assert.assertEquals("test", f.get(o)); + + f = clz.getDeclaredField("password"); + f.setAccessible(true); + Assert.assertEquals("1234", f.get(o)); + } + + @Test + public void testGetParameters() throws Exception { + String name = "com.sprint.litestruts.LoginAction"; + Class clz = Class.forName(name); + LoginAction action = (LoginAction)clz.newInstance(); + action.setName("test"); + action.setPassword("123456"); + + Map params = ReflectionUtil.getParameterMap(action); + Assert.assertEquals(3, params.size()); + Assert.assertEquals(null, params.get("message")); + Assert.assertEquals("test", params.get("name")); + Assert.assertEquals("123456", params.get("password")); + + } + +} diff --git a/group11/1178243325/week02/src/test/java/com/sprint/litestruts/StrutsTest.java b/group11/1178243325/week02/src/test/java/com/sprint/litestruts/StrutsTest.java new file mode 100644 index 0000000000..d8c68d2807 --- /dev/null +++ b/group11/1178243325/week02/src/test/java/com/sprint/litestruts/StrutsTest.java @@ -0,0 +1,32 @@ +package com.sprint.litestruts; + +import java.util.HashMap; +import java.util.Map; +import org.junit.Assert; +import org.junit.Test; +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + String actionName = "login"; + Map params = new HashMap<>(); + params.put("name", "test"); + params.put("password", "1234"); + + View view = Struts.runAction(actionName, params); + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap<>(); + params.put("name", "test"); + params.put("password", "123456"); //密码不一致 + + View view = Struts.runAction(actionName, params); + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} From 0e6cf0946b7a939641df0d6569d0b5d406622efe Mon Sep 17 00:00:00 2001 From: x_zhaohu Date: Mon, 27 Mar 2017 12:00:21 +0800 Subject: [PATCH 09/41] =?UTF-8?q?=E6=95=B4=E7=90=86=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E5=91=A8=E7=9A=84=E7=9F=A5=E8=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group11/1178243325/week03/build.gradle | 9 + group11/1178243325/week03/readme.md | 16 + .../main/java/com/sprint/basic/Iterator.java | 6 + .../java/com/sprint/basic/LinkedList.java | 319 ++++++++++++++++++ .../src/main/java/com/sprint/basic/List.java | 9 + .../ConcurrentModificationException.java | 9 + .../com/sprint/download/DownloadThread.java | 36 ++ .../com/sprint/download/FileDownloader.java | 89 +++++ .../com/sprint/download/api/Connection.java | 26 ++ .../download/api/ConnectionException.java | 7 + .../download/api/ConnectionManager.java | 11 + .../sprint/download/api/DownloadListener.java | 5 + .../sprint/download/impl/ConnectionImpl.java | 68 ++++ .../download/impl/ConnectionManagerImpl.java | 14 + .../sprint/download/FileDownloaderTest.java | 41 +++ .../sprint/download/api/ConnectionTest.java | 21 ++ 16 files changed, 686 insertions(+) create mode 100644 group11/1178243325/week03/build.gradle create mode 100644 group11/1178243325/week03/readme.md create mode 100644 group11/1178243325/week03/src/main/java/com/sprint/basic/Iterator.java create mode 100644 group11/1178243325/week03/src/main/java/com/sprint/basic/LinkedList.java create mode 100644 group11/1178243325/week03/src/main/java/com/sprint/basic/List.java create mode 100644 group11/1178243325/week03/src/main/java/com/sprint/basic/exception/ConcurrentModificationException.java create mode 100644 group11/1178243325/week03/src/main/java/com/sprint/download/DownloadThread.java create mode 100644 group11/1178243325/week03/src/main/java/com/sprint/download/FileDownloader.java create mode 100644 group11/1178243325/week03/src/main/java/com/sprint/download/api/Connection.java create mode 100644 group11/1178243325/week03/src/main/java/com/sprint/download/api/ConnectionException.java create mode 100644 group11/1178243325/week03/src/main/java/com/sprint/download/api/ConnectionManager.java create mode 100644 group11/1178243325/week03/src/main/java/com/sprint/download/api/DownloadListener.java create mode 100644 group11/1178243325/week03/src/main/java/com/sprint/download/impl/ConnectionImpl.java create mode 100644 group11/1178243325/week03/src/main/java/com/sprint/download/impl/ConnectionManagerImpl.java create mode 100644 group11/1178243325/week03/src/test/java/com/sprint/download/FileDownloaderTest.java create mode 100644 group11/1178243325/week03/src/test/java/com/sprint/download/api/ConnectionTest.java diff --git a/group11/1178243325/week03/build.gradle b/group11/1178243325/week03/build.gradle new file mode 100644 index 0000000000..c4da624f95 --- /dev/null +++ b/group11/1178243325/week03/build.gradle @@ -0,0 +1,9 @@ +apply plugin: 'java' + +repositories { + mavenCentral() +} + +dependencies { + testCompile("junit:junit:4.12") +} diff --git a/group11/1178243325/week03/readme.md b/group11/1178243325/week03/readme.md new file mode 100644 index 0000000000..e418f46fd6 --- /dev/null +++ b/group11/1178243325/week03/readme.md @@ -0,0 +1,16 @@ +## 讲课内容: +- 17-03-07:答疑(YY) +- 17-03-09:TDD和第二次作业讲解 +- 17-03-12:职场15年 + +## 第二周作业(3-6 至 3-12) +- 实现第二个大作业:多线程下载文件,支持断点续传 +- 5道数据结构习题 + +## 完成情况: +- 多线程完成 +- 待重构 + +## 我的收获: +- TTD大法好 +- 面向对象的思想以及抽象化,更深刻 diff --git a/group11/1178243325/week03/src/main/java/com/sprint/basic/Iterator.java b/group11/1178243325/week03/src/main/java/com/sprint/basic/Iterator.java new file mode 100644 index 0000000000..ff93e30377 --- /dev/null +++ b/group11/1178243325/week03/src/main/java/com/sprint/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group11/1178243325/week03/src/main/java/com/sprint/basic/LinkedList.java b/group11/1178243325/week03/src/main/java/com/sprint/basic/LinkedList.java new file mode 100644 index 0000000000..ac4128fe80 --- /dev/null +++ b/group11/1178243325/week03/src/main/java/com/sprint/basic/LinkedList.java @@ -0,0 +1,319 @@ +package com.sprint.basic; + +import com.sprint.basic.exception.ConcurrentModificationException; +import java.util.Objects; +public class LinkedList implements List { + + private Node head; + private int size; + public LinkedList() { + head = new Node(null, null); + size = 0; + } + + public void add(Object o){ + add(size, o); + } + + public void add(int index , Object o){ + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + Node frontNode = getNode(index-1); + Node newNode = new Node(o, frontNode.next); + frontNode.next = newNode; + size++; + + } + + /*getNode getPreNodeByElement getNextNodeByElement的效率低些*/ + private Node getNode(int index) { + Node node = head; + int i = 0; + while(node.next != null && i <= index) { + node = node.next; + i++; + } + return node; + } + + private Node getPreNodeByElement(Object obj) { + if (obj != null) { + for (int i = 0; i < size(); i++) { + if (getNode(i).data == obj) { + return getNode(i-1); + } + } + } + return null; + } + + private Node getNextNodeByElement(Object obj) { + if (obj != null) { + for (int i = 0; i < size(); i++) { + if (getNode(i).data == obj) { + return getNode(i+1); + } + } + } + return null; + } + public Object get(int index){ + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + Node node = getNode(index); + return node.data; + } + + public Object remove(int index){ + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + Node frontNode = getNode(index-1); + Node oldNode = getNode(index); + frontNode.next = oldNode.next; + size--; + return oldNode.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + add(0, o); + } + + public void addLast(Object o){ + add(size, o); + } + + public Object removeFirst(){ + return remove(0); + } + + public Object removeLast(){ + return remove(size-1); + } + + public Iterator iterator(){ + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + int index; + final int capacity = size; + LinkedListIterator() { + index = 0; + } + @Override + public boolean hasNext() { + if (capacity != size) + throw new ConcurrentModificationException("此对象没有修改同步"); + return index < capacity; + } + + @Override + public Object next() { + if (capacity != size) + throw new ConcurrentModificationException("此对象没有修改同步"); + if (index >= capacity) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + return get(index++); + } + } + + private String outOfBoundsMsg(int index) { + return "index:" + index + ", size:" + size; + } + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + + void setData(Object data) { + this.data = data; + } + + Object getData() { + return data; + } + + void setNext(Node next) { + this.next = next; + } + + Object getNext() { + return next; + } + } + + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + Object[] oldData = new Object[size]; + Node temp = head; + int index = 1; + while(temp.next != null) { + temp = temp.next; + oldData[size - index] = temp.data; + index++; + } + + index = 0; + temp = head; + while(temp.next != null) { + temp = temp.next; + temp.data = oldData[index]; + index++; + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + + public void removeFirstHalf(){ + int count = size; + if (count % 2 != 0) { + for (int i = 0; i <= count/2; i++) { + removeFirst(); + } + } else { + for (int i = 0; i < count/2; i++) { + removeFirst(); + } + } + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + + public void remove(int i, int length){ + if (i < 0 || length < 0) { + return; + } + if (i == 0) { + for (int k = 0; k < length; k++) + removeFirst(); + } else { + while (length > 0) { + remove(i-1); + length--; + } + } + } + + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + + public int[] getElements(LinkedList list){ + if (list.size() == 0) { + return new int[0]; + } + int[] array = new int[list.size()]; + int index = 0; + for (int i = 0; i < list.size(); i++) { + array[i] = (int)get((int)list.get(i)); + } + return array; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + * @param list + */ + public void subtract(LinkedList list){ + if (list.size() == 0) { + return; + } + for (int i = 0; i < list.size(); i++) { + removeElement(list.get(i)); + } + } + + private Object removeElement(Object obj) { + if (obj == null) { + return null; + } + Node preNode = getPreNodeByElement(obj); + Node nextNode = getNextNodeByElement(obj); + if (preNode == null && nextNode == null) + return null; + preNode.next = nextNode; + return obj; + } + + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + + public void removeDuplicateValues(){ + if (size == 0 || size == 1) { + return; + } + + Node p1 = head; + Node p2 = head.next; + + while (p1 != null && p2 != null) { + if (Objects.equals(p1.data, p2.data)) { + p2 = p2.next; + p1.next = p2; + size--; + } else { + p1 = p2; + p2 = p2.next; + } + } + } + + + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + + public void removeRange(int min, int max){ + } + + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + + public LinkedList intersection( LinkedList list){ + + return null; + + } +} diff --git a/group11/1178243325/week03/src/main/java/com/sprint/basic/List.java b/group11/1178243325/week03/src/main/java/com/sprint/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group11/1178243325/week03/src/main/java/com/sprint/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group11/1178243325/week03/src/main/java/com/sprint/basic/exception/ConcurrentModificationException.java b/group11/1178243325/week03/src/main/java/com/sprint/basic/exception/ConcurrentModificationException.java new file mode 100644 index 0000000000..f1c5c79721 --- /dev/null +++ b/group11/1178243325/week03/src/main/java/com/sprint/basic/exception/ConcurrentModificationException.java @@ -0,0 +1,9 @@ +package com.coding.basic.exception; + +public class ConcurrentModificationException extends RuntimeException { + public ConcurrentModificationException() {} + public ConcurrentModificationException(String msg) { + super(msg); + } +} + diff --git a/group11/1178243325/week03/src/main/java/com/sprint/download/DownloadThread.java b/group11/1178243325/week03/src/main/java/com/sprint/download/DownloadThread.java new file mode 100644 index 0000000000..434b60e7be --- /dev/null +++ b/group11/1178243325/week03/src/main/java/com/sprint/download/DownloadThread.java @@ -0,0 +1,36 @@ +package com.sprint.download; + +import com.sprint.download.api.Connection; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; +public class DownloadThread extends Thread { + Connection conn; + int startPos; + int endPos; + CyclicBarrier barrier; + String localFile; + public DownloadThread(Connection conn, int startPos, int endPos, + String localFile, CyclicBarrier barrier) { + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.localFile = localFile; + this.barrier = barrier; + } + + @Override + public void run() { + try { + System.out.println("Begin to read [" + startPos + "-" + endPos + "]"); + byte[] data = conn.read(startPos, endPos); + RandomAccessFile file = new RandomAccessFile(localFile, "rw"); + file.seek(startPos); + file.write(data); + file.close(); + conn.close(); + barrier.await(); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/group11/1178243325/week03/src/main/java/com/sprint/download/FileDownloader.java b/group11/1178243325/week03/src/main/java/com/sprint/download/FileDownloader.java new file mode 100644 index 0000000000..9a817aefaf --- /dev/null +++ b/group11/1178243325/week03/src/main/java/com/sprint/download/FileDownloader.java @@ -0,0 +1,89 @@ +package com.sprint.download; + +import com.sprint.download.api.Connection; +import com.sprint.download.api.ConnectionManager; +import com.sprint.download.api.DownloadListener; +import java.util.concurrent.CyclicBarrier; +import java.io.RandomAccessFile; +import java.io.IOException; +public class FileDownloader { + private String url; + private String localFile; + + DownloadListener listener; + ConnectionManager cm; + + private static final int DOWNLOAD_THREAD_NUM = 3; + + public FileDownloader(String _url, String localFile) { + this.url = _url; + this.localFile = localFile; + } + + public void execute() { + CyclicBarrier barrier = new CyclicBarrier(DOWNLOAD_THREAD_NUM, new Runnable() { + public void run() { + listener.notifyFinished(); + } + }); + + Connection conn = null; + + try { + conn = cm.open(this.url); + int length = conn.getContentLength(); + createPlaceHolderFile(this.localFile, length); + int[][] ranges = allocateDownloadRange(DOWNLOAD_THREAD_NUM, length); + for (int i = 0; i < DOWNLOAD_THREAD_NUM; i++) { + DownloadThread thread = new DownloadThread( + cm.open(url), + ranges[i][0], + ranges[i][1], + localFile, + barrier); + thread.start(); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (conn != null) { + conn.close(); + } + } + } + + private void createPlaceHolderFile(String fileName, int contentLen) throws IOException { + RandomAccessFile file = new RandomAccessFile(fileName, "rw"); + for (int i = 0; i < contentLen; i++) { + file.write(0); + } + file.close(); + } + + private int[][] allocateDownloadRange(int threadNum, int contentLen) { + int[][] ranges = new int[threadNum][2]; + int eachThreadSize = contentLen / threadNum; + int left = contentLen % threadNum; + for (int i = 0; i < threadNum; i++) { + int startPos = i*eachThreadSize; + int endPos = (i+1) * eachThreadSize - 1; + if (i == (threadNum -1)) { + endPos += left; + } + ranges[i][0] = startPos; + ranges[i][1] = endPos; + } + return ranges; + } + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm) { + this.cm = ucm; + } + + public DownloadListener getListener() { + return listener; + } +} diff --git a/group11/1178243325/week03/src/main/java/com/sprint/download/api/Connection.java b/group11/1178243325/week03/src/main/java/com/sprint/download/api/Connection.java new file mode 100644 index 0000000000..958a0b1ce3 --- /dev/null +++ b/group11/1178243325/week03/src/main/java/com/sprint/download/api/Connection.java @@ -0,0 +1,26 @@ +package com.sprint.download.api; + +import java.io.IOException; +public interface Connection { + /** + * 给定开始位置和结束位置,读取数据,返回值是字节 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + + public byte[] read(int startPos, int endPos) throws IOException; + + /** + * 得到数据内容的长度 + * @return + */ + + public int getContentLength(); + + /** + * 关闭连接 + */ + + public void close(); +} diff --git a/group11/1178243325/week03/src/main/java/com/sprint/download/api/ConnectionException.java b/group11/1178243325/week03/src/main/java/com/sprint/download/api/ConnectionException.java new file mode 100644 index 0000000000..f9ec627440 --- /dev/null +++ b/group11/1178243325/week03/src/main/java/com/sprint/download/api/ConnectionException.java @@ -0,0 +1,7 @@ +package com.sprint.download.api; + +public class ConnectionException extends Exception { + public ConnectionException(Exception e) { + super(e); + } +} diff --git a/group11/1178243325/week03/src/main/java/com/sprint/download/api/ConnectionManager.java b/group11/1178243325/week03/src/main/java/com/sprint/download/api/ConnectionManager.java new file mode 100644 index 0000000000..f20bbacc87 --- /dev/null +++ b/group11/1178243325/week03/src/main/java/com/sprint/download/api/ConnectionManager.java @@ -0,0 +1,11 @@ +package com.sprint.download.api; + +public interface ConnectionManager { + /** + * 给定一个url, 打开一个连接 + * @param url + * @return + */ + + public Connection open(String url) throws ConnectionException; +} diff --git a/group11/1178243325/week03/src/main/java/com/sprint/download/api/DownloadListener.java b/group11/1178243325/week03/src/main/java/com/sprint/download/api/DownloadListener.java new file mode 100644 index 0000000000..fc95ba8199 --- /dev/null +++ b/group11/1178243325/week03/src/main/java/com/sprint/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.sprint.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group11/1178243325/week03/src/main/java/com/sprint/download/impl/ConnectionImpl.java b/group11/1178243325/week03/src/main/java/com/sprint/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..c6c1f32cb4 --- /dev/null +++ b/group11/1178243325/week03/src/main/java/com/sprint/download/impl/ConnectionImpl.java @@ -0,0 +1,68 @@ +package com.sprint.download.impl; + +import com.sprint.download.api.Connection; +import com.sprint.download.api.ConnectionException; +import java.util.Arrays; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.net.HttpURLConnection; + +public class ConnectionImpl implements Connection { + + URL url; + static final int BUFFER_SIZE = 1024; + + ConnectionImpl(String _url) throws ConnectionException { + try { + url = new URL(_url); + } catch (MalformedURLException e) { + throw new ConnectionException(e); + } + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + HttpURLConnection httpConn = (HttpURLConnection)url.openConnection(); + httpConn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + + InputStream is = httpConn.getInputStream(); + byte[] buff = new byte[BUFFER_SIZE]; + int totalLen = endPos - startPos + 1; + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + while (baos.size() < totalLen) { + int len = is.read(buff); + if (len < 0) { + break; + } + baos.write(buff, 0, len); + } + + if (baos.size() > totalLen) { + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + return baos.toByteArray(); + } + + @Override + public int getContentLength() { + URLConnection con; + try { + con = url.openConnection(); + return con.getContentLength(); + } catch (IOException e) { + e.printStackTrace(); + } + return -1; + } + + @Override + public void close() { + + } +} diff --git a/group11/1178243325/week03/src/main/java/com/sprint/download/impl/ConnectionManagerImpl.java b/group11/1178243325/week03/src/main/java/com/sprint/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..7a012808ef --- /dev/null +++ b/group11/1178243325/week03/src/main/java/com/sprint/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,14 @@ +package com.sprint.download.impl; + +import com.sprint.download.api.Connection; +import com.sprint.download.api.ConnectionException; +import com.sprint.download.api.ConnectionManager; +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + return new ConnectionImpl(url); + } +} + + diff --git a/group11/1178243325/week03/src/test/java/com/sprint/download/FileDownloaderTest.java b/group11/1178243325/week03/src/test/java/com/sprint/download/FileDownloaderTest.java new file mode 100644 index 0000000000..6e3bccd2dc --- /dev/null +++ b/group11/1178243325/week03/src/test/java/com/sprint/download/FileDownloaderTest.java @@ -0,0 +1,41 @@ +package com.sprint.download; + +import com.sprint.download.api.ConnectionManager; +import com.sprint.download.api.DownloadListener; +import com.sprint.download.impl.ConnectionManagerImpl; + +import org.junit.Assert; +import org.junit.Test; +public class FileDownloaderTest { + boolean downloadFinished = false; + + @Test + public void testDownload() { + String url = "http://images2015.cnblogs.com/blog/610238/201604/610238-20160421154632101-286208268.png"; + FileDownloader downloader = new FileDownloader(url, "/home/sprint/xxx/test.jpg");// 保存地址时我的本地地址 + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = false; + } + }); + + downloader.execute(); + + //等待多线程下载 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + } +} diff --git a/group11/1178243325/week03/src/test/java/com/sprint/download/api/ConnectionTest.java b/group11/1178243325/week03/src/test/java/com/sprint/download/api/ConnectionTest.java new file mode 100644 index 0000000000..4322814936 --- /dev/null +++ b/group11/1178243325/week03/src/test/java/com/sprint/download/api/ConnectionTest.java @@ -0,0 +1,21 @@ +package com.sprint.download.api; + +import com.sprint.download.impl.ConnectionManagerImpl; +import org.junit.Assert; +import org.junit.Test; +public class ConnectionTest { + + @Test + public void testGetContentLength() throws Exception { + ConnectionManager connMan = new ConnectionManagerImpl(); + Connection conn = connMan.open("http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"); + Assert.assertEquals(35470, conn.getContentLength()); + } + + @Test + public void testRead() throws Exception { + ConnectionManager connMan = new ConnectionManagerImpl(); + Connection conn = connMan.open("http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"); + + } +} From 1e9c327afe8dc62739d5f3cfdec7af5122f4da6c Mon Sep 17 00:00:00 2001 From: x_zhaohu Date: Mon, 27 Mar 2017 12:08:08 +0800 Subject: [PATCH 10/41] ok --- group11/1178243325/week04/readme.md | 12 ++++++++++++ group11/1178243325/week05/readme.md | 13 +++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 group11/1178243325/week04/readme.md create mode 100644 group11/1178243325/week05/readme.md diff --git a/group11/1178243325/week04/readme.md b/group11/1178243325/week04/readme.md new file mode 100644 index 0000000000..72c186be44 --- /dev/null +++ b/group11/1178243325/week04/readme.md @@ -0,0 +1,12 @@ +####讲课内容: +- 17-03-15:第三次作业讲解 +- 17-03-19:Edison分享的职业发展 + +####第四周作业(3-13 至 3-19) +无,调整进度 + +####完成情况: +休养生息 + +####我的收获: +- 职业发展:从新人到老兵 diff --git a/group11/1178243325/week05/readme.md b/group11/1178243325/week05/readme.md new file mode 100644 index 0000000000..d4388e66d7 --- /dev/null +++ b/group11/1178243325/week05/readme.md @@ -0,0 +1,13 @@ +## 讲课内容: +- 17-03-22 :漫谈操作系统之虚拟内存 +- 17-03-26 :概要性介绍class文件结构和字节码的执行过程 + +## 第五周作业(3-20 至 3-26) +无,调整进度 + +## 完成情况: +修养生息 + +## 我的收获: +- 虚拟内存 +- 进一步理解JVM From dad02b76d62d045695eb69fe6fab4617b271a53f Mon Sep 17 00:00:00 2001 From: x_zhaohu Date: Mon, 27 Mar 2017 12:11:22 +0800 Subject: [PATCH 11/41] finished --- group11/1178243325/DataStructure/build.gradle | 20 -- group11/1178243325/DataStructure/readme.md | 1 - .../DataStructure/src/main/java/com/Main.java | 17 -- .../java/com/coderising/array/ArrayUtil.java | 229 -------------- .../coderising/litestruts/LoginAction.java | 39 --- .../com/coderising/litestruts/Struts.java | 62 ---- .../com/coderising/litestruts/StrutsTest.java | 43 --- .../java/com/coderising/litestruts/View.java | 23 -- .../com/coderising/litestruts/XmlUtil.java | 59 ---- .../java/com/coderising/litestruts/struts.xml | 11 - .../com/coding/basic/.LinkedList.java.swp | Bin 16384 -> 0 bytes .../main/java/com/coding/basic/ArrayList.java | 97 ------ .../java/com/coding/basic/BinaryTreeNode.java | 33 -- .../main/java/com/coding/basic/Iterator.java | 6 - .../java/com/coding/basic/LinkedList.java | 289 ------------------ .../src/main/java/com/coding/basic/List.java | 9 - .../src/main/java/com/coding/basic/Queue.java | 30 -- .../src/main/java/com/coding/basic/Stack.java | 38 --- .../ConcurrentModificationException.java | 9 - .../basic/exception/EmptyQueueException.java | 8 - .../basic/exception/EmptyStackException.java | 8 - .../src/main/resources/struts.xml | 11 - group11/1178243325/DataStructure/struts.xml | 11 - group11/1178243325/week01/.readme.md.swp | Bin 12288 -> 0 bytes group11/1178243325/week04/readme.md | 8 +- 25 files changed, 4 insertions(+), 1057 deletions(-) delete mode 100644 group11/1178243325/DataStructure/build.gradle delete mode 100644 group11/1178243325/DataStructure/readme.md delete mode 100644 group11/1178243325/DataStructure/src/main/java/com/Main.java delete mode 100644 group11/1178243325/DataStructure/src/main/java/com/coderising/array/ArrayUtil.java delete mode 100644 group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/LoginAction.java delete mode 100644 group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java delete mode 100644 group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/StrutsTest.java delete mode 100644 group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/View.java delete mode 100644 group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java delete mode 100644 group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/struts.xml delete mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/.LinkedList.java.swp delete mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/ArrayList.java delete mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java delete mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/Iterator.java delete mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/LinkedList.java delete mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/List.java delete mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/Queue.java delete mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/Stack.java delete mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/ConcurrentModificationException.java delete mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyQueueException.java delete mode 100644 group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyStackException.java delete mode 100644 group11/1178243325/DataStructure/src/main/resources/struts.xml delete mode 100644 group11/1178243325/DataStructure/struts.xml delete mode 100644 group11/1178243325/week01/.readme.md.swp diff --git a/group11/1178243325/DataStructure/build.gradle b/group11/1178243325/DataStructure/build.gradle deleted file mode 100644 index 9c6bc859e6..0000000000 --- a/group11/1178243325/DataStructure/build.gradle +++ /dev/null @@ -1,20 +0,0 @@ - -apply plugin: 'java' -apply plugin: 'eclipse' - -jar { - from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }} - manifest { - attributes 'Main-Class' : 'com.Main' - } -} - -repositories { - mavenCentral() -} - -dependencies { - compile 'junit:junit:4.12' - compile 'dom4j:dom4j:1.6.1' -} - diff --git a/group11/1178243325/DataStructure/readme.md b/group11/1178243325/DataStructure/readme.md deleted file mode 100644 index 29ad9d5c06..0000000000 --- a/group11/1178243325/DataStructure/readme.md +++ /dev/null @@ -1 +0,0 @@ -实现基本的数据结构ArrayList,LinkList,Stack,Queue,Tree,Iterator diff --git a/group11/1178243325/DataStructure/src/main/java/com/Main.java b/group11/1178243325/DataStructure/src/main/java/com/Main.java deleted file mode 100644 index 8d49de33c4..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/Main.java +++ /dev/null @@ -1,17 +0,0 @@ -package com; - -import com.coding.basic.LinkedList; -import com.coding.basic.Iterator; -public class Main { - public static void main(String[] args) { - LinkedList list = new LinkedList(); - - for(int i = 0; i < 10; i++) - list.add(i); - list.remove(2, 5); - Iterator iter = list.iterator(); - while(iter.hasNext()) { - System.out.println(iter.next()); - } - } -} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/array/ArrayUtil.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/array/ArrayUtil.java deleted file mode 100644 index f94d5d01c4..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coderising/array/ArrayUtil.java +++ /dev/null @@ -1,229 +0,0 @@ -package com.coderising.array; - -public class ArrayUtil { - - /** - * 给定一个整形数组a , 对该数组的值进行置换 - 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] - 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] - * @param origin - * @return - */ - public static void reverseArray(int[] origin){ - if (origin == null) { - return; - } - - int length = origin.length; - int[] temp = new int[length]; - for (int i = 0; i < length; i++) - temp[i] = origin[i]; - for (int i = length - 1, j = 0; i >= 0 && j < length; i--, j++) - origin[j] = temp[i]; - } - - /** - * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: - * {1,3,4,5,6,6,5,4,7,6,7,5} - * @param oldArray - * @return - */ - - public static int[] removeZero(int[] oldArray){ - if (oldArray == null) { - return null; - } - - int zeroCount = 0; - for (int i = 0; i < oldArray.length; i++) { - if (oldArray[i] == 0) - zeroCount++; - } - int[] newArray = new int[oldArray.length-zeroCount]; - for (int i = 0, j = 0; i < oldArray.length && j < newArray.length; i++) { - if (oldArray[i] != 0) { - newArray[j] = oldArray[i]; - j++; - } - } - return newArray; - } - - /** - * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 - * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 - * @param array1 - * @param array2 - * @return - */ - - public static int[] merge(int[] array1, int[] array2){ - if (array1 == null && array2 == null) - return null; - int index1 = 0, index2 = 0; - int[] array3 = new int[array1.length + array2.length]; - int index = 0; - while (index1 != array1.length && index2 != array2.length) { - if (array1[index1] < array2[index2]) { - array3[index++] = array1[index1++]; - } else if (array1[index1] > array2[index2]) { - array3[index++] = array2[index2++]; - } else if (array1[index1] == array2[index2]){ - array3[index++] = array1[index1++]; - index2++; - } - } - - if (index1 == array1.length && index2 != array2.length) { - for (int i = index2; i < array2.length; i++) - array3[index++] = array2[i]; - } else if (index2 == array2.length && index1 != array1.length) { - for (int i = index1; i < array1.length; i++) { - array3[index++] = array1[i]; - } - } - - int[] newArray = new int[index]; - for (int i = 0; i < newArray.length; i++) - newArray[i] = array3[i]; - return newArray; - } - /** - * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size - * 注意,老数组的元素在新数组中需要保持 - * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 - * [2,3,6,0,0,0] - * - * @param oldArray - * @param size - * @return - */ - public static int[] grow(int [] oldArray, int size){ - if (size <= 0) - return null; - int[] newArray = new int[oldArray.length + size]; - for (int i = 0; i < oldArray.length; i++) { - newArray[i] = oldArray[i]; - } - return newArray; - } - - /** - * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 - * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] - * max = 1, 则返回空数组 [] - * @param max - * @return - */ - public static int[] fibonacci(int max){ - if (max < 1) - return null; - if (max == 1) - return null; - int[] array = new int[max]; - int i = 0; - int value = fibonaccis(i+1); - while ( value < max) { - array[i++] = value; - value = fibonaccis(i+1); - } - int[] newArray = new int[i]; - for (int j = 0; j < newArray.length; j++) { - newArray[j] = array[j]; - } - return newArray; - } - - private static int fibonaccis(int n) { - if (n <=0) - return 0; - if (n == 1 || n ==2 ) - return 1; - return fibonaccis(n-1)+fibonaccis(n-2); - } - - /** - * 返回小于给定最大值max的所有素数数组 - * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] - * @param max - * @return - */ - public static int[] getPrimes(int max){ - if (max <= 1) { - return null; - } - int[] array = new int[max]; - int index = 0; - for (int i = 2; i < max; i++) { - if (i == 2 || i == 3 || i == 5 || i == 7) - array[index++] = i; - if (i%2 !=0 && i%3 != 0 && i%5 != 0 && i%7 != 0) - array[index++] = i; - } - int[] newArray = new int[index]; - for (int i = 0; i < newArray.length; i++) { - newArray[i] = array[i]; - } - - return newArray; - } - - /** - * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 - * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 - * @param max - * @return - */ - public static int[] getPerfectNumbers(int max){ - if (max <= 0) - return null; - int[] array = new int[max]; - int index = 0; - for (int i = 1; i < max; i++) { - if (isPerfectNumber(i)) - array[index++] = i; - } - - int[] newArray = new int[index]; - for (int i = 0; i < newArray.length; i++) - newArray[i] = array[i]; - - return newArray; - } - - private static boolean isPerfectNumber(int n) { - int sum = 0; - int i = 1; - while (i < n) { - if (n%i == 0) - sum += i; - i++; - } - if (sum == n) - return true; - return false; - } - /** - * 用seperator 把数组 array给连接起来 - * 例如array= [3,8,9], seperator = "-" - * 则返回值为"3-8-9" - * @param array - * @param s - * @return - */ - public static String join(int[] array, String seperator){ - if (array == null) - return null; - StringBuilder str = new StringBuilder(); - for (int i = 0; i < array.length; i++) { - if (i == array.length-1) - str.append(array[i]); - else - str.append(array[i] + seperator); - } - return str.toString(); - } - - -} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/LoginAction.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/LoginAction.java deleted file mode 100644 index 1005f35a29..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/LoginAction.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.coderising.litestruts; - -/** - * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 - * @author liuxin - * - */ -public class LoginAction{ - private String name ; - private String password; - private String message; - - public String getName() { - return name; - } - - public String getPassword() { - return password; - } - - public String execute(){ - if("test".equals(name) && "1234".equals(password)){ - this.message = "login successful"; - return "success"; - } - this.message = "login failed,please check your user/pwd"; - return "fail"; - } - - public void setName(String name){ - this.name = name; - } - public void setPassword(String password){ - this.password = password; - } - public String getMessage(){ - return this.message; - } -} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java deleted file mode 100644 index b3bd421435..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.coderising.litestruts; - -import java.util.Map; -import java.util.HashMap; -import java.lang.reflect.Method; -public class Struts { - - public static View runAction(String actionName, Map parameters) { - - /* - - 0. 读取配置文件struts.xml*/ - /* - 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) - 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 - ("name"="test" , "password"="1234") , - 那就应该调用 setName和setPassword方法 - - 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" - - 3. 通过反射找到对象的所有getter方法(例如 getMessage), - 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , - 放到View对象的parameters - - 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, - 放到View对象的jsp字段中。 - - */ - try { - String targetClassName = XmlUtil.parseXML("struts.xml", actionName); - Class targetClass = Class.forName(targetClassName); - - Method setName = targetClass.getMethod("setName", String.class); - Method setPassword = targetClass.getMethod("setPassword", String.class); - Object object = targetClass.newInstance(); - - setName.invoke(object, parameters.get("name")); - setPassword.invoke(object, parameters.get("password")); - - Method execute = targetClass.getMethod("execute"); - String result = (String)execute.invoke(object); - - Method getMessage = targetClass.getMethod("getMessage"); - String message = (String)getMessage.invoke(object); - - Map params = new HashMap(); - params.put("message", message); - String jspUrl = XmlUtil.getJspUrl("struts.xml", actionName, result); - View view = new View(); - view.setJsp(jspUrl); - view.setParameters(params); - return view; - - } catch (Exception e) { - e.printStackTrace(); - } - - - return null; - } - -} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/StrutsTest.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/StrutsTest.java deleted file mode 100644 index a44c1878ac..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/StrutsTest.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.coderising.litestruts; - -import java.util.HashMap; -import java.util.Map; - -import org.junit.Assert; -import org.junit.Test; - - - - - -public class StrutsTest { - - @Test - public void testLoginActionSuccess() { - - String actionName = "login"; - - Map params = new HashMap(); - params.put("name","test"); - params.put("password","1234"); - - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); - Assert.assertEquals("login successful", view.getParameters().get("message")); - } - - @Test - public void testLoginActionFailed() { - String actionName = "login"; - Map params = new HashMap(); - params.put("name","test"); - params.put("password","123456"); //密码和预设的不一致 - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); - Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); - } -} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/View.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/View.java deleted file mode 100644 index 0194c681f6..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/View.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.coderising.litestruts; - -import java.util.Map; - -public class View { - private String jsp; - private Map parameters; - - public String getJsp() { - return jsp; - } - public View setJsp(String jsp) { - this.jsp = jsp; - return this; - } - public Map getParameters() { - return parameters; - } - public View setParameters(Map parameters) { - this.parameters = parameters; - return this; - } -} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java deleted file mode 100644 index d200452cc8..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.coderising.litestruts; - -import java.io.*; -import java.util.*; -import org.dom4j.Attribute; -import org.dom4j.Document; -import org.dom4j.Element; -import org.dom4j.io.SAXReader; -import org.dom4j.io.XMLWriter; -public class XmlUtil { - - public static String parseXML(String filePath, String actionName) { - try { - File file = new File(filePath); - SAXReader reader = new SAXReader(); - Document doc = reader.read(file); - Element root = doc.getRootElement(); - for (Iterator iter = root.elementIterator("action"); iter.hasNext();) { - Element element = (Element)iter.next(); - Attribute nameAttr = element.attribute("name"); - if (nameAttr.getValue().equals(actionName)) { - Attribute classAttr = element.attribute("class"); - return classAttr.getValue(); - } - } - } catch (Exception e) { - e.printStackTrace(); - System.out.println("parse error"); - } - return null; - } - - public static String getJspUrl(String filePath, String actionName, String resultName) { - try { - File file = new File(filePath); - SAXReader reader = new SAXReader(); - Document doc = reader.read(file); - Element root = doc.getRootElement(); - for (Iterator iter = root.elementIterator("action"); iter.hasNext();) { - Element element = (Element)iter.next(); - Attribute nameAttr = element.attribute("name"); - if (nameAttr.getValue().equals(actionName)) { - for (Iterator ite = element.elementIterator("result"); ite.hasNext();) { - Element ele = (Element)ite.next(); - Attribute resultAttr = ele.attribute("name"); - if (resultAttr.getValue().equals(resultName)) { - return ele.getText(); - } - } - } - } - } catch (Exception e) { - e.printStackTrace(); - } - - return null; - } - -} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/struts.xml b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/struts.xml deleted file mode 100644 index ae0ce37fd8..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/struts.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/.LinkedList.java.swp b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/.LinkedList.java.swp deleted file mode 100644 index 7ea2e295d458709c233e753f5a1bc442a43cead0..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16384 zcmeI3X>1(j8OH}o8@67wmxVn7)tU2c7(n$t?R~W{|?lboveCT?kqrot;4H+lpO2UW# zebjtU>WpQNSWB$3M7qY74<}VuQ68UMR+mXPHpJs)@%ZE^6_X}Tthljky5(B;x|zm= z+nBM-vYA9#z2&5W;`M&%vT7^qB+6zxsl|59Y$xlMO8p<+<0v&!;8UhRqgz`tWwbG| zB3>>d|Js-v%&We1-=|E3Xt`|4;#|#xSm; zUw@SV0MGw}{eS+8hT*}humw7y3F=`!+yvv{n=lqGgTIY7j0^B*_&NLt9)Vi84X%U> zmm9`A@HX^99$tr|@GLBaYIqRl!%UbC)8Ol1z-wPHjMH!mcEeg&1FIncw}1iff8H>D z4JTkTtOpk!g)}UH*)R)c!dSQtu7zvh!ewXxZ^1cu9ge~wcn;d35i;;7d(fi@JDg0`EgDxzTC6sm zF^hN!#&pc7W_cxto6SVJG3Az3@MFS+apvPvg=r_Vw)*;?)HHR3s+?N0NHt)7!>llG zpi$j~^g*AWYG$3sY{J-)SXli^e_9@XYaL@?m=WSLFBXO7d+YFcJf)Zy1*CB9%zDb7 zKec06d9&CYv9KwX>xOmR-A%dfAA4<^`!}8Gd*+}tvbN1@Ti3txpx3g-7-|bARbwxk zFhS|>+Vu^kHM|Ez$J5Cgc_V0nN?%w=CPn2+*~?rxxpa|}w2KO-M8i~LGO1;h(-c@_ zTQ!nbRmJb}O4QP(3%4G$D1w_MrfxWBPkQy=bwc@*43etiQpH@}g;IkEb!}etBX+`F zurL@lNhy1&>7SM!i3CG@V2Pc{+SooeV0&#otvrS`Co0>E;m!W0HNB^gV0LfAHc2w$ zy zq}UwP=a@8DI3s2aYqRH8AHu~Y6G zUj3_e5@?Z54b(p-f4Iv#`byvP&*YCkli#&AclJoW^A)e7HGg>Dn1ObJ>Sc0^8q$nE z6@1aflaznNVz& zsA92!R~=`$cE)nk8God;PRdH+GF&SB5iuEjZ=njVVq(~kFKQngUZ3gIHze(P%#<~Q zHq>O6&iLZVYBap0x^DWWD3o*x-Bg}FP=~&16-gIM;$q&xM%2l(Ex8v_1tk3j8Xph#EzTGeZX;ixx7d$?*vfKrfyCWGZ+|sP|$Y zJ!}2D7=HkAP}e}opl?$Or-IuQbL?2T$(*Vsl(kzXe{lcFkGUd-U!i{+6I@wT(tsxf zJ@h9oWyL(YO9jUmxF#=^;sw`YcB|_2U59)vN*9}hi(v!&S@)=rGSpBXi>Imi^ns2R zOzQz3<7Ou`S@m2Uv?Rztd^atW9#%qmx4K!3#YB3BDvO{JM{Y5T^^K-nMf|!3)i{vgQS{|C?kb#NEl0+lcUMuF`2`{9@HDjbKy zAp8IQun+cv1x3)$9{yJ#`~8j32CeV_+z+$hPM86*_rD!(gPWle-ePZm4i3P6*auzE z37eoD+F(7b0}CF8h42tO0QbXuxDW1udGH>4{`2r8EQe*V6z+j}AhCkG;ZBfv!3>xV zw?Gw0tRM&Vkc5Ts5X^zuFca>8>)^|94O|UZ!WA$YE{98D6s%#1TMZY(Mes*#^d7te zZ^H?A3HHN2coxKtPs1MA4ZC0`?11gC4WhqDfk=T!fk=T!f&W_tmivElv&KzkaC^;d z7WXs3{Z(iKD%)O(ekD|#BAg&`OLdzr;ZPOYy0yW|SoNkP=o7u|$NKg^m4ALkZ|Bqb zmX7?EV><3N?fj`$Z}kap^~&BC_YxxWPH!S~snfiTtx`d*`>;+XteNZS>fgDBGP&+f z0-e)HlVGPREy0X4yNRy$H*NHu-NR3>XG6Yyqt~)k_rZIrx$nrCAuV_tR^&GylXmj0 z&EAn6ROh$8yIDfEx?>+u@A*?LL{4(uP4p$)6*}Q{tjcw__3rE@!}(Kd>46T2^F_(c z(Z2@`hnK%?rRrpl>6-OUs#p{^>#b!YYRh>&EMIF1=-+)uH+yTg)2aUFw&gdsFreNe zTk|h$7Nu3RSa&wRwn-$Sr(FkfJ?#`nQ}5UY`ED&MsLBxg*6-6ryu;hPT?hNlZqM(1 zIg~*LCo%AskM}<9hcm-sh>9Z_Mf&r~2}up&*16NWq=^*=drxl4?^*TxrWMruQDmFm z*g&A3*x!6B*=T16V)MFVBaK>HMn=)KBX@ecwljmxoj&WG*_z+fCY@37`re+i`KAMd zL{m;7Wl;||8Eof*6k$*M#01GUR=XK1;T8?-jMOw#KBNpa(iGJe%$;l>(r3D@eFnP+ zbFsOH$y_kM`3V|-u?+*BT4Xqw8WM2y?np2kb6i1$w_sk%v>*3SaMB1#@IQB6q||AtL= z`Jgr)63GX3@<^aOu8R*dfVW|b*WA;;;)l7OoobO{)&}d8_F3)XPs!R9Zr5KtaZj&x zwb$_+v(sy9F06=*G{5;pR=!|@NjOD?RMbZ-X{YMkMI|a!Cf5deY**y?p#~jVF^hC2 zvnqIm7Jf?Sml^R<-(p1JQQu-7_30}3&XYcRpkiI-)WSnK)fsJe37qI;qFxf2@LL}e z0}HRFIy|A5QJuT6l8Vg(A5cS8GWutnz!<%U4mt8*P0e@jRFijLJ*$@gOwMn=kZwv+ R{)L12RT~^;Qn}7={2RR2tVI9- diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/ArrayList.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/ArrayList.java deleted file mode 100644 index f6cd4c38fc..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/ArrayList.java +++ /dev/null @@ -1,97 +0,0 @@ -package com.coding.basic; - -import com.coding.basic.exception.*; -public class ArrayList implements List { - - private int size; - private Object[] elementData; - - public ArrayList () { - size = 0; - elementData = new Object[100]; - } - - public void add(Object o){ - add(size(), o); - } - - public void add(int index, Object o){ - if (size() == elementData.length) - ensureCapacity( size() * 2 + 1); - if (index > size() || index < 0) { //index == size时相当于在尾后插入 - throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); - } - for (int i = size; i > index; i--) { - elementData[i] = elementData[i-1]; - } - elementData[index] = o; - size++; - - } - - private void ensureCapacity(int newCapacity) { - if (newCapacity < size()) - return; - Object[] old = elementData; - elementData = new Object[newCapacity]; - for (int i = 0; i < size(); i++) { - elementData[i] = old[i]; - } - } - - public Object get(int index){ - if (index >= size() || index < 0) { //获取时,index==size()越界 - throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); - } - return elementData[index]; - } - - private String outOfBoundsMsg(int index) { - return "Index:" + index + ", Size:" + size; - } - - public Object remove(int index){ - if (index >= size() || index < 0) { - throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); - } - Object old = elementData[index]; - for (int i = index; i < size(); i++) { - elementData[i] = elementData[i+1]; - } - size--; - return old; - } - - /*获取表内容量*/ - public int size(){ - return size; - } - - public Iterator iterator(){ - return new ArrayListIterator(); - } - - public class ArrayListIterator implements Iterator { - private final int ONLY_CAPACITY = size; - private int index; - public ArrayListIterator() { - index = 0; - } - - @Override - public boolean hasNext() { - if (ONLY_CAPACITY != size) - throw new ConcurrentModificationException("此对象没有进行修改同步"); - return index != size; - } - - @Override - public Object next() { - if (ONLY_CAPACITY != size) - throw new ConcurrentModificationException("此对象没有进行修改同步"); - if (index >= ONLY_CAPACITY) - throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); - return elementData[index++]; - } - } -} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java deleted file mode 100644 index 1cf38aee30..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.coding.basic; - -public class BinaryTreeNode { - - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { - return data; - } - - public void setData(Object data) { - this.data = data; - } - - public BinaryTreeNode getLeft() { - return left; - } - - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - - public BinaryTreeNode getRight() { - return right; - } - - public void setRight(BinaryTreeNode right) { - this.right = right; - } - -} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Iterator.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Iterator.java deleted file mode 100644 index ff93e30377..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Iterator.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); -} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/LinkedList.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/LinkedList.java deleted file mode 100644 index 5717f2226d..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/LinkedList.java +++ /dev/null @@ -1,289 +0,0 @@ -package com.coding.basic; - -import com.coding.basic.exception.*; -public class LinkedList implements List { - - private Node head; - private int size; - public LinkedList() { - head = new Node(null, null); - size = 0; - } - - public void add(Object o){ - add(size, o); - } - - public void add(int index , Object o){ - if (index > size || index < 0) { - throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); - } - Node frontNode = getNode(index-1); - Node newNode = new Node(o, frontNode.next); - frontNode.next = newNode; - size++; - - } - - private Node getNode(int index) { - Node node = head; - int i = 0; - while(node.next != null && i <= index) { - node = node.next; - i++; - } - return node; - } - - public Object get(int index){ - if (index >= size || index < 0) { - throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); - } - - Node node = getNode(index); - return node.data; - } - - public Object remove(int index){ - if (index >= size || index < 0) { - throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); - } - Node frontNode = getNode(index-1); - Node oldNode = getNode(index); - frontNode.next = oldNode.next; - size--; - return oldNode.data; - } - - public int size(){ - return size; - } - - public void addFirst(Object o){ - add(0, o); - } - - public void addLast(Object o){ - add(size, o); - } - - public Object removeFirst(){ - return remove(0); - } - - public Object removeLast(){ - return remove(size-1); - } - - public Iterator iterator(){ - return new LinkedListIterator(); - } - - private class LinkedListIterator implements Iterator { - int index; - final int capacity = size; - LinkedListIterator() { - index = 0; - } - @Override - public boolean hasNext() { - if (capacity != size) - throw new ConcurrentModificationException("此对象没有修改同步"); - return index < capacity; - } - - @Override - public Object next() { - if (capacity != size) - throw new ConcurrentModificationException("此对象没有修改同步"); - if (index >= capacity) - throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); - return get(index++); - } - } - - private String outOfBoundsMsg(int index) { - return "index:" + index + ", size:" + size; - } - - private static class Node { - Object data; - Node next; - - Node(Object data, Node next) { - this.data = data; - this.next = next; - } - - void setData(Object data) { - this.data = data; - } - - Object getData() { - return data; - } - - void setNext(Node next) { - this.next = next; - } - - Object getNext() { - return next; - } - } - - - /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse(){ - Object[] oldData = new Object[size]; - Node temp = head; - int index = 1; - while(temp.next != null) { - temp = temp.next; - oldData[size - index] = temp.data; - index++; - } - - index = 0; - temp = head; - while(temp.next != null) { - temp = temp.next; - temp.data = oldData[index]; - index++; - } - } - - /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - */ - - public void removeFirstHalf(){ - int count = size; - if (count % 2 != 0) { - for (int i = 0; i <= count/2; i++) { - removeFirst(); - } - } else { - for (int i = 0; i < count/2; i++) { - removeFirst(); - } - } - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * @param i - * @param length - */ - - public void remove(int i, int length){ - if (i < 0 || length < 0) { - return; - } - if (i == 0) { - for (int k = 0; k < length; k++) - removeFirst(); - } else { - while (length > 0) { - remove(i-1); - length--; - } - } - } - - /** - * 假定当前链表和list均包含已升序排列的整数 - * 从当前链表中取出那些list所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * @param list - */ - - public static int[] getElements(LinkedList list){ - - return null; - - } - - - - /** - - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - - * 从当前链表中中删除在list中出现的元素 - - - - * @param list - - */ - - - - public void subtract(LinkedList list){ - - - - } - - - - /** - - * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 - - * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) - - */ - - public void removeDuplicateValues(){ - - - - } - - - - /** - - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - - * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) - - * @param min - - * @param max - - */ - - public void removeRange(int min, int max){ - - - - } - - - - /** - - * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) - - * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 - - * @param list - - */ - - public LinkedList intersection( LinkedList list){ - - return null; - - } -} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/List.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/List.java deleted file mode 100644 index 396b1f6416..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/List.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.coding.basic; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Queue.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Queue.java deleted file mode 100644 index a5c31f5a09..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Queue.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.coding.basic; -import com.coding.basic.exception.*; -public class Queue { - - private LinkedList elementData; - - public Queue() { - elementData = new LinkedList(); - } - - public void enQueue(Object o){ - elementData.addLast(o); - } - - public Object deQueue(){ - if (isEmpty()) { - throw new EmptyQueueException("队空"); - } - Object result = elementData.removeFirst(); - return result; - } - - public boolean isEmpty(){ - return elementData.size() == 0; - } - - public int size(){ - return elementData.size(); - } -} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Stack.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Stack.java deleted file mode 100644 index e41c662792..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Stack.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.coding.basic; - -import com.coding.basic.exception.*; -public class Stack { - - private ArrayList elementData; - public Stack() { - elementData = new ArrayList(); - } - - public void push(Object o){ - elementData.add(o); - } - - public Object pop(){ - if (isEmpty()) { - throw new EmptyStackException("栈空"); - } - Object result = elementData.get(size()-1); - elementData.remove(size()-1); - return result; - } - - public Object peek(){ - if (isEmpty()) { - throw new EmptyStackException("栈空"); - } - return elementData.get(0); - } - - public boolean isEmpty(){ - return elementData.size() == 0; - } - - public int size(){ - return elementData.size(); - } -} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/ConcurrentModificationException.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/ConcurrentModificationException.java deleted file mode 100644 index f1c5c79721..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/ConcurrentModificationException.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.coding.basic.exception; - -public class ConcurrentModificationException extends RuntimeException { - public ConcurrentModificationException() {} - public ConcurrentModificationException(String msg) { - super(msg); - } -} - diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyQueueException.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyQueueException.java deleted file mode 100644 index 2ee7aa4ee7..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyQueueException.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.coding.basic.exception; - -public class EmptyQueueException extends RuntimeException { - public EmptyQueueException() {} - public EmptyQueueException(String msg) { - super(msg); - } -} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyStackException.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyStackException.java deleted file mode 100644 index 2a5ae4055d..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyStackException.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.coding.basic.exception; - -public class EmptyStackException extends RuntimeException { - public EmptyStackException() {} - public EmptyStackException(String msg) { - super(msg); - } -} diff --git a/group11/1178243325/DataStructure/src/main/resources/struts.xml b/group11/1178243325/DataStructure/src/main/resources/struts.xml deleted file mode 100644 index ae0ce37fd8..0000000000 --- a/group11/1178243325/DataStructure/src/main/resources/struts.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - diff --git a/group11/1178243325/DataStructure/struts.xml b/group11/1178243325/DataStructure/struts.xml deleted file mode 100644 index 0582b7d4ea..0000000000 --- a/group11/1178243325/DataStructure/struts.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - diff --git a/group11/1178243325/week01/.readme.md.swp b/group11/1178243325/week01/.readme.md.swp deleted file mode 100644 index 44d97db14f1c5235f5e0a65a0bdaee85c90f52de..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI&OHUI~6ae6h3*CqsUAQx&Y$Tzb&J=7hQA1o93?>@IkQhzIb_%7Tt;5vDjgnZ} zYRk(9k6J*qhDJ(hKm^*t4{+(iU(h>uSeP#S1D>IPMgnnXFlUl8uY2y?bHCe7JH2nY z-F%I+nd=E`#|XL7_O0zp=P5FCn2>J2;`XXW!`|E9<~^ky@HTVf&W2^e(xOs^O6hHwK zKmim$0Te(16hHwKKmim$fqf_-!CUpFYC^^#jQ9Ur-~YcKCFCo_ID`Y@BE$uVlMt&% z2vHy&K-`141aS!BAjFTYXYE4^EExq*00mG01yBG5Pyhu`00sV;fQh@K6=(EZRL@Sa z?1+}kvqDIV4$}TfHW;Qa;;cB#(lLE*R!{fbJ=>wG{_{K^3 zYM~#U%xlSv_ByBaWm(~+HW8vr^E4ZWGC57IM|M_j-3Z75xsJP~$gpo#WkphbO1b#V zD4b<7P$eBomsXRd{)yp>d< Date: Wed, 29 Mar 2017 10:30:51 +0800 Subject: [PATCH 12/41] fileDownloader MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 多线程下载修改 --- .../coderising/download/FileDownloader.java | 1 - .../download/impl/ConnectionImpl.java | 31 ++++++++++++++----- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/group11/542194147/myDataStructure/src/com/coderising/download/FileDownloader.java b/group11/542194147/myDataStructure/src/com/coderising/download/FileDownloader.java index 75ade93046..96989a4559 100644 --- a/group11/542194147/myDataStructure/src/com/coderising/download/FileDownloader.java +++ b/group11/542194147/myDataStructure/src/com/coderising/download/FileDownloader.java @@ -44,7 +44,6 @@ public void execute(){ int length = conn.getContentLength(); int downloadBlock=length/3; int appendBlock=length%3; - RandomAccessFile rdaFile=new RandomAccessFile("downloadFile","wr"); for(int i=0;i totalLen){ + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + + return baos.toByteArray(); + } @Override From 7941760ca42aeda17fc7d4b7ba9df7e4529c4acf Mon Sep 17 00:00:00 2001 From: xmt <542194147@qq.com> Date: Sat, 1 Apr 2017 15:36:39 +0800 Subject: [PATCH 13/41] jvm --- .../jvm/loader/ClassFileLoader.java | 75 +++++++++++++++ .../jvm/test/ClassFileloaderTest.java | 94 +++++++++++++++++++ .../com/coderising/jvm/test/EmployeeV1.java | 28 ++++++ .../src/com/coding/basic/LRUPageFrame.java | 74 +++++++++++++++ .../com/coding/basic/LRUPageFrameTest.java | 31 ++++++ 5 files changed, 302 insertions(+) create mode 100644 group11/542194147/myDataStructure/src/com/coderising/jvm/loader/ClassFileLoader.java create mode 100644 group11/542194147/myDataStructure/src/com/coderising/jvm/test/ClassFileloaderTest.java create mode 100644 group11/542194147/myDataStructure/src/com/coderising/jvm/test/EmployeeV1.java create mode 100644 group11/542194147/myDataStructure/src/com/coding/basic/LRUPageFrame.java create mode 100644 group11/542194147/myDataStructure/src/com/coding/basic/LRUPageFrameTest.java diff --git a/group11/542194147/myDataStructure/src/com/coderising/jvm/loader/ClassFileLoader.java b/group11/542194147/myDataStructure/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..7e19ae9c85 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,75 @@ +package com.coderising.jvm.loader; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + private static final int BUFFER_SIZE=1024; + + public byte[] readBinaryCode(String className) { + className=className.replace(".","\\"); + StringBuffer sb=new StringBuffer(); + String absolutePath=sb.append(clzPaths.get(0)).append("\\").append(className).append(".class").toString(); + File file=new File(absolutePath); + FileInputStream fis = null; + ByteArrayOutputStream baos=new ByteArrayOutputStream(); + byte[]buffer=new byte[BUFFER_SIZE]; + try { + fis=new FileInputStream(file); + while(baos.size()file.length()){ + return Arrays.copyOf(baos.toByteArray(), (int) file.length()); + } + } catch (IOException e) { + e.printStackTrace(); + }finally{ + try { + fis.close(); + baos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return baos.toByteArray(); + } + + + public void addClassPath(String path) { + clzPaths.add(path); + } + + + + public String getClassPath(){ + StringBuffer sb=new StringBuffer(); + for(int i=0;i Date: Sat, 1 Apr 2017 22:45:25 +0800 Subject: [PATCH 14/41] FourthHomework --- .../BaseDataStructure/LRUPageFrame.java | 143 ++++++++++++++++++ .../FourthHomework/jvm/ClassFileLoader.java | 51 +++++++ .../src/FourthHomework/jvm/TestJVM.java | 7 + .../BaseDataStructure/TestLRUPageFrame.java | 31 ++++ .../test/FourthHomework/JVM/EmployeeV1.java | 28 ++++ .../JVM/TestClassFileLoader.java | 67 ++++++++ 6 files changed, 327 insertions(+) create mode 100644 group11/1310368322/src/FourthHomework/BaseDataStructure/LRUPageFrame.java create mode 100644 group11/1310368322/src/FourthHomework/jvm/ClassFileLoader.java create mode 100644 group11/1310368322/src/FourthHomework/jvm/TestJVM.java create mode 100644 group11/1310368322/test/FourthHomework/BaseDataStructure/TestLRUPageFrame.java create mode 100644 group11/1310368322/test/FourthHomework/JVM/EmployeeV1.java create mode 100644 group11/1310368322/test/FourthHomework/JVM/TestClassFileLoader.java diff --git a/group11/1310368322/src/FourthHomework/BaseDataStructure/LRUPageFrame.java b/group11/1310368322/src/FourthHomework/BaseDataStructure/LRUPageFrame.java new file mode 100644 index 0000000000..5e860a31d7 --- /dev/null +++ b/group11/1310368322/src/FourthHomework/BaseDataStructure/LRUPageFrame.java @@ -0,0 +1,143 @@ +package DataStructure_4_LRU; + +import org.junit.runners.Parameterized.Parameters; + +/* + * ˫ʵLRU㷨 + */ +public class LRUPageFrame { + private static class Node{ + Node prev; + Node next; + int pageNum = -1;// ҳ + + Node(){ + + } + } + + private int capacity; + + private Node first;// ͷ + private Node last;// β + boolean tag = false; + + public LRUPageFrame(int capacity){ + this.capacity = capacity; + + for(int i = 0; i < capacity; i++){ + Node curNode = new Node(); + if(null == first){ + last = first = curNode; + }else{ + last.next = curNode; + curNode.prev = last; + last = last.next; + } + last.next = null; + } + } + public void printList(){ + Node curNode = first; + while(curNode != null){ + curNode = curNode.next; + } + } + /* + * ȡж + * @param key + * @return + */ + public void access(int pageNum){ + printList(); + Node index = findLogicPage(pageNum); + modifyPhysicalPage(index,pageNum); + } + + /* + * @param pageNum ʾҪѯ߼ҳ + * @return ҳҵҪѯ߼ҳ棬򷵻ظҳڵã򷵻null + */ + public Node findLogicPage(int pageNum){ + + Node index = null; + Node curNode = first; + while(curNode != null){ + if(curNode.pageNum == pageNum){ + index = curNode; + tag = true; + } + curNode = curNode.next; + } + return index; + } + /* + * @prama index ߼ҳҳĽڵ + */ + public void modifyPhysicalPage(Node index,int pageNum){ + push(pageNum,index); + } + /* + * @param pageNum Ҫ push߼ҳ棬 Ĭջ first, bottom ջ ָջĴС + */ + public void push(int pageNum,Node bottom){ + Node index = checkWhichListNodeNotUsed(); + if(index != null){ + index.pageNum = pageNum; + return; + } + + Node lastNode; + if(null == bottom){ + lastNode = last; + }else{ + lastNode = bottom; + } + Node curNode = lastNode.prev; + while(curNode != null){ + lastNode.pageNum = curNode.pageNum; + lastNode = curNode; + curNode = curNode.prev; + } + lastNode.pageNum = pageNum; + return; + } + + /* + * @return ҳ pageNum ûбʹõĽڵ(ջ)ȫʹã򷵻 null + */ + public Node checkWhichListNodeNotUsed(){ + Node node = first; + Node index = null; + while(node != null){ + if(node.pageNum == -1){ + index = node; + } + node = node.next; + } + return index; + } + + public String toString(){ + StringBuffer buffer = new StringBuffer(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + + + + + + + + + +} diff --git a/group11/1310368322/src/FourthHomework/jvm/ClassFileLoader.java b/group11/1310368322/src/FourthHomework/jvm/ClassFileLoader.java new file mode 100644 index 0000000000..6db696e5aa --- /dev/null +++ b/group11/1310368322/src/FourthHomework/jvm/ClassFileLoader.java @@ -0,0 +1,51 @@ +package com.coderising.jvm.loader; + +import java.io.BufferedInputStream; +import java.io.DataInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.junit.runners.Parameterized.Parameters; + +public class ClassFileLoader { + private List clzPaths = new ArrayList(); + int countForClassPath = 0; + int countForReadBinaryCode = 0; + byte [] a = new byte[10000]; + + /* ָ·ȡļ䱣浽һֽУ + * @Parameters ָ· + * @ֽ + */ + public byte[] readBinaryCode(String className) throws IOException{ + DataInputStream dis = new DataInputStream( + new BufferedInputStream(new FileInputStream(className))); + for(int i = 0; dis.available() != 0; i++){ + a[i] = dis.readByte(); + countForReadBinaryCode++; + } + byte []target = new byte[countForReadBinaryCode]; + System.arraycopy(a, 0, target, 0, countForReadBinaryCode); + dis.close(); + return target; + } + + public void addClassPath(String path){ + clzPaths.add(path); + countForClassPath++; + } + + public String getClassPath(){ + StringBuffer buffer = new StringBuffer(); + for(int i = 0; i < countForClassPath; i++ ){ + if(i==countForClassPath-1){ + buffer.append(clzPaths.get(i)); + }else{ + buffer.append(clzPaths.get(i)+";"); + } + } + return buffer.toString(); + } +} diff --git a/group11/1310368322/src/FourthHomework/jvm/TestJVM.java b/group11/1310368322/src/FourthHomework/jvm/TestJVM.java new file mode 100644 index 0000000000..735e4d1dc2 --- /dev/null +++ b/group11/1310368322/src/FourthHomework/jvm/TestJVM.java @@ -0,0 +1,7 @@ +package com.coderising.jvm.loader; + +public class TestJVM { + public static void main(String[] args) { + System.out.println("Hello"); + } +} diff --git a/group11/1310368322/test/FourthHomework/BaseDataStructure/TestLRUPageFrame.java b/group11/1310368322/test/FourthHomework/BaseDataStructure/TestLRUPageFrame.java new file mode 100644 index 0000000000..227599c187 --- /dev/null +++ b/group11/1310368322/test/FourthHomework/BaseDataStructure/TestLRUPageFrame.java @@ -0,0 +1,31 @@ +package DataStructure_4_LRU; + +import static org.junit.Assert.*; +import org.junit.*; + +import org.junit.Test; + +public class TestLRUPageFrame { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3);// ҳ洢Ϊ3ҳ + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7",frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0",frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1",frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2",frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2",frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3",frame.toString()); + } + +} diff --git a/group11/1310368322/test/FourthHomework/JVM/EmployeeV1.java b/group11/1310368322/test/FourthHomework/JVM/EmployeeV1.java new file mode 100644 index 0000000000..acbc34c9bb --- /dev/null +++ b/group11/1310368322/test/FourthHomework/JVM/EmployeeV1.java @@ -0,0 +1,28 @@ +package com.coderising.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(); + } +} diff --git a/group11/1310368322/test/FourthHomework/JVM/TestClassFileLoader.java b/group11/1310368322/test/FourthHomework/JVM/TestClassFileLoader.java new file mode 100644 index 0000000000..263384e71a --- /dev/null +++ b/group11/1310368322/test/FourthHomework/JVM/TestClassFileLoader.java @@ -0,0 +1,67 @@ +package com.coderising.jvm.loader; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import org.junit.*; + +public class TestClassFileLoader { + static String path1 = "D:/ProgramWorld"; + static String path2 = "D:/ProgramWorld/Java"; + @Test + public void test() { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + String clzPath = loader.getClassPath(); + Assert.assertEquals(path1 + ";" + path2, clzPath); + } + @Test + public void testClassFileLength() throws IOException{ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "D:/ProgramWorld/Java/Practice/LangSi/2017Ⱥ/bin/com/coderising/jvm/loader/EmployeeV1.class"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // ע⣺ ֽܺJVM汾йϵԿõൽж + Assert.assertEquals(1058,byteCodes.length); + } + @Test + public void testMagicNumber() throws IOException{ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "D:/ProgramWorld/Java/Practice/LangSi/2017Ⱥ/bin/com/coderising/jvm/loader/EmployeeV1.class"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{ + byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3] + }; + String actualValue = this.byteToHexString(codes); + Assert.assertEquals("cafebabe",actualValue); + + } + + private String byteToHexString(byte[] codes){ + StringBuffer buffer = new StringBuffer(); + for(int i = 0; i < codes.length; i++){ + byte b = codes[i]; + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if(strHex.length() < 2){ + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } + + + + + + + + +} From 437e2cada5b54728063cfd79dfc569810af514f3 Mon Sep 17 00:00:00 2001 From: "wangxg922@chinaunincom.cn" <996108220@qq.com> Date: Wed, 5 Apr 2017 16:21:39 +0800 Subject: [PATCH 15/41] =?UTF-8?q?Signed-off-by:=20JVM=E4=BD=9C=E4=B8=9A?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=20<996108220@qq.com>?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jvm/loader/ClassFileLoader.java | 70 +++++++++ .../jvm/test/ClassFileloaderTest.java | 93 ++++++++++++ .../com/coderising/jvm/test/EmployeeV1.java | 28 ++++ .../coding/basic/linklist/LRUPageFrame.java | 139 ++++++++++++++++++ .../basic/linklist/LRUPageFrameTest.java | 31 ++++ 5 files changed, 361 insertions(+) create mode 100644 group11/996108220/src/com/coderising/jvm/loader/ClassFileLoader.java create mode 100644 group11/996108220/src/com/coderising/jvm/test/ClassFileloaderTest.java create mode 100644 group11/996108220/src/com/coderising/jvm/test/EmployeeV1.java create mode 100644 group11/996108220/src/com/coding/basic/linklist/LRUPageFrame.java create mode 100644 group11/996108220/src/com/coding/basic/linklist/LRUPageFrameTest.java diff --git a/group11/996108220/src/com/coderising/jvm/loader/ClassFileLoader.java b/group11/996108220/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..7bde3f4288 --- /dev/null +++ b/group11/996108220/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,70 @@ +package com.coderising.jvm.loader; + + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + StringBuffer fileName=new StringBuffer(clzPaths.get(0)); + String nameString[]=className.split("\\."); + fileName.append("\\"); + fileName.append(nameString[nameString.length-1]); + fileName.append(".class"); + ArrayList list=new ArrayList(); + try { + InputStream in=new FileInputStream(fileName.toString()); + int length=-1; + byte[] buffer=new byte[1024]; + while ((length=in.read(buffer))!=-1) { + int size=list.size(); + for (int i = size; i < size+length; i++) { + list.add(buffer[i-size]); + } + } + + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + byte[] byteCodes=new byte[list.size()]; + for (int i = 0; i < byteCodes.length; i++) { + byteCodes[i]=list.get(i); + } + + return byteCodes; + } + + + public void addClassPath(String path) { + clzPaths.add(path); + + } + + + + public String getClassPath(){ + String string=""; + for (int i = 0; i < clzPaths.size(); i++) { + string=i==0?string+clzPaths.get(i):string+";"+clzPaths.get(i); + } + return string; + } + + + + + +} diff --git a/group11/996108220/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group11/996108220/src/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..8fdc5db819 --- /dev/null +++ b/group11/996108220/src/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,93 @@ +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 = "F:\\mycoding2017\\group11\\996108220\\bin\\com\\coderising\\jvm\\test"; + + 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 Date: Sat, 8 Apr 2017 21:00:57 +0800 Subject: [PATCH 16/41] =?UTF-8?q?400=E5=A4=9A=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group11/1178243325/week06/build.gradle | 13 ++++ group11/1178243325/week06/readme.md | 11 +++ .../com/sprint/jvm/clz/.ClassIndex.java.swp | Bin 0 -> 12288 bytes .../java/com/sprint/jvm/clz/AccessFlag.java | 25 ++++++ .../java/com/sprint/jvm/clz/ClassFile.java | 71 ++++++++++++++++++ .../java/com/sprint/jvm/clz/ClassIndex.java | 22 ++++++ .../com/sprint/jvm/constant/ClassInfo.java | 28 +++++++ .../com/sprint/jvm/constant/ConstantInfo.java | 29 +++++++ .../com/sprint/jvm/constant/ConstantPool.java | 28 +++++++ .../com/sprint/jvm/constant/UTF8Info.java | 35 +++++++++ .../sprint/jvm/loader/ByteCodeIterator.java | 5 ++ .../sprint/jvm/loader/ClassFileLoader.java | 52 +++++++++++++ .../sprint/jvm/loader/ClassFileParser.java | 25 ++++++ .../jvm/loader/ClassFileLoaderTest.java | 20 +++++ .../com/sprint/jvm/loader/EmployeeV1.java | 28 +++++++ 15 files changed, 392 insertions(+) create mode 100644 group11/1178243325/week06/build.gradle create mode 100644 group11/1178243325/week06/readme.md create mode 100644 group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/.ClassIndex.java.swp create mode 100644 group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/AccessFlag.java create mode 100644 group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/ClassFile.java create mode 100644 group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/ClassIndex.java create mode 100644 group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/ClassInfo.java create mode 100644 group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/ConstantInfo.java create mode 100644 group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/ConstantPool.java create mode 100644 group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/UTF8Info.java create mode 100644 group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ByteCodeIterator.java create mode 100644 group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ClassFileLoader.java create mode 100644 group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ClassFileParser.java create mode 100644 group11/1178243325/week06/src/test/java/com/sprint/jvm/loader/ClassFileLoaderTest.java create mode 100644 group11/1178243325/week06/src/test/java/com/sprint/jvm/loader/EmployeeV1.java diff --git a/group11/1178243325/week06/build.gradle b/group11/1178243325/week06/build.gradle new file mode 100644 index 0000000000..128f6fda07 --- /dev/null +++ b/group11/1178243325/week06/build.gradle @@ -0,0 +1,13 @@ +apply plugin: 'java' + +repositories { + mavenCentral() +} + +dependencies { + compile("commons-io:commons-io:2.4") + //compile("commons-lang:commons-lang:2.6") + compile("org.apache.commons:commons-lang3:3.4") + testCompile("junit:junit:4.12") +} + diff --git a/group11/1178243325/week06/readme.md b/group11/1178243325/week06/readme.md new file mode 100644 index 0000000000..d32158bcdf --- /dev/null +++ b/group11/1178243325/week06/readme.md @@ -0,0 +1,11 @@ +## 讲课内容: +- 17-03-29 :JVM之classLoader + +## 第六周作业(3-27 至 04-02) +读取常量池 + +## 完成情况: +未补 + +## 我的收获: + diff --git a/group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/.ClassIndex.java.swp b/group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/.ClassIndex.java.swp new file mode 100644 index 0000000000000000000000000000000000000000..0d37255a755129f38ce5d0a21bf3eb0c2f18fc4d GIT binary patch literal 12288 zcmeI2y-wpm6h?2LN&$;NqM$$nG)v=&6Cj9)ii!eO;!kud#~B6V*fF+a5NP2EK=2+s z!>&ZfJJ3PFBTxZ%)`>y_ifAFD%BSg=akGsF!Nm`m9 zih|JbV|}Cd`s}&W^q}0g5=+|kiqaRyujX@RQC7oxBQQ;An#INZT%nMkmxoHZxdj=8 zw)8B=Z$0P9R!-tcyLK#BYE~4j`W1C#=;+?X;w}vIt$`sb 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/group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/UTF8Info.java b/group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/UTF8Info.java new file mode 100644 index 0000000000..5516999c0e --- /dev/null +++ b/group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/UTF8Info.java @@ -0,0 +1,35 @@ +package com.sprint.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; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + @Override + public String toString() { + return "UTF8Info [type=" + type + ", length=" + length + ", value=" + value + "]"; + } +} diff --git a/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ByteCodeIterator.java b/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ByteCodeIterator.java new file mode 100644 index 0000000000..3144d5828d --- /dev/null +++ b/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ByteCodeIterator.java @@ -0,0 +1,5 @@ +package com.sprint.jvm.loader; + +public class ByteCodeIterator { + +} diff --git a/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ClassFileLoader.java b/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..f190f54915 --- /dev/null +++ b/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ClassFileLoader.java @@ -0,0 +1,52 @@ +package com.sprint.jvm.loader; + +import com.sprint.jvm.clz.ClassFile; +import java.io.IOException; +import java.io.File; +import java.io.FileInputStream; +import java.util.List; +import java.util.ArrayList; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; + +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); + } +} diff --git a/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ClassFileParser.java b/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ClassFileParser.java new file mode 100644 index 0000000000..4c32858fa4 --- /dev/null +++ b/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ClassFileParser.java @@ -0,0 +1,25 @@ +package com.sprint.jvm.loader; + +import com.sprint.jvm.clz.ClassFile; +import com.sprint.jvm.clz.AccessFlag; +import com.sprint.jvm.clz.ClassIndex; +import com.sprint.jvm.constant.ConstantPool; +public class ClassFileParser { + public ClassFile parse(byte[] codes) { + return null; + } + + private AccessFlag parseAccessFlag(ByteCodeIterator iter) { + return null; + } + + private ClassIndex parseClassIndex(ByteCodeIterator iter) { + return null; + } + + private ConstantPool parseConstantPool(ByteCodeIterator iter) { + return null; + } + + +} diff --git a/group11/1178243325/week06/src/test/java/com/sprint/jvm/loader/ClassFileLoaderTest.java b/group11/1178243325/week06/src/test/java/com/sprint/jvm/loader/ClassFileLoaderTest.java new file mode 100644 index 0000000000..6795595e89 --- /dev/null +++ b/group11/1178243325/week06/src/test/java/com/sprint/jvm/loader/ClassFileLoaderTest.java @@ -0,0 +1,20 @@ +package com.sprint.jvm.loader; + +import com.sprint.jvm.clz.ClassFile; +import org.junit.Test; +import org.junit.Assert; +public class ClassFileLoaderTest { + + private static final String FULL_QUALTFIED_CLASS_NAME = "com/sprint/jvm/EmployeeV1"; + + static String path1 = ""; + static String path2 = ""; + static ClassFile clzFile = null; + static { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.sprint.jvm.EmployeeV1"; + clzFile = loader.loadClass(className); + clzFile.print(); + } +} diff --git a/group11/1178243325/week06/src/test/java/com/sprint/jvm/loader/EmployeeV1.java b/group11/1178243325/week06/src/test/java/com/sprint/jvm/loader/EmployeeV1.java new file mode 100644 index 0000000000..6b8532842b --- /dev/null +++ b/group11/1178243325/week06/src/test/java/com/sprint/jvm/loader/EmployeeV1.java @@ -0,0 +1,28 @@ +package com.sprint.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", 20); + p.sayHello(); + } +} From 02c7c6b4809dfc725ac8e6e37fbd21724de22fa2 Mon Sep 17 00:00:00 2001 From: x_zhaohu Date: Sat, 8 Apr 2017 21:01:44 +0800 Subject: [PATCH 17/41] delete swp --- .../com/sprint/jvm/clz/.ClassIndex.java.swp | Bin 12288 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/.ClassIndex.java.swp diff --git a/group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/.ClassIndex.java.swp b/group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/.ClassIndex.java.swp deleted file mode 100644 index 0d37255a755129f38ce5d0a21bf3eb0c2f18fc4d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2y-wpm6h?2LN&$;NqM$$nG)v=&6Cj9)ii!eO;!kud#~B6V*fF+a5NP2EK=2+s z!>&ZfJJ3PFBTxZ%)`>y_ifAFD%BSg=akGsF!Nm`m9 zih|JbV|}Cd`s}&W^q}0g5=+|kiqaRyujX@RQC7oxBQQ;An#INZT%nMkmxoHZxdj=8 zw)8B=Z$0P9R!-tcyLK#BYE~4j`W1C#=;+?X;w}vIt$`sb Date: Sun, 9 Apr 2017 19:41:22 +0800 Subject: [PATCH 18/41] FifthHomework --- .../DataStructure_5_Stack/StackUtil.java | 106 +++++++++++ .../src/FifthHomework/jvm/clz/AccessFlag.java | 26 +++ .../src/FifthHomework/jvm/clz/ClassFile.java | 75 ++++++++ .../src/FifthHomework/jvm/clz/ClassIndex.java | 23 +++ .../FifthHomework/jvm/constant/ClassInfo.java | 33 ++++ .../jvm/constant/ConstantInfo.java | 34 ++++ .../jvm/constant/ConstantPool.java | 30 +++ .../jvm/constant/FieldRefInfo.java | 62 +++++++ .../jvm/constant/MethodRefInfo.java | 52 ++++++ .../jvm/constant/NameAndTypeInfo.java | 52 ++++++ .../jvm/constant/NullConstantInfo.java | 13 ++ .../jvm/constant/StringInfo.java | 30 +++ .../FifthHomework/jvm/constant/UTF8Info.java | 40 ++++ .../jvm/loader/ByteCodeIterator.java | 45 +++++ .../jvm/loader/ClassFileLoader.java | 69 +++++++ .../jvm/loader/ClassFileParser.java | 130 +++++++++++++ .../src/FifthHomework/jvm/loader/TestJVM.java | 7 + .../DataStructure_5_Stack/TestStackUtil.java | 77 ++++++++ .../DataStructure_5_Stack/TestSwitch.java | 15 ++ .../FifthHomework/jvm/loader/EmployeeV1.java | 28 +++ .../FifthHomework/jvm/loader/TestArray.java | 8 + .../jvm/loader/TestArrayList.java | 14 ++ .../jvm/loader/TestByteArrayToHexString.java | 15 ++ .../jvm/loader/TestByteArrayToInt.java | 12 ++ .../jvm/loader/TestByteToInt.java | 12 ++ .../jvm/loader/TestClassFileLoader.java | 174 ++++++++++++++++++ .../jvm/loader/TestIntegerToHexString.java | 10 + 27 files changed, 1192 insertions(+) create mode 100644 group11/1310368322/src/FifthHomework/DataStructure_5_Stack/StackUtil.java create mode 100644 group11/1310368322/src/FifthHomework/jvm/clz/AccessFlag.java create mode 100644 group11/1310368322/src/FifthHomework/jvm/clz/ClassFile.java create mode 100644 group11/1310368322/src/FifthHomework/jvm/clz/ClassIndex.java create mode 100644 group11/1310368322/src/FifthHomework/jvm/constant/ClassInfo.java create mode 100644 group11/1310368322/src/FifthHomework/jvm/constant/ConstantInfo.java create mode 100644 group11/1310368322/src/FifthHomework/jvm/constant/ConstantPool.java create mode 100644 group11/1310368322/src/FifthHomework/jvm/constant/FieldRefInfo.java create mode 100644 group11/1310368322/src/FifthHomework/jvm/constant/MethodRefInfo.java create mode 100644 group11/1310368322/src/FifthHomework/jvm/constant/NameAndTypeInfo.java create mode 100644 group11/1310368322/src/FifthHomework/jvm/constant/NullConstantInfo.java create mode 100644 group11/1310368322/src/FifthHomework/jvm/constant/StringInfo.java create mode 100644 group11/1310368322/src/FifthHomework/jvm/constant/UTF8Info.java create mode 100644 group11/1310368322/src/FifthHomework/jvm/loader/ByteCodeIterator.java create mode 100644 group11/1310368322/src/FifthHomework/jvm/loader/ClassFileLoader.java create mode 100644 group11/1310368322/src/FifthHomework/jvm/loader/ClassFileParser.java create mode 100644 group11/1310368322/src/FifthHomework/jvm/loader/TestJVM.java create mode 100644 group11/1310368322/test/FifthHomework/DataStructure_5_Stack/TestStackUtil.java create mode 100644 group11/1310368322/test/FifthHomework/DataStructure_5_Stack/TestSwitch.java create mode 100644 group11/1310368322/test/FifthHomework/jvm/loader/EmployeeV1.java create mode 100644 group11/1310368322/test/FifthHomework/jvm/loader/TestArray.java create mode 100644 group11/1310368322/test/FifthHomework/jvm/loader/TestArrayList.java create mode 100644 group11/1310368322/test/FifthHomework/jvm/loader/TestByteArrayToHexString.java create mode 100644 group11/1310368322/test/FifthHomework/jvm/loader/TestByteArrayToInt.java create mode 100644 group11/1310368322/test/FifthHomework/jvm/loader/TestByteToInt.java create mode 100644 group11/1310368322/test/FifthHomework/jvm/loader/TestClassFileLoader.java create mode 100644 group11/1310368322/test/FifthHomework/jvm/loader/TestIntegerToHexString.java diff --git a/group11/1310368322/src/FifthHomework/DataStructure_5_Stack/StackUtil.java b/group11/1310368322/src/FifthHomework/DataStructure_5_Stack/StackUtil.java new file mode 100644 index 0000000000..782c7d59a6 --- /dev/null +++ b/group11/1310368322/src/FifthHomework/DataStructure_5_Stack/StackUtil.java @@ -0,0 +1,106 @@ +package DataStructure_5_Stack; + +import java.util.Stack; + +public class StackUtil { + /* + * ջеԪIntegerջջǣ 5,4,3,2,1 ø÷󣬴ջջ׻ 1,2,3,4,5 + * ע⣺ ֻʹ StackĻpush,pop,peek,isEmpty + */ + public static void reverse(Stack s){ + if(s.isEmpty()){ return; } + int length = s.size(); + Object []temp = new Object[length]; + for(int i = 0; i < length; i++){ + temp[i] = s.pop(); + } + for(int i = 0; i < length; i++){ + s.push(temp[i]); + } + return; + } + + /* + * ɾջָԪأע⣺ֻʹStackĻpush,pop,peek,isEmpty + * + */ + public static void remove(Stack s, Object o){ + int length = s.size(); + Object elementTemp; + System.out.println(length); + int count = 0; + Object []temp = new Object[length]; + if(s.isEmpty()){ return; } + for(int i = 0; i < length;i++){ + elementTemp = s.pop(); + if(!o.equals(elementTemp)){ + temp[count++] = elementTemp; + System.out.println(temp[i]); + } + } + + for(int i = count-1; i >= 0; i--){ + s.push(temp[i]); + } + return; + } + + /* + * ջȡlenԪأԭջеԪرֲ + * @param len + * @return + */ + public static Object[] getTop(Stack s , int len){ + if(s.isEmpty() || len > s.size() || len < 0){ + return null; + } + Object []result = new Object[len]; + for(int i = 0; i < len; i++){ + result[i] = s.pop(); + } + return result; + } + + /* + * ַ s ܰЩַ () [] {}, a, b, c ... x, y, z + * ʹöջַ s еDzdzɶԳֵ + * 磺 s = ([e{d}f]),ַеdzɶԳֵģ÷ true + * s = "([b{x]})",ַеŲdzɶԳֵģ÷ false + * @param s + * @return + */ + public static boolean isValidPairs(String s){ + Stack stack = new Stack(); + System.out.println(stack.isEmpty()); + char elementTemp; + boolean tag = false; + char [] a = s.toCharArray(); + for(int i = 0; i < a.length; i++){ + if((!tag) && (a[i] == '(' || a[i] == ')' || a[i] == '[' || a[i] == ']' || a[i] == '{' || a[i] == '}')){ + stack.push(a[i]); + tag = true; + }else{ + if(a[i] == '(' || a[i] == ')' || a[i] == '[' || a[i] == ']' || a[i] == '{' || a[i] == '}'){ + elementTemp = (char) stack.pop(); + switch(elementTemp){ + case '(': if(a[i]==')'){}else{ stack.push(elementTemp); stack.push(a[i]); }; break; + case '[': if(a[i]==']'){}else{ stack.push(elementTemp); stack.push(a[i]); }; break; + case '{': if(a[i]=='}'){}else{ stack.push(elementTemp); stack.push(a[i]); }; break; + + } + } + } + + } + if(stack.isEmpty()){ + return true; + } + return false; + } +} + + + + + + diff --git a/group11/1310368322/src/FifthHomework/jvm/clz/AccessFlag.java b/group11/1310368322/src/FifthHomework/jvm/clz/AccessFlag.java new file mode 100644 index 0000000000..4e26442ed5 --- /dev/null +++ b/group11/1310368322/src/FifthHomework/jvm/clz/AccessFlag.java @@ -0,0 +1,26 @@ +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; + } + +} diff --git a/group11/1310368322/src/FifthHomework/jvm/clz/ClassFile.java b/group11/1310368322/src/FifthHomework/jvm/clz/ClassFile.java new file mode 100644 index 0000000000..3bf1a8c56e --- /dev/null +++ b/group11/1310368322/src/FifthHomework/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 AccessFlag getAccessFlag(){ + return accessFlag; + } + + public ClassIndex getClzIndex(){ + return clzIndex; + } + + public void setClassIndex(ClassIndex clzIndex){ + this.clzIndex = clzIndex; + } + + public int getMinorVersion(){ + System.out.println(minorVersion); + return minorVersion; + } + + public void setMinorVersion(int minorVersion){ + this.minorVersion = minorVersion; + } + + public int getMajorVersion(){ + return majorVersion; + } + + public void setMajorVersion(int majorVersion){ + this.majorVersion = majorVersion; + } + + public ConstantPool getConstantPool(){ + return this.pool; + } + public void setConstantPool(ConstantPool pool){ + this.pool = pool; + } + + public void print(){ + if(this.accessFlag.isPublicClass()){ + System.out.println("Access flag : public "); + } + System.out.println("ClassName: " + getClassName()); + System.out.println("SuperClassName: " + getSuperClassName()); + + } + + public void setAccessFlag(AccessFlag accessFlag){ + this.accessFlag = accessFlag; + } + + 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/group11/1310368322/src/FifthHomework/jvm/clz/ClassIndex.java b/group11/1310368322/src/FifthHomework/jvm/clz/ClassIndex.java new file mode 100644 index 0000000000..8382ef03f9 --- /dev/null +++ b/group11/1310368322/src/FifthHomework/jvm/clz/ClassIndex.java @@ -0,0 +1,23 @@ +package com.coderising.jvm.clz; + +public class ClassIndex { + private int thisClassIndex; + private int superClassIndex; + + public int getThisClassIndex(){ + System.out.println(thisClassIndex); + return thisClassIndex; + } + + public void setThisClassIndex(int thisClassIndex){ + this.thisClassIndex = thisClassIndex; + } + + public int getSuperClassIndex(){ + return superClassIndex; + } + + public void setSuperClassIndex(int superClassIndex){ + this.superClassIndex = superClassIndex; + } +} diff --git a/group11/1310368322/src/FifthHomework/jvm/constant/ClassInfo.java b/group11/1310368322/src/FifthHomework/jvm/constant/ClassInfo.java new file mode 100644 index 0000000000..13fc4101a3 --- /dev/null +++ b/group11/1310368322/src/FifthHomework/jvm/constant/ClassInfo.java @@ -0,0 +1,33 @@ +package com.coderising.jvm.constant; + +public class ClassInfo extends ConstantInfo{ + + private int type = ConstantInfo.CLASS_INFO;// ʾóΪ ӿڵķ + private int utf8Index;// CONSTANT_Class_info ͳ ṹе name_index name_index ָһ CONSTANT_Utf8_info ͵ij + + public ClassInfo(ConstantPool pool){ + super(pool); + } + + public int getUtf8Index(){ + return utf8Index; + } + + public void setUtf8Index(int utf8Index){ + this.utf8Index = utf8Index; + } + + @Override + public int getType(){ + return type; + } + + // + public String getClassName(){ + int index = getUtf8Index(); + UTF8Info utf8Info = (UTF8Info)constantPool.getConstantInfo(index); + return utf8Info.getValue(); + } + + +} diff --git a/group11/1310368322/src/FifthHomework/jvm/constant/ConstantInfo.java b/group11/1310368322/src/FifthHomework/jvm/constant/ConstantInfo.java new file mode 100644 index 0000000000..e20be200d6 --- /dev/null +++ b/group11/1310368322/src/FifthHomework/jvm/constant/ConstantInfo.java @@ -0,0 +1,34 @@ +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);// õ ConstantInfo ı constantPool е getConstantInfo + // ContantInfo е getConstantInfo ͬ + } + + +} diff --git a/group11/1310368322/src/FifthHomework/jvm/constant/ConstantPool.java b/group11/1310368322/src/FifthHomework/jvm/constant/ConstantPool.java new file mode 100644 index 0000000000..9dbd3ed9ef --- /dev/null +++ b/group11/1310368322/src/FifthHomework/jvm/constant/ConstantPool.java @@ -0,0 +1,30 @@ +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 constantInfos.size() - 1; + } + +} diff --git a/group11/1310368322/src/FifthHomework/jvm/constant/FieldRefInfo.java b/group11/1310368322/src/FifthHomework/jvm/constant/FieldRefInfo.java new file mode 100644 index 0000000000..731c35b118 --- /dev/null +++ b/group11/1310368322/src/FifthHomework/jvm/constant/FieldRefInfo.java @@ -0,0 +1,62 @@ +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(nameAndTypeIndex); + return typeInfo.getName(); + } + + public String getFieldType(){ + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(nameAndTypeIndex); + return typeInfo.getTypeInfo(); + } +} + + + + + + + + + diff --git a/group11/1310368322/src/FifthHomework/jvm/constant/MethodRefInfo.java b/group11/1310368322/src/FifthHomework/jvm/constant/MethodRefInfo.java new file mode 100644 index 0000000000..49d89083fd --- /dev/null +++ b/group11/1310368322/src/FifthHomework/jvm/constant/MethodRefInfo.java @@ -0,0 +1,52 @@ +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); + } + + @Override + 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 getClassName(){ + ConstantPool pool = this.getConstantPool(); + ClassInfo clzInfo = (ClassInfo)pool.getConstantInfo(this.classInfoIndex); + return clzInfo.getClassName(); + } + + public String getMethodName(){ + ConstantPool pool = this.getConstantPool(); + NameAndTypeInfo typeInfo = (NameAndTypeInfo)pool.getConstantInfo(this.nameAndTypeIndex); + return typeInfo.getName(); + } + + public String getParamAndReturnType(){ + ConstantPool pool = this.getConstantPool(); + NameAndTypeInfo typeInfo = (NameAndTypeInfo)pool.getConstantInfo(this.nameAndTypeIndex); + return typeInfo.getTypeInfo(); + } + +} diff --git a/group11/1310368322/src/FifthHomework/jvm/constant/NameAndTypeInfo.java b/group11/1310368322/src/FifthHomework/jvm/constant/NameAndTypeInfo.java new file mode 100644 index 0000000000..8494e5e76a --- /dev/null +++ b/group11/1310368322/src/FifthHomework/jvm/constant/NameAndTypeInfo.java @@ -0,0 +1,52 @@ +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; + } + + @Override + public int getType(){ + return type; + } + + + public String getName(){ + ConstantPool pool = this.getConstantPool(); + UTF8Info utf8Info1 = (UTF8Info)pool.getConstantInfo(index1);// õ ֶ Name + return utf8Info1.getValue(); // ֶ Nameֵ + } + + 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/group11/1310368322/src/FifthHomework/jvm/constant/NullConstantInfo.java b/group11/1310368322/src/FifthHomework/jvm/constant/NullConstantInfo.java new file mode 100644 index 0000000000..f0be39e410 --- /dev/null +++ b/group11/1310368322/src/FifthHomework/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/group11/1310368322/src/FifthHomework/jvm/constant/StringInfo.java b/group11/1310368322/src/FifthHomework/jvm/constant/StringInfo.java new file mode 100644 index 0000000000..3ff8e9402b --- /dev/null +++ b/group11/1310368322/src/FifthHomework/jvm/constant/StringInfo.java @@ -0,0 +1,30 @@ +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); + } + + + @Override + 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/group11/1310368322/src/FifthHomework/jvm/constant/UTF8Info.java b/group11/1310368322/src/FifthHomework/jvm/constant/UTF8Info.java new file mode 100644 index 0000000000..6374764b5f --- /dev/null +++ b/group11/1310368322/src/FifthHomework/jvm/constant/UTF8Info.java @@ -0,0 +1,40 @@ +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; + } + + @Override + public int getType() { + return type; + } + + public String getValue(){ + return value; + } + + public void setValue(String value){ + this.value = value; + } + + @Override + public String toString(){ + return "UTF8Info [type=" + type + ",length" + length + ",value" + value + ")]"; + } + + +} diff --git a/group11/1310368322/src/FifthHomework/jvm/loader/ByteCodeIterator.java b/group11/1310368322/src/FifthHomework/jvm/loader/ByteCodeIterator.java new file mode 100644 index 0000000000..4d740d8149 --- /dev/null +++ b/group11/1310368322/src/FifthHomework/jvm/loader/ByteCodeIterator.java @@ -0,0 +1,45 @@ +package com.coderising.jvm.loader; + +public class ByteCodeIterator { + + byte[] codes; + int pos;// άλ + + public ByteCodeIterator(byte[] codes){ + this.codes = codes; + } + + public int nextU1toInt(){ + return (codes[pos++] & 0xFF); + } + + public int nextU2toInt(){ + byte [] a = new byte[]{ codes[pos++], codes[pos++]}; + return (a[0]<<8) + a[1]; + } + + public int nextU4toInt(){ + byte [] a = new byte[]{ codes[pos++], codes[pos++], codes[pos++], codes[pos++]}; + return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; + } + + public byte[] getByte(int length){ + byte []a = new byte[length]; + for(int i = 0; i < length; i++){ + a[i] = codes[pos++]; + } + return a; + } + public String nextU4ToHexString(){ + StringBuffer buffer = new StringBuffer(); + for(int i = 0; i < 4; i++){ + int a = codes[pos++] & 0xFF; + String strHex = Integer.toHexString(a); + if(strHex.length() < 2){ + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } +} diff --git a/group11/1310368322/src/FifthHomework/jvm/loader/ClassFileLoader.java b/group11/1310368322/src/FifthHomework/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..6e479bbce9 --- /dev/null +++ b/group11/1310368322/src/FifthHomework/jvm/loader/ClassFileLoader.java @@ -0,0 +1,69 @@ +package com.coderising.jvm.loader; + +import java.io.BufferedInputStream; +import java.io.DataInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.junit.runners.Parameterized.Parameters; + +import com.coderising.jvm.clz.ClassFile; + +public class ClassFileLoader { + private List clzPaths = new ArrayList(); + int countForClassPath = 0; + int countForReadBinaryCode = 0; + byte [] a = new byte[10000]; + + /* ָ·ȡļ䱣浽һֽУ + * @Parameters ָ· + * @ֽ + */ + public byte[] readBinaryCode(String className) throws IOException{ + DataInputStream dis = new DataInputStream( + new BufferedInputStream(new FileInputStream(className))); + for(int i = 0; dis.available() != 0; i++){ + a[i] = dis.readByte(); + countForReadBinaryCode++; + } + byte []target = new byte[countForReadBinaryCode]; + System.arraycopy(a, 0, target, 0, countForReadBinaryCode); + dis.close(); + return target; + } + + public void addClassPath(String path){ + clzPaths.add(path); + countForClassPath++; + } + + public String getClassPath(){ + StringBuffer buffer = new StringBuffer(); + for(int i = 0; i < countForClassPath; i++ ){ + if(i==countForClassPath-1){ + buffer.append(clzPaths.get(i)); + }else{ + buffer.append(clzPaths.get(i)+";"); + } + } + return buffer.toString(); + } + + public ClassFile loadClass(String className) throws IOException{ + byte[] codes = this.readBinaryCode(className); + ClassFileParser parser = new ClassFileParser(); + return parser.parse(codes); + } + + + + + + + + + + +} diff --git a/group11/1310368322/src/FifthHomework/jvm/loader/ClassFileParser.java b/group11/1310368322/src/FifthHomework/jvm/loader/ClassFileParser.java new file mode 100644 index 0000000000..a2de56dfc7 --- /dev/null +++ b/group11/1310368322/src/FifthHomework/jvm/loader/ClassFileParser.java @@ -0,0 +1,130 @@ +package com.coderising.jvm.loader; + +import java.io.UnsupportedEncodingException; + +import javax.management.RuntimeErrorException; + +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.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; + +/* + * һֽ飬ɶа ClassFile + */ +public class ClassFileParser { + + // غ ClassFile + public ClassFile parse(byte[] codes){ + ClassFile clzFile = new ClassFile(); + ByteCodeIterator iter = new ByteCodeIterator(codes); + String magicNumber = iter.nextU4ToHexString(); + if(!magicNumber.equals("cafebabe")){ + return null; + } + clzFile.setMinorVersion(iter.nextU2toInt()); + clzFile.setMajorVersion(iter.nextU2toInt()); + + // ȡ + ConstantPool pool = parseConstantPool(iter); + AccessFlag accessFlag = parseAccessFlag(iter); + ClassIndex clzIndex = parseClassIndex(iter); + clzFile.setAccessFlag(accessFlag); + clzFile.setClassIndex(clzIndex); + clzFile.setConstantPool(pool);// м볣 + + + return clzFile; + } + + private AccessFlag parseAccessFlag(ByteCodeIterator iter){ + int accessFlagValue = iter.nextU2toInt(); + AccessFlag accessFlag = new AccessFlag(accessFlagValue); + accessFlag.setFlagValue(accessFlagValue); + return accessFlag; + } + + private ClassIndex parseClassIndex(ByteCodeIterator iter){ + int thisClassIndex = iter.nextU2toInt(); + int superClassIndex = iter.nextU2toInt(); + ClassIndex classIndex = new ClassIndex(); + classIndex.setThisClassIndex(thisClassIndex); + classIndex.setSuperClassIndex(superClassIndex); + return classIndex; + } + + private ConstantPool parseConstantPool(ByteCodeIterator iter){ + + int constantPoolCount = iter.nextU2toInt(); + System.out.println("ConstantPool Count : " + constantPoolCount); + ConstantPool pool = new ConstantPool(); + pool.addConstantInfo(new NullConstantInfo()); + + // ӳϢ + for(int i = 1; i <= constantPoolCount-1; i++){// JVM 涨Ҫ ȥ 1ȥ 1ij + + int tag = iter.nextU1toInt(); + if(tag == 7){ + // ӿڵķ ClassInfo + int utf8Index = iter.nextU2toInt(); + ClassInfo clzInfo= new ClassInfo(pool);// һ ClassInfo + clzInfo.setUtf8Index(utf8Index); + pool.addConstantInfo(clzInfo); + }else if(tag == 1){ + // UTF-8 ַ + int length = iter.nextU2toInt(); + byte data[] = iter.getByte(length); + String value = null; + try { + value = new String(data,"UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + UTF8Info utf8Info = new UTF8Info(pool); + utf8Info.setLength(length); + utf8Info.setValue(value); + pool.addConstantInfo(utf8Info); + }else if(tag == 12){ + // NameAndTypeInfo ֶλ򷽷IJַ + int NameIndex = iter.nextU2toInt(); + int TypeIndex = iter.nextU2toInt(); + NameAndTypeInfo nameAndType = new NameAndTypeInfo(pool); + nameAndType.setIndex1(NameIndex); + nameAndType.setIndex2(TypeIndex); + pool.addConstantInfo(nameAndType); + }else if(tag == 9){ + // FieldRefInfo ֶεķ + int classInfoIndex = iter.nextU2toInt(); + int nameAndTypeIndex = iter.nextU2toInt(); + FieldRefInfo fieldRefInfo = new FieldRefInfo(pool); + fieldRefInfo.setClassInfoIndex(classInfoIndex); + fieldRefInfo.setNameAndTypeIndex(nameAndTypeIndex); + pool.addConstantInfo(fieldRefInfo); + }else if(tag == 10){ + // MethodRefInfo зķ + int classInfoIndex = iter.nextU2toInt(); + int nameAndTypeIndex = iter.nextU2toInt(); + MethodRefInfo methodRefInfo = new MethodRefInfo(pool); + methodRefInfo.setClassInfoIndex(classInfoIndex); + methodRefInfo.setNameAndTypeIndex(nameAndTypeIndex); + pool.addConstantInfo(methodRefInfo); + }else if(tag == 8){ + int stringIndex = iter.nextU2toInt();// ַָ + StringInfo stringInfo = new StringInfo(pool); + stringInfo.setIndex(stringIndex); + pool.addConstantInfo(stringInfo); + }else{ + throw new RuntimeException("the constant pool tag" + tag + "has not been implemented yet"); + } + + } + return pool; + } +} diff --git a/group11/1310368322/src/FifthHomework/jvm/loader/TestJVM.java b/group11/1310368322/src/FifthHomework/jvm/loader/TestJVM.java new file mode 100644 index 0000000000..735e4d1dc2 --- /dev/null +++ b/group11/1310368322/src/FifthHomework/jvm/loader/TestJVM.java @@ -0,0 +1,7 @@ +package com.coderising.jvm.loader; + +public class TestJVM { + public static void main(String[] args) { + System.out.println("Hello"); + } +} diff --git a/group11/1310368322/test/FifthHomework/DataStructure_5_Stack/TestStackUtil.java b/group11/1310368322/test/FifthHomework/DataStructure_5_Stack/TestStackUtil.java new file mode 100644 index 0000000000..0b227e9ff6 --- /dev/null +++ b/group11/1310368322/test/FifthHomework/DataStructure_5_Stack/TestStackUtil.java @@ -0,0 +1,77 @@ +package DataStructure_5_Stack; + +import java.util.Stack; + +import org.junit.Assert; + +import org.junit.Test; + +public class TestStackUtil { + + @Test + public void test_reverse_1() { + Stack s = new Stack(); + for(int i = 0; i < 5; i++){ + s.push(i+1); + } + System.out.println(s.toString()); + StackUtil.reverse(s); + System.out.println(s.toString()); + } + + @Test + public void test_reverse_2() { + Stack s = new Stack(); + System.out.println(s.isEmpty()); + System.out.println(s.toString()); + StackUtil.reverse(s); + System.out.println(s.toString()); + } + + @Test + public void testRemove_1(){ + Stack actual = new Stack(); + Stack expected = new Stack(); + for(int i = 0; i < 5; i++){ + actual.push(i+1); + if(i != 2){ expected.push(i+1); } + } + StackUtil.remove(actual, 3); + Assert.assertEquals(expected, actual); + } + @Test + public void testRemove_2(){ + Stack actual = new Stack(); + Stack expected = new Stack(); + StackUtil.remove(actual, 3); + Assert.assertEquals(expected, actual); + } + + @Test + public void testGetTop(){ + Stack s = new Stack(); + for(int i = 0; i < 5; i++){ + s.push(i+1); + } + Object expected[] = {5,4,3}; + Object[] actual = StackUtil.getTop(s, 3); + Assert.assertArrayEquals(expected, actual); + + } + + + @Test + public void testIsValidPairs_1(){ + boolean actual = StackUtil.isValidPairs("([e{d}f])"); + boolean expected = true; + Assert.assertEquals(expected, actual); + } + + @Test + public void testValidPairs_2(){ + boolean actual = StackUtil.isValidPairs("([b{x]})"); + boolean expected = false; + Assert.assertEquals(expected, actual); + } + +} diff --git a/group11/1310368322/test/FifthHomework/DataStructure_5_Stack/TestSwitch.java b/group11/1310368322/test/FifthHomework/DataStructure_5_Stack/TestSwitch.java new file mode 100644 index 0000000000..a14ae1683b --- /dev/null +++ b/group11/1310368322/test/FifthHomework/DataStructure_5_Stack/TestSwitch.java @@ -0,0 +1,15 @@ +package DataStructure_5_Stack; + +public class TestSwitch { + public static void main(String[] args) { + int i = 3; + while(true){ + switch(i){ + case 1: System.out.println("1"); break; + case 3: System.out.println("3"); + case 4: System.out.println("4"); break; + } + } + + } +} diff --git a/group11/1310368322/test/FifthHomework/jvm/loader/EmployeeV1.java b/group11/1310368322/test/FifthHomework/jvm/loader/EmployeeV1.java new file mode 100644 index 0000000000..acbc34c9bb --- /dev/null +++ b/group11/1310368322/test/FifthHomework/jvm/loader/EmployeeV1.java @@ -0,0 +1,28 @@ +package com.coderising.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(); + } +} diff --git a/group11/1310368322/test/FifthHomework/jvm/loader/TestArray.java b/group11/1310368322/test/FifthHomework/jvm/loader/TestArray.java new file mode 100644 index 0000000000..6d78273d8c --- /dev/null +++ b/group11/1310368322/test/FifthHomework/jvm/loader/TestArray.java @@ -0,0 +1,8 @@ +package com.coderising.jvm.loader; + +public class TestArray { + public static void main(String[] args) { + byte [] a; + + } +} diff --git a/group11/1310368322/test/FifthHomework/jvm/loader/TestArrayList.java b/group11/1310368322/test/FifthHomework/jvm/loader/TestArrayList.java new file mode 100644 index 0000000000..13bfb372fb --- /dev/null +++ b/group11/1310368322/test/FifthHomework/jvm/loader/TestArrayList.java @@ -0,0 +1,14 @@ +package com.coderising.jvm.loader; + +import java.util.ArrayList; + +public class TestArrayList { + public static void main(String[] args) { + ArrayList list = new ArrayList(); + for(int i = 0; i < 5; i++){ + list.add(i); + } + System.out.println(list.size()); + } + +} diff --git a/group11/1310368322/test/FifthHomework/jvm/loader/TestByteArrayToHexString.java b/group11/1310368322/test/FifthHomework/jvm/loader/TestByteArrayToHexString.java new file mode 100644 index 0000000000..d0ef456005 --- /dev/null +++ b/group11/1310368322/test/FifthHomework/jvm/loader/TestByteArrayToHexString.java @@ -0,0 +1,15 @@ +package com.coderising.jvm.loader; + +public class TestByteArrayToHexString { + + public static void main(String[] args) { + byte [] a = { -54,-2,-70,-66}; + for(int i = 0; i < 2; i++){ + byte b = a[i]; + int j = b & 0xFF; + String s = Integer.toHexString(j); + System.out.println(s.length()); + System.out.println(s); + } + } +} diff --git a/group11/1310368322/test/FifthHomework/jvm/loader/TestByteArrayToInt.java b/group11/1310368322/test/FifthHomework/jvm/loader/TestByteArrayToInt.java new file mode 100644 index 0000000000..8bde4d5974 --- /dev/null +++ b/group11/1310368322/test/FifthHomework/jvm/loader/TestByteArrayToInt.java @@ -0,0 +1,12 @@ +package com.coderising.jvm.loader; + +public class TestByteArrayToInt { + public static void main(String[] args) { + byte a[] = {1,1}; + // System.out.println(a[0]<<7); ƣߵ k λҶ˲ k 0 + // int b = a[0]<<8 + a[1]; + int b = (a[0]<<8) + a[1]; + // System.out.println(a[0]); ƶԭڴеֵûӰ + System.out.println(b); + } +} diff --git a/group11/1310368322/test/FifthHomework/jvm/loader/TestByteToInt.java b/group11/1310368322/test/FifthHomework/jvm/loader/TestByteToInt.java new file mode 100644 index 0000000000..2c51afb572 --- /dev/null +++ b/group11/1310368322/test/FifthHomework/jvm/loader/TestByteToInt.java @@ -0,0 +1,12 @@ +package com.coderising.jvm.loader; + +public class TestByteToInt { + public static void main(String[] args) { + byte a = -1; + int b = a;// aֱֵӸ bᷢı䣬ڴУ Ὣ ǰȫ 1 -128 byteеڴʾΪ 1000 0000Ȼintǣ 1111 1111 1111 1111 1111 1111 1000 0000-128intͲ룩 + int c = a & 0xFFFF;// λڴнеģҲ˵ǶaIJвģ a & 0xFF е a λ0xFF(Ĭint͵) + System.out.println(b); + System.out.println(c); + System.out.println(a>>>24); + } +} diff --git a/group11/1310368322/test/FifthHomework/jvm/loader/TestClassFileLoader.java b/group11/1310368322/test/FifthHomework/jvm/loader/TestClassFileLoader.java new file mode 100644 index 0000000000..3971ac81cd --- /dev/null +++ b/group11/1310368322/test/FifthHomework/jvm/loader/TestClassFileLoader.java @@ -0,0 +1,174 @@ +package com.coderising.jvm.loader; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import org.junit.*; + +import com.coderising.jvm.clz.ClassFile; +import com.coderising.jvm.clz.ClassIndex; +import com.coderising.jvm.constant.*; + +public class TestClassFileLoader { + static String path1 = "D:/ProgramWorld"; + static String path2 = "D:/ProgramWorld/Java"; + private static final String FULL_QUALIFIED_CLASS_NAME = "com/coderising/jvm/loader/EmployeeV1"; + static String path = "D:/ProgramWorld/Java/Practice/LangSi/2017Ⱥ/bin/com/coderising/jvm/loader/EmployeeV1.class"; + static ClassFile clzFile = null; + + + + @Test + public void test() { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + String clzPath = loader.getClassPath(); + Assert.assertEquals(path1 + ";" + path2, clzPath); + } + @Test + public void testClassFileLength() throws IOException{ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "D:/ProgramWorld/Java/Practice/LangSi/2017Ⱥ/bin/com/coderising/jvm/loader/EmployeeV1.class"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // ע⣺ ֽܺJVM汾йϵԿõൽж + Assert.assertEquals(1058,byteCodes.length); + } + @Test + public void testMagicNumber() throws IOException{ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "D:/ProgramWorld/Java/Practice/LangSi/2017Ⱥ/bin/com/coderising/jvm/loader/EmployeeV1.class"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{ + byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3] + }; + System.out.println("ddd"); + String actualValue = this.byteToHexString(codes); + Assert.assertEquals("cafebabe",actualValue); + + } + + private String byteToHexString(byte[] codes){ + StringBuffer buffer = new StringBuffer(); + for(int i = 0; i < codes.length; i++){ + byte b = codes[i]; + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if(strHex.length() < 2){ + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } + + //--------------------------------------------- + + + @Test + public void testVersion() throws IOException{ + ClassFileLoader loader = new ClassFileLoader(); + String className = "D:/TestJVM.class"; + clzFile = loader.loadClass(className); + Assert.assertEquals(0, clzFile.getMinorVersion()); + Assert.assertEquals(51, clzFile.getMajorVersion()); + + } + + @Test + public void testConstantPool() throws IOException{ + + ClassFileLoader loader = new ClassFileLoader(); + clzFile = loader.loadClass(path); + ConstantPool pool = clzFile.getConstantPool(); + + Assert.assertEquals(53, pool.getSize()); + + { + ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(1); + Assert.assertEquals(2, clzInfo.getUtf8Index()); + + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(2); + Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, utf8Info.getValue()); + } + { + ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(3); + Assert.assertEquals(4, clzInfo.getUtf8Index()); + + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(4); + Assert.assertEquals("java/lang/Object", utf8Info.getValue()); + } + { + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(5); + Assert.assertEquals("name", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(6); + Assert.assertEquals("Ljava/lang/String;", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(7); + Assert.assertEquals("age", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(8); + Assert.assertEquals("I", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(9); + Assert.assertEquals("", 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() throws IOException{ + + ClassFileLoader loader = new ClassFileLoader(); + clzFile = loader.loadClass(path); + ClassIndex clzIndex = clzFile.getClzIndex(); + System.out.println("clzIndex="+clzIndex); + ClassInfo thisClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getThisClassIndex()); + ClassInfo superClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getSuperClassIndex()); + System.out.println(thisClassInfo.getClassName()); + System.out.println(superClassInfo.getClassName()); + Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, thisClassInfo.getClassName()); + Assert.assertEquals("java/lang/Object", superClassInfo.getClassName()); + } + + + + + + +} diff --git a/group11/1310368322/test/FifthHomework/jvm/loader/TestIntegerToHexString.java b/group11/1310368322/test/FifthHomework/jvm/loader/TestIntegerToHexString.java new file mode 100644 index 0000000000..b5be2a16d1 --- /dev/null +++ b/group11/1310368322/test/FifthHomework/jvm/loader/TestIntegerToHexString.java @@ -0,0 +1,10 @@ +package com.coderising.jvm.loader; + +public class TestIntegerToHexString { + public static void main(String[] args) { + int i = 10; + System.out.println(i); + String s = Integer.toHexString(i); + System.out.println(s); + } +} From e554b99b4c1742c5a607380c6260ce379858d582 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=9A=E5=90=AF=E7=9B=BC?= Date: Mon, 10 Apr 2017 16:35:22 +0800 Subject: [PATCH 19/41] =?UTF-8?q?=E9=87=8D=E6=96=B0=E6=90=AD=E5=BB=BA?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E9=A1=B9=E7=9B=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group11/252308879/README.md | 29 ++ .../main/java/com/pan/week01/Iterator.java | 12 + .../main/java/com/pan/week02/ArrayUtil.java | 282 ++++++++++++++++++ 3 files changed, 323 insertions(+) create mode 100644 group11/252308879/README.md create mode 100644 group11/252308879/data-structure/src/main/java/com/pan/week01/Iterator.java create mode 100644 group11/252308879/data-structure/src/main/java/com/pan/week02/ArrayUtil.java diff --git a/group11/252308879/README.md b/group11/252308879/README.md new file mode 100644 index 0000000000..99f5dc58e0 --- /dev/null +++ b/group11/252308879/README.md @@ -0,0 +1,29 @@ +Coding2017 项目 +=== + +## 简介 + +* 考虑到目前的项目目录比较凌乱,所以整理一下目录。目前还未整理完成。 + +* 该项目主要是个人的日常作业,包含三大部分(后续会更多): + * 前三周的打基础(计算机系统知识) + * 数据结构 + * mini-jvm + +--- + +## 目录 + +[TOC] + +--- + +## 正文 + +* data-structure 项目中主要包含前三周的作业 + * 基本的数据结构 + * 解析struts.xml + * 多线程下载 +* mini-jvm 项目中主要包含解析jvm的作业 + +* 在目前这两个大模块中,都包含了每周的数据结构练习 diff --git a/group11/252308879/data-structure/src/main/java/com/pan/week01/Iterator.java b/group11/252308879/data-structure/src/main/java/com/pan/week01/Iterator.java new file mode 100644 index 0000000000..b59719cd2b --- /dev/null +++ b/group11/252308879/data-structure/src/main/java/com/pan/week01/Iterator.java @@ -0,0 +1,12 @@ +package com.pan.week01; + +/** + * Created by QiPan on 2017/2/23. + */ +public interface Iterator { + boolean hasNext(); + + Object next(); + + void remove(); +} diff --git a/group11/252308879/data-structure/src/main/java/com/pan/week02/ArrayUtil.java b/group11/252308879/data-structure/src/main/java/com/pan/week02/ArrayUtil.java new file mode 100644 index 0000000000..e835e31269 --- /dev/null +++ b/group11/252308879/data-structure/src/main/java/com/pan/week02/ArrayUtil.java @@ -0,0 +1,282 @@ +package com.pan.week02; + +import java.util.Arrays; + +/** + * Created by QiPan on 2017/2/27. + */ +public class ArrayUtil { + + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public static void reverseArray(int[] origin) { + + // 如果是null, 或者长度小于等于1, 直接返回 + if (origin == null || origin.length <= 1) { + return; + } + for (int i = 0; i < origin.length / 2; i++) { + int tmp = origin[i]; + origin[i] = origin[origin.length - 1 - i]; + origin[origin.length - 1 - i] = tmp; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray) { + + if (oldArray == null) { + return oldArray; + } + int[] newArray = null; + int count = 0; //统计被移出的数组的个数 + for (int i = 0; i < oldArray.length; i++) { + int num = oldArray[i]; + if (num == 0) { + count++; + System.arraycopy(oldArray, i + 1, oldArray, i, oldArray.length - i - 1); + i--; + } + } + if (count == 0) { + newArray = oldArray; + } else { + newArray = new int[oldArray.length - count]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length - count); + } + return newArray; + } + + /** + * 不用JavaAPI来做 + * + * @param oldArray + * @return + */ + public static int[] removeZero_2(int[] oldArray) { + + if (oldArray == null) { + return oldArray; + } + int count = 0; + for (int num : oldArray) { + if (num == 0) { + count++; + } + } + int[] newArray = new int[oldArray.length - count]; + for (int i = 0, j = 0; i < oldArray.length; i++, j++) { + int num = oldArray[i]; + if (num == 0) { + j--; + } else { + newArray[j] = num; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + public static int[] merge(int[] array1, int[] array2) { + //先初始化一个array3,但不是最后返回的数组 + int[] array3 = new int[array1.length + array2.length]; + int i = 0, j = 0; + int array3Size = 0; // 统计实际上合并数组后的大小 + while (i < array1.length && j < array2.length) { + if (array1[i] < array2[j]) {// 如果array1中元素小,则插入到array3中 + array3[array3Size++] = array1[i]; + ++i; + } else if (array1[i] > array2[j]) {//如果array2中元素小,则插入到array3中 + array3[array3Size++] = array2[j]; + ++j; + } else {//否则随便插入一个,但是计数要同时加1 + array3[array3Size++] = array1[i]; + ++i; + ++j; + } + } + + if (i == array1.length) { //如果array1中全部循环完毕了,那么需要去处理array2中剩余的元素 + for (int n = j; n < array2.length; n++) { + array3[array3Size++] = array2[n]; + } + } else if (j == array2.length) {// 如果array2中全部循环完毕,那么需要去处理array1中剩余的元素 + for (int n = i; n < array1.length; n++) { + array3[array3Size++] = array1[n]; + } + } + int[] returnResultArray = new int[array3Size]; + System.arraycopy(array3, 0, returnResultArray, 0, + array3Size); + return returnResultArray; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + if (oldArray == null) { + oldArray = new int[size]; + } + oldArray = Arrays.copyOf(oldArray, oldArray.length + size); + return oldArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + if (max <= 1) { + return new int[0]; + } + int[] arrays = new int[max / 2]; + int firstNum = 1; //第一个数字 + int secondNum = 1; // 第二个数字 + int arraySize = 0; + arrays[arraySize++] = 1; // 初始化第一位 + while (secondNum < max) { + arrays[arraySize++] = secondNum; + int tmpNum = secondNum; // 保存第二个数,得会需要付给第一个数 + secondNum = firstNum + secondNum; // 为前两个数之和 + firstNum = tmpNum; // 第一个数,后移 + } + return Arrays.copyOf(arrays, arraySize); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + int[] returnResultArray = new int[max + 1]; + int arraySize = 0; + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + returnResultArray[arraySize++] = i; + } + + } + if (arraySize == returnResultArray.length) { + return returnResultArray; + } + return Arrays.copyOf(returnResultArray, arraySize); + } + + private static boolean isPrime(final int number) { + if (number < 2) { + return false; + } + // 因为不可能将一个数除与所有小于它的数字,只要检查到N的平方根就好了。 + // 但直接开根号还有个精度的问题。这个可能会产生误差。 索性将判断条件写成 i*i<=number + for (int i = 2; i * i <= number; i++) { + if (number % i == 0) {//查看所有小于number平方根的数,能够被整除 + return false; + } + } + // 如果一个都没有,那么就是素数 + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + int[] array = new int[max]; + int arraySize = 0; + for (int n = 0; n < max; n++) { + int fac,// 被除的因子 + sum,// 用来统计因子之和 + num;// 除数的因子,中间变量 + for (sum = 1, num = n, fac = 2; fac < num; fac++) { + + if (n % fac == 0) {// 如果余数为0,那么说明有因子 + sum += fac; // 统计因子和 + num = n / fac; // num=等于除数的最大因子 + if (num == fac) // 如果最大和最小相等跳出循环 + break; + sum += num; // 再统计因子 + } + } + + if (sum == n) { //因子和与整数相等,那么就是一个完美数 + if (n != 1) { + System.out.println(n + "是一个完全数,其因子为:"); + } + for (fac = 1; fac < n; fac++) { + if (n % fac == 0) {// 列出所有的因子 + System.out.print(fac + " "); + } + } + System.out.println(); + array[arraySize++] = n; // 放到数组中 + } + } + return Arrays.copyOf(array, arraySize); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public static String join(int[] array, String seperator) { + if (array == null) { + return null; + } + StringBuilder str = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + if (i == array.length - 1) { + str.append(array[i]); + continue; + } + str.append(array[i]).append(seperator); + } + return str.toString(); + } + +} From 7d692349b71aaa579e681088a9ebb5e58dd28acb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=9A=E5=90=AF=E7=9B=BC?= Date: Mon, 10 Apr 2017 16:55:13 +0800 Subject: [PATCH 20/41] init mini-jvm --- .../main/java/com/pan/alg/LRUPageFrame.java | 133 ++++++++++++++++++ .../java/com/pan/jvm/ClassFileLoader.java | 49 +++++++ .../main/java/com/pan/jvm/clz/AccessFlag.java | 26 ++++ .../main/java/com/pan/jvm/clz/ClassFile.java | 82 +++++++++++ .../main/java/com/pan/jvm/clz/ClassIndex.java | 22 +++ .../java/com/pan/jvm/constant/ClassInfo.java | 28 ++++ .../com/pan/jvm/constant/ConstantInfo.java | 31 ++++ .../com/pan/jvm/constant/ConstantPool.java | 32 +++++ .../com/pan/jvm/constant/FieldRefInfo.java | 58 ++++++++ .../com/pan/jvm/constant/MethodRefInfo.java | 58 ++++++++ .../com/pan/jvm/constant/NameAndTypeInfo.java | 49 +++++++ .../pan/jvm/constant/NullConstantInfo.java | 14 ++ .../java/com/pan/jvm/constant/StringInfo.java | 28 ++++ .../java/com/pan/jvm/constant/UTF8Info.java | 38 +++++ .../com/pan/jvm/loader/ByteCodeIterator.java | 5 + .../com/pan/jvm/loader/ClassFileLoader.java | 130 +++++++++++++++++ .../com/pan/jvm/loader/ClassFileParser.java | 33 +++++ .../src/main/java/com/pan/jvm/util/Util.java | 23 +++ .../test/java/com/pan/jvm/TestReadCFBB.java | 10 ++ 19 files changed, 849 insertions(+) create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/alg/LRUPageFrame.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/ClassFileLoader.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/AccessFlag.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/ClassFile.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/ClassIndex.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/ClassInfo.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/ConstantInfo.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/ConstantPool.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/FieldRefInfo.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/MethodRefInfo.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/NameAndTypeInfo.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/NullConstantInfo.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/StringInfo.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/UTF8Info.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ByteCodeIterator.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileLoader.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileParser.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/util/Util.java create mode 100644 group11/252308879/mini-jvm/src/test/java/com/pan/jvm/TestReadCFBB.java diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/alg/LRUPageFrame.java b/group11/252308879/mini-jvm/src/main/java/com/pan/alg/LRUPageFrame.java new file mode 100644 index 0000000000..d9455d3807 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/alg/LRUPageFrame.java @@ -0,0 +1,133 @@ +package com.pan.alg; + + +/* + * 用双向链表实现LRU算法 + */ +public class LRUPageFrame { + private static class Node{ + Node prev; + Node next; + int pageNum = -1;// 物理页 + + Node(){ + + } + } + + private int capacity; + + private Node first;// 链表头 + private Node last;// 链表尾 + boolean tag = false; + + public LRUPageFrame(int capacity){ + this.capacity = capacity; + + for(int i = 0; i < capacity; i++){ + Node curNode = new Node(); + if(null == first){ + last = first = curNode; + }else{ + last.next = curNode; + curNode.prev = last; + last = last.next; + } + last.next = null; + } + } + public void printList(){ + Node curNode = first; + while(curNode != null){ + curNode = curNode.next; + } + } + /* + * 获取缓存中对象 + * @param key + * @return + */ + public void access(int pageNum){ + printList(); + Node index = findLogicPage(pageNum); + modifyPhysicalPage(index,pageNum); + } + + /* + * @param pageNum 表示要查询的逻辑页面 + * @return 若在物理页中找到要查询的逻辑页面,则返回该物理页节点的引用,否则返回null + */ + public Node findLogicPage(int pageNum){ + + Node index = null; + Node curNode = first; + while(curNode != null){ + if(curNode.pageNum == pageNum){ + index = curNode; + tag = true; + } + curNode = curNode.next; + } + return index; + } + /* + * @prama index 代表了 有逻辑页的物理页的节点的引用 + */ + public void modifyPhysicalPage(Node index,int pageNum){ + push(pageNum,index); + } + /* + * @param pageNum 要 push的逻辑页面, 默认栈顶是 first, bottom 栈底 指定了栈的大小 + */ + public void push(int pageNum,Node bottom){ + Node index = checkWhichListNodeNotUsed(); + if(index != null){ + index.pageNum = pageNum; + return; + } + + Node lastNode; + if(null == bottom){ + lastNode = last; + }else{ + lastNode = bottom; + } + Node curNode = lastNode.prev; + while(curNode != null){ + lastNode.pageNum = curNode.pageNum; + lastNode = curNode; + curNode = curNode.prev; + } + lastNode.pageNum = pageNum; + return; + } + + /* + * @return 返回物理页中 pageNum 没有被使用的节点的引用(返回栈中最下面的),如果全部都被使用,则返回 null + */ + public Node checkWhichListNodeNotUsed(){ + Node node = first; + Node index = null; + while(node != null){ + if(node.pageNum == -1){ + index = node; + } + node = node.next; + } + return index; + } + + public String toString(){ + StringBuffer buffer = new StringBuffer(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } +} diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/ClassFileLoader.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/ClassFileLoader.java new file mode 100644 index 0000000000..0961c5ddd5 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/ClassFileLoader.java @@ -0,0 +1,49 @@ +package com.pan.jvm; + +import java.io.BufferedInputStream; +import java.io.DataInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class ClassFileLoader { + private List clzPaths = new ArrayList(); + int countForClassPath = 0; + int countForReadBinaryCode = 0; + byte [] a = new byte[10000]; + + /* 从指定路径读取二进制文件流,并将其保存到一个字节数组中,并返回 + * @Parameters 指定路径 + * @字节数组 + */ + public byte[] readBinaryCode(String className) throws IOException{ + DataInputStream dis = new DataInputStream( + new BufferedInputStream(new FileInputStream(className))); + for(int i = 0; dis.available() != 0; i++){ + a[i] = dis.readByte(); + countForReadBinaryCode++; + } + byte []target = new byte[countForReadBinaryCode]; + System.arraycopy(a, 0, target, 0, countForReadBinaryCode); + dis.close(); + return target; + } + + public void addClassPath(String path){ + clzPaths.add(path); + countForClassPath++; + } + + public String getClassPath(){ + StringBuffer buffer = new StringBuffer(); + for(int i = 0; i < countForClassPath; i++ ){ + if(i==countForClassPath-1){ + buffer.append(clzPaths.get(i)); + }else{ + buffer.append(clzPaths.get(i)+";"); + } + } + return buffer.toString(); + } +} diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/AccessFlag.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/AccessFlag.java new file mode 100644 index 0000000000..8c253df200 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/AccessFlag.java @@ -0,0 +1,26 @@ +package com.pan.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/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/ClassFile.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/ClassFile.java new file mode 100644 index 0000000000..a192ad0c82 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/ClassFile.java @@ -0,0 +1,82 @@ +package com.pan.jvm.clz; + + +import com.pan.jvm.constant.ClassInfo; +import com.pan.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/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/ClassIndex.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/ClassIndex.java new file mode 100644 index 0000000000..563592ac47 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/ClassIndex.java @@ -0,0 +1,22 @@ +package com.pan.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/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/ClassInfo.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/ClassInfo.java new file mode 100644 index 0000000000..b842755586 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/ClassInfo.java @@ -0,0 +1,28 @@ +package com.pan.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/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/ConstantInfo.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/ConstantInfo.java new file mode 100644 index 0000000000..464e44a79d --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/ConstantInfo.java @@ -0,0 +1,31 @@ +package com.pan.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/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/ConstantPool.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/ConstantPool.java new file mode 100644 index 0000000000..c2da2637c5 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/ConstantPool.java @@ -0,0 +1,32 @@ +package com.pan.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/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/FieldRefInfo.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/FieldRefInfo.java new file mode 100644 index 0000000000..2aaced31d3 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/FieldRefInfo.java @@ -0,0 +1,58 @@ +package com.pan.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/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/MethodRefInfo.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/MethodRefInfo.java new file mode 100644 index 0000000000..a3e6f969b0 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/MethodRefInfo.java @@ -0,0 +1,58 @@ +package com.pan.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/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/NameAndTypeInfo.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/NameAndTypeInfo.java new file mode 100644 index 0000000000..30be83ef3a --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/NameAndTypeInfo.java @@ -0,0 +1,49 @@ +package com.pan.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/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/NullConstantInfo.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/NullConstantInfo.java new file mode 100644 index 0000000000..cc6989bef7 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/NullConstantInfo.java @@ -0,0 +1,14 @@ +package com.pan.jvm.constant; + +public class NullConstantInfo extends ConstantInfo { + + public NullConstantInfo() { + + } + + @Override + public int getType() { + return -1; + } + +} diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/StringInfo.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/StringInfo.java new file mode 100644 index 0000000000..ad3f397949 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/StringInfo.java @@ -0,0 +1,28 @@ +package com.pan.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/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/UTF8Info.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/UTF8Info.java new file mode 100644 index 0000000000..502adae968 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/constant/UTF8Info.java @@ -0,0 +1,38 @@ +package com.pan.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/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ByteCodeIterator.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ByteCodeIterator.java new file mode 100644 index 0000000000..f808720ef9 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ByteCodeIterator.java @@ -0,0 +1,5 @@ +package com.pan.jvm.loader; + +public class ByteCodeIterator { + +} diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileLoader.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..1418fa9b83 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileLoader.java @@ -0,0 +1,130 @@ +package com.pan.jvm.loader; + +import com.pan.jvm.clz.ClassFile; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; + +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 { + + 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 < this.clzPaths.size(); i++) { + buffer.append(this.clzPaths.get(i)); + if (i < this.clzPaths.size() - 1) { + buffer.append(";"); + } + } + 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 null; + + } + + +} \ No newline at end of file diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileParser.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileParser.java new file mode 100644 index 0000000000..d2ba152e05 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileParser.java @@ -0,0 +1,33 @@ +package com.pan.jvm.loader; + +import com.pan.jvm.clz.AccessFlag; +import com.pan.jvm.clz.ClassFile; +import com.pan.jvm.clz.ClassIndex; +import com.pan.jvm.constant.ConstantPool; + +public class ClassFileParser { + + public ClassFile parse(byte[] codes) { + + + return null; + } + + private AccessFlag parseAccessFlag(ByteCodeIterator iter) { + + return null; + } + + private ClassIndex parseClassInfex(ByteCodeIterator iter) { + + return null; + + } + + private ConstantPool parseConstantPool(ByteCodeIterator iter) { + + return null; + } + + +} diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/util/Util.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/util/Util.java new file mode 100644 index 0000000000..ae130b4183 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/util/Util.java @@ -0,0 +1,23 @@ +package com.pan.jvm.util; + +public class Util { + public static int byteToInt(byte[] codes) { + String s1 = byteToHexString(codes); + return Integer.valueOf(s1, 16).intValue(); + } + + + public static String byteToHexString(byte[] codes) { + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < codes.length; i++) { + byte b = codes[i]; + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if (strHex.length() < 2) { + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } +} diff --git a/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/TestReadCFBB.java b/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/TestReadCFBB.java new file mode 100644 index 0000000000..5523fa7696 --- /dev/null +++ b/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/TestReadCFBB.java @@ -0,0 +1,10 @@ +package com.pan.jvm; + +/** + * 用于测试第一次JVM作业,读取.class作业 和 魔幻数字 + */ +public class TestReadCFBB { + + + +} From ae1dada92d2c3b7705203f88e400179066b8997d Mon Sep 17 00:00:00 2001 From: "wangxg922@chinaunincom.cn" <996108220@qq.com> Date: Mon, 10 Apr 2017 17:45:55 +0800 Subject: [PATCH 21/41] =?UTF-8?q?Signed-off-by:=20=E7=AC=AC=E4=BA=8C?= =?UTF-8?q?=E6=AC=A1jvm<996108220@qq.com>?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/coderising/jvm/clz/AccessFlag.java | 25 ++++ .../src/com/coderising/jvm/clz/ClassFile.java | 74 ++++++++++ .../com/coderising/jvm/clz/ClassIndex.java | 19 +++ .../coderising/jvm/constant/ClassInfo.java | 24 +++ .../coderising/jvm/constant/ConstantInfo.java | 29 ++++ .../coderising/jvm/constant/ConstantPool.java | 29 ++++ .../coderising/jvm/constant/FieldRefInfo.java | 54 +++++++ .../jvm/constant/MethodRefInfo.java | 55 +++++++ .../jvm/constant/NameAndTypeInfo.java | 45 ++++++ .../jvm/constant/NullConstantInfo.java | 13 ++ .../coderising/jvm/constant/StringInfo.java | 26 ++++ .../com/coderising/jvm/constant/UTF8Info.java | 32 ++++ .../jvm/loader/ByteCodeIterator.java | 51 +++++++ .../jvm/loader/ClassFileLoader.java | 34 ++++- .../jvm/loader/ClassFileParser.java | 111 ++++++++++++++ .../jvm/test/ClassFileloaderTest.java | 138 ++++++++++++++++-- .../src/com/coderising/jvm/util/Util.java | 33 +++++ .../src/com/coding/basic/ArrayList.java | 6 +- .../996108220/src/com/coding/basic/Stack.java | 24 --- .../src/com/coding/basic/stack/Stack.java | 40 +++++ .../src/com/coding/basic/stack/StackUtil.java | 131 +++++++++++++++++ .../com/coding/basic/stack/StackUtilTest.java | 82 +++++++++++ 22 files changed, 1027 insertions(+), 48 deletions(-) create mode 100644 group11/996108220/src/com/coderising/jvm/clz/AccessFlag.java create mode 100644 group11/996108220/src/com/coderising/jvm/clz/ClassFile.java create mode 100644 group11/996108220/src/com/coderising/jvm/clz/ClassIndex.java create mode 100644 group11/996108220/src/com/coderising/jvm/constant/ClassInfo.java create mode 100644 group11/996108220/src/com/coderising/jvm/constant/ConstantInfo.java create mode 100644 group11/996108220/src/com/coderising/jvm/constant/ConstantPool.java create mode 100644 group11/996108220/src/com/coderising/jvm/constant/FieldRefInfo.java create mode 100644 group11/996108220/src/com/coderising/jvm/constant/MethodRefInfo.java create mode 100644 group11/996108220/src/com/coderising/jvm/constant/NameAndTypeInfo.java create mode 100644 group11/996108220/src/com/coderising/jvm/constant/NullConstantInfo.java create mode 100644 group11/996108220/src/com/coderising/jvm/constant/StringInfo.java create mode 100644 group11/996108220/src/com/coderising/jvm/constant/UTF8Info.java create mode 100644 group11/996108220/src/com/coderising/jvm/loader/ByteCodeIterator.java create mode 100644 group11/996108220/src/com/coderising/jvm/loader/ClassFileParser.java create mode 100644 group11/996108220/src/com/coderising/jvm/util/Util.java delete mode 100644 group11/996108220/src/com/coding/basic/Stack.java create mode 100644 group11/996108220/src/com/coding/basic/stack/Stack.java create mode 100644 group11/996108220/src/com/coding/basic/stack/StackUtil.java create mode 100644 group11/996108220/src/com/coding/basic/stack/StackUtilTest.java diff --git a/group11/996108220/src/com/coderising/jvm/clz/AccessFlag.java b/group11/996108220/src/com/coderising/jvm/clz/AccessFlag.java new file mode 100644 index 0000000000..faae056835 --- /dev/null +++ b/group11/996108220/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/group11/996108220/src/com/coderising/jvm/clz/ClassFile.java b/group11/996108220/src/com/coderising/jvm/clz/ClassFile.java new file mode 100644 index 0000000000..8575f5641a --- /dev/null +++ b/group11/996108220/src/com/coderising/jvm/clz/ClassFile.java @@ -0,0 +1,74 @@ +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/group11/996108220/src/com/coderising/jvm/clz/ClassIndex.java b/group11/996108220/src/com/coderising/jvm/clz/ClassIndex.java new file mode 100644 index 0000000000..f42e45d48f --- /dev/null +++ b/group11/996108220/src/com/coderising/jvm/clz/ClassIndex.java @@ -0,0 +1,19 @@ +package com.coderising.jvm.clz; + +public class ClassIndex { + private int thisClassIndex;//u2 + private int superClassIndex;//u2 + + 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/group11/996108220/src/com/coderising/jvm/constant/ClassInfo.java b/group11/996108220/src/com/coderising/jvm/constant/ClassInfo.java new file mode 100644 index 0000000000..d673cae1bd --- /dev/null +++ b/group11/996108220/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 ;//u2 + 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/group11/996108220/src/com/coderising/jvm/constant/ConstantInfo.java b/group11/996108220/src/com/coderising/jvm/constant/ConstantInfo.java new file mode 100644 index 0000000000..466b072244 --- /dev/null +++ b/group11/996108220/src/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/group11/996108220/src/com/coderising/jvm/constant/ConstantPool.java b/group11/996108220/src/com/coderising/jvm/constant/ConstantPool.java new file mode 100644 index 0000000000..86c0445695 --- /dev/null +++ b/group11/996108220/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/group11/996108220/src/com/coderising/jvm/constant/FieldRefInfo.java b/group11/996108220/src/com/coderising/jvm/constant/FieldRefInfo.java new file mode 100644 index 0000000000..07133aa3a1 --- /dev/null +++ b/group11/996108220/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;//u2 + private int nameAndTypeIndex;//u2 + + 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/group11/996108220/src/com/coderising/jvm/constant/MethodRefInfo.java b/group11/996108220/src/com/coderising/jvm/constant/MethodRefInfo.java new file mode 100644 index 0000000000..ad3723ad0c --- /dev/null +++ b/group11/996108220/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; //u2 + private int nameAndTypeIndex;//u2 + + 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/group11/996108220/src/com/coderising/jvm/constant/NameAndTypeInfo.java b/group11/996108220/src/com/coderising/jvm/constant/NameAndTypeInfo.java new file mode 100644 index 0000000000..ee05963d21 --- /dev/null +++ b/group11/996108220/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;//u2 + private int index2;//u2 + + 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/group11/996108220/src/com/coderising/jvm/constant/NullConstantInfo.java b/group11/996108220/src/com/coderising/jvm/constant/NullConstantInfo.java new file mode 100644 index 0000000000..936736016f --- /dev/null +++ b/group11/996108220/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/group11/996108220/src/com/coderising/jvm/constant/StringInfo.java b/group11/996108220/src/com/coderising/jvm/constant/StringInfo.java new file mode 100644 index 0000000000..1f0539dda4 --- /dev/null +++ b/group11/996108220/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;//u2 + 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/group11/996108220/src/com/coderising/jvm/constant/UTF8Info.java b/group11/996108220/src/com/coderising/jvm/constant/UTF8Info.java new file mode 100644 index 0000000000..c2c4408421 --- /dev/null +++ b/group11/996108220/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 ;//2 + private String value;//length + 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/group11/996108220/src/com/coderising/jvm/loader/ByteCodeIterator.java b/group11/996108220/src/com/coderising/jvm/loader/ByteCodeIterator.java new file mode 100644 index 0000000000..a151decad8 --- /dev/null +++ b/group11/996108220/src/com/coderising/jvm/loader/ByteCodeIterator.java @@ -0,0 +1,51 @@ +package com.coderising.jvm.loader; + +import java.util.Arrays; +import com.coderising.jvm.util.Util; + + + +public class ByteCodeIterator { + byte[] codes; + int cursor=0; + public ByteCodeIterator(byte[] codes) { + this.codes=codes; + } + public ByteCodeIterator(byte[] codes,int cursor) { + this.codes=codes; + this.cursor=cursor; + } + public boolean hasNext() { + return cursor != codes.length; + } + + + public int next() { + + int i = cursor; + if (i >= codes.length) + throw new ArrayIndexOutOfBoundsException(); + cursor = i + 1; + return codes[i]&0xFF; + } + public int nextU2ToInt() { + + return Util.byteToInt(new byte[]{codes[cursor++],codes[cursor++]}); + } + public int nextU4ToInt() { + + return Util.byteToInt(new byte[]{codes[cursor++],codes[cursor++], + codes[cursor++],codes[cursor++]}); + } + public String nextU4ToHexString() { + return Util.byteToHexString(new byte[]{codes[cursor++],codes[cursor++], + codes[cursor++],codes[cursor++]}); + + } + public byte[] getByte(int length) { + int i=cursor; + cursor=cursor+length; + return Arrays.copyOfRange(codes,i, cursor); + } + +} diff --git a/group11/996108220/src/com/coderising/jvm/loader/ClassFileLoader.java b/group11/996108220/src/com/coderising/jvm/loader/ClassFileLoader.java index 7bde3f4288..eccf6dc7b0 100644 --- a/group11/996108220/src/com/coderising/jvm/loader/ClassFileLoader.java +++ b/group11/996108220/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -1,12 +1,13 @@ package com.coderising.jvm.loader; - +import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; +import com.coderising.jvm.clz.ClassFile; @@ -15,11 +16,23 @@ public class ClassFileLoader { private List clzPaths = new ArrayList(); public byte[] readBinaryCode(String className) { - StringBuffer fileName=new StringBuffer(clzPaths.get(0)); - String nameString[]=className.split("\\."); - fileName.append("\\"); - fileName.append(nameString[nameString.length-1]); - fileName.append(".class"); + + 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 fileName) { ArrayList list=new ArrayList(); try { InputStream in=new FileInputStream(fileName.toString()); @@ -44,10 +57,15 @@ public byte[] readBinaryCode(String className) { byteCodes[i]=list.get(i); } - return byteCodes; + return byteCodes; } - + public ClassFile loadClass(String className) { + byte[] codes = this.readBinaryCode(className); + ClassFileParser parser = new ClassFileParser(); + return parser.parse(codes); + + } public void addClassPath(String path) { clzPaths.add(path); diff --git a/group11/996108220/src/com/coderising/jvm/loader/ClassFileParser.java b/group11/996108220/src/com/coderising/jvm/loader/ClassFileParser.java new file mode 100644 index 0000000000..ac55f25e0e --- /dev/null +++ b/group11/996108220/src/com/coderising/jvm/loader/ClassFileParser.java @@ -0,0 +1,111 @@ +package com.coderising.jvm.loader; +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.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; +import com.coderising.jvm.util.Util; + +public class ClassFileParser { + + public ClassFile parse(byte[] codes) { + ByteCodeIterator iter=new ByteCodeIterator(codes); + String magicNumber=iter.nextU4ToHexString(); + if (!magicNumber.equals("cafebabe")) { + return null; + } + ClassFile classFile=new ClassFile(); + classFile.setMinorVersion(iter.nextU2ToInt()); + classFile.setMajorVersion(iter.nextU2ToInt()); + classFile.setConstPool(parseConstantPool(iter)); + classFile.setAccessFlag(parseAccessFlag(iter)); + classFile.setClassIndex(parseClassInfex(iter)); + return classFile; + } + + private AccessFlag parseAccessFlag(ByteCodeIterator iter) { + + return new AccessFlag(iter.nextU2ToInt()); + } + + private ClassIndex parseClassInfex(ByteCodeIterator iter) { + ClassIndex classIndex=new ClassIndex(); + classIndex.setThisClassIndex(iter.nextU2ToInt()); + classIndex.setSuperClassIndex(iter.nextU2ToInt()); + return classIndex; + + } + + private ConstantPool parseConstantPool(ByteCodeIterator iter) { + int constPoolCount=iter.nextU2ToInt(); + + ConstantPool pool=new ConstantPool(); + pool.addConstantInfo(new NullConstantInfo()); + for (int i = 1; i <= constPoolCount-1; i++) { + int tag=iter.next(); + if (tag==1) { + //UTF8Info + UTF8Info utf8Info=new UTF8Info(pool); + int length=iter.nextU2ToInt(); + utf8Info.setLength(length); + String value=Util.byteToString(iter.getByte(length)); + utf8Info.setValue(value); + pool.addConstantInfo(utf8Info); + } + else if (tag==7) { + //ClassInfo + ClassInfo classInfo=new ClassInfo(pool); + int utf8Index=iter.nextU2ToInt(); + classInfo.setUtf8Index(utf8Index); + pool.addConstantInfo(classInfo); + } + else if (tag==8) { + //StringInfo + StringInfo stringInfo=new StringInfo(pool); + int index=iter.nextU2ToInt(); + stringInfo.setIndex(index); + pool.addConstantInfo(stringInfo); + } + else if (tag==9) { + //FieldRefInfo + FieldRefInfo fieldRefInfo=new FieldRefInfo(pool); + int classInfoIndex=iter.nextU2ToInt(); + int nameAndTypeIndex=iter.nextU2ToInt(); + fieldRefInfo.setClassInfoIndex(classInfoIndex); + fieldRefInfo.setNameAndTypeIndex(nameAndTypeIndex); + pool.addConstantInfo(fieldRefInfo); + } + else if (tag==10) { + //MethodRefInfo + MethodRefInfo methodRefInfo=new MethodRefInfo(pool); + int classInfoIndex=iter.nextU2ToInt(); + int nameAndTypeIndex=iter.nextU2ToInt(); + methodRefInfo.setClassInfoIndex(classInfoIndex); + methodRefInfo.setNameAndTypeIndex(nameAndTypeIndex); + pool.addConstantInfo(methodRefInfo); + } + else if (tag==12) { + //NameAndTypeInfo + NameAndTypeInfo nameAndTypeInfo=new NameAndTypeInfo(pool); + int index1=iter.nextU2ToInt(); + int index2=iter.nextU2ToInt(); + nameAndTypeInfo.setIndex1(index1); + nameAndTypeInfo.setIndex2(index2); + pool.addConstantInfo(nameAndTypeInfo); + } + else { + new RuntimeException("缺少tag为"+tag+"的常量"); + } + } + + return pool; + } + + +} diff --git a/group11/996108220/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group11/996108220/src/com/coderising/jvm/test/ClassFileloaderTest.java index 8fdc5db819..44d000a19a 100644 --- a/group11/996108220/src/com/coderising/jvm/test/ClassFileloaderTest.java +++ b/group11/996108220/src/com/coderising/jvm/test/ClassFileloaderTest.java @@ -4,7 +4,13 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; - +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.MethodRefInfo; +import com.coderising.jvm.constant.NameAndTypeInfo; +import com.coderising.jvm.constant.UTF8Info; import com.coderising.jvm.loader.ClassFileLoader; @@ -14,10 +20,20 @@ public class ClassFileloaderTest { - static String path1 = "F:\\mycoding2017\\group11\\996108220\\bin\\com\\coderising\\jvm\\test"; + private static final String FULL_QUALIFIED_CLASS_NAME = "com/coderising/jvm/test/EmployeeV1"; - static String path2 = "C:\\temp"; + static String path1 = "F:\\mycoding2017\\group11\\996108220\\bin"; + static String path2 = "C:\temp"; + static ClassFile clzFile = null; + static { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + + clzFile = loader.loadClass(className); + //clzFile.print(); + } @Before @@ -73,21 +89,113 @@ public void testMagicNumber(){ + private 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()); + } + + } diff --git a/group11/996108220/src/com/coderising/jvm/util/Util.java b/group11/996108220/src/com/coderising/jvm/util/Util.java new file mode 100644 index 0000000000..c9808b1c1c --- /dev/null +++ b/group11/996108220/src/com/coderising/jvm/util/Util.java @@ -0,0 +1,33 @@ +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;ielementData.length)this.grow(elementData); - else elementData[size++]=o; + else{ + elementData[size]=o; + size++; + } + } /** * 在index处添加元素,index+1到size-1元素向后移动 diff --git a/group11/996108220/src/com/coding/basic/Stack.java b/group11/996108220/src/com/coding/basic/Stack.java deleted file mode 100644 index ea41c87d1b..0000000000 --- a/group11/996108220/src/com/coding/basic/Stack.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.coding.basic; - -public class Stack { - //用动态数组实现栈 - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - elementData.add(elementData.size()); - } - - public Object pop(){ - return elementData.remove(elementData.size()-1); - } - - public Object peek(){ - return elementData.get(elementData.size()-1); - } - public boolean isEmpty(){ - return elementData.size()==0?true:false; - } - public int size(){ - return elementData.size(); - } -} diff --git a/group11/996108220/src/com/coding/basic/stack/Stack.java b/group11/996108220/src/com/coding/basic/stack/Stack.java new file mode 100644 index 0000000000..05f380a304 --- /dev/null +++ b/group11/996108220/src/com/coding/basic/stack/Stack.java @@ -0,0 +1,40 @@ +package com.coding.basic.stack; + +import com.coding.basic.ArrayList; + +public class Stack { + //用动态数组实现栈 + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + return elementData.remove(elementData.size()-1); + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return elementData.size()==0?true:false; + } + public int size(){ + return elementData.size(); + } + public String toString() { + + StringBuffer buffer=new StringBuffer("["); + for (int i = elementData.size()-1; i >=0; i--) { + if (i==0) { + buffer.append(elementData.get(i).toString()+"]"); + } + else { + buffer.append(elementData.get(i).toString()+","); + } + } + return buffer.toString(); + } + +} diff --git a/group11/996108220/src/com/coding/basic/stack/StackUtil.java b/group11/996108220/src/com/coding/basic/stack/StackUtil.java new file mode 100644 index 0000000000..2499f144ea --- /dev/null +++ b/group11/996108220/src/com/coding/basic/stack/StackUtil.java @@ -0,0 +1,131 @@ +package com.coding.basic.stack; + +public class StackUtil { + + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + + reverse(s,s.size()); + } + + private static void reverse(Stack s, int length) { + if (length==1) { + return ; + } + Stack s1=new Stack(); + Object o=s.pop(); + for (int i = 1; i < length; i++) { + s1.push(s.pop()); + } + s.push(o); + for (int i = 1; i < length; i++) { + s.push(s1.pop()); + } + reverse(s, length-1); + + } + + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param o + */ + public static void remove(Stack s,Object o) { + if (s.size()==0) { + return; + } + Stack s1=new Stack(); + while(s.size()!=0) { + if (!s.peek().equals(o)) { + s1.push(s.pop()); + + } + else { + s.pop(); + break; + } + } + while(s1.size()!=0) { + s.push(s1.pop()); + } + + + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param len + * @return + */ + public static Object[] getTop(Stack s,int len) { + if (len>s.size()||len<=0) { + return null; + } + Stack s1=new Stack(); + Object[] array=new Object[len]; + for (int i = 0; i < len; i++) { + Object object=s.pop(); + array[i]=object; + s1.push(object); + } + while(s1.size()!=0) { + s.push(s1.pop()); + } + return array; + } + /** + * 字符串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){ + Stack stack=new Stack(); + for (int i = 0; i < s.length(); i++) { + switch (s.charAt(i)) { + case '(': + stack.push(s.charAt(i)); + break; + case '[': + stack.push(s.charAt(i)); + break; + case '{': + stack.push(s.charAt(i)); + break; + case ')': + if (stack.size()==0||(!stack.pop().equals('('))) { + return false; + } + break; + case ']': + if (stack.size()==0||(!stack.pop().equals('['))) { + return false; + } + break; + case '}': + if (stack.size()==0||(!stack.pop().equals('{'))) { + return false; + } + break; + default: + break; + } + } + if (stack.size()!=0) { + return false; + } + else { + return true; + } + + } + + +} diff --git a/group11/996108220/src/com/coding/basic/stack/StackUtilTest.java b/group11/996108220/src/com/coding/basic/stack/StackUtilTest.java new file mode 100644 index 0000000000..91e3211683 --- /dev/null +++ b/group11/996108220/src/com/coding/basic/stack/StackUtilTest.java @@ -0,0 +1,82 @@ +package com.coding.basic.stack; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class StackUtilTest { + Stack s; + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + } + @Test + public void testToString() { + s=new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + s.push(6); + + Assert.assertEquals("[6,5,4,3,2,1]", s.toString()); + } + + @Test + public void testReverse() { + s=new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + s.push(6); + StackUtil.reverse(s); + //System.out.println(s.size()); + Assert.assertEquals("[1,2,3,4,5,6]", s.toString()); + } + @Test + public void testRemove() { + s=new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + s.push(6); + StackUtil.remove(s, 6); + //System.out.println(s.toString()); + Assert.assertEquals("[5,4,3,2,1]", s.toString()); + } + @Test + public void testGetTop() { + s=new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + s.push(6); + Object[] s1=StackUtil.getTop(s, s.size()); + for (int i = 0; i < s1.length; i++) { + System.out.println(s1[i]); + } + //Assert.assertEquals("[5,6]", s1.toString()); + } + @Test + public void testIsValidPairs() { + String s="([e{df])" ; + Assert.assertEquals(false, StackUtil.isValidPairs(s)); + } + +} From 83a4a6fcb6e8b6ae85d7fb00ef56d25c0eef684d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=9A=E5=90=AF=E7=9B=BC?= Date: Tue, 11 Apr 2017 16:38:30 +0800 Subject: [PATCH 22/41] modify --- .../src/main/java/com/pan/week01/{ => base}/Iterator.java | 2 +- .../src/main/java/com/pan/jvm/loader/ClassFileLoader.java | 2 +- group11/group11.md | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) rename group11/252308879/data-structure/src/main/java/com/pan/week01/{ => base}/Iterator.java (82%) delete mode 100644 group11/group11.md diff --git a/group11/252308879/data-structure/src/main/java/com/pan/week01/Iterator.java b/group11/252308879/data-structure/src/main/java/com/pan/week01/base/Iterator.java similarity index 82% rename from group11/252308879/data-structure/src/main/java/com/pan/week01/Iterator.java rename to group11/252308879/data-structure/src/main/java/com/pan/week01/base/Iterator.java index b59719cd2b..2f4317524e 100644 --- a/group11/252308879/data-structure/src/main/java/com/pan/week01/Iterator.java +++ b/group11/252308879/data-structure/src/main/java/com/pan/week01/base/Iterator.java @@ -1,4 +1,4 @@ -package com.pan.week01; +package com.pan.week01.base; /** * Created by QiPan on 2017/2/23. diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileLoader.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileLoader.java index 1418fa9b83..a21d246399 100644 --- a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileLoader.java +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileLoader.java @@ -15,7 +15,7 @@ public class ClassFileLoader { - private List clzPaths = new ArrayList(); + private List clzPaths = new ArrayList(); public byte[] readBinaryCode(String className) { diff --git a/group11/group11.md b/group11/group11.md deleted file mode 100644 index 8b13789179..0000000000 --- a/group11/group11.md +++ /dev/null @@ -1 +0,0 @@ - From f92d3e31ec87abf06dc991cbb0fd29abe58a2b53 Mon Sep 17 00:00:00 2001 From: xmt <542194147@qq.com> Date: Tue, 11 Apr 2017 16:59:40 +0800 Subject: [PATCH 23/41] =?UTF-8?q?jvm=E8=AF=BB=E5=8F=96=E6=A8=A1=E6=95=B0?= =?UTF-8?q?=E4=BD=9C=E4=B8=9AV2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化完全类名的拼接方法(好吧,就是借鉴老刘的方法) --- .../jvm/loader/ClassFileLoader.java | 20 ++++- .../jvm/test/ClassFileloaderTest.java | 4 +- .../src/com/coding/basic/LRUPageFrame.java | 80 +++++++++++++++---- 3 files changed, 82 insertions(+), 22 deletions(-) diff --git a/group11/542194147/myDataStructure/src/com/coderising/jvm/loader/ClassFileLoader.java b/group11/542194147/myDataStructure/src/com/coderising/jvm/loader/ClassFileLoader.java index 7e19ae9c85..f6b93bfb41 100644 --- a/group11/542194147/myDataStructure/src/com/coderising/jvm/loader/ClassFileLoader.java +++ b/group11/542194147/myDataStructure/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -16,9 +16,20 @@ public class ClassFileLoader { private static final int BUFFER_SIZE=1024; public byte[] readBinaryCode(String className) { - className=className.replace(".","\\"); - StringBuffer sb=new StringBuffer(); - String absolutePath=sb.append(clzPaths.get(0)).append("\\").append(className).append(".class").toString(); + className=className.replace(".","\\")+".class"; + String absolutePath=null; + for(int i=0;i=capacity){//缓存已满删除最后的数据 + removeLastNode(); } + addNewNodeTOHead(newNode);//把新数据加到缓存头部 } - - } + private void addNewNodeTOHead(Node node) { + if(first==null&&last==null){ + first=node; + last=node; + node.next=null; + node.prev=null; + }else{ + first.prev=node; + node.prev=null; + node.next=first; + first=node; + } + currentSize++; + } + + private void removeLastNode() { + Node node=last.prev; + node.next=null; + last.prev=null; + last=node; + currentSize--; + } + + private void moveExistingNodeToHead(Node node) { + if(node==first){ + return; + }else if(node==last){ + Node prevNode=node.prev; + last=prevNode; + prevNode.next=null; + }else{ + node.prev.next=node.next; + node.next.prev=node.prev; + } + node.prev=null; + node.next=first; + first.prev=node; + first=node; + } + + private Node findNode(int data) { + Node node=first; + while(node!=null){ + if(node.pageNum==data){ + return node; + } + node=node.next; + } + return null; + } public String toString(){ StringBuilder buffer = new StringBuilder(); From 885ca6574e66dad9afec3509005e32c402e6a787 Mon Sep 17 00:00:00 2001 From: PanPan Date: Tue, 11 Apr 2017 16:59:43 +0800 Subject: [PATCH 24/41] Add files via upload --- group11/252308879/pom.xml | 140 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 group11/252308879/pom.xml diff --git a/group11/252308879/pom.xml b/group11/252308879/pom.xml new file mode 100644 index 0000000000..642387b95a --- /dev/null +++ b/group11/252308879/pom.xml @@ -0,0 +1,140 @@ + + + + + + data-structure + mini-jvm + + + com.pan + 252308879 + pom + 1.0.0-SNAPSHOT + + 4.0.0 + + + UTF-8 + 1.8 + 1.8 + 3.3 + 2.4 + 2.5 + 2.5 + 2.8.2 + 2.3.1 + 2.5 + 2.12.2 + + + + + + junit + junit + 4.12 + test + + + dom4j + dom4j + 1.6.1 + + + org.apache.commons + commons-lang3 + 3.4 + + + commons-io + commons-io + 2.4 + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven_compiler_plugin_version} + + ${java_source_version} + ${java_target_version} + ${project.build.sourceEncoding} + + + + + + org.apache.maven.plugins + maven-surefire-report-plugin + ${maven_surefire_report_plugin} + + false + + + + + + org.apache.maven.plugins + maven-source-plugin + ${maven_source_plugin_version} + + + attach-sources + + jar-no-fork + + + + + true + + + + + org.apache.maven.plugins + maven-jar-plugin + ${maven_jar_plugin_version} + + + true + + + + + + + org.apache.maven.plugins + maven-war-plugin + ${maven_war_plugin_version} + + + true + + + + + + org.codehaus.mojo + findbugs-maven-plugin + ${findbugs_maven_plugin_version} + + true + target/site + + + + org.apache.maven.plugins + maven-checkstyle-plugin + ${maven_checkstyle_plugin_version} + + + + \ No newline at end of file From d214704ce93117fedf1465b5841f802d03c3d6a8 Mon Sep 17 00:00:00 2001 From: PanPan Date: Tue, 11 Apr 2017 17:02:20 +0800 Subject: [PATCH 25/41] Add files via upload --- group11/252308879/data-structure/pom.xml | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 group11/252308879/data-structure/pom.xml diff --git a/group11/252308879/data-structure/pom.xml b/group11/252308879/data-structure/pom.xml new file mode 100644 index 0000000000..6277256487 --- /dev/null +++ b/group11/252308879/data-structure/pom.xml @@ -0,0 +1,25 @@ + + 4.0.0 + + com.pan + data-structure + 1.0.0-SNAPSHOT + jar + + data-structure + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 3.8.1 + test + + + From 00c2662a25e75846fb001cf5bf958ad7b287310b Mon Sep 17 00:00:00 2001 From: PanPan Date: Tue, 11 Apr 2017 17:03:33 +0800 Subject: [PATCH 26/41] Create pom.xml --- group11/252308879/mini-jvm/pom.xml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 group11/252308879/mini-jvm/pom.xml diff --git a/group11/252308879/mini-jvm/pom.xml b/group11/252308879/mini-jvm/pom.xml new file mode 100644 index 0000000000..687a89a5dc --- /dev/null +++ b/group11/252308879/mini-jvm/pom.xml @@ -0,0 +1,25 @@ + + 4.0.0 + + com.pan + data-structure + 1.0.0-SNAPSHOT + jar + + data-structure + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 3.8.1 + test + + + From c75c75d865cbd67227e0ed8042c6464b5f74411f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=9A=E5=90=AF=E7=9B=BC?= Date: Tue, 11 Apr 2017 17:14:28 +0800 Subject: [PATCH 27/41] parent --- group11/252308879/data-structure/pom.xml | 46 ++++++++++++--------- group11/252308879/mini-jvm/pom.xml | 51 +++++++++++++++--------- 2 files changed, 59 insertions(+), 38 deletions(-) diff --git a/group11/252308879/data-structure/pom.xml b/group11/252308879/data-structure/pom.xml index 6277256487..8725d8517b 100644 --- a/group11/252308879/data-structure/pom.xml +++ b/group11/252308879/data-structure/pom.xml @@ -1,25 +1,33 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - com.pan - data-structure - 1.0.0-SNAPSHOT - jar + + com.pan + 252308879 + 1.0.0-SNAPSHOT + - data-structure - http://maven.apache.org + com.pan + data-structure + 1.0.0-SNAPSHOT + jar - - UTF-8 - + data-structure + http://maven.apache.org - - - junit - junit - 3.8.1 - test - - + + UTF-8 + + + + + junit + junit + + + dom4j + dom4j + + diff --git a/group11/252308879/mini-jvm/pom.xml b/group11/252308879/mini-jvm/pom.xml index 687a89a5dc..d1bd424bc7 100644 --- a/group11/252308879/mini-jvm/pom.xml +++ b/group11/252308879/mini-jvm/pom.xml @@ -1,25 +1,38 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - com.pan - data-structure - 1.0.0-SNAPSHOT - jar + + com.pan + 252308879 + 1.0.0-SNAPSHOT + - data-structure - http://maven.apache.org + com.pan + mini-jvm + 1.0.0-SNAPSHOT + jar - - UTF-8 - + mini-jvm + http://maven.apache.org - - - junit - junit - 3.8.1 - test - - + + UTF-8 + + + + + junit + junit + + + + org.apache.commons + commons-lang3 + + + commons-io + commons-io + + From 8afbe08dcf65dcbc45b4dbe56acadd5e52e65290 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=9A=E5=90=AF=E7=9B=BC?= Date: Tue, 11 Apr 2017 17:19:40 +0800 Subject: [PATCH 28/41] clean and compile --- group11/252308879/data-structure/pom.xml | 36 ++++++++++++++++++++++++ group11/252308879/mini-jvm/pom.xml | 35 +++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/group11/252308879/data-structure/pom.xml b/group11/252308879/data-structure/pom.xml index 8725d8517b..445ba45741 100644 --- a/group11/252308879/data-structure/pom.xml +++ b/group11/252308879/data-structure/pom.xml @@ -30,4 +30,40 @@ dom4j + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + + org.apache.maven.plugins + maven-surefire-report-plugin + + + + + org.apache.maven.plugins + maven-source-plugin + + + + org.apache.maven.plugins + maven-jar-plugin + + + + org.codehaus.mojo + findbugs-maven-plugin + + + org.apache.maven.plugins + maven-checkstyle-plugin + + + diff --git a/group11/252308879/mini-jvm/pom.xml b/group11/252308879/mini-jvm/pom.xml index d1bd424bc7..9487a16199 100644 --- a/group11/252308879/mini-jvm/pom.xml +++ b/group11/252308879/mini-jvm/pom.xml @@ -35,4 +35,39 @@ commons-io + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + + org.apache.maven.plugins + maven-surefire-report-plugin + + + + + org.apache.maven.plugins + maven-source-plugin + + + + org.apache.maven.plugins + maven-jar-plugin + + + + org.codehaus.mojo + findbugs-maven-plugin + + + org.apache.maven.plugins + maven-checkstyle-plugin + + + From 8d670e65341038f06f2ff71a5d10ad3084c7523b Mon Sep 17 00:00:00 2001 From: x_zhaohu Date: Tue, 11 Apr 2017 18:20:54 +0800 Subject: [PATCH 29/41] update error --- group11/1178243325/week03/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/group11/1178243325/week03/readme.md b/group11/1178243325/week03/readme.md index e418f46fd6..5de5dada98 100644 --- a/group11/1178243325/week03/readme.md +++ b/group11/1178243325/week03/readme.md @@ -3,7 +3,7 @@ - 17-03-09:TDD和第二次作业讲解 - 17-03-12:职场15年 -## 第二周作业(3-6 至 3-12) +## 第三周作业(3-6 至 3-12) - 实现第二个大作业:多线程下载文件,支持断点续传 - 5道数据结构习题 From 2c7a3cfe00d7fa16e6ba171aa76fa0f0ae7ad5e4 Mon Sep 17 00:00:00 2001 From: x_zhaohu Date: Tue, 11 Apr 2017 18:21:14 +0800 Subject: [PATCH 30/41] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group11/1178243325/week06/readme.md | 13 +- .../java/com/sprint/basic/LRUPageFrame.java | 117 ++++++++++++++++++ .../java/com/sprint/jvm/clz/AccessFlag.java | 25 ---- .../java/com/sprint/jvm/clz/ClassFile.java | 70 +---------- .../java/com/sprint/jvm/clz/ClassIndex.java | 22 ---- .../com/sprint/jvm/constant/ClassInfo.java | 28 ----- .../com/sprint/jvm/constant/ConstantInfo.java | 29 ----- .../com/sprint/jvm/constant/ConstantPool.java | 28 ----- .../com/sprint/jvm/constant/UTF8Info.java | 35 ------ .../sprint/jvm/loader/ByteCodeIterator.java | 12 ++ .../sprint/jvm/loader/ClassFileLoader.java | 39 +++--- .../sprint/jvm/loader/ClassFileParser.java | 25 ++-- .../main/java/com/sprint/jvm/util/Util.java | 17 +++ .../com/sprint/basic/LRUPageFrameTest.java | 33 +++++ .../jvm/loader/ClassFileLoaderTest.java | 11 +- 15 files changed, 224 insertions(+), 280 deletions(-) create mode 100644 group11/1178243325/week06/src/main/java/com/sprint/basic/LRUPageFrame.java delete mode 100644 group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/AccessFlag.java delete mode 100644 group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/ClassIndex.java delete mode 100644 group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/ClassInfo.java delete mode 100644 group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/ConstantInfo.java delete mode 100644 group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/ConstantPool.java delete mode 100644 group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/UTF8Info.java create mode 100644 group11/1178243325/week06/src/main/java/com/sprint/jvm/util/Util.java create mode 100644 group11/1178243325/week06/src/test/java/com/sprint/basic/LRUPageFrameTest.java diff --git a/group11/1178243325/week06/readme.md b/group11/1178243325/week06/readme.md index d32158bcdf..d89a5c904e 100644 --- a/group11/1178243325/week06/readme.md +++ b/group11/1178243325/week06/readme.md @@ -1,11 +1,16 @@ ## 讲课内容: -- 17-03-29 :JVM之classLoader +- 17-03-27:JVM第一周 +- 17-03-29:JVM之classLoader ## 第六周作业(3-27 至 04-02) -读取常量池 +- 完成对一个.class文件的读取和对.class文件开头四个字节的魔数的判断需要实现ClassLoader.java +- 实现LRU算法 +- 一篇文章 ## 完成情况: -未补 +- ClassLoader.java已完 +- LRU已完 +- [文章](http://www.jianshu.com/p/02a8b4ee4596) ## 我的收获: - +使用开源工具读取文件,并利用工作类进行字符转换.LRU算法(双链表操作),但是更重要的抽象化思想. diff --git a/group11/1178243325/week06/src/main/java/com/sprint/basic/LRUPageFrame.java b/group11/1178243325/week06/src/main/java/com/sprint/basic/LRUPageFrame.java new file mode 100644 index 0000000000..0a2febd715 --- /dev/null +++ b/group11/1178243325/week06/src/main/java/com/sprint/basic/LRUPageFrame.java @@ -0,0 +1,117 @@ +package com.sprint.basic; + +public class LRUPageFrame { + + /** + * 用双向链表实现LRU算法 + */ + + private static class Node { + Node prev; + Node next; + int pageNum; + + Node() { + + } + } + + private int capacity; + private int currentSize; + private Node first; + private Node last; + + public LRUPageFrame(int capacity) { + this.currentSize = 0; + this.capacity = capacity; + } + + /** + * + */ + public void access(int pageNum) { + Node node = find(pageNum); + if (node != null) { + moveExistingNodeToHead(node); + } else { + node = new Node(); + node.pageNum = pageNum; + if (currentSize >= capacity) { + removeLast(); + } + addNewNodeToHead(node); + } + } + + private Node find(int pageNum) { + Node node = first; + while (node != null) { + if (node.pageNum == pageNum) { + return node; + } + node = node.next; + } + return null; + } + + private void moveExistingNodeToHead(Node node) { + if (node == first) { + return; + } else if (node == last) { + Node prevNode = node.prev; + prevNode.next = null; + last.prev = null; + last = prevNode; + } else { + Node prevNode = node.prev; + prevNode.next = node.next; + Node nextNode = node.next; + nextNode.prev = prevNode; + } + + node.prev = null; + node.next = first; + first.prev = node; + first = node; + } + + private void removeLast() { + Node prev = last.prev; + prev.next = null; + last.prev = null; + last = prev; + this.currentSize--; + } + + private void addNewNodeToHead(Node node) { + if (isEmpty()) { + node.prev = null; + node.next = null; + first = node; + last = node; + } else { + node.prev = null; + node.next = first; + first.prev = node; + first = node; + } + this.currentSize++; + } + + private boolean isEmpty() { + return (first == null) && (last == null); + } + @Override + public String toString() { + StringBuilder buffer = new StringBuilder(); + Node node = first; + while (node != null) { + buffer.append(node.pageNum); + node = node.next; + if (node != null) { + buffer.append(","); + } + } + return buffer.toString(); + } +} diff --git a/group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/AccessFlag.java b/group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/AccessFlag.java deleted file mode 100644 index e74740aa3e..0000000000 --- a/group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/AccessFlag.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.sprint.jvm.clz; - -public class AccessFlag { - private int flagValue; - - public AccessFlag(int value) { - this.flagValue = value; - } - - public int getFlagValue() { - return flagValue; - } - - public void setFlagValue(int flagValue) { - this.flagValue = flagValue; - } - - public boolean isPublicClass() { - return (this.flagValue & 0x0001) != 0; - } - - public boolean isFinalClass() { - return (this.flagValue & 0x0010) != 0; - } -} diff --git a/group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/ClassFile.java b/group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/ClassFile.java index 637cd63592..c40c88b183 100644 --- a/group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/ClassFile.java +++ b/group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/ClassFile.java @@ -1,71 +1,9 @@ package com.sprint.jvm.clz; -import com.sprint.jvm.constant.ConstantPool; -import com.sprint.jvm.constant.ClassInfo; public class ClassFile { - private int minorVersion; - private int majorVersion; - - private AccessFlag accessFlag; - private ClassIndex clzIndex; - private ConstantPool 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 AccessFlag getAccessFlag() { - return accessFlag; - } - - public void setAccessFlag(AccessFlag accessFlag) { - this.accessFlag = accessFlag; - } - - public ClassIndex getClzIndex() { - return clzIndex; - } - - public void setClzIndex(ClassIndex clzIndex) { - this.clzIndex = clzIndex; - } - - public ConstantPool getConstantPool() { - return pool; - } - - public void setConstantPool(ConstantPool pool) { - this.pool = pool; - } - - 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(); + private final String cafebabe = "cafebabe"; + + public String getCafebabe() { + return cafebabe; } } diff --git a/group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/ClassIndex.java b/group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/ClassIndex.java deleted file mode 100644 index 46e7443d90..0000000000 --- a/group11/1178243325/week06/src/main/java/com/sprint/jvm/clz/ClassIndex.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.sprint.jvm.clz; - -public class ClassIndex { - private int thisClassIndex; - private int superClassIndex; - - public int getThisClassIndex() { - return thisClassIndex; - } - - public void setThisClassIndex(int index) { - this.thisClassIndex = index; - } - - public int getSuperClassIndex() { - return superClassIndex; - } - - public void setSuperClassIndex(int index) { - this.superClassIndex = index; - } -} diff --git a/group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/ClassInfo.java b/group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/ClassInfo.java deleted file mode 100644 index b8da3c656d..0000000000 --- a/group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/ClassInfo.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.sprint.jvm.constant; - -public class ClassInfo extends ConstantInfo { - private int type = ConstantInfo.CLASS_INFO; - private int utf8Index; - - public ClassInfo(ConstantPool pool) { - super(pool); - } - - public void setUtf8Index(int index) { - this.utf8Index = index; - } - - public int getUtf8Index() { - return utf8Index; - } - - public int getType() { - return type; - } - - public String getClassName() { - int index = getUtf8Index(); - UTF8Info utf8Info = (UTF8Info)constantPool.getConstantInfo(index); - return utf8Info.getValue(); - } -} diff --git a/group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/ConstantInfo.java b/group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/ConstantInfo.java deleted file mode 100644 index a8db82689e..0000000000 --- a/group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/ConstantInfo.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.sprint.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/group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/ConstantPool.java b/group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/ConstantPool.java deleted file mode 100644 index 3a35c22ce0..0000000000 --- a/group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/ConstantPool.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.sprint.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/group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/UTF8Info.java b/group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/UTF8Info.java deleted file mode 100644 index 5516999c0e..0000000000 --- a/group11/1178243325/week06/src/main/java/com/sprint/jvm/constant/UTF8Info.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.sprint.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; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } - - @Override - public String toString() { - return "UTF8Info [type=" + type + ", length=" + length + ", value=" + value + "]"; - } -} diff --git a/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ByteCodeIterator.java b/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ByteCodeIterator.java index 3144d5828d..445519b3be 100644 --- a/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ByteCodeIterator.java +++ b/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ByteCodeIterator.java @@ -1,5 +1,17 @@ package com.sprint.jvm.loader; +import com.sprint.jvm.util.Util; +import java.util.Arrays; public class ByteCodeIterator { + byte[] codes; + int pos = 0; + ByteCodeIterator(byte[] codes) { + this.codes = codes; + } + + public String nextU4ToHexString() { + return Util.byteToHexString(new byte[] {codes[pos++], codes[pos++], codes[pos++], codes[pos++]}); + } + } diff --git a/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ClassFileLoader.java b/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ClassFileLoader.java index f190f54915..1191be47eb 100644 --- a/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ClassFileLoader.java +++ b/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ClassFileLoader.java @@ -10,10 +10,24 @@ import org.apache.commons.lang3.StringUtils; public class ClassFileLoader { - private List clzPaths = new ArrayList(); - public byte[] readBinaryCode(String className) { + List clzPaths = new ArrayList<>(); + + public ClassFile loadClass(String className) { + byte[] codes = this.readBinaryCode(className); + ClassFileParser parser = new ClassFileParser(); + return parser.parse(codes); + } + + public void addClassPath(String clzPath) { + if (this.clzPaths.contains(clzPath)) { + return; + } + this.clzPaths.add(clzPath); + } + + private byte[] readBinaryCode(String className) { className = className.replace('.', File.separatorChar) + ".class"; - for (String path : this.clzPaths) { + for (String path : clzPaths) { String clzFileName = path + File.separatorChar + className; byte[] codes = loadClassFile(clzFileName); if (codes != null) { @@ -27,26 +41,9 @@ private byte[] loadClassFile(String clzFileName) { File f = new File(clzFileName); try { return IOUtils.toByteArray(new FileInputStream(f)); - } catch(IOException e) { + } 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); - } } diff --git a/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ClassFileParser.java b/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ClassFileParser.java index 4c32858fa4..229e7db360 100644 --- a/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ClassFileParser.java +++ b/group11/1178243325/week06/src/main/java/com/sprint/jvm/loader/ClassFileParser.java @@ -1,25 +1,14 @@ package com.sprint.jvm.loader; import com.sprint.jvm.clz.ClassFile; -import com.sprint.jvm.clz.AccessFlag; -import com.sprint.jvm.clz.ClassIndex; -import com.sprint.jvm.constant.ConstantPool; public class ClassFileParser { public ClassFile parse(byte[] codes) { - return null; + ClassFile clzFile = new ClassFile(); + ByteCodeIterator iter = new ByteCodeIterator(codes); + String magicNumber = iter.nextU4ToHexString(); + if (!"cafebabe".equals(magicNumber)) { + return null; + } + return clzFile; } - - private AccessFlag parseAccessFlag(ByteCodeIterator iter) { - return null; - } - - private ClassIndex parseClassIndex(ByteCodeIterator iter) { - return null; - } - - private ConstantPool parseConstantPool(ByteCodeIterator iter) { - return null; - } - - } diff --git a/group11/1178243325/week06/src/main/java/com/sprint/jvm/util/Util.java b/group11/1178243325/week06/src/main/java/com/sprint/jvm/util/Util.java new file mode 100644 index 0000000000..6081635316 --- /dev/null +++ b/group11/1178243325/week06/src/main/java/com/sprint/jvm/util/Util.java @@ -0,0 +1,17 @@ +package com.sprint.jvm.util; + +public class Util { + public static String byteToHexString(byte[] codes) { + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < codes.length; i++) { + byte b = codes[i]; + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if (strHex.length() < 2) { + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } +} diff --git a/group11/1178243325/week06/src/test/java/com/sprint/basic/LRUPageFrameTest.java b/group11/1178243325/week06/src/test/java/com/sprint/basic/LRUPageFrameTest.java new file mode 100644 index 0000000000..ac28668dbe --- /dev/null +++ b/group11/1178243325/week06/src/test/java/com/sprint/basic/LRUPageFrameTest.java @@ -0,0 +1,33 @@ +package com.sprint.basic; +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + frame.access(5); + Assert.assertEquals("5,4,0", frame.toString()); + + } + +} diff --git a/group11/1178243325/week06/src/test/java/com/sprint/jvm/loader/ClassFileLoaderTest.java b/group11/1178243325/week06/src/test/java/com/sprint/jvm/loader/ClassFileLoaderTest.java index 6795595e89..d3d84b414f 100644 --- a/group11/1178243325/week06/src/test/java/com/sprint/jvm/loader/ClassFileLoaderTest.java +++ b/group11/1178243325/week06/src/test/java/com/sprint/jvm/loader/ClassFileLoaderTest.java @@ -7,14 +7,17 @@ public class ClassFileLoaderTest { private static final String FULL_QUALTFIED_CLASS_NAME = "com/sprint/jvm/EmployeeV1"; - static String path1 = ""; - static String path2 = ""; + static String path1 = "/home/sprint/java/code/coding2017/group11/1178243325/week06/build/classes/test"; static ClassFile clzFile = null; static { ClassFileLoader loader = new ClassFileLoader(); loader.addClassPath(path1); - String className = "com.sprint.jvm.EmployeeV1"; + String className = "com.sprint.jvm.loader.EmployeeV1"; clzFile = loader.loadClass(className); - clzFile.print(); + } + + @Test + public void test() { + Assert.assertEquals("cafebabe", clzFile.getCafebabe()); } } From a260cd7a7f7c141fe312dd9f0ce8558415507f88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=9A=E5=90=AF=E7=9B=BC?= Date: Tue, 11 Apr 2017 19:15:04 +0800 Subject: [PATCH 31/41] jvm --- ...ileLoader.java => ClassFileLoaderTmp.java} | 2 +- .../src/main/java/com/pan/jvm/EmployeeV1.java | 31 +++++++++++++++++ .../com/pan/jvm/loader/ClassFileLoader.java | 26 -------------- .../java/com/pan/alg/LRUPageFrameTest.java | 34 +++++++++++++++++++ .../src/test/java/com/pan/jvm/EmployeeV1.java | 31 +++++++++++++++++ .../test/java/com/pan/jvm/TestReadCFBB.java | 31 +++++++++++++++++ 6 files changed, 128 insertions(+), 27 deletions(-) rename group11/252308879/mini-jvm/src/main/java/com/pan/jvm/{ClassFileLoader.java => ClassFileLoaderTmp.java} (97%) create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/EmployeeV1.java create mode 100644 group11/252308879/mini-jvm/src/test/java/com/pan/alg/LRUPageFrameTest.java create mode 100644 group11/252308879/mini-jvm/src/test/java/com/pan/jvm/EmployeeV1.java diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/ClassFileLoader.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/ClassFileLoaderTmp.java similarity index 97% rename from group11/252308879/mini-jvm/src/main/java/com/pan/jvm/ClassFileLoader.java rename to group11/252308879/mini-jvm/src/main/java/com/pan/jvm/ClassFileLoaderTmp.java index 0961c5ddd5..340f6599c6 100644 --- a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/ClassFileLoader.java +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/ClassFileLoaderTmp.java @@ -7,7 +7,7 @@ import java.util.ArrayList; import java.util.List; -public class ClassFileLoader { +public class ClassFileLoaderTmp { private List clzPaths = new ArrayList(); int countForClassPath = 0; int countForReadBinaryCode = 0; diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/EmployeeV1.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/EmployeeV1.java new file mode 100644 index 0000000000..92b5b403ca --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/EmployeeV1.java @@ -0,0 +1,31 @@ +package com.pan.jvm; + +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/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileLoader.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileLoader.java index a21d246399..c1d0553522 100644 --- a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileLoader.java +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileLoader.java @@ -18,31 +18,21 @@ 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; @@ -54,9 +44,7 @@ public void addClassPath(String path) { if (this.clzPaths.contains(path)) { return; } - this.clzPaths.add(path); - } @@ -74,7 +62,6 @@ public ClassFile loadClass(String className) { // ------------------------------backup------------------------ public String getClassPath_V1() { - StringBuffer buffer = new StringBuffer(); for (int i = 0; i < this.clzPaths.size(); i++) { buffer.append(this.clzPaths.get(i)); @@ -88,31 +75,19 @@ public String getClassPath_V1() { 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 { @@ -123,7 +98,6 @@ private byte[] loadClassFile_V1(String clzFileName) { } } return null; - } diff --git a/group11/252308879/mini-jvm/src/test/java/com/pan/alg/LRUPageFrameTest.java b/group11/252308879/mini-jvm/src/test/java/com/pan/alg/LRUPageFrameTest.java new file mode 100644 index 0000000000..8f6803c11f --- /dev/null +++ b/group11/252308879/mini-jvm/src/test/java/com/pan/alg/LRUPageFrameTest.java @@ -0,0 +1,34 @@ +package com.pan.alg; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + frame.access(5); + Assert.assertEquals("5,4,0", frame.toString()); + + } + +} diff --git a/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/EmployeeV1.java b/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/EmployeeV1.java new file mode 100644 index 0000000000..92b5b403ca --- /dev/null +++ b/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/EmployeeV1.java @@ -0,0 +1,31 @@ +package com.pan.jvm; + +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/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/TestReadCFBB.java b/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/TestReadCFBB.java index 5523fa7696..8590e78c09 100644 --- a/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/TestReadCFBB.java +++ b/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/TestReadCFBB.java @@ -1,10 +1,41 @@ package com.pan.jvm; +import com.pan.jvm.loader.ClassFileLoader; +import org.junit.Test; + +import java.io.IOException; + /** * 用于测试第一次JVM作业,读取.class作业 和 魔幻数字 */ public class TestReadCFBB { + @Test + public void testClassPath(){ + ClassFileLoader classFileLoader = new ClassFileLoader(); + String path = ClassFileLoader.class.getClassLoader().getResource("").getPath(); + path = path.replace("test-classes", "classes"); + classFileLoader.addClassPath(path); + classFileLoader.addClassPath("d://tmp"); + + String clzPath = classFileLoader.getClassPath(); + System.out.println(clzPath); + } + + + + @Test + public void testReadCFBB() throws IOException { + ClassFileLoader classFileLoader = new ClassFileLoader(); + String path = ClassFileLoader.class.getClassLoader().getResource("").getPath(); + path = path.replace("test-classes", "classes"); + classFileLoader.addClassPath(path); + byte[] bytes = classFileLoader.readBinaryCode("com.pan.jvm.loader.ClassFileLoader"); + for (byte b : bytes) { + String toHexString = Integer.toHexString(b & 0xFF).toUpperCase(); + System.out.print(toHexString + " "); + } + } } From f8e5fb284f90436a1553a7ddebba3f58956f3ee6 Mon Sep 17 00:00:00 2001 From: x_zhaohu Date: Tue, 11 Apr 2017 21:41:00 +0800 Subject: [PATCH 32/41] =?UTF-8?q?=E7=AC=AC=E4=BA=94=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group11/1178243325/week07/.readme.md.swp | Bin 0 -> 12288 bytes group11/1178243325/week07/build.gradle | 13 ++ group11/1178243325/week07/readme.md | 12 ++ .../java/com/sprint/jvm/clz/AccessFlag.java | 25 +++ .../java/com/sprint/jvm/clz/ClassFile.java | 71 +++++++ .../java/com/sprint/jvm/clz/ClassIndex.java | 22 +++ .../com/sprint/jvm/constant/ClassInfo.java | 28 +++ .../com/sprint/jvm/constant/ConstantInfo.java | 29 +++ .../com/sprint/jvm/constant/ConstantPool.java | 28 +++ .../com/sprint/jvm/constant/FieldRefInfo.java | 52 +++++ .../sprint/jvm/constant/MethodRefInfo.java | 55 ++++++ .../sprint/jvm/constant/NameAndTypeInfo.java | 52 +++++ .../sprint/jvm/constant/NullConstantInfo.java | 12 ++ .../com/sprint/jvm/constant/StringInfo.java | 26 +++ .../com/sprint/jvm/constant/UTF8Info.java | 35 ++++ .../sprint/jvm/loader/ByteCodeIterator.java | 50 +++++ .../sprint/jvm/loader/ClassFileLoader.java | 52 +++++ .../sprint/jvm/loader/ClassFileParser.java | 107 +++++++++++ .../main/java/com/sprint/jvm/util/Util.java | 22 +++ .../jvm/loader/ClassFileLoaderTest.java | 181 ++++++++++++++++++ .../com/sprint/jvm/loader/EmployeeV1.java | 28 +++ 21 files changed, 900 insertions(+) create mode 100644 group11/1178243325/week07/.readme.md.swp create mode 100644 group11/1178243325/week07/build.gradle create mode 100644 group11/1178243325/week07/readme.md create mode 100644 group11/1178243325/week07/src/main/java/com/sprint/jvm/clz/AccessFlag.java create mode 100644 group11/1178243325/week07/src/main/java/com/sprint/jvm/clz/ClassFile.java create mode 100644 group11/1178243325/week07/src/main/java/com/sprint/jvm/clz/ClassIndex.java create mode 100644 group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/ClassInfo.java create mode 100644 group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/ConstantInfo.java create mode 100644 group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/ConstantPool.java create mode 100644 group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/FieldRefInfo.java create mode 100644 group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/MethodRefInfo.java create mode 100644 group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/NameAndTypeInfo.java create mode 100644 group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/NullConstantInfo.java create mode 100644 group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/StringInfo.java create mode 100644 group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/UTF8Info.java create mode 100644 group11/1178243325/week07/src/main/java/com/sprint/jvm/loader/ByteCodeIterator.java create mode 100644 group11/1178243325/week07/src/main/java/com/sprint/jvm/loader/ClassFileLoader.java create mode 100644 group11/1178243325/week07/src/main/java/com/sprint/jvm/loader/ClassFileParser.java create mode 100644 group11/1178243325/week07/src/main/java/com/sprint/jvm/util/Util.java create mode 100644 group11/1178243325/week07/src/test/java/com/sprint/jvm/loader/ClassFileLoaderTest.java create mode 100644 group11/1178243325/week07/src/test/java/com/sprint/jvm/loader/EmployeeV1.java diff --git a/group11/1178243325/week07/.readme.md.swp b/group11/1178243325/week07/.readme.md.swp new file mode 100644 index 0000000000000000000000000000000000000000..3d7e4464a84ed43e0b59dc8f2f80c33ee769a7a0 GIT binary patch literal 12288 zcmeI&J#W)M7zgkx8;Cd2nPTbzI>cu?iQMoFTG0tLLP89OZQ3+VVkJ(q@uI#Y5~!sT z>JTB+2_dKwUPN)!mQMg98>7!L5~yE+#}F8hS}_9ppY->|cjwb{zs+)P==q5$#nLj2 z+G)le5P8ddxpUc z!x}d;>9m>U4c9H|7O%R(DY;t7IlKzYLI45>3e^3ZsqqsmZ5n#AMjbt`44;`ka5*L* z009U<00Izz00bZaf&Wjy_lDSQI@XcF(T)%Lq2Kz!EjkE500Izz00bZa0SG_<0uX=z z1pYw*L0{GPPBM1%6ctb3|9`&!f2I2ml=qY;lm&`IF)8OLBb2k0W0d6C3lua6KmY;| zfB*y_009U<00Izzz~3yOs!XMS#HvcvTJN`)qKDh@>f8O@Hq}gnvU5LbJ%|>U<;t66 zFr{3JHk$qCo7XSS`o4FG^G2hg%@stsHe1*1N{M?stDCmrW^EyI))+k@JCFOTFRu8) zE>8P}`ClWl+Y5s`Vem||=S8hX`-VH)RDukG&&{>y+ZRwemo>t=KYbE)W8)LMsVbNI1%qdjqWPU2_-TS`r<%)3JYO+n-c@=lx$(3c< I=_RN624bkC)c^nh literal 0 HcmV?d00001 diff --git a/group11/1178243325/week07/build.gradle b/group11/1178243325/week07/build.gradle new file mode 100644 index 0000000000..128f6fda07 --- /dev/null +++ b/group11/1178243325/week07/build.gradle @@ -0,0 +1,13 @@ +apply plugin: 'java' + +repositories { + mavenCentral() +} + +dependencies { + compile("commons-io:commons-io:2.4") + //compile("commons-lang:commons-lang:2.6") + compile("org.apache.commons:commons-lang3:3.4") + testCompile("junit:junit:4.12") +} + diff --git a/group11/1178243325/week07/readme.md b/group11/1178243325/week07/readme.md new file mode 100644 index 0000000000..cc9d7c6f3c --- /dev/null +++ b/group11/1178243325/week07/readme.md @@ -0,0 +1,12 @@ +## 讲课内容: +- 17-03-29 :JVM之classLoader + +## 第七周作业(04-03 至 04-09) +- 实现对一个.class文件的常量池读取 +- 实现StackUtil +- [文章](http://www.jianshu.com/p/502c1e5caa97) +## 完成情况: + + +## 我的收获: + diff --git a/group11/1178243325/week07/src/main/java/com/sprint/jvm/clz/AccessFlag.java b/group11/1178243325/week07/src/main/java/com/sprint/jvm/clz/AccessFlag.java new file mode 100644 index 0000000000..e74740aa3e --- /dev/null +++ b/group11/1178243325/week07/src/main/java/com/sprint/jvm/clz/AccessFlag.java @@ -0,0 +1,25 @@ +package com.sprint.jvm.clz; + +public class AccessFlag { + private int flagValue; + + public AccessFlag(int value) { + this.flagValue = value; + } + + public int getFlagValue() { + return flagValue; + } + + public void setFlagValue(int flagValue) { + this.flagValue = flagValue; + } + + public boolean isPublicClass() { + return (this.flagValue & 0x0001) != 0; + } + + public boolean isFinalClass() { + return (this.flagValue & 0x0010) != 0; + } +} diff --git a/group11/1178243325/week07/src/main/java/com/sprint/jvm/clz/ClassFile.java b/group11/1178243325/week07/src/main/java/com/sprint/jvm/clz/ClassFile.java new file mode 100644 index 0000000000..4e5aad16f9 --- /dev/null +++ b/group11/1178243325/week07/src/main/java/com/sprint/jvm/clz/ClassFile.java @@ -0,0 +1,71 @@ +package com.sprint.jvm.clz; + +import com.sprint.jvm.constant.ConstantPool; +import com.sprint.jvm.constant.ClassInfo; +public class ClassFile { + private int minorVersion; + private int majorVersion; + + private AccessFlag accessFlag; + private ClassIndex clzIndex; + private ConstantPool 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 AccessFlag getAccessFlag() { + return accessFlag; + } + + public void setAccessFlag(AccessFlag accessFlag) { + this.accessFlag = accessFlag; + } + + public ClassIndex getClassIndex() { + return clzIndex; + } + + public void setClassIndex(ClassIndex clzIndex) { + this.clzIndex = clzIndex; + } + + public ConstantPool getConstantPool() { + return pool; + } + + public void setConstantPool(ConstantPool pool) { + this.pool = pool; + } + + 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/group11/1178243325/week07/src/main/java/com/sprint/jvm/clz/ClassIndex.java b/group11/1178243325/week07/src/main/java/com/sprint/jvm/clz/ClassIndex.java new file mode 100644 index 0000000000..46e7443d90 --- /dev/null +++ b/group11/1178243325/week07/src/main/java/com/sprint/jvm/clz/ClassIndex.java @@ -0,0 +1,22 @@ +package com.sprint.jvm.clz; + +public class ClassIndex { + private int thisClassIndex; + private int superClassIndex; + + public int getThisClassIndex() { + return thisClassIndex; + } + + public void setThisClassIndex(int index) { + this.thisClassIndex = index; + } + + public int getSuperClassIndex() { + return superClassIndex; + } + + public void setSuperClassIndex(int index) { + this.superClassIndex = index; + } +} diff --git a/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/ClassInfo.java b/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/ClassInfo.java new file mode 100644 index 0000000000..b8da3c656d --- /dev/null +++ b/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/ClassInfo.java @@ -0,0 +1,28 @@ +package com.sprint.jvm.constant; + +public class ClassInfo extends ConstantInfo { + private int type = ConstantInfo.CLASS_INFO; + private int utf8Index; + + public ClassInfo(ConstantPool pool) { + super(pool); + } + + public void setUtf8Index(int index) { + this.utf8Index = index; + } + + public int getUtf8Index() { + return utf8Index; + } + + public int getType() { + return type; + } + + public String getClassName() { + int index = getUtf8Index(); + UTF8Info utf8Info = (UTF8Info)constantPool.getConstantInfo(index); + return utf8Info.getValue(); + } +} diff --git a/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/ConstantInfo.java b/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/ConstantInfo.java new file mode 100644 index 0000000000..a8db82689e --- /dev/null +++ b/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/ConstantInfo.java @@ -0,0 +1,29 @@ +package com.sprint.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/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/ConstantPool.java b/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/ConstantPool.java new file mode 100644 index 0000000000..3a35c22ce0 --- /dev/null +++ b/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/ConstantPool.java @@ -0,0 +1,28 @@ +package com.sprint.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/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/FieldRefInfo.java b/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/FieldRefInfo.java new file mode 100644 index 0000000000..c0eb449085 --- /dev/null +++ b/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/FieldRefInfo.java @@ -0,0 +1,52 @@ +package com.sprint.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/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/MethodRefInfo.java b/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/MethodRefInfo.java new file mode 100644 index 0000000000..85bb5c4934 --- /dev/null +++ b/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/MethodRefInfo.java @@ -0,0 +1,55 @@ +package com.sprint.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/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/NameAndTypeInfo.java b/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/NameAndTypeInfo.java new file mode 100644 index 0000000000..492fd6e0db --- /dev/null +++ b/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/NameAndTypeInfo.java @@ -0,0 +1,52 @@ +package com.sprint.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 getType() { + return type; + } + + 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 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/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/NullConstantInfo.java b/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/NullConstantInfo.java new file mode 100644 index 0000000000..f257cea240 --- /dev/null +++ b/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/NullConstantInfo.java @@ -0,0 +1,12 @@ +package com.sprint.jvm.constant; + +public class NullConstantInfo extends ConstantInfo { + public NullConstantInfo() { + + } + + @Override + public int getType() { + return -1; + } +} diff --git a/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/StringInfo.java b/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/StringInfo.java new file mode 100644 index 0000000000..73c58a5e71 --- /dev/null +++ b/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/StringInfo.java @@ -0,0 +1,26 @@ +package com.sprint.jvm.constant; + +public class StringInfo extends ConstantInfo { + private int type = ConstantInfo.STRING_INFO; + private int index; + + public StringInfo(ConstantPool constantPool) { + super(constantPool); + } + + 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/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/UTF8Info.java b/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/UTF8Info.java new file mode 100644 index 0000000000..5516999c0e --- /dev/null +++ b/group11/1178243325/week07/src/main/java/com/sprint/jvm/constant/UTF8Info.java @@ -0,0 +1,35 @@ +package com.sprint.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; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + @Override + public String toString() { + return "UTF8Info [type=" + type + ", length=" + length + ", value=" + value + "]"; + } +} diff --git a/group11/1178243325/week07/src/main/java/com/sprint/jvm/loader/ByteCodeIterator.java b/group11/1178243325/week07/src/main/java/com/sprint/jvm/loader/ByteCodeIterator.java new file mode 100644 index 0000000000..fc5d1b2ac2 --- /dev/null +++ b/group11/1178243325/week07/src/main/java/com/sprint/jvm/loader/ByteCodeIterator.java @@ -0,0 +1,50 @@ +package com.sprint.jvm.loader; + +import com.sprint.jvm.util.Util; +import java.util.Arrays; +public class ByteCodeIterator { + byte[] codes; + int pos = 0; + ByteCodeIterator(byte[] codes) { + this.codes = codes; + } + + public byte[] getBytes(int len) { + if (pos + len >= codes.length) { + throw new ArrayIndexOutOfBoundsException(); + } + byte[] data = Arrays.copyOfRange(codes, pos, pos + len); + pos += len; + return data; + } + + public int nextU1ToInt() { + return Util.byteToInt(new byte[] {codes[pos++]}); + } + + public int nextU2ToInt() { + return Util.byteToInt(new byte[] {codes[pos++], codes[pos++]}); + } + + public int nextU4ToInt() { + return Util.byteToInt(new byte[] {codes[pos++], codes[pos++], codes[pos++], codes[pos++]}); + } + + public String nextU4ToHexString() { + return Util.byteToHexString(new byte[] {codes[pos++], codes[pos++], codes[pos++], codes[pos++]}); + } + + public String nextUxToHexString(int len) { + byte[] tmp = new byte[len]; + for (int i = 0; i < len; i++) { + tmp[i] = codes[pos++]; + } + return Util.byteToHexString(tmp).toLowerCase(); + } + + public void back(int n) { + this.pos -= n; + } + + +} diff --git a/group11/1178243325/week07/src/main/java/com/sprint/jvm/loader/ClassFileLoader.java b/group11/1178243325/week07/src/main/java/com/sprint/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..f190f54915 --- /dev/null +++ b/group11/1178243325/week07/src/main/java/com/sprint/jvm/loader/ClassFileLoader.java @@ -0,0 +1,52 @@ +package com.sprint.jvm.loader; + +import com.sprint.jvm.clz.ClassFile; +import java.io.IOException; +import java.io.File; +import java.io.FileInputStream; +import java.util.List; +import java.util.ArrayList; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; + +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); + } +} diff --git a/group11/1178243325/week07/src/main/java/com/sprint/jvm/loader/ClassFileParser.java b/group11/1178243325/week07/src/main/java/com/sprint/jvm/loader/ClassFileParser.java new file mode 100644 index 0000000000..4b77190a04 --- /dev/null +++ b/group11/1178243325/week07/src/main/java/com/sprint/jvm/loader/ClassFileParser.java @@ -0,0 +1,107 @@ +package com.sprint.jvm.loader; + +import com.sprint.jvm.clz.ClassFile; +import com.sprint.jvm.clz.AccessFlag; +import com.sprint.jvm.clz.ClassIndex; +import com.sprint.jvm.constant.ClassInfo; +import com.sprint.jvm.constant.ConstantPool; +import com.sprint.jvm.constant.FieldRefInfo; +import com.sprint.jvm.constant.NameAndTypeInfo; +import com.sprint.jvm.constant.NullConstantInfo; +import com.sprint.jvm.constant.MethodRefInfo; +import com.sprint.jvm.constant.StringInfo; +import com.sprint.jvm.constant.UTF8Info; +import java.io.UnsupportedEncodingException; +public class ClassFileParser { + public ClassFile parse(byte[] codes) { + ClassFile clzFile = new ClassFile(); + ByteCodeIterator iter = new ByteCodeIterator(codes); + String magicNumber = iter.nextU4ToHexString(); + if (!"cafebabe".equals(magicNumber)) { + return null; + } + clzFile.setMinorVersion(iter.nextU2ToInt()); + System.out.println("minor:" + clzFile.getMinorVersion()); + clzFile.setMajorVersion(iter.nextU2ToInt()); + System.out.println("marjor:" + clzFile.getMajorVersion()); + ConstantPool pool = parseConstantPool(iter); + clzFile.setConstantPool(pool); + + AccessFlag flag = parseAccessFlag(iter); + clzFile.setAccessFlag(flag); + + ClassIndex clzIndex = parseClassIndex(iter); + clzFile.setClassIndex(clzIndex); + return clzFile; + } + + private AccessFlag parseAccessFlag(ByteCodeIterator iter) { + AccessFlag flag = new AccessFlag(iter.nextU2ToInt()); + return flag; + } + + private ClassIndex parseClassIndex(ByteCodeIterator iter) { + int thisClassIndex = iter.nextU2ToInt(); + int superClassIndex = iter.nextU2ToInt(); + + ClassIndex clzIndex = new ClassIndex(); + clzIndex.setThisClassIndex(thisClassIndex); + clzIndex.setSuperClassIndex(superClassIndex); + return clzIndex; + } + + private ConstantPool parseConstantPool(ByteCodeIterator iter) { + int constPoolCount = iter.nextU2ToInt(); + System.out.println("Constant Pool Count :" + constPoolCount); + ConstantPool pool = new ConstantPool(); + pool.addConstantInfo(new NullConstantInfo()); + for (int i = 1; i <= constPoolCount - 1; i++) { + int tag = iter.nextU1ToInt(); + if (tag == 7) { + int utf8Index = iter.nextU2ToInt(); + ClassInfo clzInfo = new ClassInfo(pool); + clzInfo.setUtf8Index(utf8Index); + pool.addConstantInfo(clzInfo); + } else if (tag == 1) { + int len = iter.nextU2ToInt(); + byte[] data = iter.getBytes(len); + String value = null; + try { + value = new String(data, "UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + + UTF8Info utf8Str = new UTF8Info(pool); + utf8Str.setLength(len); + utf8Str.setValue(value); + pool.addConstantInfo(utf8Str); + } else if (tag == 8) { + StringInfo info = new StringInfo(pool); + info.setIndex(iter.nextU2ToInt()); + pool.addConstantInfo(info); + } else if (tag == 9) { + FieldRefInfo field = new FieldRefInfo(pool); + field.setClassInfoIndex(iter.nextU2ToInt()); + field.setNameAndTypeIndex(iter.nextU2ToInt()); + pool.addConstantInfo(field); + } else if (tag == 10) { + MethodRefInfo method = new MethodRefInfo(pool); + method.setClassInfoIndex(iter.nextU2ToInt()); + method.setNameAndTypeIndex(iter.nextU2ToInt()); + pool.addConstantInfo(method); + } else if (tag == 12) { + NameAndTypeInfo nameType = new NameAndTypeInfo(pool); + nameType.setIndex1(iter.nextU2ToInt()); + nameType.setIndex2(iter.nextU2ToInt()); + pool.addConstantInfo(nameType); + } else { + throw new RuntimeException("the constant pool tag:" + tag + "has no been implemented yet."); + } + } + System.out.println("Finished reading Constant Pool"); + return pool; + } + + +} diff --git a/group11/1178243325/week07/src/main/java/com/sprint/jvm/util/Util.java b/group11/1178243325/week07/src/main/java/com/sprint/jvm/util/Util.java new file mode 100644 index 0000000000..0f5dc89626 --- /dev/null +++ b/group11/1178243325/week07/src/main/java/com/sprint/jvm/util/Util.java @@ -0,0 +1,22 @@ +package com.sprint.jvm.util; + +public class Util { + public static int byteToInt(byte[] codes) { + String s1 = byteToHexString(codes); + return Integer.valueOf(s1, 16).intValue(); + } + + public static String byteToHexString(byte[] codes) { + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < codes.length; i++) { + byte b = codes[i]; + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if (strHex.length() < 2) { + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } +} diff --git a/group11/1178243325/week07/src/test/java/com/sprint/jvm/loader/ClassFileLoaderTest.java b/group11/1178243325/week07/src/test/java/com/sprint/jvm/loader/ClassFileLoaderTest.java new file mode 100644 index 0000000000..5cbc24577e --- /dev/null +++ b/group11/1178243325/week07/src/test/java/com/sprint/jvm/loader/ClassFileLoaderTest.java @@ -0,0 +1,181 @@ +package com.sprint.jvm.loader; + + +import com.sprint.jvm.clz.ClassFile; +import com.sprint.jvm.clz.ClassIndex; +import com.sprint.jvm.constant.*; +import org.junit.Test; +import org.junit.Assert; +public class ClassFileLoaderTest { + + private static final String FULL_QUALIFIED_CLASS_NAME = "com/sprint/jvm/loader/EmployeeV1"; + static String path1 = "/home/sprint/java/code/coding2017/group11/1178243325/week07/build/classes/test"; + static String path2 = "/home/sprint/xxx"; + static ClassFile clzFile = null; + static { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.sprint.jvm.loader.EmployeeV1"; + clzFile = loader.loadClass(className); + clzFile.print(); + } + + + @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.sprint.jvm.loader.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1050, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.sprint.jvm.loader.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", 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.getClassIndex(); + 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/group11/1178243325/week07/src/test/java/com/sprint/jvm/loader/EmployeeV1.java b/group11/1178243325/week07/src/test/java/com/sprint/jvm/loader/EmployeeV1.java new file mode 100644 index 0000000000..6b8532842b --- /dev/null +++ b/group11/1178243325/week07/src/test/java/com/sprint/jvm/loader/EmployeeV1.java @@ -0,0 +1,28 @@ +package com.sprint.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", 20); + p.sayHello(); + } +} From f62dd151fcd2d8acc346944da62822732807a877 Mon Sep 17 00:00:00 2001 From: Pan Date: Tue, 11 Apr 2017 22:37:29 +0800 Subject: [PATCH 33/41] jvm --- .../java/com/pan/jvm/attr/AttributeInfo.java | 19 ++ .../main/java/com/pan/jvm/attr/CodeAttr.java | 56 ++++ .../com/pan/jvm/attr/LineNumberTable.java | 42 +++ .../com/pan/jvm/attr/LocalVariableItem.java | 39 +++ .../com/pan/jvm/attr/LocalVariableTable.java | 28 ++ .../java/com/pan/jvm/attr/StackMapTable.java | 30 ++ .../main/java/com/pan/jvm/clz/AccessFlag.java | 37 ++- .../main/java/com/pan/jvm/clz/ClassFile.java | 158 ++++++----- .../main/java/com/pan/jvm/clz/ClassIndex.java | 33 +-- .../main/java/com/pan/jvm/field/Field.java | 32 +++ .../com/pan/jvm/loader/ByteCodeIterator.java | 54 +++- .../main/java/com/pan/jvm/method/Method.java | 53 ++++ .../java/com/pan/jvm/ClassFileloaderTest.java | 260 ++++++++++++++++++ 13 files changed, 729 insertions(+), 112 deletions(-) create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/AttributeInfo.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/CodeAttr.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/LineNumberTable.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/LocalVariableItem.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/LocalVariableTable.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/StackMapTable.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/field/Field.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/jvm/method/Method.java create mode 100644 group11/252308879/mini-jvm/src/test/java/com/pan/jvm/ClassFileloaderTest.java diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/AttributeInfo.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/AttributeInfo.java new file mode 100644 index 0000000000..1a81e8b473 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/AttributeInfo.java @@ -0,0 +1,19 @@ +package com.pan.jvm.attr; + +public abstract class AttributeInfo { + public static final String CODE = "Code"; + public static final String CONST_VALUE = "ConstantValue"; + public static final String EXCEPTIONS = "Exceptions"; + public static final String LINE_NUM_TABLE = "LineNumberTable"; + public static final String LOCAL_VAR_TABLE = "LocalVariableTable"; + public static final String STACK_MAP_TABLE = "StackMapTable"; + int attrNameIndex; + int attrLen ; + public AttributeInfo(int attrNameIndex, int attrLen) { + + this.attrNameIndex = attrNameIndex; + this.attrLen = attrLen; + } + + +} diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/CodeAttr.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/CodeAttr.java new file mode 100644 index 0000000000..08bfa18f85 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/CodeAttr.java @@ -0,0 +1,56 @@ +package com.pan.jvm.attr; + +import com.pan.jvm.clz.ClassFile; +import com.pan.jvm.constant.ConstantPool; +import com.pan.jvm.loader.ByteCodeIterator; + + +public class CodeAttr extends AttributeInfo { + private int maxStack ; + private int maxLocals ; + private int codeLen ; + private String code; + public String getCode() { + return code; + } + + //private ByteCodeCommand[] cmds ; + //public ByteCodeCommand[] getCmds() { + // return cmds; + //} + private LineNumberTable lineNumTable; + private LocalVariableTable localVarTable; + private StackMapTable stackMapTable; + + public CodeAttr(int attrNameIndex, int attrLen, int maxStack, int maxLocals, int codeLen,String code /*ByteCodeCommand[] cmds*/) { + super(attrNameIndex, attrLen); + this.maxStack = maxStack; + this.maxLocals = maxLocals; + this.codeLen = codeLen; + this.code = code; + //this.cmds = cmds; + } + + public void setLineNumberTable(LineNumberTable t) { + this.lineNumTable = t; + } + + public void setLocalVariableTable(LocalVariableTable t) { + this.localVarTable = t; + } + + public static CodeAttr parse(ClassFile clzFile, ByteCodeIterator iter){ + + + return null; + } + private void setStackMapTable(StackMapTable t) { + this.stackMapTable = t; + + } + + + + + +} diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/LineNumberTable.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/LineNumberTable.java new file mode 100644 index 0000000000..31d2ab3d7e --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/LineNumberTable.java @@ -0,0 +1,42 @@ +package com.pan.jvm.attr; + +import java.util.ArrayList; +import java.util.List; + +import com.pan.jvm.loader.ByteCodeIterator; + +public class LineNumberTable extends AttributeInfo { + List items = new ArrayList(); + + private static class LineNumberItem{ + int startPC; + int lineNum; + public int getStartPC() { + return startPC; + } + public void setStartPC(int startPC) { + this.startPC = startPC; + } + public int getLineNum() { + return lineNum; + } + public void setLineNum(int lineNum) { + this.lineNum = lineNum; + } + } + public void addLineNumberItem(LineNumberItem item){ + this.items.add(item); + } + public LineNumberTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + + } + + public static LineNumberTable parse(ByteCodeIterator iter){ + + return null; + } + + + +} diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/LocalVariableItem.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/LocalVariableItem.java new file mode 100644 index 0000000000..7e202a3931 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/LocalVariableItem.java @@ -0,0 +1,39 @@ +package com.pan.jvm.attr; + +public class LocalVariableItem { + private int startPC; + private int length; + private int nameIndex; + private int descIndex; + private int index; + public int getStartPC() { + return startPC; + } + public void setStartPC(int startPC) { + this.startPC = startPC; + } + public int getLength() { + return length; + } + public void setLength(int length) { + this.length = length; + } + public int getNameIndex() { + return nameIndex; + } + public void setNameIndex(int nameIndex) { + this.nameIndex = nameIndex; + } + public int getDescIndex() { + return descIndex; + } + public void setDescIndex(int descIndex) { + this.descIndex = descIndex; + } + public int getIndex() { + return index; + } + public void setIndex(int index) { + this.index = index; + } +} diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/LocalVariableTable.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/LocalVariableTable.java new file mode 100644 index 0000000000..745f1e77d2 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/LocalVariableTable.java @@ -0,0 +1,28 @@ +package com.pan.jvm.attr; + + +import java.util.ArrayList; +import java.util.List; + +import com.pan.jvm.constant.ConstantPool; + +import com.pan.jvm.loader.ByteCodeIterator; + +public class LocalVariableTable extends AttributeInfo{ + + List items = new ArrayList(); + + public LocalVariableTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + } + + public static LocalVariableTable parse(ByteCodeIterator iter){ + + return null; + } + private void addLocalVariableItem(LocalVariableItem item) { + this.items.add(item); + } + + +} diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/StackMapTable.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/StackMapTable.java new file mode 100644 index 0000000000..8d7c32b4bd --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/StackMapTable.java @@ -0,0 +1,30 @@ +package com.pan.jvm.attr; + + +import com.pan.jvm.loader.ByteCodeIterator; + +public class StackMapTable extends AttributeInfo{ + + private String originalCode; + + public StackMapTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + } + + public static StackMapTable parse(ByteCodeIterator iter){ + int index = iter.nextU2ToInt(); + int len = iter.nextU4ToInt(); + StackMapTable t = new StackMapTable(index,len); + + //后面的StackMapTable太过复杂, 不再处理, 只把原始的代码读进来保存 + String code = iter.nextUxToHexString(len); + t.setOriginalCode(code); + + return t; + } + + private void setOriginalCode(String code) { + this.originalCode = code; + + } +} diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/AccessFlag.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/AccessFlag.java index 8c253df200..045741bd1e 100644 --- a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/AccessFlag.java +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/AccessFlag.java @@ -1,26 +1,25 @@ package com.pan.jvm.clz; public class AccessFlag { - private int flagValue; + private int flagValue; - public AccessFlag(int value) { - this.flagValue = value; - } + 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; - } + 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/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/ClassFile.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/ClassFile.java index a192ad0c82..2b3a185ac6 100644 --- a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/ClassFile.java +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/ClassFile.java @@ -1,82 +1,92 @@ package com.pan.jvm.clz; +import java.util.ArrayList; +import java.util.List; import com.pan.jvm.constant.ClassInfo; import com.pan.jvm.constant.ConstantPool; +import com.pan.jvm.field.Field; +import com.pan.jvm.method.Method; 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(); - } + + private int minorVersion; + private int majorVersion; + + private AccessFlag accessFlag; + private ClassIndex clzIndex; + private ConstantPool pool; + private List fields = new ArrayList(); + private List methods = new ArrayList(); + + public ClassIndex getClzIndex() { + return clzIndex; + } + public AccessFlag getAccessFlag() { + return accessFlag; + } + public void setAccessFlag(AccessFlag accessFlag) { + this.accessFlag = accessFlag; + } + + + + public ConstantPool getConstantPool() { + return pool; + } + public int getMinorVersion() { + return minorVersion; + } + public void setMinorVersion(int minorVersion) { + this.minorVersion = minorVersion; + } + public int getMajorVersion() { + return majorVersion; + } + public void setMajorVersion(int majorVersion) { + this.majorVersion = majorVersion; + } + public void setConstPool(ConstantPool pool) { + this.pool = pool; + + } + public void setClassIndex(ClassIndex clzIndex) { + this.clzIndex = clzIndex; + } + + public void addField(Field f){ + this.fields.add(f); + } + public List getFields(){ + return this.fields; + } + public void addMethod(Method m){ + this.methods.add(m); + } + public List getMethods() { + return methods; + } + + + public void print(){ + + if(this.accessFlag.isPublicClass()){ + System.out.println("Access flag : public "); + } + System.out.println("Class Name:"+ getClassName()); + + System.out.println("Super Class Name:"+ getSuperClassName()); + + + } + + private String getClassName(){ + int thisClassIndex = this.clzIndex.getThisClassIndex(); + ClassInfo thisClass = (ClassInfo)this.getConstantPool().getConstantInfo(thisClassIndex); + return thisClass.getClassName(); + } + private String getSuperClassName(){ + ClassInfo superClass = (ClassInfo)this.getConstantPool().getConstantInfo(this.clzIndex.getSuperClassIndex()); + return superClass.getClassName(); + } } diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/ClassIndex.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/ClassIndex.java index 563592ac47..e09d6f0076 100644 --- a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/ClassIndex.java +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/clz/ClassIndex.java @@ -1,22 +1,19 @@ package com.pan.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; - } + 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/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/field/Field.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/field/Field.java new file mode 100644 index 0000000000..3fc8da4cdf --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/field/Field.java @@ -0,0 +1,32 @@ +package com.pan.jvm.field; + + +import com.pan.jvm.constant.ConstantPool; +import com.pan.jvm.loader.ByteCodeIterator; + +public class Field { + private int accessFlag; + private int nameIndex; + private int descriptorIndex; + + + + private ConstantPool pool; + + public Field( int accessFlag, int nameIndex, int descriptorIndex,ConstantPool pool) { + + this.accessFlag = accessFlag; + this.nameIndex = nameIndex; + this.descriptorIndex = descriptorIndex; + this.pool = pool; + } + + + + + public static Field parse(ConstantPool pool,ByteCodeIterator iter){ + + return null; + } + +} diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ByteCodeIterator.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ByteCodeIterator.java index f808720ef9..c0f9a6eaa5 100644 --- a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ByteCodeIterator.java +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ByteCodeIterator.java @@ -1,5 +1,57 @@ package com.pan.jvm.loader; -public class ByteCodeIterator { +import java.util.Arrays; +import com.pan.jvm.util.Util; + +public class ByteCodeIterator { + byte[] codes; + int pos = 0; + + ByteCodeIterator(byte[] codes) { + this.codes = codes; + } + + + + public byte[] getBytes(int len) { + if (pos + len >= codes.length) { + throw new ArrayIndexOutOfBoundsException(); + } + + byte[] data = Arrays.copyOfRange(codes, pos, pos + len); + pos += len; + return data; + } + + public int nextU1toInt() { + + return Util.byteToInt(new byte[] { codes[pos++] }); + } + + public int nextU2ToInt() { + return Util.byteToInt(new byte[] { codes[pos++], codes[pos++] }); + } + + public int nextU4ToInt() { + return Util.byteToInt(new byte[] { codes[pos++], codes[pos++], codes[pos++], codes[pos++] }); + } + + public String nextU4ToHexString() { + return Util.byteToHexString((new byte[] { codes[pos++], codes[pos++], codes[pos++], codes[pos++] })); + } + + public String nextUxToHexString(int len) { + byte[] tmp = new byte[len]; + + for (int i = 0; i < len; i++) { + tmp[i] = codes[pos++]; + } + return Util.byteToHexString(tmp).toLowerCase(); + + } + + public void back(int n) { + this.pos -= n; + } } diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/method/Method.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/method/Method.java new file mode 100644 index 0000000000..ec3561a566 --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/method/Method.java @@ -0,0 +1,53 @@ +package com.pan.jvm.method; + + +import com.pan.jvm.attr.CodeAttr; +import com.pan.jvm.clz.ClassFile; +import com.pan.jvm.loader.ByteCodeIterator; + +public class Method { + + private int accessFlag; + private int nameIndex; + private int descriptorIndex; + + private CodeAttr codeAttr; + + private ClassFile clzFile; + + + public ClassFile getClzFile() { + return clzFile; + } + + public int getNameIndex() { + return nameIndex; + } + public int getDescriptorIndex() { + return descriptorIndex; + } + + public CodeAttr getCodeAttr() { + return codeAttr; + } + + public void setCodeAttr(CodeAttr code) { + this.codeAttr = code; + } + + public Method(ClassFile clzFile,int accessFlag, int nameIndex, int descriptorIndex) { + this.clzFile = clzFile; + this.accessFlag = accessFlag; + this.nameIndex = nameIndex; + this.descriptorIndex = descriptorIndex; + } + + + + + + public static Method parse(ClassFile clzFile, ByteCodeIterator iter){ + return null; + + } +} diff --git a/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/ClassFileloaderTest.java b/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/ClassFileloaderTest.java new file mode 100644 index 0000000000..4bcb38e917 --- /dev/null +++ b/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/ClassFileloaderTest.java @@ -0,0 +1,260 @@ +package com.pan.jvm; + +import java.util.List; + +import com.pan.jvm.clz.ClassFile; +import com.pan.jvm.clz.ClassIndex; +import com.pan.jvm.constant.*; +import com.pan.jvm.field.Field; +import com.pan.jvm.loader.ClassFileLoader; +import com.pan.jvm.method.Method; +import com.pan.jvm.util.Util; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + + + + +public class ClassFileloaderTest { + + + private static final String FULL_QUALIFIED_CLASS_NAME = "com/coderising/jvm/test/EmployeeV1"; + + static String path1 = EmployeeV1.class.getClassLoader().getResource("").getPath() + .replace("test-classes", "classes"); + static String path2 = "C:\temp"; + +// static ClassFile clzFile = null; +// static { +// ClassFileLoader loader = new ClassFileLoader(); +// loader.addClassPath(path1); +// String className = "com.pan.jvm.EmployeeV1"; +// +// clzFile = loader.loadClass(className); +// clzFile.print(); +// } + + + @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.pan.jvm.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1032, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.pan.jvm.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = Util.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + /** + * ---------------------------------------------------------------------- + */ +// +// +// @Test +// public void testVersion(){ +// +// Assert.assertEquals(0, clzFile.getMinorVersion()); +// Assert.assertEquals(52, clzFile.getMajorVersion()); +// +// } +// +// @Test +// public void testConstantPool(){ +// +// +// ConstantPool pool = clzFile.getConstantPool(); +// +// Assert.assertEquals(53, pool.getSize()); +// +// { +// ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(1); +// Assert.assertEquals(2, clzInfo.getUtf8Index()); +// +// UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(2); +// Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, utf8Info.getValue()); +// } +// { +// ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(3); +// Assert.assertEquals(4, clzInfo.getUtf8Index()); +// +// UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(4); +// Assert.assertEquals("java/lang/Object", utf8Info.getValue()); +// } +// { +// UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(5); +// Assert.assertEquals("name", utf8Info.getValue()); +// +// utf8Info = (UTF8Info) pool.getConstantInfo(6); +// Assert.assertEquals("Ljava/lang/String;", utf8Info.getValue()); +// +// utf8Info = (UTF8Info) pool.getConstantInfo(7); +// Assert.assertEquals("age", utf8Info.getValue()); +// +// utf8Info = (UTF8Info) pool.getConstantInfo(8); +// Assert.assertEquals("I", utf8Info.getValue()); +// +// utf8Info = (UTF8Info) pool.getConstantInfo(9); +// Assert.assertEquals("", utf8Info.getValue()); +// +// utf8Info = (UTF8Info) pool.getConstantInfo(10); +// Assert.assertEquals("(Ljava/lang/String;I)V", utf8Info.getValue()); +// +// utf8Info = (UTF8Info) pool.getConstantInfo(11); +// Assert.assertEquals("Code", utf8Info.getValue()); +// } +// +// { +// MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(12); +// Assert.assertEquals(3, methodRef.getClassInfoIndex()); +// Assert.assertEquals(13, methodRef.getNameAndTypeIndex()); +// } +// +// { +// NameAndTypeInfo nameAndType = (NameAndTypeInfo) pool.getConstantInfo(13); +// Assert.assertEquals(9, nameAndType.getIndex1()); +// Assert.assertEquals(14, nameAndType.getIndex2()); +// } +// //抽查几个吧 +// { +// MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(45); +// Assert.assertEquals(1, methodRef.getClassInfoIndex()); +// Assert.assertEquals(46, methodRef.getNameAndTypeIndex()); +// } +// +// { +// UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(53); +// Assert.assertEquals("EmployeeV1.java", utf8Info.getValue()); +// } +// } +// @Test +// public void testClassIndex(){ +// +// ClassIndex clzIndex = clzFile.getClzIndex(); +// ClassInfo thisClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getThisClassIndex()); +// ClassInfo superClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getSuperClassIndex()); +// +// +// Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, thisClassInfo.getClassName()); +// Assert.assertEquals("java/lang/Object", superClassInfo.getClassName()); +// } +// +// /** +// * 下面是第三次JVM课应实现的测试用例 +// */ +// @Test +// public void testReadFields(){ +// +// List fields = clzFile.getFields(); +// Assert.assertEquals(2, fields.size()); +// { +// Field f = fields.get(0); +// Assert.assertEquals("name:Ljava/lang/String;", f.toString()); +// } +// { +// Field f = fields.get(1); +// Assert.assertEquals("age:I", f.toString()); +// } +// } +// @Test +// public void testMethods(){ +// +// List methods = clzFile.getMethods(); +// ConstantPool pool = clzFile.getConstantPool(); +// +// { +// Method m = methods.get(0); +// assertMethodEquals(pool,m, +// "", +// "(Ljava/lang/String;I)V", +// "2ab7000c2a2bb5000f2a1cb50011b1"); +// +// } +// { +// Method m = methods.get(1); +// assertMethodEquals(pool,m, +// "setName", +// "(Ljava/lang/String;)V", +// "2a2bb5000fb1"); +// +// } +// { +// Method m = methods.get(2); +// assertMethodEquals(pool,m, +// "setAge", +// "(I)V", +// "2a1bb50011b1"); +// } +// { +// Method m = methods.get(3); +// assertMethodEquals(pool,m, +// "sayHello", +// "()V", +// "b2001c1222b60024b1"); +// +// } +// { +// Method m = methods.get(4); +// assertMethodEquals(pool,m, +// "main", +// "([Ljava/lang/String;)V", +// "bb000159122b101db7002d4c2bb6002fb1"); +// } +// } +// +// private void assertMethodEquals(ConstantPool pool,Method m , String expectedName, String expectedDesc,String expectedCode){ +// String methodName = pool.getUTF8String(m.getNameIndex()); +// String methodDesc = pool.getUTF8String(m.getDescriptorIndex()); +// String code = m.getCodeAttr().getCode(); +// Assert.assertEquals(expectedName, methodName); +// Assert.assertEquals(expectedDesc, methodDesc); +// Assert.assertEquals(expectedCode, code); +// } + + +} From 8aafd6a0b95fa06f4a933c59ceef332212330f90 Mon Sep 17 00:00:00 2001 From: GZ-RXP <283091182@qq.com> Date: Tue, 11 Apr 2017 23:35:12 +0800 Subject: [PATCH 34/41] Jvm first week - Magical Number Data Structure - LRU --- .../jvm/loader/ClassFileLoader.java | 93 ++++++++++++ .../jvm/test/ClassFileloaderTest.java | 92 ++++++++++++ .../com/coderising/jvm/test/EmployeeV1.java | 28 ++++ .../coding/basic/linklist/LRUPageFrame.java | 141 ++++++++++++++++++ .../basic/linklist/LRUPageFrameTest.java | 31 ++++ 5 files changed, 385 insertions(+) create mode 100644 group11/283091182/mini-jvm/com/coderising/jvm/loader/ClassFileLoader.java create mode 100644 group11/283091182/mini-jvm/com/coderising/jvm/test/ClassFileloaderTest.java create mode 100644 group11/283091182/mini-jvm/com/coderising/jvm/test/EmployeeV1.java create mode 100644 group11/283091182/src/com/coding/basic/linklist/LRUPageFrame.java create mode 100644 group11/283091182/src/com/coding/basic/linklist/LRUPageFrameTest.java diff --git a/group11/283091182/mini-jvm/com/coderising/jvm/loader/ClassFileLoader.java b/group11/283091182/mini-jvm/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..527e2f14d8 --- /dev/null +++ b/group11/283091182/mini-jvm/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,93 @@ +package com.coderising.jvm.loader; + +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.Iterator; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + + File classFile = getClassFileFromPath(className); + + byte[] buffer = new byte[1024]; + try { + FileInputStream fis = new FileInputStream(classFile); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + int readLen; + while((readLen = fis.read(buffer))>-1){ + baos.write(buffer, 0, readLen); + } + + return baos.toByteArray(); + + } catch (FileNotFoundException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } catch (IOException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + } + + + public void addClassPath(String path) { + File clzPath = new File(path); + if(clzPath.exists() && clzPath.isDirectory()){ + this.clzPaths.add(path); + }else{ + System.out.println("Invalid path:"+ path); + } + } + + + + public String getClassPath(){ + StringBuilder sb = new StringBuilder(); + Iterator it = this.clzPaths.iterator(); + while(it.hasNext()){ + if(sb.length()>0){ + sb.append(";"); + } + sb.append(it.next()); + } + return sb.toString(); + } + + public File getClassFileFromPath(String className) { + Iterator it = this.clzPaths.iterator(); + + //replace "." with "\\" in windows + String fullclassPath = className.replaceAll("\\.", (File.separatorChar=='\\')?"\\\\":"/")+".class"; + + while(it.hasNext()){ + File clzFile; + String path = (String)it.next(); + if(path.endsWith(String.valueOf(File.separatorChar))){ + clzFile = new File(path+fullclassPath); + }else{ + clzFile = new File(path+File.separatorChar+fullclassPath); + } + + //Check file before further proceed + if(clzFile.exists()&&clzFile.isFile()){ + return clzFile; + } + } + + throw new RuntimeException("Class not found:"+className); + } + + + +} diff --git a/group11/283091182/mini-jvm/com/coderising/jvm/test/ClassFileloaderTest.java b/group11/283091182/mini-jvm/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..21d7e97074 --- /dev/null +++ b/group11/283091182/mini-jvm/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,92 @@ +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 = "C:\\Users\\Administrator\\mygit\\coding2017\\liuxin\\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;icapacity){ + removeLast(); + } + } + + } + + private Node find(int value){ + if(size==0){ + return null; + } + Node temp = first; + while(temp!=null){ + if(temp.pageNum==value){ + return temp; + }else{ + temp = temp.next; + } + } + return null; + } + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/group11/283091182/src/com/coding/basic/linklist/LRUPageFrameTest.java b/group11/283091182/src/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..4070f1f2b3 --- /dev/null +++ b/group11/283091182/src/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,31 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + } + +} From d8fe97e589c148970258dc3a032ad62c24ad1541 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=9A=E5=90=AF=E7=9B=BC?= Date: Wed, 12 Apr 2017 15:23:35 +0800 Subject: [PATCH 35/41] jvm second --- .../java/com/pan/basic/BinaryTreeNode.java | 32 ++ .../src/main/java/com/pan/basic/Iterator.java | 7 + .../src/main/java/com/pan/basic/List.java | 9 + .../src/main/java/com/pan/basic/Queue.java | 19 ++ .../java/com/pan/linklist/LRUPageFrame.java | 133 +++++++++ .../main/java/com/pan/stack/StackUtil.java | 141 +++++++++ .../java/com/pan/week01/base/Iterator.java | 12 - .../main/java/com/pan/week02/ArrayUtil.java | 282 ------------------ group11/252308879/mini-jvm/pom.xml | 7 +- .../src/main/java/com/pan/alg/StackUtil.java | 141 +++++++++ .../src/main/java/com/pan/jvm/EmployeeV1.java | 1 - .../main/java/com/pan/jvm/clz/ClassFile.java | 30 +- .../com/pan/jvm/loader/ByteCodeIterator.java | 95 +++--- .../com/pan/jvm/loader/ClassFileParser.java | 121 +++++++- .../test/java/com/pan/alg/StackUtilTest.java | 78 +++++ ...aderTest.java => ClassFileLoaderTest.java} | 210 +++++++------ 16 files changed, 843 insertions(+), 475 deletions(-) create mode 100644 group11/252308879/data-structure/src/main/java/com/pan/basic/BinaryTreeNode.java create mode 100644 group11/252308879/data-structure/src/main/java/com/pan/basic/Iterator.java create mode 100644 group11/252308879/data-structure/src/main/java/com/pan/basic/List.java create mode 100644 group11/252308879/data-structure/src/main/java/com/pan/basic/Queue.java create mode 100644 group11/252308879/data-structure/src/main/java/com/pan/linklist/LRUPageFrame.java create mode 100644 group11/252308879/data-structure/src/main/java/com/pan/stack/StackUtil.java delete mode 100644 group11/252308879/data-structure/src/main/java/com/pan/week01/base/Iterator.java delete mode 100644 group11/252308879/data-structure/src/main/java/com/pan/week02/ArrayUtil.java create mode 100644 group11/252308879/mini-jvm/src/main/java/com/pan/alg/StackUtil.java create mode 100644 group11/252308879/mini-jvm/src/test/java/com/pan/alg/StackUtilTest.java rename group11/252308879/mini-jvm/src/test/java/com/pan/jvm/{ClassFileloaderTest.java => ClassFileLoaderTest.java} (50%) diff --git a/group11/252308879/data-structure/src/main/java/com/pan/basic/BinaryTreeNode.java b/group11/252308879/data-structure/src/main/java/com/pan/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..aab398a9b6 --- /dev/null +++ b/group11/252308879/data-structure/src/main/java/com/pan/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.pan.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group11/252308879/data-structure/src/main/java/com/pan/basic/Iterator.java b/group11/252308879/data-structure/src/main/java/com/pan/basic/Iterator.java new file mode 100644 index 0000000000..70735fa34e --- /dev/null +++ b/group11/252308879/data-structure/src/main/java/com/pan/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.pan.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group11/252308879/data-structure/src/main/java/com/pan/basic/List.java b/group11/252308879/data-structure/src/main/java/com/pan/basic/List.java new file mode 100644 index 0000000000..8ac9cf8e2e --- /dev/null +++ b/group11/252308879/data-structure/src/main/java/com/pan/basic/List.java @@ -0,0 +1,9 @@ +package com.pan.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group11/252308879/data-structure/src/main/java/com/pan/basic/Queue.java b/group11/252308879/data-structure/src/main/java/com/pan/basic/Queue.java new file mode 100644 index 0000000000..874d8a5690 --- /dev/null +++ b/group11/252308879/data-structure/src/main/java/com/pan/basic/Queue.java @@ -0,0 +1,19 @@ +package com.pan.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group11/252308879/data-structure/src/main/java/com/pan/linklist/LRUPageFrame.java b/group11/252308879/data-structure/src/main/java/com/pan/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..5208e5bc76 --- /dev/null +++ b/group11/252308879/data-structure/src/main/java/com/pan/linklist/LRUPageFrame.java @@ -0,0 +1,133 @@ +package com.pan.linklist; + + +/* + * 用双向链表实现LRU算法 + */ +public class LRUPageFrame { + private static class Node{ + Node prev; + Node next; + int pageNum = -1;// 物理页 + + Node(){ + + } + } + + private int capacity; + + private Node first;// 链表头 + private Node last;// 链表尾 + boolean tag = false; + + public LRUPageFrame(int capacity){ + this.capacity = capacity; + + for(int i = 0; i < capacity; i++){ + Node curNode = new Node(); + if(null == first){ + last = first = curNode; + }else{ + last.next = curNode; + curNode.prev = last; + last = last.next; + } + last.next = null; + } + } + public void printList(){ + Node curNode = first; + while(curNode != null){ + curNode = curNode.next; + } + } + /* + * 获取缓存中对象 + * @param key + * @return + */ + public void access(int pageNum){ + printList(); + Node index = findLogicPage(pageNum); + modifyPhysicalPage(index,pageNum); + } + + /* + * @param pageNum 表示要查询的逻辑页面 + * @return 若在物理页中找到要查询的逻辑页面,则返回该物理页节点的引用,否则返回null + */ + public Node findLogicPage(int pageNum){ + + Node index = null; + Node curNode = first; + while(curNode != null){ + if(curNode.pageNum == pageNum){ + index = curNode; + tag = true; + } + curNode = curNode.next; + } + return index; + } + /* + * @prama index 代表了 有逻辑页的物理页的节点的引用 + */ + public void modifyPhysicalPage(Node index,int pageNum){ + push(pageNum,index); + } + /* + * @param pageNum 要 push的逻辑页面, 默认栈顶是 first, bottom 栈底 指定了栈的大小 + */ + public void push(int pageNum,Node bottom){ + Node index = checkWhichListNodeNotUsed(); + if(index != null){ + index.pageNum = pageNum; + return; + } + + Node lastNode; + if(null == bottom){ + lastNode = last; + }else{ + lastNode = bottom; + } + Node curNode = lastNode.prev; + while(curNode != null){ + lastNode.pageNum = curNode.pageNum; + lastNode = curNode; + curNode = curNode.prev; + } + lastNode.pageNum = pageNum; + return; + } + + /* + * @return 返回物理页中 pageNum 没有被使用的节点的引用(返回栈中最下面的),如果全部都被使用,则返回 null + */ + public Node checkWhichListNodeNotUsed(){ + Node node = first; + Node index = null; + while(node != null){ + if(node.pageNum == -1){ + index = node; + } + node = node.next; + } + return index; + } + + public String toString(){ + StringBuffer buffer = new StringBuffer(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } +} diff --git a/group11/252308879/data-structure/src/main/java/com/pan/stack/StackUtil.java b/group11/252308879/data-structure/src/main/java/com/pan/stack/StackUtil.java new file mode 100644 index 0000000000..b4056e830a --- /dev/null +++ b/group11/252308879/data-structure/src/main/java/com/pan/stack/StackUtil.java @@ -0,0 +1,141 @@ +package com.pan.stack; + + +import java.util.Stack; + +/** + * Created by QiPan on 2017/4/12. + */ +public class StackUtil { + + public static void bad_reverse(Stack s) { + if(s == null || s.isEmpty()){ + return; + } + Stack tmpStack = new Stack(); + while(!s.isEmpty()){ + tmpStack.push(s.pop()); + } + + s = tmpStack; + + } + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + if(s == null || s.isEmpty()){ + return; + } + Integer top = s.pop(); + reverse(s); + addToBottom(s,top); + + + } + public static void addToBottom(Stack s, Integer value){ + if(s.isEmpty()){ + s.push(value); + } else{ + Integer top = s.pop(); + addToBottom(s,value); + s.push(top); + } + + } + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param o + */ + public static void remove(Stack s,Object o) { + if(s == null || s.isEmpty()){ + return; + } + Stack tmpStack = new Stack(); + + while(!s.isEmpty()){ + Object value = s.pop(); + if(!value.equals(o)){ + tmpStack.push(value); + } + } + + while(!tmpStack.isEmpty()){ + s.push(tmpStack.pop()); + } + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param len + * @return + */ + public static Object[] getTop(Stack s,int len) { + + if(s == null || s.isEmpty() || s.size() stack = new Stack(); + for(int i=0;i array2[j]) {//如果array2中元素小,则插入到array3中 - array3[array3Size++] = array2[j]; - ++j; - } else {//否则随便插入一个,但是计数要同时加1 - array3[array3Size++] = array1[i]; - ++i; - ++j; - } - } - - if (i == array1.length) { //如果array1中全部循环完毕了,那么需要去处理array2中剩余的元素 - for (int n = j; n < array2.length; n++) { - array3[array3Size++] = array2[n]; - } - } else if (j == array2.length) {// 如果array2中全部循环完毕,那么需要去处理array1中剩余的元素 - for (int n = i; n < array1.length; n++) { - array3[array3Size++] = array1[n]; - } - } - int[] returnResultArray = new int[array3Size]; - System.arraycopy(array3, 0, returnResultArray, 0, - array3Size); - return returnResultArray; - } - - /** - * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size - * 注意,老数组的元素在新数组中需要保持 - * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 - * [2,3,6,0,0,0] - * - * @param oldArray - * @param size - * @return - */ - public static int[] grow(int[] oldArray, int size) { - if (oldArray == null) { - oldArray = new int[size]; - } - oldArray = Arrays.copyOf(oldArray, oldArray.length + size); - return oldArray; - } - - /** - * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 - * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] - * max = 1, 则返回空数组 [] - * - * @param max - * @return - */ - public static int[] fibonacci(int max) { - if (max <= 1) { - return new int[0]; - } - int[] arrays = new int[max / 2]; - int firstNum = 1; //第一个数字 - int secondNum = 1; // 第二个数字 - int arraySize = 0; - arrays[arraySize++] = 1; // 初始化第一位 - while (secondNum < max) { - arrays[arraySize++] = secondNum; - int tmpNum = secondNum; // 保存第二个数,得会需要付给第一个数 - secondNum = firstNum + secondNum; // 为前两个数之和 - firstNum = tmpNum; // 第一个数,后移 - } - return Arrays.copyOf(arrays, arraySize); - } - - /** - * 返回小于给定最大值max的所有素数数组 - * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] - * - * @param max - * @return - */ - public static int[] getPrimes(int max) { - int[] returnResultArray = new int[max + 1]; - int arraySize = 0; - for (int i = 2; i < max; i++) { - if (isPrime(i)) { - returnResultArray[arraySize++] = i; - } - - } - if (arraySize == returnResultArray.length) { - return returnResultArray; - } - return Arrays.copyOf(returnResultArray, arraySize); - } - - private static boolean isPrime(final int number) { - if (number < 2) { - return false; - } - // 因为不可能将一个数除与所有小于它的数字,只要检查到N的平方根就好了。 - // 但直接开根号还有个精度的问题。这个可能会产生误差。 索性将判断条件写成 i*i<=number - for (int i = 2; i * i <= number; i++) { - if (number % i == 0) {//查看所有小于number平方根的数,能够被整除 - return false; - } - } - // 如果一个都没有,那么就是素数 - return true; - } - - /** - * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 - * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 - * - * @param max - * @return - */ - public static int[] getPerfectNumbers(int max) { - int[] array = new int[max]; - int arraySize = 0; - for (int n = 0; n < max; n++) { - int fac,// 被除的因子 - sum,// 用来统计因子之和 - num;// 除数的因子,中间变量 - for (sum = 1, num = n, fac = 2; fac < num; fac++) { - - if (n % fac == 0) {// 如果余数为0,那么说明有因子 - sum += fac; // 统计因子和 - num = n / fac; // num=等于除数的最大因子 - if (num == fac) // 如果最大和最小相等跳出循环 - break; - sum += num; // 再统计因子 - } - } - - if (sum == n) { //因子和与整数相等,那么就是一个完美数 - if (n != 1) { - System.out.println(n + "是一个完全数,其因子为:"); - } - for (fac = 1; fac < n; fac++) { - if (n % fac == 0) {// 列出所有的因子 - System.out.print(fac + " "); - } - } - System.out.println(); - array[arraySize++] = n; // 放到数组中 - } - } - return Arrays.copyOf(array, arraySize); - } - - /** - * 用seperator 把数组 array给连接起来 - * 例如array= [3,8,9], seperator = "-" - * 则返回值为"3-8-9" - * - * @param array - * @param seperator - * @return - */ - public static String join(int[] array, String seperator) { - if (array == null) { - return null; - } - StringBuilder str = new StringBuilder(); - for (int i = 0; i < array.length; i++) { - if (i == array.length - 1) { - str.append(array[i]); - continue; - } - str.append(array[i]).append(seperator); - } - return str.toString(); - } - -} diff --git a/group11/252308879/mini-jvm/pom.xml b/group11/252308879/mini-jvm/pom.xml index 9487a16199..a1421f550e 100644 --- a/group11/252308879/mini-jvm/pom.xml +++ b/group11/252308879/mini-jvm/pom.xml @@ -21,11 +21,16 @@ + + com.pan + data-structure + 1.0.0-SNAPSHOT + + junit junit - org.apache.commons commons-lang3 diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/alg/StackUtil.java b/group11/252308879/mini-jvm/src/main/java/com/pan/alg/StackUtil.java new file mode 100644 index 0000000000..1c7e0a5d0e --- /dev/null +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/alg/StackUtil.java @@ -0,0 +1,141 @@ +package com.pan.alg; + + +import java.util.Stack; + +/** + * Created by QiPan on 2017/4/12. + */ +public class StackUtil { + + public static void bad_reverse(Stack s) { + if(s == null || s.isEmpty()){ + return; + } + Stack tmpStack = new Stack(); + while(!s.isEmpty()){ + tmpStack.push(s.pop()); + } + + s = tmpStack; + + } + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + if(s == null || s.isEmpty()){ + return; + } + Integer top = s.pop(); + reverse(s); + addToBottom(s,top); + + + } + public static void addToBottom(Stack s, Integer value){ + if(s.isEmpty()){ + s.push(value); + } else{ + Integer top = s.pop(); + addToBottom(s,value); + s.push(top); + } + + } + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param o + */ + public static void remove(Stack s,Object o) { + if(s == null || s.isEmpty()){ + return; + } + Stack tmpStack = new Stack(); + + while(!s.isEmpty()){ + Object value = s.pop(); + if(!value.equals(o)){ + tmpStack.push(value); + } + } + + while(!tmpStack.isEmpty()){ + s.push(tmpStack.pop()); + } + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param len + * @return + */ + public static Object[] getTop(Stack s,int len) { + + if(s == null || s.isEmpty() || s.size() stack = new Stack(); + for(int i=0;i fields = new ArrayList(); private List methods = new ArrayList(); - + public ClassIndex getClzIndex() { return clzIndex; } @@ -28,10 +28,10 @@ public AccessFlag getAccessFlag() { public void setAccessFlag(AccessFlag accessFlag) { this.accessFlag = accessFlag; } - - - - public ConstantPool getConstantPool() { + + + + public ConstantPool getConstantPool() { return pool; } public int getMinorVersion() { @@ -48,12 +48,12 @@ public void setMajorVersion(int majorVersion) { } public void setConstPool(ConstantPool pool) { this.pool = pool; - + } public void setClassIndex(ClassIndex clzIndex) { - this.clzIndex = clzIndex; + this.clzIndex = clzIndex; } - + public void addField(Field f){ this.fields.add(f); } @@ -66,20 +66,16 @@ public void addMethod(Method m){ public List getMethods() { return methods; } - - + + public void print(){ - if(this.accessFlag.isPublicClass()){ System.out.println("Access flag : public "); } System.out.println("Class Name:"+ getClassName()); - System.out.println("Super Class Name:"+ getSuperClassName()); - - } - + private String getClassName(){ int thisClassIndex = this.clzIndex.getThisClassIndex(); ClassInfo thisClass = (ClassInfo)this.getConstantPool().getConstantInfo(thisClassIndex); diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ByteCodeIterator.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ByteCodeIterator.java index c0f9a6eaa5..24f00ccc5d 100644 --- a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ByteCodeIterator.java +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ByteCodeIterator.java @@ -4,54 +4,49 @@ import com.pan.jvm.util.Util; -public class ByteCodeIterator { - byte[] codes; - int pos = 0; - - ByteCodeIterator(byte[] codes) { - this.codes = codes; - } - - - - public byte[] getBytes(int len) { - if (pos + len >= codes.length) { - throw new ArrayIndexOutOfBoundsException(); - } - - byte[] data = Arrays.copyOfRange(codes, pos, pos + len); - pos += len; - return data; - } - - public int nextU1toInt() { - - return Util.byteToInt(new byte[] { codes[pos++] }); - } - - public int nextU2ToInt() { - return Util.byteToInt(new byte[] { codes[pos++], codes[pos++] }); - } - - public int nextU4ToInt() { - return Util.byteToInt(new byte[] { codes[pos++], codes[pos++], codes[pos++], codes[pos++] }); - } - - public String nextU4ToHexString() { - return Util.byteToHexString((new byte[] { codes[pos++], codes[pos++], codes[pos++], codes[pos++] })); - } - - public String nextUxToHexString(int len) { - byte[] tmp = new byte[len]; - - for (int i = 0; i < len; i++) { - tmp[i] = codes[pos++]; - } - return Util.byteToHexString(tmp).toLowerCase(); - - } - - public void back(int n) { - this.pos -= n; - } +public class ByteCodeIterator { + byte[] codes; + int pos = 0; + + ByteCodeIterator(byte[] codes) { + this.codes = codes; + } + + public byte[] getBytes(int len) { + if (pos + len >= codes.length) { + throw new ArrayIndexOutOfBoundsException(); + } + byte[] data = Arrays.copyOfRange(codes, pos, pos + len); + pos += len; + return data; + } + + public int nextU1toInt() { + return Util.byteToInt(new byte[]{codes[pos++]}); + } + + public int nextU2ToInt() { + return Util.byteToInt(new byte[]{codes[pos++], codes[pos++]}); + } + + public int nextU4ToInt() { + return Util.byteToInt(new byte[]{codes[pos++], codes[pos++], codes[pos++], codes[pos++]}); + } + + public String nextU4ToHexString() { + return Util.byteToHexString((new byte[]{codes[pos++], codes[pos++], codes[pos++], codes[pos++]})); + } + + public String nextUxToHexString(int len) { + byte[] tmp = new byte[len]; + + for (int i = 0; i < len; i++) { + tmp[i] = codes[pos++]; + } + return Util.byteToHexString(tmp).toLowerCase(); + } + + public void back(int n) { + this.pos -= n; + } } diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileParser.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileParser.java index d2ba152e05..7ab096b4f5 100644 --- a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileParser.java +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileParser.java @@ -3,30 +3,139 @@ import com.pan.jvm.clz.AccessFlag; import com.pan.jvm.clz.ClassFile; import com.pan.jvm.clz.ClassIndex; -import com.pan.jvm.constant.ConstantPool; +import com.pan.jvm.constant.*; + +import java.io.UnsupportedEncodingException; public class ClassFileParser { public ClassFile parse(byte[] codes) { + ByteCodeIterator iterator = new ByteCodeIterator(codes); + String magicNumber = iterator.nextU4ToHexString(); + if (!"cafebabe".equals(magicNumber)) {// 验证是否为Java的.class文件 + return null; + } + ClassFile classFile = new ClassFile(); + classFile.setMinorVersion(iterator.nextU2ToInt()); + classFile.setMajorVersion(iterator.nextU2ToInt()); + + ConstantPool constantPool = parseConstantPool(iterator); + classFile.setConstPool(constantPool); + + AccessFlag flag = parseAccessFlag(iterator); + classFile.setAccessFlag(flag); - return null; + // this clz 和 supper clz + ClassIndex clzIndex = parseClassIndex(iterator); + classFile.setClassIndex(clzIndex); + + parseInterfaces(iterator); + return classFile; } private AccessFlag parseAccessFlag(ByteCodeIterator iter) { + AccessFlag accessFlag = new AccessFlag(iter.nextU2ToInt()); + return accessFlag; + } - return null; + private ClassIndex parseClassIndex(ByteCodeIterator iter) { + + int thisClassIndex = iter.nextU2ToInt(); + int supperClassIndex = iter.nextU2ToInt(); + + ClassIndex classIndex = new ClassIndex(); + classIndex.setThisClassIndex(thisClassIndex); + classIndex.setSuperClassIndex(supperClassIndex); + return classIndex; } - private ClassIndex parseClassInfex(ByteCodeIterator iter) { + private void parseInterfaces(ByteCodeIterator iter) { + int interfaceCount = iter.nextU2ToInt(); - return null; + System.out.println("interfaceCount:" + interfaceCount); + // TODO : 如果实现了interface, 这里需要解析 } + /** + * 解析常量池 + * + * @param iter + * @return + */ private ConstantPool parseConstantPool(ByteCodeIterator iter) { - return null; + int constPoolCount = iter.nextU2ToInt(); + + System.out.println("Constant Pool Count :" + constPoolCount); + ConstantPool pool = new ConstantPool(); + // 因为常量池中的信息是从 1 开始的,但是数组或者List 下标是从0开始,所以设置第一个为空的常量 + pool.addConstantInfo(new NullConstantInfo()); + + for (int i = 1; i <= constPoolCount - 1; i++) { + + // 获取标识符信息 + int tag = iter.nextU1toInt(); + + switch (tag) { + + case 7: //CONSTANT_Class + int utf8Index = iter.nextU2ToInt(); + ClassInfo classInfo = new ClassInfo(pool); + classInfo.setUtf8Index(utf8Index); + + pool.addConstantInfo(classInfo); + break; + case 9: // CONSTANT_Fieldref + FieldRefInfo fieldRefInfo = new FieldRefInfo(pool); + fieldRefInfo.setClassInfoIndex(iter.nextU2ToInt()); + fieldRefInfo.setNameAndTypeIndex(iter.nextU2ToInt()); + + pool.addConstantInfo(fieldRefInfo); + break; + case 10: // CONSTANT_Methodref + MethodRefInfo methodRefInfo = new MethodRefInfo(pool); + methodRefInfo.setClassInfoIndex(iter.nextU2ToInt()); + methodRefInfo.setNameAndTypeIndex(iter.nextU2ToInt()); + + pool.addConstantInfo(methodRefInfo); + break; + case 8: + StringInfo info = new StringInfo(pool); + info.setIndex(iter.nextU2ToInt()); + pool.addConstantInfo(info); + break; + case 12: // CONSTANT_NameAndType + NameAndTypeInfo nameAndTypeInfo = new NameAndTypeInfo(pool); + nameAndTypeInfo.setIndex1(iter.nextU2ToInt()); + nameAndTypeInfo.setIndex2(iter.nextU2ToInt()); + + pool.addConstantInfo(nameAndTypeInfo); + break; + case 1: // CONSTANT_Utf8 + int length = iter.nextU2ToInt(); + byte[] data = iter.getBytes(length); + String value = null; + try { + value = new String(data, "utf-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + + UTF8Info utf8Str = new UTF8Info(pool); + utf8Str.setLength(length); + utf8Str.setValue(value); + + pool.addConstantInfo(utf8Str); + break; + default: + throw new RuntimeException("the constant pool tag " + tag + " has not been implemented yet."); + + } + } + System.out.println("Finished reading Constant pool "); + return pool; } diff --git a/group11/252308879/mini-jvm/src/test/java/com/pan/alg/StackUtilTest.java b/group11/252308879/mini-jvm/src/test/java/com/pan/alg/StackUtilTest.java new file mode 100644 index 0000000000..f94a01b2d7 --- /dev/null +++ b/group11/252308879/mini-jvm/src/test/java/com/pan/alg/StackUtilTest.java @@ -0,0 +1,78 @@ +package com.pan.alg; + +import static org.junit.Assert.fail; + +import java.util.Stack; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +public class StackUtilTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAddToBottom() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + + StackUtil.addToBottom(s, 0); + + Assert.assertEquals("[0, 1, 2, 3]", s.toString()); + + } + @Test + public void testReverse() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString()); + StackUtil.reverse(s); + Assert.assertEquals("[5, 4, 3, 2, 1]", s.toString()); + } + + @Test + public void testRemove() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + StackUtil.remove(s, 2); + Assert.assertEquals("[1, 3]", s.toString()); + } + + @Test + public void testGetTop() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + { + Object[] values = StackUtil.getTop(s, 3); + Assert.assertEquals(5, values[0]); + Assert.assertEquals(4, values[1]); + Assert.assertEquals(3, values[2]); + } + } + + @Test + public void testIsValidPairs() { + Assert.assertTrue(StackUtil.isValidPairs("([e{d}f])")); + Assert.assertFalse(StackUtil.isValidPairs("([b{x]y})")); + } + +} diff --git a/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/ClassFileloaderTest.java b/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/ClassFileLoaderTest.java similarity index 50% rename from group11/252308879/mini-jvm/src/test/java/com/pan/jvm/ClassFileloaderTest.java rename to group11/252308879/mini-jvm/src/test/java/com/pan/jvm/ClassFileLoaderTest.java index 4bcb38e917..2aed2e5e44 100644 --- a/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/ClassFileloaderTest.java +++ b/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/ClassFileLoaderTest.java @@ -19,28 +19,28 @@ -public class ClassFileloaderTest { +public class ClassFileLoaderTest { - private static final String FULL_QUALIFIED_CLASS_NAME = "com/coderising/jvm/test/EmployeeV1"; + private static final String FULL_QUALIFIED_CLASS_NAME = "com/pan/jvm/EmployeeV1"; static String path1 = EmployeeV1.class.getClassLoader().getResource("").getPath() .replace("test-classes", "classes"); - static String path2 = "C:\temp"; - -// static ClassFile clzFile = null; -// static { -// ClassFileLoader loader = new ClassFileLoader(); -// loader.addClassPath(path1); -// String className = "com.pan.jvm.EmployeeV1"; -// -// clzFile = loader.loadClass(className); -// clzFile.print(); -// } - + static String path2 = "C:/temp"; + static ClassFile clzFile = null; + static { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.pan.jvm.EmployeeV1"; + + clzFile = loader.loadClass(className); + clzFile.print(); + } + + @Before - public void setUp() throws Exception { + public void setUp() throws Exception { } @After @@ -85,104 +85,102 @@ public void testMagicNumber(){ byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; - String acctualValue = Util.byteToHexString(codes); + String actualValue = Util.byteToHexString(codes); - Assert.assertEquals("cafebabe", acctualValue); + Assert.assertEquals("cafebabe", actualValue); } /** * ---------------------------------------------------------------------- */ -// -// -// @Test -// public void testVersion(){ -// -// Assert.assertEquals(0, clzFile.getMinorVersion()); -// Assert.assertEquals(52, clzFile.getMajorVersion()); -// -// } -// -// @Test -// public void testConstantPool(){ -// -// -// ConstantPool pool = clzFile.getConstantPool(); -// -// Assert.assertEquals(53, pool.getSize()); -// -// { -// ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(1); -// Assert.assertEquals(2, clzInfo.getUtf8Index()); -// -// UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(2); -// Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, utf8Info.getValue()); -// } -// { -// ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(3); -// Assert.assertEquals(4, clzInfo.getUtf8Index()); -// -// UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(4); -// Assert.assertEquals("java/lang/Object", utf8Info.getValue()); -// } -// { -// UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(5); -// Assert.assertEquals("name", utf8Info.getValue()); -// -// utf8Info = (UTF8Info) pool.getConstantInfo(6); -// Assert.assertEquals("Ljava/lang/String;", utf8Info.getValue()); -// -// utf8Info = (UTF8Info) pool.getConstantInfo(7); -// Assert.assertEquals("age", utf8Info.getValue()); -// -// utf8Info = (UTF8Info) pool.getConstantInfo(8); -// Assert.assertEquals("I", utf8Info.getValue()); -// -// utf8Info = (UTF8Info) pool.getConstantInfo(9); -// Assert.assertEquals("", 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()); -// } + + + @Test + public void testVersion(){ + Assert.assertEquals(0, clzFile.getMinorVersion()); + Assert.assertEquals(52, clzFile.getMajorVersion()); + } + + @Test + public void testConstantPool(){ + + + ConstantPool pool = clzFile.getConstantPool(); + + Assert.assertEquals(53, pool.getSize()); + + { + ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(1); + Assert.assertEquals(2, clzInfo.getUtf8Index()); + + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(2); + Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, utf8Info.getValue()); + } + { + ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(3); + Assert.assertEquals(4, clzInfo.getUtf8Index()); + + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(4); + Assert.assertEquals("java/lang/Object", utf8Info.getValue()); + } + { + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(5); + Assert.assertEquals("name", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(6); + Assert.assertEquals("Ljava/lang/String;", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(7); + Assert.assertEquals("age", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(8); + Assert.assertEquals("I", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(9); + Assert.assertEquals("", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(10); + Assert.assertEquals("(Ljava/lang/String;I)V", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(11); + Assert.assertEquals("Code", utf8Info.getValue()); + } + + { + MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(12); + Assert.assertEquals(3, methodRef.getClassInfoIndex()); + Assert.assertEquals(13, methodRef.getNameAndTypeIndex()); + } + + { + NameAndTypeInfo nameAndType = (NameAndTypeInfo) pool.getConstantInfo(13); + Assert.assertEquals(9, nameAndType.getIndex1()); + Assert.assertEquals(14, nameAndType.getIndex2()); + } + //抽查几个吧 + { + MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(45); + Assert.assertEquals(1, methodRef.getClassInfoIndex()); + Assert.assertEquals(46, methodRef.getNameAndTypeIndex()); + } + + { + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(53); + Assert.assertEquals("EmployeeV1.java", utf8Info.getValue()); + } + } + @Test + public void testClassIndex(){ + + ClassIndex clzIndex = clzFile.getClzIndex(); + ClassInfo thisClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getThisClassIndex()); + ClassInfo superClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getSuperClassIndex()); + + + Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, thisClassInfo.getClassName()); + Assert.assertEquals("java/lang/Object", superClassInfo.getClassName()); + } // // /** // * 下面是第三次JVM课应实现的测试用例 From 0d12fae46beaf9f97c1cf6eefe1c53ab6cd89284 Mon Sep 17 00:00:00 2001 From: Pan Date: Sat, 15 Apr 2017 18:31:27 +0800 Subject: [PATCH 36/41] jvm third field and method, dataStructure not fish --- .../main/java/com/pan/jvm/attr/CodeAttr.java | 45 +++++- .../com/pan/jvm/attr/LineNumberTable.java | 15 +- .../com/pan/jvm/attr/LocalVariableTable.java | 20 ++- .../main/java/com/pan/jvm/field/Field.java | 28 +++- .../com/pan/jvm/loader/ClassFileParser.java | 30 ++++ .../main/java/com/pan/jvm/method/Method.java | 26 +++- .../java/com/pan/jvm/ClassFileLoaderTest.java | 144 +++++++++--------- 7 files changed, 219 insertions(+), 89 deletions(-) diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/CodeAttr.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/CodeAttr.java index 08bfa18f85..5b913a36c2 100644 --- a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/CodeAttr.java +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/CodeAttr.java @@ -40,9 +40,48 @@ public void setLocalVariableTable(LocalVariableTable t) { } public static CodeAttr parse(ClassFile clzFile, ByteCodeIterator iter){ - - - return null; + int attrNameIndex = iter.nextU2ToInt(); + int attrLen = iter.nextU4ToInt(); + int maxStack = iter.nextU2ToInt(); + int maxLocals = iter.nextU2ToInt(); + int codeLen = iter.nextU4ToInt(); + + String code = iter.nextUxToHexString(codeLen); + + System.out.println("CODE-code: "+code); + + CodeAttr codeAttr = new CodeAttr(attrNameIndex, attrLen, maxStack, maxLocals, codeLen, code); + + int exceptionTableLen = iter.nextU2ToInt(); + + if (exceptionTableLen > 0){ + String exTable = iter.nextUxToHexString(exceptionTableLen); + System.out.println("Encountered exception table, just ignore!"); + } + + // 处理子属性 + int subAttrCount = iter.nextU2ToInt(); + for (int i = 1; i <= subAttrCount; i++) { + int suAttrIndex = iter.nextU2ToInt(); + String subAttrName = clzFile.getConstantPool().getUTF8String(suAttrIndex); + + iter.back(2);// 便于在子属性中获取 attrNameIndex + + if (AttributeInfo.LINE_NUM_TABLE.equalsIgnoreCase(subAttrName)){ + LineNumberTable lineNumberTable = LineNumberTable.parse(iter); + codeAttr.setLineNumberTable(lineNumberTable); + }else if (AttributeInfo.LOCAL_VAR_TABLE.equalsIgnoreCase(subAttrName)){ + LocalVariableTable localVariableTable = LocalVariableTable.parse(iter); + codeAttr.setLocalVariableTable(localVariableTable); + }else if (AttributeInfo.STACK_MAP_TABLE.equalsIgnoreCase(subAttrName)){ + StackMapTable stackMapTable = StackMapTable.parse(iter); + codeAttr.setStackMapTable(stackMapTable); + }else { + throw new RuntimeException("Need code to process :" + subAttrName); + } + + } + return codeAttr; } private void setStackMapTable(StackMapTable t) { this.stackMapTable = t; diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/LineNumberTable.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/LineNumberTable.java index 31d2ab3d7e..563e8b19d5 100644 --- a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/LineNumberTable.java +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/LineNumberTable.java @@ -33,8 +33,19 @@ public LineNumberTable(int attrNameIndex, int attrLen) { } public static LineNumberTable parse(ByteCodeIterator iter){ - - return null; + int attrNameIndex = iter.nextU2ToInt(); + int attrLen = iter.nextU4ToInt(); + LineNumberTable lineNumberTable = new LineNumberTable(attrNameIndex, attrLen); + + int attrItemSize = iter.nextU2ToInt(); + for (int i = 1; i <= attrItemSize; i++) { + LineNumberItem lineNumberItem = new LineNumberItem(); + lineNumberItem.setStartPC(iter.nextU2ToInt()); + lineNumberItem.setLineNum(iter.nextU2ToInt()); + + lineNumberTable.addLineNumberItem(lineNumberItem); + } + return lineNumberTable; } diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/LocalVariableTable.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/LocalVariableTable.java index 745f1e77d2..6291f9a605 100644 --- a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/LocalVariableTable.java +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/attr/LocalVariableTable.java @@ -17,8 +17,24 @@ public LocalVariableTable(int attrNameIndex, int attrLen) { } public static LocalVariableTable parse(ByteCodeIterator iter){ - - return null; + + int attrNameIndex = iter.nextU2ToInt(); + int attrLen = iter.nextU4ToInt(); + + LocalVariableTable localVariableTable = new LocalVariableTable(attrNameIndex, attrLen); + + int attrItemSize = iter.nextU2ToInt(); + for (int i = 1; i <= attrItemSize; i++) { + LocalVariableItem localVariableItem = new LocalVariableItem(); + localVariableItem.setStartPC(iter.nextU2ToInt()); + localVariableItem.setLength(iter.nextU2ToInt()); + localVariableItem.setNameIndex(iter.nextU2ToInt()); + localVariableItem.setDescIndex(iter.nextU2ToInt()); + localVariableItem.setIndex(iter.nextU2ToInt()); + localVariableTable.addLocalVariableItem(localVariableItem); + } + + return localVariableTable; } private void addLocalVariableItem(LocalVariableItem item) { this.items.add(item); diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/field/Field.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/field/Field.java index 3fc8da4cdf..e427618564 100644 --- a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/field/Field.java +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/field/Field.java @@ -1,7 +1,9 @@ package com.pan.jvm.field; +import com.pan.jvm.constant.ConstantInfo; import com.pan.jvm.constant.ConstantPool; +import com.pan.jvm.constant.UTF8Info; import com.pan.jvm.loader.ByteCodeIterator; public class Field { @@ -21,12 +23,26 @@ public Field( int accessFlag, int nameIndex, int descriptorIndex,ConstantPool po this.pool = pool; } - - - - public static Field parse(ConstantPool pool,ByteCodeIterator iter){ - - return null; + @Override + public String toString() { + String name = ((UTF8Info) pool.getConstantInfo(this.nameIndex)).getValue(); + String desc = ((UTF8Info) pool.getConstantInfo(this.descriptorIndex)).getValue(); + + return name + ":" + desc; + } + + public static Field parse(ConstantPool pool, ByteCodeIterator iter){ + + int accessFlags = iter.nextU2ToInt(); + int nameIndex = iter.nextU2ToInt(); + int descriptorIndex = iter.nextU2ToInt(); + int attrCount = iter.nextU2ToInt(); + System.out.println("Field Attributes Count: " + attrCount); + Field field = new Field(accessFlags, nameIndex, descriptorIndex, pool); + if (attrCount > 0){ + throw new RuntimeException("Attributes Count > 0"); + } + return field; } } diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileParser.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileParser.java index 7ab096b4f5..b2bddb85c2 100644 --- a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileParser.java +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/loader/ClassFileParser.java @@ -4,6 +4,8 @@ import com.pan.jvm.clz.ClassFile; import com.pan.jvm.clz.ClassIndex; import com.pan.jvm.constant.*; +import com.pan.jvm.field.Field; +import com.pan.jvm.method.Method; import java.io.UnsupportedEncodingException; @@ -30,10 +32,38 @@ public ClassFile parse(byte[] codes) { ClassIndex clzIndex = parseClassIndex(iterator); classFile.setClassIndex(clzIndex); + // interface parseInterfaces(iterator); + + // field + parseFields(classFile, iterator); + + // method + parseMethods(classFile, iterator); + return classFile; } + private void parseMethods(ClassFile classFile, ByteCodeIterator iterator) { + int methodsCount = iterator.nextU2ToInt(); + System.out.println("Methods Count: " + methodsCount); + + for (int i = 1; i <= methodsCount; i++) { + Method method = Method.parse(classFile, iterator); + classFile.addMethod(method); + } + } + + + private void parseFields(ClassFile clzFile, ByteCodeIterator iterator) { + int fieldsCount = iterator.nextU2ToInt(); + System.out.println("Field count:" + fieldsCount); + for (int i = 1; i <= fieldsCount; i++) {// 从第一个开始,因为不包含本身 + Field field = Field.parse(clzFile.getConstantPool(), iterator); + clzFile.addField(field); + } + } + private AccessFlag parseAccessFlag(ByteCodeIterator iter) { AccessFlag accessFlag = new AccessFlag(iter.nextU2ToInt()); return accessFlag; diff --git a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/method/Method.java b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/method/Method.java index ec3561a566..0fbb0f0946 100644 --- a/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/method/Method.java +++ b/group11/252308879/mini-jvm/src/main/java/com/pan/jvm/method/Method.java @@ -1,6 +1,7 @@ package com.pan.jvm.method; +import com.pan.jvm.attr.AttributeInfo; import com.pan.jvm.attr.CodeAttr; import com.pan.jvm.clz.ClassFile; import com.pan.jvm.loader.ByteCodeIterator; @@ -43,11 +44,28 @@ public Method(ClassFile clzFile,int accessFlag, int nameIndex, int descriptorInd } - - - public static Method parse(ClassFile clzFile, ByteCodeIterator iter){ - return null; + int accessFlags = iter.nextU2ToInt(); + int nameIndex = iter.nextU2ToInt(); + int descriptorIndex = iter.nextU2ToInt(); + int attrCount = iter.nextU2ToInt(); + + System.out.println("Method Attributes Count: " + attrCount); + Method method = new Method(clzFile, accessFlags, nameIndex, descriptorIndex); + if (attrCount > 0){ + for (int i = 1; i <= attrCount; i++) { + int attrNameIndex = iter.nextU2ToInt(); + String attrName = clzFile.getConstantPool().getUTF8String(attrNameIndex); + iter.back(2); // 回退两个,便于Code 中读取属性 + if (AttributeInfo.CODE.equalsIgnoreCase(attrName)){ + CodeAttr codeAttr = CodeAttr.parse(clzFile, iter); + method.setCodeAttr(codeAttr); + }else { + throw new RuntimeException("Current Has CODE. Not Support Other"); + } + } + } + return method; } } diff --git a/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/ClassFileLoaderTest.java b/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/ClassFileLoaderTest.java index 2aed2e5e44..b0a61b242a 100644 --- a/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/ClassFileLoaderTest.java +++ b/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/ClassFileLoaderTest.java @@ -181,78 +181,78 @@ public void testClassIndex(){ Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, thisClassInfo.getClassName()); Assert.assertEquals("java/lang/Object", superClassInfo.getClassName()); } -// -// /** -// * 下面是第三次JVM课应实现的测试用例 -// */ -// @Test -// public void testReadFields(){ -// -// List fields = clzFile.getFields(); -// Assert.assertEquals(2, fields.size()); -// { -// Field f = fields.get(0); -// Assert.assertEquals("name:Ljava/lang/String;", f.toString()); -// } -// { -// Field f = fields.get(1); -// Assert.assertEquals("age:I", f.toString()); -// } -// } -// @Test -// public void testMethods(){ -// -// List methods = clzFile.getMethods(); -// ConstantPool pool = clzFile.getConstantPool(); -// -// { -// Method m = methods.get(0); -// assertMethodEquals(pool,m, -// "", -// "(Ljava/lang/String;I)V", -// "2ab7000c2a2bb5000f2a1cb50011b1"); -// -// } -// { -// Method m = methods.get(1); -// assertMethodEquals(pool,m, -// "setName", -// "(Ljava/lang/String;)V", -// "2a2bb5000fb1"); -// -// } -// { -// Method m = methods.get(2); -// assertMethodEquals(pool,m, -// "setAge", -// "(I)V", -// "2a1bb50011b1"); -// } -// { -// Method m = methods.get(3); -// assertMethodEquals(pool,m, -// "sayHello", -// "()V", -// "b2001c1222b60024b1"); -// -// } -// { -// Method m = methods.get(4); -// assertMethodEquals(pool,m, -// "main", -// "([Ljava/lang/String;)V", -// "bb000159122b101db7002d4c2bb6002fb1"); -// } -// } -// -// private void assertMethodEquals(ConstantPool pool,Method m , String expectedName, String expectedDesc,String expectedCode){ -// String methodName = pool.getUTF8String(m.getNameIndex()); -// String methodDesc = pool.getUTF8String(m.getDescriptorIndex()); -// String code = m.getCodeAttr().getCode(); -// Assert.assertEquals(expectedName, methodName); -// Assert.assertEquals(expectedDesc, methodDesc); -// Assert.assertEquals(expectedCode, code); -// } + + /** + * 下面是第三次JVM课应实现的测试用例 + */ + @Test + public void testReadFields(){ + + List fields = clzFile.getFields(); + Assert.assertEquals(2, fields.size()); + { + Field f = fields.get(0); + Assert.assertEquals("name:Ljava/lang/String;", f.toString()); + } + { + Field f = fields.get(1); + Assert.assertEquals("age:I", f.toString()); + } + } + @Test + public void testMethods(){ + + List methods = clzFile.getMethods(); + ConstantPool pool = clzFile.getConstantPool(); + + { + Method m = methods.get(0); + assertMethodEquals(pool,m, + "", + "(Ljava/lang/String;I)V", + "2ab700012a2bb500022a1cb50003b1"); + + } + { + Method m = methods.get(1); + assertMethodEquals(pool,m, + "setName", + "(Ljava/lang/String;)V", + "2a2bb50002b1"); + + } + { + Method m = methods.get(2); + assertMethodEquals(pool,m, + "setAge", + "(I)V", + "2a1bb50003b1"); + } + { + Method m = methods.get(3); + assertMethodEquals(pool,m, + "sayHello", + "()V", + "b200041205b60006b1"); + + } + { + Method m = methods.get(4); + assertMethodEquals(pool,m, + "main", + "([Ljava/lang/String;)V", + "bb0007591208101db700094c2bb6000ab1"); + } + } + + private void assertMethodEquals(ConstantPool pool,Method m , String expectedName, String expectedDesc,String expectedCode){ + String methodName = pool.getUTF8String(m.getNameIndex()); + String methodDesc = pool.getUTF8String(m.getDescriptorIndex()); + String code = m.getCodeAttr().getCode(); + Assert.assertEquals(expectedName, methodName); + Assert.assertEquals(expectedDesc, methodDesc); + Assert.assertEquals(expectedCode, code); + } } From 2c40a6f0a102324ca8fd00734155514c372fc90e Mon Sep 17 00:00:00 2001 From: Pan Date: Sat, 15 Apr 2017 22:48:59 +0800 Subject: [PATCH 37/41] fix warring --- .../src/test/java/com/pan/alg/StackUtilTest.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/group11/252308879/mini-jvm/src/test/java/com/pan/alg/StackUtilTest.java b/group11/252308879/mini-jvm/src/test/java/com/pan/alg/StackUtilTest.java index f94a01b2d7..71671c9b47 100644 --- a/group11/252308879/mini-jvm/src/test/java/com/pan/alg/StackUtilTest.java +++ b/group11/252308879/mini-jvm/src/test/java/com/pan/alg/StackUtilTest.java @@ -1,7 +1,5 @@ package com.pan.alg; -import static org.junit.Assert.fail; - import java.util.Stack; import org.junit.After; @@ -20,7 +18,7 @@ public void tearDown() throws Exception { @Test public void testAddToBottom() { - Stack s = new Stack(); + Stack s = new Stack<>(); s.push(1); s.push(2); s.push(3); @@ -32,7 +30,7 @@ public void testAddToBottom() { } @Test public void testReverse() { - Stack s = new Stack(); + Stack s = new Stack<>(); s.push(1); s.push(2); s.push(3); @@ -45,7 +43,7 @@ public void testReverse() { @Test public void testRemove() { - Stack s = new Stack(); + Stack s = new Stack<>(); s.push(1); s.push(2); s.push(3); @@ -55,7 +53,7 @@ public void testRemove() { @Test public void testGetTop() { - Stack s = new Stack(); + Stack s = new Stack<>(); s.push(1); s.push(2); s.push(3); From f1e39f3b65075a999ef8833f5b59610ad38a2b25 Mon Sep 17 00:00:00 2001 From: Pan Date: Sat, 15 Apr 2017 23:21:19 +0800 Subject: [PATCH 38/41] Test user eclipe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用Eclipe试一下生成的字节码首个常量为class_info文件问题 --- .../main/java/com/pan/stack/StackUtil.java | 4 +-- .../src/main/java/com/pan/alg/StackUtil.java | 4 +-- .../main/java/com/pan/jvm/attr/CodeAttr.java | 1 - .../com/pan/jvm/attr/LocalVariableTable.java | 2 -- .../main/java/com/pan/jvm/field/Field.java | 1 - .../com/pan/jvm/loader/ClassFileLoader.java | 2 +- .../src/test/java/com/pan/jvm/EmployeeV1.java | 31 ------------------- 7 files changed, 5 insertions(+), 40 deletions(-) delete mode 100644 group11/252308879/mini-jvm/src/test/java/com/pan/jvm/EmployeeV1.java diff --git a/group11/252308879/data-structure/src/main/java/com/pan/stack/StackUtil.java b/group11/252308879/data-structure/src/main/java/com/pan/stack/StackUtil.java index b4056e830a..74918b1e37 100644 --- a/group11/252308879/data-structure/src/main/java/com/pan/stack/StackUtil.java +++ b/group11/252308879/data-structure/src/main/java/com/pan/stack/StackUtil.java @@ -12,7 +12,7 @@ public static void bad_reverse(Stack s) { if(s == null || s.isEmpty()){ return; } - Stack tmpStack = new Stack(); + Stack tmpStack = new Stack<>(); while(!s.isEmpty()){ tmpStack.push(s.pop()); } @@ -104,7 +104,7 @@ public static Object[] getTop(Stack s,int len) { */ public static boolean isValidPairs(String s){ - Stack stack = new Stack(); + Stack stack = new Stack<>(); for(int i=0;i s) { if(s == null || s.isEmpty()){ return; } - Stack tmpStack = new Stack(); + Stack tmpStack = new Stack<>(); while(!s.isEmpty()){ tmpStack.push(s.pop()); } @@ -104,7 +104,7 @@ public static Object[] getTop(Stack s,int len) { */ public static boolean isValidPairs(String s){ - Stack stack = new Stack(); + Stack stack = new Stack<>(); for(int i=0;i clzPaths = new ArrayList(); + private List clzPaths = new ArrayList<>(); public byte[] readBinaryCode(String className) { className = className.replace('.', File.separatorChar) + ".class"; diff --git a/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/EmployeeV1.java b/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/EmployeeV1.java deleted file mode 100644 index 92b5b403ca..0000000000 --- a/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/EmployeeV1.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.pan.jvm; - -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 From 3f3b383ac239cf88f7e28fce1eb22e6cdb60a563 Mon Sep 17 00:00:00 2001 From: Pan Date: Sat, 15 Apr 2017 23:39:33 +0800 Subject: [PATCH 39/41] modify dir struct --- group11/252308879/data-structure/pom.xml | 1 - group11/252308879/mini-jvm/pom.xml | 1 - .../java/com/pan/jvm/ClassFileLoaderTest.java | 30 +++++++++---------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/group11/252308879/data-structure/pom.xml b/group11/252308879/data-structure/pom.xml index 445ba45741..42eb53c993 100644 --- a/group11/252308879/data-structure/pom.xml +++ b/group11/252308879/data-structure/pom.xml @@ -8,7 +8,6 @@ 1.0.0-SNAPSHOT - com.pan data-structure 1.0.0-SNAPSHOT jar diff --git a/group11/252308879/mini-jvm/pom.xml b/group11/252308879/mini-jvm/pom.xml index a1421f550e..b1f01e4594 100644 --- a/group11/252308879/mini-jvm/pom.xml +++ b/group11/252308879/mini-jvm/pom.xml @@ -8,7 +8,6 @@ 1.0.0-SNAPSHOT - com.pan mini-jvm 1.0.0-SNAPSHOT jar diff --git a/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/ClassFileLoaderTest.java b/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/ClassFileLoaderTest.java index b0a61b242a..d341553c70 100644 --- a/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/ClassFileLoaderTest.java +++ b/group11/252308879/mini-jvm/src/test/java/com/pan/jvm/ClassFileLoaderTest.java @@ -201,47 +201,47 @@ public void testReadFields(){ } @Test public void testMethods(){ - + List methods = clzFile.getMethods(); ConstantPool pool = clzFile.getConstantPool(); - + { - Method m = methods.get(0); + Method m = methods.get(0); assertMethodEquals(pool,m, "", "(Ljava/lang/String;I)V", - "2ab700012a2bb500022a1cb50003b1"); - + "2ab7000c2a2bb5000f2a1cb50011b1"); + } { - Method m = methods.get(1); + Method m = methods.get(1); assertMethodEquals(pool,m, "setName", "(Ljava/lang/String;)V", - "2a2bb50002b1"); - + "2a2bb5000fb1"); + } { - Method m = methods.get(2); + Method m = methods.get(2); assertMethodEquals(pool,m, "setAge", "(I)V", - "2a1bb50003b1"); + "2a1bb50011b1"); } { - Method m = methods.get(3); + Method m = methods.get(3); assertMethodEquals(pool,m, "sayHello", "()V", - "b200041205b60006b1"); - + "b2001c1222b60024b1"); + } { - Method m = methods.get(4); + Method m = methods.get(4); assertMethodEquals(pool,m, "main", "([Ljava/lang/String;)V", - "bb0007591208101db700094c2bb6000ab1"); + "bb000159122b101db7002d4c2bb6002fb1"); } } From 1249d07b4113598d203af6edb35c7d81d6079a83 Mon Sep 17 00:00:00 2001 From: GZ-RXP <283091182@qq.com> Date: Sun, 16 Apr 2017 15:32:45 +0800 Subject: [PATCH 40/41] jvm 2nd week DataStructure -StackUtil --- .../loader/ClassFileLoader.java | 93 +++++++++++ .../test/ClassFileloaderTest.java | 92 +++++++++++ .../test/EmployeeV1.java | 0 .../com/coderising/jvm/clz/AccessFlag.java | 25 +++ .../com/coderising/jvm/clz/ClassFile.java | 75 +++++++++ .../com/coderising/jvm/clz/ClassIndex.java | 19 +++ .../coderising/jvm/constant/ClassInfo.java | 24 +++ .../coderising/jvm/constant/ConstantInfo.java | 29 ++++ .../coderising/jvm/constant/ConstantPool.java | 29 ++++ .../coderising/jvm/constant/FieldRefInfo.java | 54 +++++++ .../jvm/constant/MethodRefInfo.java | 55 +++++++ .../jvm/constant/NameAndTypeInfo.java | 45 ++++++ .../jvm/constant/NullConstantInfo.java | 13 ++ .../coderising/jvm/constant/StringInfo.java | 26 ++++ .../com/coderising/jvm/constant/UTF8Info.java | 32 ++++ .../jvm/loader/ByteCodeIterator.java | 87 +++++++++++ .../jvm/loader/ClassFileLoader.java | 147 ++++++++++++------ .../jvm/loader/ClassFileParser.java | 101 ++++++++++++ .../jvm/test/ClassFileloaderTest.java | 134 ++++++++++++++-- .../basic => coderising/array}/ArrayList.java | 5 +- .../array}/ArrayListTest.java | 22 +-- .../283091182/src/com/coding/basic/Queue.java | 2 + .../283091182/src/com/coding/basic/Stack.java | 2 + .../basic/{ => linklist}/LinkedList.java | 5 +- .../src/com/coding/basic/stack/Stack.java | 50 ++++++ .../src/com/coding/basic/stack/StackUtil.java | 140 +++++++++++++++++ 26 files changed, 1231 insertions(+), 75 deletions(-) create mode 100644 group11/283091182/mini-jvm-week1-bk/loader/ClassFileLoader.java create mode 100644 group11/283091182/mini-jvm-week1-bk/test/ClassFileloaderTest.java rename group11/283091182/{mini-jvm/com/coderising/jvm => mini-jvm-week1-bk}/test/EmployeeV1.java (100%) create mode 100644 group11/283091182/mini-jvm/com/coderising/jvm/clz/AccessFlag.java create mode 100644 group11/283091182/mini-jvm/com/coderising/jvm/clz/ClassFile.java create mode 100644 group11/283091182/mini-jvm/com/coderising/jvm/clz/ClassIndex.java create mode 100644 group11/283091182/mini-jvm/com/coderising/jvm/constant/ClassInfo.java create mode 100644 group11/283091182/mini-jvm/com/coderising/jvm/constant/ConstantInfo.java create mode 100644 group11/283091182/mini-jvm/com/coderising/jvm/constant/ConstantPool.java create mode 100644 group11/283091182/mini-jvm/com/coderising/jvm/constant/FieldRefInfo.java create mode 100644 group11/283091182/mini-jvm/com/coderising/jvm/constant/MethodRefInfo.java create mode 100644 group11/283091182/mini-jvm/com/coderising/jvm/constant/NameAndTypeInfo.java create mode 100644 group11/283091182/mini-jvm/com/coderising/jvm/constant/NullConstantInfo.java create mode 100644 group11/283091182/mini-jvm/com/coderising/jvm/constant/StringInfo.java create mode 100644 group11/283091182/mini-jvm/com/coderising/jvm/constant/UTF8Info.java create mode 100644 group11/283091182/mini-jvm/com/coderising/jvm/loader/ByteCodeIterator.java create mode 100644 group11/283091182/mini-jvm/com/coderising/jvm/loader/ClassFileParser.java rename group11/283091182/src/com/{coding/basic => coderising/array}/ArrayList.java (91%) rename group11/283091182/src/com/{coding/basic => coderising/array}/ArrayListTest.java (69%) rename group11/283091182/src/com/coding/basic/{ => linklist}/LinkedList.java (92%) create mode 100644 group11/283091182/src/com/coding/basic/stack/Stack.java create mode 100644 group11/283091182/src/com/coding/basic/stack/StackUtil.java diff --git a/group11/283091182/mini-jvm-week1-bk/loader/ClassFileLoader.java b/group11/283091182/mini-jvm-week1-bk/loader/ClassFileLoader.java new file mode 100644 index 0000000000..527e2f14d8 --- /dev/null +++ b/group11/283091182/mini-jvm-week1-bk/loader/ClassFileLoader.java @@ -0,0 +1,93 @@ +package com.coderising.jvm.loader; + +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.Iterator; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + + File classFile = getClassFileFromPath(className); + + byte[] buffer = new byte[1024]; + try { + FileInputStream fis = new FileInputStream(classFile); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + int readLen; + while((readLen = fis.read(buffer))>-1){ + baos.write(buffer, 0, readLen); + } + + return baos.toByteArray(); + + } catch (FileNotFoundException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } catch (IOException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + } + + + public void addClassPath(String path) { + File clzPath = new File(path); + if(clzPath.exists() && clzPath.isDirectory()){ + this.clzPaths.add(path); + }else{ + System.out.println("Invalid path:"+ path); + } + } + + + + public String getClassPath(){ + StringBuilder sb = new StringBuilder(); + Iterator it = this.clzPaths.iterator(); + while(it.hasNext()){ + if(sb.length()>0){ + sb.append(";"); + } + sb.append(it.next()); + } + return sb.toString(); + } + + public File getClassFileFromPath(String className) { + Iterator it = this.clzPaths.iterator(); + + //replace "." with "\\" in windows + String fullclassPath = className.replaceAll("\\.", (File.separatorChar=='\\')?"\\\\":"/")+".class"; + + while(it.hasNext()){ + File clzFile; + String path = (String)it.next(); + if(path.endsWith(String.valueOf(File.separatorChar))){ + clzFile = new File(path+fullclassPath); + }else{ + clzFile = new File(path+File.separatorChar+fullclassPath); + } + + //Check file before further proceed + if(clzFile.exists()&&clzFile.isFile()){ + return clzFile; + } + } + + throw new RuntimeException("Class not found:"+className); + } + + + +} diff --git a/group11/283091182/mini-jvm-week1-bk/test/ClassFileloaderTest.java b/group11/283091182/mini-jvm-week1-bk/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..21d7e97074 --- /dev/null +++ b/group11/283091182/mini-jvm-week1-bk/test/ClassFileloaderTest.java @@ -0,0 +1,92 @@ +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 = "C:\\Users\\Administrator\\mygit\\coding2017\\liuxin\\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 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/group11/283091182/mini-jvm/com/coderising/jvm/constant/FieldRefInfo.java b/group11/283091182/mini-jvm/com/coderising/jvm/constant/FieldRefInfo.java new file mode 100644 index 0000000000..65475e194c --- /dev/null +++ b/group11/283091182/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/group11/283091182/mini-jvm/com/coderising/jvm/constant/MethodRefInfo.java b/group11/283091182/mini-jvm/com/coderising/jvm/constant/MethodRefInfo.java new file mode 100644 index 0000000000..7f05870020 --- /dev/null +++ b/group11/283091182/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/group11/283091182/mini-jvm/com/coderising/jvm/constant/NameAndTypeInfo.java b/group11/283091182/mini-jvm/com/coderising/jvm/constant/NameAndTypeInfo.java new file mode 100644 index 0000000000..402f9dec86 --- /dev/null +++ b/group11/283091182/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/group11/283091182/mini-jvm/com/coderising/jvm/constant/NullConstantInfo.java b/group11/283091182/mini-jvm/com/coderising/jvm/constant/NullConstantInfo.java new file mode 100644 index 0000000000..936736016f --- /dev/null +++ b/group11/283091182/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/group11/283091182/mini-jvm/com/coderising/jvm/constant/StringInfo.java b/group11/283091182/mini-jvm/com/coderising/jvm/constant/StringInfo.java new file mode 100644 index 0000000000..f1f8eb4ed4 --- /dev/null +++ b/group11/283091182/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/group11/283091182/mini-jvm/com/coderising/jvm/constant/UTF8Info.java b/group11/283091182/mini-jvm/com/coderising/jvm/constant/UTF8Info.java new file mode 100644 index 0000000000..5cac9f04f7 --- /dev/null +++ b/group11/283091182/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/group11/283091182/mini-jvm/com/coderising/jvm/loader/ByteCodeIterator.java b/group11/283091182/mini-jvm/com/coderising/jvm/loader/ByteCodeIterator.java new file mode 100644 index 0000000000..514d85e08f --- /dev/null +++ b/group11/283091182/mini-jvm/com/coderising/jvm/loader/ByteCodeIterator.java @@ -0,0 +1,87 @@ +package com.coderising.jvm.loader; + +public class ByteCodeIterator { + private byte[] bytes; + private int pos = 0; + public ByteCodeIterator(byte[] byteCodes){ + this.bytes = byteCodes; + } + + public boolean hasNext(){ + return this.pos < bytes.length-1; + } + + public byte next(){ + byte b = bytes[pos]; + pos ++; + return b; + } + + public byte[] getBytes(int len){ + if(pos+len>bytes.length){ + throw new RuntimeException("Index out of bounds:"+(pos+len)); + } + byte[] bytes = new byte[len]; + int idx = 0; + while(hasNext() && idx-1){ - baos.write(buffer, 0, readLen); - } + 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)); - return baos.toByteArray(); - - } catch (FileNotFoundException e) { - e.printStackTrace(); - throw new RuntimeException(e); - } catch (IOException e) { + } catch (IOException e) { e.printStackTrace(); - throw new RuntimeException(e); + return null; } } + public void addClassPath(String path) { - File clzPath = new File(path); - if(clzPath.exists() && clzPath.isDirectory()){ - this.clzPaths.add(path); - }else{ - System.out.println("Invalid path:"+ path); + if(this.clzPaths.contains(path)){ + return; } + + this.clzPaths.add(path); + } public String getClassPath(){ - StringBuilder sb = new StringBuilder(); - Iterator it = this.clzPaths.iterator(); - while(it.hasNext()){ - if(sb.length()>0){ - sb.append(";"); + 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/group11/283091182/src/com/coding/basic/ArrayList.java b/group11/283091182/src/com/coderising/array/ArrayList.java similarity index 91% rename from group11/283091182/src/com/coding/basic/ArrayList.java rename to group11/283091182/src/com/coderising/array/ArrayList.java index 402d05c019..18b0dcca6f 100644 --- a/group11/283091182/src/com/coding/basic/ArrayList.java +++ b/group11/283091182/src/com/coderising/array/ArrayList.java @@ -1,7 +1,10 @@ -package com.coding.basic; +package com.coderising.array; import java.util.Arrays; +import com.coding.basic.Iterator; +import com.coding.basic.List; + public class ArrayList implements List { private int size = 0; diff --git a/group11/283091182/src/com/coding/basic/ArrayListTest.java b/group11/283091182/src/com/coderising/array/ArrayListTest.java similarity index 69% rename from group11/283091182/src/com/coding/basic/ArrayListTest.java rename to group11/283091182/src/com/coderising/array/ArrayListTest.java index 7807fa831e..8bdc0515d1 100644 --- a/group11/283091182/src/com/coding/basic/ArrayListTest.java +++ b/group11/283091182/src/com/coderising/array/ArrayListTest.java @@ -1,7 +1,7 @@ /** * */ -package com.coding.basic; +package com.coderising.array; import static org.junit.Assert.*; @@ -51,7 +51,7 @@ public void tearDown() throws Exception { } /** - * Test method for {@link com.coding.basic.ArrayList#add(java.lang.Object)}. + * Test method for {@link com.coderising.array.ArrayList#add(java.lang.Object)}. */ @Test public final void testAddObject() { @@ -65,7 +65,7 @@ public final void testAddObject() { } /** - * Test method for {@link com.coding.basic.ArrayList#add(int, java.lang.Object)}. + * Test method for {@link com.coderising.array.ArrayList#add(int, java.lang.Object)}. */ @Test public final void testAddIntObject() { @@ -78,14 +78,14 @@ public final void testAddIntObject() { assertEquals(3,al.size()); } /** - * Test method for {@link com.coding.basic.ArrayList#add(int, java.lang.Object)}. + * Test method for {@link com.coderising.array.ArrayList#add(int, java.lang.Object)}. */ @Test(expected=IndexOutOfBoundsException.class) public final void testAddIntObjectWithException1() { al.add(-1, "aaa"); } /** - * Test method for {@link com.coding.basic.ArrayList#add(int, java.lang.Object)}. + * Test method for {@link com.coderising.array.ArrayList#add(int, java.lang.Object)}. */ @Test(expected=IndexOutOfBoundsException.class) public final void testAddIntObjectWithException2() { @@ -94,21 +94,21 @@ public final void testAddIntObjectWithException2() { } /** - * Test method for {@link com.coding.basic.ArrayList#get(int)}. + * Test method for {@link com.coderising.array.ArrayList#get(int)}. */ @Test public final void testGet() { fail("Not yet implemented"); // TODO } /** - * Test method for {@link com.coding.basic.ArrayList#get(int)}. + * Test method for {@link com.coderising.array.ArrayList#get(int)}. */ @Test public final void testGetWithException1() { fail("Not yet implemented"); // TODO } /** - * Test method for {@link com.coding.basic.ArrayList#get(int)}. + * Test method for {@link com.coderising.array.ArrayList#get(int)}. */ @Test public final void testGetWithException2() { @@ -116,7 +116,7 @@ public final void testGetWithException2() { } /** - * Test method for {@link com.coding.basic.ArrayList#remove(int)}. + * Test method for {@link com.coderising.array.ArrayList#remove(int)}. */ @Test public final void testRemove() { @@ -124,7 +124,7 @@ public final void testRemove() { } /** - * Test method for {@link com.coding.basic.ArrayList#size()}. + * Test method for {@link com.coderising.array.ArrayList#size()}. */ @Test public final void testSize() { @@ -132,7 +132,7 @@ public final void testSize() { } /** - * Test method for {@link com.coding.basic.ArrayList#iterator()}. + * Test method for {@link com.coderising.array.ArrayList#iterator()}. */ @Test public final void testIterator() { diff --git a/group11/283091182/src/com/coding/basic/Queue.java b/group11/283091182/src/com/coding/basic/Queue.java index 45fea2a118..0f1f068e19 100644 --- a/group11/283091182/src/com/coding/basic/Queue.java +++ b/group11/283091182/src/com/coding/basic/Queue.java @@ -1,5 +1,7 @@ package com.coding.basic; +import com.coding.basic.linklist.LinkedList; + public class Queue { private LinkedList list = new LinkedList(); diff --git a/group11/283091182/src/com/coding/basic/Stack.java b/group11/283091182/src/com/coding/basic/Stack.java index 915d173b1b..5ff5ce5279 100644 --- a/group11/283091182/src/com/coding/basic/Stack.java +++ b/group11/283091182/src/com/coding/basic/Stack.java @@ -1,5 +1,7 @@ package com.coding.basic; +import com.coderising.array.ArrayList; + public class Stack { private ArrayList elementData = new ArrayList(); diff --git a/group11/283091182/src/com/coding/basic/LinkedList.java b/group11/283091182/src/com/coding/basic/linklist/LinkedList.java similarity index 92% rename from group11/283091182/src/com/coding/basic/LinkedList.java rename to group11/283091182/src/com/coding/basic/linklist/LinkedList.java index 233c243130..2c5959890a 100644 --- a/group11/283091182/src/com/coding/basic/LinkedList.java +++ b/group11/283091182/src/com/coding/basic/linklist/LinkedList.java @@ -1,4 +1,7 @@ -package com.coding.basic; +package com.coding.basic.linklist; + +import com.coding.basic.Iterator; +import com.coding.basic.List; public class LinkedList implements List { diff --git a/group11/283091182/src/com/coding/basic/stack/Stack.java b/group11/283091182/src/com/coding/basic/stack/Stack.java new file mode 100644 index 0000000000..c59be3c1e4 --- /dev/null +++ b/group11/283091182/src/com/coding/basic/stack/Stack.java @@ -0,0 +1,50 @@ +package com.coding.basic.stack; + +import com.coderising.array.ArrayList; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(elementData.size()==0)throw new RuntimeException("Stack is empty."); + return elementData.remove(elementData.size()-1); + } + + public Object peek(){ + if(elementData.size()==0)throw new RuntimeException("Stack is empty."); + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return (elementData.size()==0); + } + public int size(){ + return elementData.size(); + } + + @Override + public String toString(){ + return elementData.toString(); + } + + public static void main(String[] args){ + Stack s = new Stack(); + s.push("aaa"); + s.push("bbb"); + s.push("ccc"); + System.out.println(s); + System.out.println(s.isEmpty()); + System.out.println(s.size()); + System.out.println(s.peek()); + System.out.println(s.pop()); + System.out.println(s.pop()); + System.out.println(s.pop()); + System.out.println(s); + System.out.println(s.isEmpty()); + System.out.println(s.size()); + //System.out.println(s.pop()); + } +} diff --git a/group11/283091182/src/com/coding/basic/stack/StackUtil.java b/group11/283091182/src/com/coding/basic/stack/StackUtil.java new file mode 100644 index 0000000000..cd5aa08d06 --- /dev/null +++ b/group11/283091182/src/com/coding/basic/stack/StackUtil.java @@ -0,0 +1,140 @@ +package com.coding.basic.stack; + +public class StackUtil { + + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + if(s==null || s.isEmpty()){ + return; + }; + Stack temp = new Stack(); + int counter = s.size(); + while(counter>1){ + //Get the peek one + Object o = s.pop(); + for(int i=0;is.size()){ + throw new RuntimeException("Index Out of Bound:"+ len); + } + Object[] objArr = new Object[len]; + Stack tmpStk = new Stack(); + for(int i=0;i Date: Sun, 16 Apr 2017 15:50:16 +0800 Subject: [PATCH 41/41] sixHomework --- .../RemoteSystemsTempFiles/.project | 12 ++ .../FourthHomework/jvm/ClassFileLoader.java | 51 --------- .../src/FourthHomework/jvm/TestJVM.java | 7 -- .../coderising/jvm/attr/AttributeInfo.java | 19 ++++ .../com/coderising/jvm/attr/CodeAttr.java | 86 ++++++++++++++ .../coderising/jvm/attr/LineNumberTable.java | 52 +++++++++ .../jvm/attr/LocalVariableItem.java | 41 +++++++ .../jvm/attr/LocalVariableTable.java | 41 +++++++ .../coderising/jvm/attr/StackMapTable.java | 29 +++++ .../com/coderising}/jvm/clz/AccessFlag.java | 0 .../com/coderising}/jvm/clz/ClassFile.java | 20 ++++ .../com/coderising}/jvm/clz/ClassIndex.java | 0 .../coderising}/jvm/constant/ClassInfo.java | 0 .../jvm/constant/ConstantInfo.java | 0 .../jvm/constant/ConstantPool.java | 1 + .../jvm/constant/FieldRefInfo.java | 0 .../jvm/constant/MethodRefInfo.java | 0 .../jvm/constant/NameAndTypeInfo.java | 0 .../jvm/constant/NullConstantInfo.java | 0 .../coderising}/jvm/constant/StringInfo.java | 0 .../coderising}/jvm/constant/UTF8Info.java | 0 .../com/coderising/jvm/field/Field.java | 43 +++++++ .../jvm/loader/ByteCodeIterator.java | 16 +++ .../jvm/loader/ClassFileLoader.java | 0 .../jvm/loader/ClassFileParser.java | 43 ++++++- .../com/coderising}/jvm/loader/TestJVM.java | 0 .../com/coderising/jvm/method/Method.java | 68 +++++++++++ .../BigHomework}/Download/DownloadThread.java | 0 .../BigHomework}/Download/FileDownloader.java | 0 .../BigHomework}/Download/api/Connection.java | 0 .../Download/api/ConnectionException.java | 0 .../Download/api/ConnectionManager.java | 0 .../Download/api/DownloadListener.java | 0 .../Download/impl/ConnectionImpl.java | 0 .../Download/impl/ConnectionManagerImpl.java | 0 .../baseDataStructure_1}/ArrayList.java | 0 .../baseDataStructure_1}/LinkedList.java | 0 .../baseDataStructure_2}/ArrayUtil.java | 0 .../baseDataStructure_3}/LinkedList.java | 0 .../LRUPageFrame.java | 0 .../baseDataStructure_5_Stack}/StackUtil.java | 0 .../InfixExpr.java | 106 ++++++++++++++++++ .../DataStructure_5_Stack/TestSwitch.java | 15 --- .../FifthHomework/jvm/loader/TestArray.java | 8 -- .../jvm/loader/TestArrayList.java | 14 --- .../jvm/loader/TestByteArrayToHexString.java | 15 --- .../jvm/loader/TestByteArrayToInt.java | 12 -- .../jvm/loader/TestByteToInt.java | 12 -- .../jvm/loader/TestIntegerToHexString.java | 10 -- .../test/FourthHomework/JVM/EmployeeV1.java | 28 ----- .../JVM/TestClassFileLoader.java | 67 ----------- .../jvm/loader => Mini_JVM}/EmployeeV1.java | 0 .../TestClassFileLoader.java | 92 +++++++++++++-- .../Download/FileDownloaderTest.java | 0 .../baseDataStructure_2}/ArrayUtilTest.java | 0 .../baseDataStructure_3}/TestLinkedList.java | 0 .../TestLRUPageFrame.java | 0 .../TestStackUtil.java | 0 .../InfixExpr.java | 106 ++++++++++++++++++ 59 files changed, 763 insertions(+), 251 deletions(-) create mode 100644 group11/1310368322/RemoteSystemsTempFiles/.project delete mode 100644 group11/1310368322/src/FourthHomework/jvm/ClassFileLoader.java delete mode 100644 group11/1310368322/src/FourthHomework/jvm/TestJVM.java create mode 100644 group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/AttributeInfo.java create mode 100644 group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/CodeAttr.java create mode 100644 group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/LineNumberTable.java create mode 100644 group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/LocalVariableItem.java create mode 100644 group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/LocalVariableTable.java create mode 100644 group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/StackMapTable.java rename group11/1310368322/src/{FifthHomework => Mini_JVM/com/coderising}/jvm/clz/AccessFlag.java (100%) rename group11/1310368322/src/{FifthHomework => Mini_JVM/com/coderising}/jvm/clz/ClassFile.java (78%) rename group11/1310368322/src/{FifthHomework => Mini_JVM/com/coderising}/jvm/clz/ClassIndex.java (100%) rename group11/1310368322/src/{FifthHomework => Mini_JVM/com/coderising}/jvm/constant/ClassInfo.java (100%) rename group11/1310368322/src/{FifthHomework => Mini_JVM/com/coderising}/jvm/constant/ConstantInfo.java (100%) rename group11/1310368322/src/{FifthHomework => Mini_JVM/com/coderising}/jvm/constant/ConstantPool.java (93%) rename group11/1310368322/src/{FifthHomework => Mini_JVM/com/coderising}/jvm/constant/FieldRefInfo.java (100%) rename group11/1310368322/src/{FifthHomework => Mini_JVM/com/coderising}/jvm/constant/MethodRefInfo.java (100%) rename group11/1310368322/src/{FifthHomework => Mini_JVM/com/coderising}/jvm/constant/NameAndTypeInfo.java (100%) rename group11/1310368322/src/{FifthHomework => Mini_JVM/com/coderising}/jvm/constant/NullConstantInfo.java (100%) rename group11/1310368322/src/{FifthHomework => Mini_JVM/com/coderising}/jvm/constant/StringInfo.java (100%) rename group11/1310368322/src/{FifthHomework => Mini_JVM/com/coderising}/jvm/constant/UTF8Info.java (100%) create mode 100644 group11/1310368322/src/Mini_JVM/com/coderising/jvm/field/Field.java rename group11/1310368322/src/{FifthHomework => Mini_JVM/com/coderising}/jvm/loader/ByteCodeIterator.java (74%) rename group11/1310368322/src/{FifthHomework => Mini_JVM/com/coderising}/jvm/loader/ClassFileLoader.java (100%) rename group11/1310368322/src/{FifthHomework => Mini_JVM/com/coderising}/jvm/loader/ClassFileParser.java (81%) rename group11/1310368322/src/{FifthHomework => Mini_JVM/com/coderising}/jvm/loader/TestJVM.java (100%) create mode 100644 group11/1310368322/src/Mini_JVM/com/coderising/jvm/method/Method.java rename group11/1310368322/src/{ThirdHomework => data_structure/BigHomework}/Download/DownloadThread.java (100%) rename group11/1310368322/src/{ThirdHomework => data_structure/BigHomework}/Download/FileDownloader.java (100%) rename group11/1310368322/src/{ThirdHomework => data_structure/BigHomework}/Download/api/Connection.java (100%) rename group11/1310368322/src/{ThirdHomework => data_structure/BigHomework}/Download/api/ConnectionException.java (100%) rename group11/1310368322/src/{ThirdHomework => data_structure/BigHomework}/Download/api/ConnectionManager.java (100%) rename group11/1310368322/src/{ThirdHomework => data_structure/BigHomework}/Download/api/DownloadListener.java (100%) rename group11/1310368322/src/{ThirdHomework => data_structure/BigHomework}/Download/impl/ConnectionImpl.java (100%) rename group11/1310368322/src/{ThirdHomework => data_structure/BigHomework}/Download/impl/ConnectionManagerImpl.java (100%) rename group11/1310368322/src/{FirstHomework/BaseDataStructure => data_structure/baseDataStructure_1}/ArrayList.java (100%) rename group11/1310368322/src/{FirstHomework/BaseDataStructure => data_structure/baseDataStructure_1}/LinkedList.java (100%) rename group11/1310368322/src/{SecondHomework/BaseDataStructure => data_structure/baseDataStructure_2}/ArrayUtil.java (100%) rename group11/1310368322/src/{ThirdHomework/BaseDataStructure => data_structure/baseDataStructure_3}/LinkedList.java (100%) rename group11/1310368322/src/{FourthHomework/BaseDataStructure => data_structure/baseDataStructure_4_LRU}/LRUPageFrame.java (100%) rename group11/1310368322/src/{FifthHomework/DataStructure_5_Stack => data_structure/baseDataStructure_5_Stack}/StackUtil.java (100%) create mode 100644 group11/1310368322/src/data_structure/baseDataStructure_6InfixExpr/InfixExpr.java delete mode 100644 group11/1310368322/test/FifthHomework/DataStructure_5_Stack/TestSwitch.java delete mode 100644 group11/1310368322/test/FifthHomework/jvm/loader/TestArray.java delete mode 100644 group11/1310368322/test/FifthHomework/jvm/loader/TestArrayList.java delete mode 100644 group11/1310368322/test/FifthHomework/jvm/loader/TestByteArrayToHexString.java delete mode 100644 group11/1310368322/test/FifthHomework/jvm/loader/TestByteArrayToInt.java delete mode 100644 group11/1310368322/test/FifthHomework/jvm/loader/TestByteToInt.java delete mode 100644 group11/1310368322/test/FifthHomework/jvm/loader/TestIntegerToHexString.java delete mode 100644 group11/1310368322/test/FourthHomework/JVM/EmployeeV1.java delete mode 100644 group11/1310368322/test/FourthHomework/JVM/TestClassFileLoader.java rename group11/1310368322/test/{FifthHomework/jvm/loader => Mini_JVM}/EmployeeV1.java (100%) rename group11/1310368322/test/{FifthHomework/jvm/loader => Mini_JVM}/TestClassFileLoader.java (68%) rename group11/1310368322/test/{ThirdHomework => data_structure/BigHomework}/Download/FileDownloaderTest.java (100%) rename group11/1310368322/test/{SecondHomwork/BaseDataStructure => data_structure/baseDataStructure_2}/ArrayUtilTest.java (100%) rename group11/1310368322/test/{ThirdHomework/BaseDataStructure => data_structure/baseDataStructure_3}/TestLinkedList.java (100%) rename group11/1310368322/test/{FourthHomework/BaseDataStructure => data_structure/baseDataStructure_4_LRU}/TestLRUPageFrame.java (100%) rename group11/1310368322/test/{FifthHomework/DataStructure_5_Stack => data_structure/baseDataStructure_5_Stack}/TestStackUtil.java (100%) create mode 100644 group11/1310368322/test/data_structure/baseDataStructure_6InfixExpr/InfixExpr.java diff --git a/group11/1310368322/RemoteSystemsTempFiles/.project b/group11/1310368322/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group11/1310368322/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group11/1310368322/src/FourthHomework/jvm/ClassFileLoader.java b/group11/1310368322/src/FourthHomework/jvm/ClassFileLoader.java deleted file mode 100644 index 6db696e5aa..0000000000 --- a/group11/1310368322/src/FourthHomework/jvm/ClassFileLoader.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.coderising.jvm.loader; - -import java.io.BufferedInputStream; -import java.io.DataInputStream; -import java.io.FileInputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import org.junit.runners.Parameterized.Parameters; - -public class ClassFileLoader { - private List clzPaths = new ArrayList(); - int countForClassPath = 0; - int countForReadBinaryCode = 0; - byte [] a = new byte[10000]; - - /* ָ·ȡļ䱣浽һֽУ - * @Parameters ָ· - * @ֽ - */ - public byte[] readBinaryCode(String className) throws IOException{ - DataInputStream dis = new DataInputStream( - new BufferedInputStream(new FileInputStream(className))); - for(int i = 0; dis.available() != 0; i++){ - a[i] = dis.readByte(); - countForReadBinaryCode++; - } - byte []target = new byte[countForReadBinaryCode]; - System.arraycopy(a, 0, target, 0, countForReadBinaryCode); - dis.close(); - return target; - } - - public void addClassPath(String path){ - clzPaths.add(path); - countForClassPath++; - } - - public String getClassPath(){ - StringBuffer buffer = new StringBuffer(); - for(int i = 0; i < countForClassPath; i++ ){ - if(i==countForClassPath-1){ - buffer.append(clzPaths.get(i)); - }else{ - buffer.append(clzPaths.get(i)+";"); - } - } - return buffer.toString(); - } -} diff --git a/group11/1310368322/src/FourthHomework/jvm/TestJVM.java b/group11/1310368322/src/FourthHomework/jvm/TestJVM.java deleted file mode 100644 index 735e4d1dc2..0000000000 --- a/group11/1310368322/src/FourthHomework/jvm/TestJVM.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.coderising.jvm.loader; - -public class TestJVM { - public static void main(String[] args) { - System.out.println("Hello"); - } -} diff --git a/group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/AttributeInfo.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/AttributeInfo.java new file mode 100644 index 0000000000..58f2190146 --- /dev/null +++ b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/AttributeInfo.java @@ -0,0 +1,19 @@ +package com.coderising.jvm.attr; + +public abstract class AttributeInfo { + public static final String CODE = "Code"; + public static final String CONST_VALUE = "ConstantValue"; + public static final String EXCEPTIONS = "Exceptions"; + public static final String LINE_NUM_TABLE = "LineNumberTable"; + public static final String LOCAL_VAR_TABLE = "LocalVariableTable"; + public static final String STACK_MAP_TABLE = "StackMapTable"; + + int attrNameIndex; + int attrLen ; + + public AttributeInfo(int attrNameIndex, int attrLen) { + this.attrNameIndex = attrNameIndex; + this.attrLen = attrLen; + } + +} diff --git a/group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/CodeAttr.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/CodeAttr.java new file mode 100644 index 0000000000..e07662a68b --- /dev/null +++ b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/CodeAttr.java @@ -0,0 +1,86 @@ +package com.coderising.jvm.attr; + +import com.coderising.jvm.clz.ClassFile; +import com.coderising.jvm.loader.ByteCodeIterator; + +public class CodeAttr extends AttributeInfo{ + + private int maxStack; + private int maxLocals; + private int codeLen; + private String code; + + public String getCode(){ + return code; + } + + + private LineNumberTable lineNumTable; + private LocalVariableTable localVarTable; + private StackMapTable stackMapTable; + + + + public CodeAttr(int attrNameIndex, int attrLen , int maxStack, int maxLocals, int codeLen,String code) { + super(attrNameIndex, attrLen); + this.maxStack = maxStack; + this.maxLocals = maxLocals; + this.codeLen = codeLen; + this.code = code; + } + + public void setLineNumberTable(LineNumberTable t) { + this.lineNumTable = t; + } + + public void setLocalVariableTable(LocalVariableTable t) { + this.localVarTable = t; + } + + private void setStackMapTable(StackMapTable t) { + this.stackMapTable = t; + + } + + public static CodeAttr parse(ClassFile clzFile, ByteCodeIterator iter){ + int attrNameIndex = iter.nextU2toInt(); + int attrLen = iter.nextU2toInt(); + int maxStack = iter.nextU2toInt(); + int max_Locals = iter.nextU2toInt(); + int codeLen = iter.nextU4toInt(); + // code + String code = iter.nextUxToHexString(codeLen); + CodeAttr codeAttr = new CodeAttr(attrNameIndex, attrLen, maxStack, max_Locals, max_Locals, code); + + int exceptionTableLen = iter.nextU2toInt(); + + if(exceptionTableLen > 0){ + String exTable = iter.nextUxToHexString(exceptionTableLen); + System.out.println("Encounted exception table, just ignore it"); + } + + int subAttributesCount = iter.nextU2toInt(); + + for(int i = 0; i < subAttributesCount; i++){ + int subAttrNameIndex = iter.nextU2toInt(); + String subAttrName = clzFile.getConstantPool().getUTF8String(subAttrNameIndex); + if(CodeAttr.LOCAL_VAR_TABLE.equalsIgnoreCase(subAttrName)){ + int subAttrLen = iter.nextU4toInt();// localVariableTable Ե + LocalVariableTable locVarTable = LocalVariableTable.parse(iter, subAttrNameIndex, subAttrLen); + codeAttr.setLocalVariableTable(locVarTable); + }else if(CodeAttr.LINE_NUM_TABLE.equalsIgnoreCase(subAttrName)){ + int subAttrLen = iter.nextU4toInt(); + LineNumberTable lineNumTable = LineNumberTable.parse(iter, subAttrNameIndex, subAttrLen); + codeAttr.setLineNumberTable(lineNumTable); + }else if(CodeAttr.STACK_MAP_TABLE.equalsIgnoreCase(subAttrName)){ + int subAttrLen = iter.nextU4toInt(); + StackMapTable stackMapTable = StackMapTable.parse(iter); + codeAttr.setStackMapTable(stackMapTable); + }else{ + throw new RuntimeException("need code to process" + subAttrName); + } + } + + return codeAttr; + } +} diff --git a/group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/LineNumberTable.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/LineNumberTable.java new file mode 100644 index 0000000000..24bcb25572 --- /dev/null +++ b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/LineNumberTable.java @@ -0,0 +1,52 @@ +package com.coderising.jvm.attr; + +import java.util.ArrayList; +import java.util.List; + +import com.coderising.jvm.loader.ByteCodeIterator; + +public class LineNumberTable extends AttributeInfo{ + + List items = new ArrayList();// кűкܶġкŶӦ + + // LineNumberTableһṹñ˿ + private static class LineNumberItem{ + int startPC;// ¼ֽк + int lineNum;// ¼JavaԴк + public int getStartPC() { + return startPC; + } + public void setStartPC(int startPC) { + this.startPC = startPC; + } + public int getLineNum() { + return lineNum; + } + public void setLineNum(int lineNum) { + this.lineNum = lineNum; + } + } + + public void addLineNumberItem(LineNumberItem item){ + this.items.add(item); + } + + public LineNumberTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + } + + public static LineNumberTable parse(ByteCodeIterator iter, int subAttrNameIndex, int subAttrLen){ + LineNumberTable lineNumTable = new LineNumberTable(subAttrNameIndex, subAttrLen); + int lineNumTableLen = iter.nextU2toInt(); + for(int i = 0; i < lineNumTableLen; i ++){ + int startPC = iter.nextU2toInt(); + int lineNum = iter.nextU2toInt(); + LineNumberItem lineNumItem = new LineNumberItem(); + lineNumItem.setStartPC(startPC); + lineNumItem.setLineNum(lineNum); + lineNumTable.addLineNumberItem(lineNumItem); + } + return lineNumTable; + } + +} diff --git a/group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/LocalVariableItem.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/LocalVariableItem.java new file mode 100644 index 0000000000..842e1d0a96 --- /dev/null +++ b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/LocalVariableItem.java @@ -0,0 +1,41 @@ +package com.coderising.jvm.attr; + +public class LocalVariableItem { + + private int startPC;// ֲڿʼʱֽƫ + private int length; // ֽij + private int nameIndex; // ֲ + private int descIndex; // ֲ + private int index; // ֲջ֡еľֲе slot λ + + public int getStartPC() { + return startPC; + } + public void setStartPC(int startPC) { + this.startPC = startPC; + } + public int getLength() { + return length; + } + public void setLength(int length) { + this.length = length; + } + public int getNameIndex() { + return nameIndex; + } + public void setNameIndex(int nameIndex) { + this.nameIndex = nameIndex; + } + public int getDescIndex() { + return descIndex; + } + public void setDescIndex(int descIndex) { + this.descIndex = descIndex; + } + public int getIndex() { + return index; + } + public void setIndex(int index) { + this.index = index; + } +} diff --git a/group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/LocalVariableTable.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/LocalVariableTable.java new file mode 100644 index 0000000000..757f28918b --- /dev/null +++ b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/LocalVariableTable.java @@ -0,0 +1,41 @@ +package com.coderising.jvm.attr; + +import java.util.ArrayList; +import java.util.List; + +import com.coderising.jvm.loader.ByteCodeIterator; + +public class LocalVariableTable extends AttributeInfo{ + + List items = new ArrayList(); + + public LocalVariableTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + } + + public static LocalVariableTable parse(ByteCodeIterator iter, int subAttrName, int subAttrLen){ + + LocalVariableTable locVarTable = new LocalVariableTable(subAttrName, subAttrLen); + int localVarTableLen = iter.nextU2toInt(); + for(int i = 0; i < localVarTableLen; i++){ + int startPC = iter.nextU2toInt(); + int length = iter.nextU2toInt(); + int nameIndex = iter.nextU2toInt(); + int descIndex = iter.nextU2toInt(); + int index = iter.nextU2toInt(); + LocalVariableItem locVarItem = new LocalVariableItem(); + locVarItem.setStartPC(startPC); + locVarItem.setLength(length); + locVarItem.setNameIndex(nameIndex); + locVarItem.setDescIndex(descIndex); + locVarItem.setIndex(index); + locVarTable.addLocalVariableItem(locVarItem); + } + + return locVarTable; + } + private void addLocalVariableItem(LocalVariableItem item) { + this.items.add(item); + } + +} diff --git a/group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/StackMapTable.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/StackMapTable.java new file mode 100644 index 0000000000..9df44fb5ca --- /dev/null +++ b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/attr/StackMapTable.java @@ -0,0 +1,29 @@ +package com.coderising.jvm.attr; + +import com.coderising.jvm.loader.ByteCodeIterator; + +public class StackMapTable extends AttributeInfo{ + + private String originalCode; + + public StackMapTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + } + + public static StackMapTable parse(ByteCodeIterator iter){ + int index = iter.nextU2toInt(); + int len = iter.nextU4toInt(); + StackMapTable t = new StackMapTable(index,len); + + //StackMapTable̫ӣ ٴ ֻԭʼĴ + String code = iter.nextUxToHexString(len); + t.setOriginalCode(code); + + return t; + } + + private void setOriginalCode(String code) { + this.originalCode = code; + + } +} diff --git a/group11/1310368322/src/FifthHomework/jvm/clz/AccessFlag.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/clz/AccessFlag.java similarity index 100% rename from group11/1310368322/src/FifthHomework/jvm/clz/AccessFlag.java rename to group11/1310368322/src/Mini_JVM/com/coderising/jvm/clz/AccessFlag.java diff --git a/group11/1310368322/src/FifthHomework/jvm/clz/ClassFile.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/clz/ClassFile.java similarity index 78% rename from group11/1310368322/src/FifthHomework/jvm/clz/ClassFile.java rename to group11/1310368322/src/Mini_JVM/com/coderising/jvm/clz/ClassFile.java index 3bf1a8c56e..4492d2df9e 100644 --- a/group11/1310368322/src/FifthHomework/jvm/clz/ClassFile.java +++ b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/clz/ClassFile.java @@ -1,7 +1,12 @@ package com.coderising.jvm.clz; +import java.util.ArrayList; +import java.util.List; + import com.coderising.jvm.constant.ClassInfo; import com.coderising.jvm.constant.ConstantPool; +import com.coderising.jvm.field.Field; +import com.coderising.jvm.method.Method; public class ClassFile { private int minorVersion; @@ -10,6 +15,8 @@ public class ClassFile { private AccessFlag accessFlag;// ڳ֮ private ClassIndex clzIndex;// ڷʱ־֮, ͽӿ private ConstantPool pool; + private List fields = new ArrayList(); + private List methods = new ArrayList(); public AccessFlag getAccessFlag(){ @@ -48,6 +55,19 @@ public void setConstantPool(ConstantPool pool){ this.pool = pool; } + public void addField(Field f){ + this.fields.add(f); + } + public List getFields(){ + return this.fields; + } + public void addMethod(Method m){ + this.methods.add(m); + } + public List getMethods() { + return methods; + } + public void print(){ if(this.accessFlag.isPublicClass()){ System.out.println("Access flag : public "); diff --git a/group11/1310368322/src/FifthHomework/jvm/clz/ClassIndex.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/clz/ClassIndex.java similarity index 100% rename from group11/1310368322/src/FifthHomework/jvm/clz/ClassIndex.java rename to group11/1310368322/src/Mini_JVM/com/coderising/jvm/clz/ClassIndex.java diff --git a/group11/1310368322/src/FifthHomework/jvm/constant/ClassInfo.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/constant/ClassInfo.java similarity index 100% rename from group11/1310368322/src/FifthHomework/jvm/constant/ClassInfo.java rename to group11/1310368322/src/Mini_JVM/com/coderising/jvm/constant/ClassInfo.java diff --git a/group11/1310368322/src/FifthHomework/jvm/constant/ConstantInfo.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/constant/ConstantInfo.java similarity index 100% rename from group11/1310368322/src/FifthHomework/jvm/constant/ConstantInfo.java rename to group11/1310368322/src/Mini_JVM/com/coderising/jvm/constant/ConstantInfo.java diff --git a/group11/1310368322/src/FifthHomework/jvm/constant/ConstantPool.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/constant/ConstantPool.java similarity index 93% rename from group11/1310368322/src/FifthHomework/jvm/constant/ConstantPool.java rename to group11/1310368322/src/Mini_JVM/com/coderising/jvm/constant/ConstantPool.java index 9dbd3ed9ef..82565b7f59 100644 --- a/group11/1310368322/src/FifthHomework/jvm/constant/ConstantPool.java +++ b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/constant/ConstantPool.java @@ -20,6 +20,7 @@ public ConstantInfo getConstantInfo(int index){ } public String getUTF8String(int index){ + System.out.println("index: " + index); return ((UTF8Info)this.constantInfos.get(index)).getValue(); } diff --git a/group11/1310368322/src/FifthHomework/jvm/constant/FieldRefInfo.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/constant/FieldRefInfo.java similarity index 100% rename from group11/1310368322/src/FifthHomework/jvm/constant/FieldRefInfo.java rename to group11/1310368322/src/Mini_JVM/com/coderising/jvm/constant/FieldRefInfo.java diff --git a/group11/1310368322/src/FifthHomework/jvm/constant/MethodRefInfo.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/constant/MethodRefInfo.java similarity index 100% rename from group11/1310368322/src/FifthHomework/jvm/constant/MethodRefInfo.java rename to group11/1310368322/src/Mini_JVM/com/coderising/jvm/constant/MethodRefInfo.java diff --git a/group11/1310368322/src/FifthHomework/jvm/constant/NameAndTypeInfo.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/constant/NameAndTypeInfo.java similarity index 100% rename from group11/1310368322/src/FifthHomework/jvm/constant/NameAndTypeInfo.java rename to group11/1310368322/src/Mini_JVM/com/coderising/jvm/constant/NameAndTypeInfo.java diff --git a/group11/1310368322/src/FifthHomework/jvm/constant/NullConstantInfo.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/constant/NullConstantInfo.java similarity index 100% rename from group11/1310368322/src/FifthHomework/jvm/constant/NullConstantInfo.java rename to group11/1310368322/src/Mini_JVM/com/coderising/jvm/constant/NullConstantInfo.java diff --git a/group11/1310368322/src/FifthHomework/jvm/constant/StringInfo.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/constant/StringInfo.java similarity index 100% rename from group11/1310368322/src/FifthHomework/jvm/constant/StringInfo.java rename to group11/1310368322/src/Mini_JVM/com/coderising/jvm/constant/StringInfo.java diff --git a/group11/1310368322/src/FifthHomework/jvm/constant/UTF8Info.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/constant/UTF8Info.java similarity index 100% rename from group11/1310368322/src/FifthHomework/jvm/constant/UTF8Info.java rename to group11/1310368322/src/Mini_JVM/com/coderising/jvm/constant/UTF8Info.java diff --git a/group11/1310368322/src/Mini_JVM/com/coderising/jvm/field/Field.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/field/Field.java new file mode 100644 index 0000000000..9e110b1867 --- /dev/null +++ b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/field/Field.java @@ -0,0 +1,43 @@ +package com.coderising.jvm.field; + +import com.coderising.jvm.constant.ConstantPool; +import com.coderising.jvm.loader.ByteCodeIterator; + +public class Field { + private int accessFlag; + private int nameIndex; + private int descriptorIndex; + + private ConstantPool pool; + public int getNameIndex(){ + return nameIndex; + } + public int getDescIndex(){ + return descriptorIndex; + } + public String toString(){ + return pool.getUTF8String(nameIndex) + ":" + pool.getUTF8String(descriptorIndex); + } + public Field(int accessFlag, int nameIndex, int descriptorIndex, ConstantPool pool){ + this.accessFlag = accessFlag; + this.nameIndex = nameIndex; + this.descriptorIndex = descriptorIndex; + this.pool = pool; + } + + + public static Field parse(ConstantPool pool, ByteCodeIterator iter){ + int accessFlag = iter.nextU2toInt(); + int nameIndex = iter.nextU2toInt(); + int descriptorIndex = iter.nextU2toInt(); + int attrLen = iter.nextU2toInt(); + if(attrLen > 0){ + throw new RuntimeException("Field attributes has not been implemented"); + } + Field field = new Field(accessFlag,nameIndex,descriptorIndex,pool); + + return field; + } + + +} diff --git a/group11/1310368322/src/FifthHomework/jvm/loader/ByteCodeIterator.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/loader/ByteCodeIterator.java similarity index 74% rename from group11/1310368322/src/FifthHomework/jvm/loader/ByteCodeIterator.java rename to group11/1310368322/src/Mini_JVM/com/coderising/jvm/loader/ByteCodeIterator.java index 4d740d8149..41516cd506 100644 --- a/group11/1310368322/src/FifthHomework/jvm/loader/ByteCodeIterator.java +++ b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/loader/ByteCodeIterator.java @@ -42,4 +42,20 @@ public String nextU4ToHexString(){ } return buffer.toString(); } + + public String nextUxToHexString(int len) { + + StringBuffer buffer = new StringBuffer(); + for(int i = 0; i < len; i++){ + int a = codes[pos++] & 0xFF; + String strHex = Integer.toHexString(a); + if(strHex.length() < 2){ + strHex = "0" + strHex; + } + buffer.append(strHex); + } + + return buffer.toString(); + + } } diff --git a/group11/1310368322/src/FifthHomework/jvm/loader/ClassFileLoader.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/loader/ClassFileLoader.java similarity index 100% rename from group11/1310368322/src/FifthHomework/jvm/loader/ClassFileLoader.java rename to group11/1310368322/src/Mini_JVM/com/coderising/jvm/loader/ClassFileLoader.java diff --git a/group11/1310368322/src/FifthHomework/jvm/loader/ClassFileParser.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/loader/ClassFileParser.java similarity index 81% rename from group11/1310368322/src/FifthHomework/jvm/loader/ClassFileParser.java rename to group11/1310368322/src/Mini_JVM/com/coderising/jvm/loader/ClassFileParser.java index a2de56dfc7..43e3796ef5 100644 --- a/group11/1310368322/src/FifthHomework/jvm/loader/ClassFileParser.java +++ b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/loader/ClassFileParser.java @@ -15,6 +15,8 @@ import com.coderising.jvm.constant.NullConstantInfo; import com.coderising.jvm.constant.StringInfo; import com.coderising.jvm.constant.UTF8Info; +import com.coderising.jvm.field.Field; +import com.coderising.jvm.method.Method; /* * һֽ飬ɶа ClassFile @@ -31,7 +33,7 @@ public ClassFile parse(byte[] codes){ } clzFile.setMinorVersion(iter.nextU2toInt()); clzFile.setMajorVersion(iter.nextU2toInt()); - + // ȡ ConstantPool pool = parseConstantPool(iter); AccessFlag accessFlag = parseAccessFlag(iter); @@ -40,10 +42,17 @@ public ClassFile parse(byte[] codes){ clzFile.setClassIndex(clzIndex); clzFile.setConstantPool(pool);// м볣 + parseInterfaces(iter); + parseFields(clzFile,iter); + parseMethods(clzFile,iter); + return clzFile; } - + + + + private AccessFlag parseAccessFlag(ByteCodeIterator iter){ int accessFlagValue = iter.nextU2toInt(); AccessFlag accessFlag = new AccessFlag(accessFlagValue); @@ -127,4 +136,34 @@ private ConstantPool parseConstantPool(ByteCodeIterator iter){ } return pool; } + + private void parseInterfaces(ByteCodeIterator iter) { + int interfacesCount = iter.nextU2toInt(); + System.out.println("interfacesCount: " + interfacesCount); + if(interfacesCount > 0){ + throw new RuntimeException("interfaces has not been implemented"); + } + + } + + private void parseFields(ClassFile clzFile, ByteCodeIterator iter) { + int fieldsCount = iter.nextU2toInt(); + System.out.println("fieldsCount: " + fieldsCount); + for(int i = 0; i < fieldsCount; i++){ + Field f = Field.parse(clzFile.getConstantPool(), iter); + clzFile.addField(f); + } + + } + + private void parseMethods(ClassFile clzFile, ByteCodeIterator iter) { + int methodsCount = iter.nextU2toInt(); + System.out.println("methodsCount: " + methodsCount); + for(int i = 0; i < methodsCount; i++){ + Method m = Method.parse(clzFile, iter); + clzFile.addMethod(m); + } + + } + } diff --git a/group11/1310368322/src/FifthHomework/jvm/loader/TestJVM.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/loader/TestJVM.java similarity index 100% rename from group11/1310368322/src/FifthHomework/jvm/loader/TestJVM.java rename to group11/1310368322/src/Mini_JVM/com/coderising/jvm/loader/TestJVM.java diff --git a/group11/1310368322/src/Mini_JVM/com/coderising/jvm/method/Method.java b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/method/Method.java new file mode 100644 index 0000000000..572880f729 --- /dev/null +++ b/group11/1310368322/src/Mini_JVM/com/coderising/jvm/method/Method.java @@ -0,0 +1,68 @@ +package com.coderising.jvm.method; + +import com.coderising.jvm.attr.AttributeInfo; +import com.coderising.jvm.attr.CodeAttr; +import com.coderising.jvm.clz.ClassFile; +import com.coderising.jvm.loader.ByteCodeIterator; + +public class Method { + + private int accessFlag; + private int nameIndex; + private int descriptorIndex; + + private CodeAttr codeAttr; + + private ClassFile clzFile; + + public int getNameIndex() { + return nameIndex; + } + + public int getDescriptorIndex() { + return descriptorIndex; + } + + public CodeAttr getCodeAttr() { + return codeAttr; + } + + public void setCodeAttr(CodeAttr code) { + this.codeAttr = code; + } + + public ClassFile getClzFile() { + return clzFile; + } + + public Method(ClassFile clzFile,int accessFlag, int nameIndex, int descriptorIndex) { + this.clzFile = clzFile; + this.accessFlag = accessFlag; + this.nameIndex = nameIndex; + this.descriptorIndex = descriptorIndex; + } + + + public static Method parse(ClassFile clzFile, ByteCodeIterator iter){ + int accessFlag = iter.nextU2toInt(); + int nameIndex = iter.nextU2toInt(); + int descriptor = iter.nextU2toInt(); + int attrCount = iter.nextU2toInt(); + + + Method method = new Method(clzFile,accessFlag,nameIndex,descriptor); + System.out.println("attrCount: " + attrCount); + // methodе + for(int i = 0; i < attrCount; i++){ + int attrNameIndex = iter.nextU2toInt(); + String attrName = clzFile.getConstantPool().getUTF8String(attrNameIndex); + System.out.println(attrName); + if(AttributeInfo.CODE.equalsIgnoreCase(attrName)){ + CodeAttr attrCode = CodeAttr.parse(clzFile, iter); + method.setCodeAttr(attrCode); + } + + } + return method; + } +} diff --git a/group11/1310368322/src/ThirdHomework/Download/DownloadThread.java b/group11/1310368322/src/data_structure/BigHomework/Download/DownloadThread.java similarity index 100% rename from group11/1310368322/src/ThirdHomework/Download/DownloadThread.java rename to group11/1310368322/src/data_structure/BigHomework/Download/DownloadThread.java diff --git a/group11/1310368322/src/ThirdHomework/Download/FileDownloader.java b/group11/1310368322/src/data_structure/BigHomework/Download/FileDownloader.java similarity index 100% rename from group11/1310368322/src/ThirdHomework/Download/FileDownloader.java rename to group11/1310368322/src/data_structure/BigHomework/Download/FileDownloader.java diff --git a/group11/1310368322/src/ThirdHomework/Download/api/Connection.java b/group11/1310368322/src/data_structure/BigHomework/Download/api/Connection.java similarity index 100% rename from group11/1310368322/src/ThirdHomework/Download/api/Connection.java rename to group11/1310368322/src/data_structure/BigHomework/Download/api/Connection.java diff --git a/group11/1310368322/src/ThirdHomework/Download/api/ConnectionException.java b/group11/1310368322/src/data_structure/BigHomework/Download/api/ConnectionException.java similarity index 100% rename from group11/1310368322/src/ThirdHomework/Download/api/ConnectionException.java rename to group11/1310368322/src/data_structure/BigHomework/Download/api/ConnectionException.java diff --git a/group11/1310368322/src/ThirdHomework/Download/api/ConnectionManager.java b/group11/1310368322/src/data_structure/BigHomework/Download/api/ConnectionManager.java similarity index 100% rename from group11/1310368322/src/ThirdHomework/Download/api/ConnectionManager.java rename to group11/1310368322/src/data_structure/BigHomework/Download/api/ConnectionManager.java diff --git a/group11/1310368322/src/ThirdHomework/Download/api/DownloadListener.java b/group11/1310368322/src/data_structure/BigHomework/Download/api/DownloadListener.java similarity index 100% rename from group11/1310368322/src/ThirdHomework/Download/api/DownloadListener.java rename to group11/1310368322/src/data_structure/BigHomework/Download/api/DownloadListener.java diff --git a/group11/1310368322/src/ThirdHomework/Download/impl/ConnectionImpl.java b/group11/1310368322/src/data_structure/BigHomework/Download/impl/ConnectionImpl.java similarity index 100% rename from group11/1310368322/src/ThirdHomework/Download/impl/ConnectionImpl.java rename to group11/1310368322/src/data_structure/BigHomework/Download/impl/ConnectionImpl.java diff --git a/group11/1310368322/src/ThirdHomework/Download/impl/ConnectionManagerImpl.java b/group11/1310368322/src/data_structure/BigHomework/Download/impl/ConnectionManagerImpl.java similarity index 100% rename from group11/1310368322/src/ThirdHomework/Download/impl/ConnectionManagerImpl.java rename to group11/1310368322/src/data_structure/BigHomework/Download/impl/ConnectionManagerImpl.java diff --git a/group11/1310368322/src/FirstHomework/BaseDataStructure/ArrayList.java b/group11/1310368322/src/data_structure/baseDataStructure_1/ArrayList.java similarity index 100% rename from group11/1310368322/src/FirstHomework/BaseDataStructure/ArrayList.java rename to group11/1310368322/src/data_structure/baseDataStructure_1/ArrayList.java diff --git a/group11/1310368322/src/FirstHomework/BaseDataStructure/LinkedList.java b/group11/1310368322/src/data_structure/baseDataStructure_1/LinkedList.java similarity index 100% rename from group11/1310368322/src/FirstHomework/BaseDataStructure/LinkedList.java rename to group11/1310368322/src/data_structure/baseDataStructure_1/LinkedList.java diff --git a/group11/1310368322/src/SecondHomework/BaseDataStructure/ArrayUtil.java b/group11/1310368322/src/data_structure/baseDataStructure_2/ArrayUtil.java similarity index 100% rename from group11/1310368322/src/SecondHomework/BaseDataStructure/ArrayUtil.java rename to group11/1310368322/src/data_structure/baseDataStructure_2/ArrayUtil.java diff --git a/group11/1310368322/src/ThirdHomework/BaseDataStructure/LinkedList.java b/group11/1310368322/src/data_structure/baseDataStructure_3/LinkedList.java similarity index 100% rename from group11/1310368322/src/ThirdHomework/BaseDataStructure/LinkedList.java rename to group11/1310368322/src/data_structure/baseDataStructure_3/LinkedList.java diff --git a/group11/1310368322/src/FourthHomework/BaseDataStructure/LRUPageFrame.java b/group11/1310368322/src/data_structure/baseDataStructure_4_LRU/LRUPageFrame.java similarity index 100% rename from group11/1310368322/src/FourthHomework/BaseDataStructure/LRUPageFrame.java rename to group11/1310368322/src/data_structure/baseDataStructure_4_LRU/LRUPageFrame.java diff --git a/group11/1310368322/src/FifthHomework/DataStructure_5_Stack/StackUtil.java b/group11/1310368322/src/data_structure/baseDataStructure_5_Stack/StackUtil.java similarity index 100% rename from group11/1310368322/src/FifthHomework/DataStructure_5_Stack/StackUtil.java rename to group11/1310368322/src/data_structure/baseDataStructure_5_Stack/StackUtil.java diff --git a/group11/1310368322/src/data_structure/baseDataStructure_6InfixExpr/InfixExpr.java b/group11/1310368322/src/data_structure/baseDataStructure_6InfixExpr/InfixExpr.java new file mode 100644 index 0000000000..c7285dd9a2 --- /dev/null +++ b/group11/1310368322/src/data_structure/baseDataStructure_6InfixExpr/InfixExpr.java @@ -0,0 +1,106 @@ +package dataStructure_6InfixExpr; + +import java.util.Stack; + +public class InfixExpr { + + String expr = null; + + public InfixExpr(String expr){ + this.expr = expr; + } + + public double evaluate(){ + + Stack operatorStack = new Stack(); + Stack operandStack = new Stack(); + + int tag = -1; + for(int i = 0; i < expr.length(); i++){ + if(operatorStack.isEmpty()){ + tag = -1; + } + char c = expr.charAt(i); + if( tag == 1 && (c == '+' || c == '-' || c == '*' || c == '/')){ + System.out.println("i= " + i); + char down = (char) operatorStack.pop(); + System.out.println("down: " + down); + System.out.println("up: " + c); + if(judgePriority(down,c)){ + double operand = (double) operandStack.pop(); + double operanded = (double) operandStack.pop(); + operandStack.push(operator(down,operanded,operand)); + operatorStack.push(c); + }else{ + operatorStack.push(down); + operatorStack.push(c); + } + }else if(tag == -1 && (c == '+' || c == '-' || c == '*' || c == '/')){ + tag = 1; + operatorStack.push(c); + + }else{ + String number = extractNumber(i,expr); + int length = number.length(); + i += length-1; + double operand = Double.parseDouble(number); + operandStack.push(operand); + } + } + + while(!operatorStack.isEmpty()){ + char operator = (char) operatorStack.pop(); + System.out.println(operator); + double operand = (double) operandStack.pop(); + System.out.println(operand); + double operanded = (double) operandStack.pop(); + System.out.println(operanded); + operandStack.push( operator(operator,operanded,operand)); + } + + return (double) operandStack.pop(); + } + + private String extractNumber(int i, String expr2) { + + StringBuffer buffer = new StringBuffer(); + while( (expr.charAt(i) != '+') && (expr.charAt(i) != '-') && (expr.charAt(i) != '*') && (expr.charAt(i) != '/') ){ + buffer.append(expr.charAt(i)); + if(i >= expr2.length()-1){ + break; + } + i++; + } + return buffer.toString(); + } + + private boolean judgePriority(char down, char up) { + boolean tag = false; + + if((up == '+' || up == '-') && (down == '*' || down == '/')){ + tag = true; + }else if( (up == '*') && (down == '/')){ + tag = true; + }else if( (up == '/') && (down == '*')){ + tag = true; + }else if( (up == '+') && (down == '-') ){ + tag = true; + }else if( (up == '-') && (down == '+') ){ + tag = true; + } + return tag; + } + + private double operator(char operator, double operanded, double operand) { + double result = 0; + + switch(operator){ + case '+': result = operanded + operand; break; + case '-': result = operanded - operand; break; + case '*': System.out.println("˷"); result = operanded * operand; break; + case '/': result = operanded / operand; break; + } + + return result; + } +} diff --git a/group11/1310368322/test/FifthHomework/DataStructure_5_Stack/TestSwitch.java b/group11/1310368322/test/FifthHomework/DataStructure_5_Stack/TestSwitch.java deleted file mode 100644 index a14ae1683b..0000000000 --- a/group11/1310368322/test/FifthHomework/DataStructure_5_Stack/TestSwitch.java +++ /dev/null @@ -1,15 +0,0 @@ -package DataStructure_5_Stack; - -public class TestSwitch { - public static void main(String[] args) { - int i = 3; - while(true){ - switch(i){ - case 1: System.out.println("1"); break; - case 3: System.out.println("3"); - case 4: System.out.println("4"); break; - } - } - - } -} diff --git a/group11/1310368322/test/FifthHomework/jvm/loader/TestArray.java b/group11/1310368322/test/FifthHomework/jvm/loader/TestArray.java deleted file mode 100644 index 6d78273d8c..0000000000 --- a/group11/1310368322/test/FifthHomework/jvm/loader/TestArray.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.coderising.jvm.loader; - -public class TestArray { - public static void main(String[] args) { - byte [] a; - - } -} diff --git a/group11/1310368322/test/FifthHomework/jvm/loader/TestArrayList.java b/group11/1310368322/test/FifthHomework/jvm/loader/TestArrayList.java deleted file mode 100644 index 13bfb372fb..0000000000 --- a/group11/1310368322/test/FifthHomework/jvm/loader/TestArrayList.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.coderising.jvm.loader; - -import java.util.ArrayList; - -public class TestArrayList { - public static void main(String[] args) { - ArrayList list = new ArrayList(); - for(int i = 0; i < 5; i++){ - list.add(i); - } - System.out.println(list.size()); - } - -} diff --git a/group11/1310368322/test/FifthHomework/jvm/loader/TestByteArrayToHexString.java b/group11/1310368322/test/FifthHomework/jvm/loader/TestByteArrayToHexString.java deleted file mode 100644 index d0ef456005..0000000000 --- a/group11/1310368322/test/FifthHomework/jvm/loader/TestByteArrayToHexString.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.coderising.jvm.loader; - -public class TestByteArrayToHexString { - - public static void main(String[] args) { - byte [] a = { -54,-2,-70,-66}; - for(int i = 0; i < 2; i++){ - byte b = a[i]; - int j = b & 0xFF; - String s = Integer.toHexString(j); - System.out.println(s.length()); - System.out.println(s); - } - } -} diff --git a/group11/1310368322/test/FifthHomework/jvm/loader/TestByteArrayToInt.java b/group11/1310368322/test/FifthHomework/jvm/loader/TestByteArrayToInt.java deleted file mode 100644 index 8bde4d5974..0000000000 --- a/group11/1310368322/test/FifthHomework/jvm/loader/TestByteArrayToInt.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.coderising.jvm.loader; - -public class TestByteArrayToInt { - public static void main(String[] args) { - byte a[] = {1,1}; - // System.out.println(a[0]<<7); ƣߵ k λҶ˲ k 0 - // int b = a[0]<<8 + a[1]; - int b = (a[0]<<8) + a[1]; - // System.out.println(a[0]); ƶԭڴеֵûӰ - System.out.println(b); - } -} diff --git a/group11/1310368322/test/FifthHomework/jvm/loader/TestByteToInt.java b/group11/1310368322/test/FifthHomework/jvm/loader/TestByteToInt.java deleted file mode 100644 index 2c51afb572..0000000000 --- a/group11/1310368322/test/FifthHomework/jvm/loader/TestByteToInt.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.coderising.jvm.loader; - -public class TestByteToInt { - public static void main(String[] args) { - byte a = -1; - int b = a;// aֱֵӸ bᷢı䣬ڴУ Ὣ ǰȫ 1 -128 byteеڴʾΪ 1000 0000Ȼintǣ 1111 1111 1111 1111 1111 1111 1000 0000-128intͲ룩 - int c = a & 0xFFFF;// λڴнеģҲ˵ǶaIJвģ a & 0xFF е a λ0xFF(Ĭint͵) - System.out.println(b); - System.out.println(c); - System.out.println(a>>>24); - } -} diff --git a/group11/1310368322/test/FifthHomework/jvm/loader/TestIntegerToHexString.java b/group11/1310368322/test/FifthHomework/jvm/loader/TestIntegerToHexString.java deleted file mode 100644 index b5be2a16d1..0000000000 --- a/group11/1310368322/test/FifthHomework/jvm/loader/TestIntegerToHexString.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.coderising.jvm.loader; - -public class TestIntegerToHexString { - public static void main(String[] args) { - int i = 10; - System.out.println(i); - String s = Integer.toHexString(i); - System.out.println(s); - } -} diff --git a/group11/1310368322/test/FourthHomework/JVM/EmployeeV1.java b/group11/1310368322/test/FourthHomework/JVM/EmployeeV1.java deleted file mode 100644 index acbc34c9bb..0000000000 --- a/group11/1310368322/test/FourthHomework/JVM/EmployeeV1.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.coderising.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(); - } -} diff --git a/group11/1310368322/test/FourthHomework/JVM/TestClassFileLoader.java b/group11/1310368322/test/FourthHomework/JVM/TestClassFileLoader.java deleted file mode 100644 index 263384e71a..0000000000 --- a/group11/1310368322/test/FourthHomework/JVM/TestClassFileLoader.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.coderising.jvm.loader; - -import static org.junit.Assert.*; - -import java.io.IOException; - -import org.junit.*; - -public class TestClassFileLoader { - static String path1 = "D:/ProgramWorld"; - static String path2 = "D:/ProgramWorld/Java"; - @Test - public void test() { - ClassFileLoader loader = new ClassFileLoader(); - loader.addClassPath(path1); - loader.addClassPath(path2); - String clzPath = loader.getClassPath(); - Assert.assertEquals(path1 + ";" + path2, clzPath); - } - @Test - public void testClassFileLength() throws IOException{ - ClassFileLoader loader = new ClassFileLoader(); - loader.addClassPath(path1); - - String className = "D:/ProgramWorld/Java/Practice/LangSi/2017Ⱥ/bin/com/coderising/jvm/loader/EmployeeV1.class"; - - byte[] byteCodes = loader.readBinaryCode(className); - - // ע⣺ ֽܺJVM汾йϵԿõൽж - Assert.assertEquals(1058,byteCodes.length); - } - @Test - public void testMagicNumber() throws IOException{ - ClassFileLoader loader = new ClassFileLoader(); - loader.addClassPath(path1); - String className = "D:/ProgramWorld/Java/Practice/LangSi/2017Ⱥ/bin/com/coderising/jvm/loader/EmployeeV1.class"; - byte[] byteCodes = loader.readBinaryCode(className); - byte[] codes = new byte[]{ - byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3] - }; - String actualValue = this.byteToHexString(codes); - Assert.assertEquals("cafebabe",actualValue); - - } - - private String byteToHexString(byte[] codes){ - StringBuffer buffer = new StringBuffer(); - for(int i = 0; i < codes.length; i++){ - byte b = codes[i]; - int value = b & 0xFF; - String strHex = Integer.toHexString(value); - if(strHex.length() < 2){ - strHex = "0" + strHex; - } - buffer.append(strHex); - } - return buffer.toString(); - } - - - - - - - - -} diff --git a/group11/1310368322/test/FifthHomework/jvm/loader/EmployeeV1.java b/group11/1310368322/test/Mini_JVM/EmployeeV1.java similarity index 100% rename from group11/1310368322/test/FifthHomework/jvm/loader/EmployeeV1.java rename to group11/1310368322/test/Mini_JVM/EmployeeV1.java diff --git a/group11/1310368322/test/FifthHomework/jvm/loader/TestClassFileLoader.java b/group11/1310368322/test/Mini_JVM/TestClassFileLoader.java similarity index 68% rename from group11/1310368322/test/FifthHomework/jvm/loader/TestClassFileLoader.java rename to group11/1310368322/test/Mini_JVM/TestClassFileLoader.java index 3971ac81cd..d1874a0834 100644 --- a/group11/1310368322/test/FifthHomework/jvm/loader/TestClassFileLoader.java +++ b/group11/1310368322/test/Mini_JVM/TestClassFileLoader.java @@ -3,21 +3,24 @@ import static org.junit.Assert.*; import java.io.IOException; +import java.util.List; import org.junit.*; import com.coderising.jvm.clz.ClassFile; import com.coderising.jvm.clz.ClassIndex; import com.coderising.jvm.constant.*; +import com.coderising.jvm.field.Field; +import com.coderising.jvm.method.Method; public class TestClassFileLoader { static String path1 = "D:/ProgramWorld"; static String path2 = "D:/ProgramWorld/Java"; private static final String FULL_QUALIFIED_CLASS_NAME = "com/coderising/jvm/loader/EmployeeV1"; - static String path = "D:/ProgramWorld/Java/Practice/LangSi/2017Ⱥ/bin/com/coderising/jvm/loader/EmployeeV1.class"; + static String classPath1 = "D:/ProgramWorld/Java/Practice/LangSi/2017Ⱥ/bin/com/coderising/jvm/loader/EmployeeV1.class"; + static String classPath2 = "D:/TestClass.class"; static ClassFile clzFile = null; - - + @Test public void test() { @@ -75,8 +78,7 @@ private String byteToHexString(byte[] codes){ @Test public void testVersion() throws IOException{ ClassFileLoader loader = new ClassFileLoader(); - String className = "D:/TestJVM.class"; - clzFile = loader.loadClass(className); + clzFile = loader.loadClass(classPath2); Assert.assertEquals(0, clzFile.getMinorVersion()); Assert.assertEquals(51, clzFile.getMajorVersion()); @@ -86,7 +88,7 @@ public void testVersion() throws IOException{ public void testConstantPool() throws IOException{ ClassFileLoader loader = new ClassFileLoader(); - clzFile = loader.loadClass(path); + clzFile = loader.loadClass(classPath1); ConstantPool pool = clzFile.getConstantPool(); Assert.assertEquals(53, pool.getSize()); @@ -155,7 +157,7 @@ public void testConstantPool() throws IOException{ public void testClassIndex() throws IOException{ ClassFileLoader loader = new ClassFileLoader(); - clzFile = loader.loadClass(path); + clzFile = loader.loadClass(classPath1); ClassIndex clzIndex = clzFile.getClzIndex(); System.out.println("clzIndex="+clzIndex); ClassInfo thisClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getThisClassIndex()); @@ -166,9 +168,79 @@ public void testClassIndex() throws IOException{ Assert.assertEquals("java/lang/Object", superClassInfo.getClassName()); } - - - + + // --------------------- JVM + @Test + public void testReadFields() throws IOException{ + ClassFileLoader loader = new ClassFileLoader(); + clzFile = loader.loadClass(classPath1); + List fields = clzFile.getFields(); + Assert.assertEquals(2, fields.size()); + { + Field f = fields.get(0); + Assert.assertEquals("name:Ljava/lang/String;", f.toString()); + } + { + Field f = fields.get(1); + Assert.assertEquals("age:I", f.toString()); + } + } + + @Test + public void testMethods() throws IOException{ + ClassFileLoader loader = new ClassFileLoader(); + clzFile = loader.loadClass(classPath1); + List methods = clzFile.getMethods(); + ConstantPool pool = clzFile.getConstantPool(); + + { + Method m = methods.get(0); + assertMethodEquals(pool,m, + "", + "(Ljava/lang/String;I)V", + "2ab7000c2a2bb5000f2a1cb50011b1"); + + } + { + Method m = methods.get(1); + assertMethodEquals(pool,m, + "setName", + "(Ljava/lang/String;)V", + "2a2bb5000fb1"); + + } + { + Method m = methods.get(2); + assertMethodEquals(pool,m, + "setAge", + "(I)V", + "2a1bb50011b1"); + } + { + Method m = methods.get(3); + assertMethodEquals(pool,m, + "sayHello", + "()V", + "b2001c1222b60024b1"); + + } + { + Method m = methods.get(4); + assertMethodEquals(pool,m, + "main", + "([Ljava/lang/String;)V", + "bb000159122b101db7002d4c2bb6002fb1"); + } + } + + private void assertMethodEquals(ConstantPool pool,Method m , String expectedName, String expectedDesc,String expectedCode){ + String methodName = pool.getUTF8String(m.getNameIndex()); + String methodDesc = pool.getUTF8String(m.getDescriptorIndex()); + String code = m.getCodeAttr().getCode(); + Assert.assertEquals(expectedName, methodName); + Assert.assertEquals(expectedDesc, methodDesc); + Assert.assertEquals(expectedCode, code); + } } diff --git a/group11/1310368322/test/ThirdHomework/Download/FileDownloaderTest.java b/group11/1310368322/test/data_structure/BigHomework/Download/FileDownloaderTest.java similarity index 100% rename from group11/1310368322/test/ThirdHomework/Download/FileDownloaderTest.java rename to group11/1310368322/test/data_structure/BigHomework/Download/FileDownloaderTest.java diff --git a/group11/1310368322/test/SecondHomwork/BaseDataStructure/ArrayUtilTest.java b/group11/1310368322/test/data_structure/baseDataStructure_2/ArrayUtilTest.java similarity index 100% rename from group11/1310368322/test/SecondHomwork/BaseDataStructure/ArrayUtilTest.java rename to group11/1310368322/test/data_structure/baseDataStructure_2/ArrayUtilTest.java diff --git a/group11/1310368322/test/ThirdHomework/BaseDataStructure/TestLinkedList.java b/group11/1310368322/test/data_structure/baseDataStructure_3/TestLinkedList.java similarity index 100% rename from group11/1310368322/test/ThirdHomework/BaseDataStructure/TestLinkedList.java rename to group11/1310368322/test/data_structure/baseDataStructure_3/TestLinkedList.java diff --git a/group11/1310368322/test/FourthHomework/BaseDataStructure/TestLRUPageFrame.java b/group11/1310368322/test/data_structure/baseDataStructure_4_LRU/TestLRUPageFrame.java similarity index 100% rename from group11/1310368322/test/FourthHomework/BaseDataStructure/TestLRUPageFrame.java rename to group11/1310368322/test/data_structure/baseDataStructure_4_LRU/TestLRUPageFrame.java diff --git a/group11/1310368322/test/FifthHomework/DataStructure_5_Stack/TestStackUtil.java b/group11/1310368322/test/data_structure/baseDataStructure_5_Stack/TestStackUtil.java similarity index 100% rename from group11/1310368322/test/FifthHomework/DataStructure_5_Stack/TestStackUtil.java rename to group11/1310368322/test/data_structure/baseDataStructure_5_Stack/TestStackUtil.java diff --git a/group11/1310368322/test/data_structure/baseDataStructure_6InfixExpr/InfixExpr.java b/group11/1310368322/test/data_structure/baseDataStructure_6InfixExpr/InfixExpr.java new file mode 100644 index 0000000000..c7285dd9a2 --- /dev/null +++ b/group11/1310368322/test/data_structure/baseDataStructure_6InfixExpr/InfixExpr.java @@ -0,0 +1,106 @@ +package dataStructure_6InfixExpr; + +import java.util.Stack; + +public class InfixExpr { + + String expr = null; + + public InfixExpr(String expr){ + this.expr = expr; + } + + public double evaluate(){ + + Stack operatorStack = new Stack(); + Stack operandStack = new Stack(); + + int tag = -1; + for(int i = 0; i < expr.length(); i++){ + if(operatorStack.isEmpty()){ + tag = -1; + } + char c = expr.charAt(i); + if( tag == 1 && (c == '+' || c == '-' || c == '*' || c == '/')){ + System.out.println("i= " + i); + char down = (char) operatorStack.pop(); + System.out.println("down: " + down); + System.out.println("up: " + c); + if(judgePriority(down,c)){ + double operand = (double) operandStack.pop(); + double operanded = (double) operandStack.pop(); + operandStack.push(operator(down,operanded,operand)); + operatorStack.push(c); + }else{ + operatorStack.push(down); + operatorStack.push(c); + } + }else if(tag == -1 && (c == '+' || c == '-' || c == '*' || c == '/')){ + tag = 1; + operatorStack.push(c); + + }else{ + String number = extractNumber(i,expr); + int length = number.length(); + i += length-1; + double operand = Double.parseDouble(number); + operandStack.push(operand); + } + } + + while(!operatorStack.isEmpty()){ + char operator = (char) operatorStack.pop(); + System.out.println(operator); + double operand = (double) operandStack.pop(); + System.out.println(operand); + double operanded = (double) operandStack.pop(); + System.out.println(operanded); + operandStack.push( operator(operator,operanded,operand)); + } + + return (double) operandStack.pop(); + } + + private String extractNumber(int i, String expr2) { + + StringBuffer buffer = new StringBuffer(); + while( (expr.charAt(i) != '+') && (expr.charAt(i) != '-') && (expr.charAt(i) != '*') && (expr.charAt(i) != '/') ){ + buffer.append(expr.charAt(i)); + if(i >= expr2.length()-1){ + break; + } + i++; + } + return buffer.toString(); + } + + private boolean judgePriority(char down, char up) { + boolean tag = false; + + if((up == '+' || up == '-') && (down == '*' || down == '/')){ + tag = true; + }else if( (up == '*') && (down == '/')){ + tag = true; + }else if( (up == '/') && (down == '*')){ + tag = true; + }else if( (up == '+') && (down == '-') ){ + tag = true; + }else if( (up == '-') && (down == '+') ){ + tag = true; + } + return tag; + } + + private double operator(char operator, double operanded, double operand) { + double result = 0; + + switch(operator){ + case '+': result = operanded + operand; break; + case '-': result = operanded - operand; break; + case '*': System.out.println("˷"); result = operanded * operand; break; + case '/': result = operanded / operand; break; + } + + return result; + } +}