Skip to content

Commit 53a0839

Browse files
authored
Merge pull request onlyliuxin#184 from luoziyihao/master
synchronize code
2 parents 98934f5 + 76b3896 commit 53a0839

File tree

296 files changed

+15338
-721
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

296 files changed

+15338
-721
lines changed

group01/275150374/275150374Learning/.idea/description.html

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

group15/1512_656512403/.idea/description.html

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

group17/102228177/work2_26/.classpath

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<classpath>
3-
<classpathentry kind="src" path="src"/>
4-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5-
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
6-
<classpathentry kind="lib" path="lib/dom4j-1.6.jar"/>
7-
<classpathentry kind="output" path="bin"/>
8-
</classpath>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
6+
<classpathentry kind="lib" path="lib/dom4j-1.6.1.jar"/>
7+
<classpathentry kind="output" path="bin"/>
8+
</classpath>

group17/102228177/work2_26/src/com/coderising/array/ArrayUtil.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static void main(String[] args) {
3434
// System.out.println(Arrays.toString(util.merge(a1, a2)));
3535
System.out.println(Arrays.toString(util.fibonacci(15)));
3636

37-
System.out.println(Arrays.toString(util.getPrimes(23)));
37+
// System.out.println(Arrays.toString(util.getPrimes(23)));
3838
}
3939

4040
/**
@@ -115,14 +115,15 @@ public int[] grow(int [] oldArray, int size){
115115
*/
116116
public int[] fibonacci(int max){
117117
List<Integer> list = new ArrayList<Integer>();
118-
int f1 = 1, f2 = 1, f = 0;
119-
list.add(f1);
120-
list.add(f2);
121-
while(f < max){
122-
f = f1+f2;
123-
f1 = f2;
124-
f2 = f;
125-
list.add(f);
118+
if (max <= 1) {
119+
return new int[]{};
120+
}
121+
int lo = 0;
122+
int hi = 1;
123+
while(hi<max){
124+
list.add(hi);
125+
hi = lo + hi;
126+
lo = hi - lo;
126127
}
127128
int[] arr = new int[list.size()];
128129
for (int i = 0; i < list.size(); i++) {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.coderising.jvm.loader;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.FileNotFoundException;
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
13+
14+
public class ClassFileLoader {
15+
16+
private List<String> clzPaths = new ArrayList<String>();
17+
18+
public byte[] readBinaryCode(String className) {
19+
String name = "";
20+
for (int i = 0; i < className.length(); i++) {
21+
if(className.charAt(i)=='.'){
22+
name += File.separatorChar;
23+
}else{
24+
name += className.charAt(i);
25+
}
26+
}
27+
File file = new File(getClassPath()+ File.separatorChar +name+".class");
28+
InputStream in = null;
29+
ByteArrayOutputStream out = null;
30+
try {
31+
in = new FileInputStream(file);
32+
out = new ByteArrayOutputStream();
33+
byte[] buff = new byte[1024*2];
34+
int len = 0;
35+
while((len=in.read(buff))!=-1){
36+
out.write(buff, 0, len);
37+
}
38+
return out.toByteArray();
39+
} catch (FileNotFoundException e) {
40+
e.printStackTrace();
41+
} catch (IOException e) {
42+
e.printStackTrace();
43+
}
44+
finally {
45+
if(in!=null){
46+
try {
47+
in.close();
48+
} catch (IOException e) {
49+
e.printStackTrace();
50+
}
51+
}
52+
if(out!=null){
53+
try {
54+
out.close();
55+
} catch (IOException e) {
56+
e.printStackTrace();
57+
}
58+
}
59+
}
60+
61+
return null;
62+
63+
64+
}
65+
66+
67+
public void addClassPath(String path) {
68+
clzPaths.add(path);
69+
}
70+
71+
72+
73+
public String getClassPath(){
74+
StringBuilder sb = new StringBuilder();
75+
for (String string : clzPaths) {
76+
sb.append(string).append(";");
77+
}
78+
sb = sb.deleteCharAt(sb.length()-1);
79+
return sb.toString();
80+
}
81+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.coderising.jvm.test;
2+
3+
import org.junit.After;
4+
import org.junit.Assert;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import com.coderising.jvm.loader.ClassFileLoader;
9+
10+
11+
12+
13+
14+
public class ClassFileloaderTest {
15+
16+
17+
// static String path1 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin";
18+
static String path1 = "D:\\git\\coding2017\\group17\\102228177\\work3_26\\bin";
19+
static String path2 = "C:\temp";
20+
21+
22+
23+
@Before
24+
public void setUp() throws Exception {
25+
}
26+
27+
@After
28+
public void tearDown() throws Exception {
29+
}
30+
31+
@Test
32+
public void testClassPath(){
33+
34+
ClassFileLoader loader = new ClassFileLoader();
35+
loader.addClassPath(path1);
36+
loader.addClassPath(path2);
37+
38+
String clzPath = loader.getClassPath();
39+
40+
Assert.assertEquals(path1+";"+path2,clzPath);
41+
42+
}
43+
44+
@Test
45+
public void testClassFileLength() {
46+
47+
ClassFileLoader loader = new ClassFileLoader();
48+
loader.addClassPath(path1);
49+
50+
String className = "com.coderising.jvm.test.EmployeeV1";
51+
52+
byte[] byteCodes = loader.readBinaryCode(className);
53+
54+
// 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大
55+
Assert.assertEquals(1056, byteCodes.length);
56+
57+
}
58+
59+
60+
@Test
61+
public void testMagicNumber(){
62+
ClassFileLoader loader = new ClassFileLoader();
63+
loader.addClassPath(path1);
64+
String className = "com.coderising.jvm.test.EmployeeV1";
65+
byte[] byteCodes = loader.readBinaryCode(className);
66+
byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]};
67+
68+
69+
String acctualValue = this.byteToHexString(codes);
70+
71+
Assert.assertEquals("cafebabe", acctualValue);
72+
}
73+
74+
75+
76+
77+
78+
79+
private String byteToHexString(byte[] codes ){
80+
StringBuffer buffer = new StringBuffer();
81+
for(int i=0;i<codes.length;i++){
82+
byte b = codes[i];
83+
int value = b & 0xFF;
84+
String strHex = Integer.toHexString(value);
85+
if(strHex.length()< 2){
86+
strHex = "0" + strHex;
87+
}
88+
buffer.append(strHex);
89+
}
90+
return buffer.toString();
91+
}
92+
93+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.coderising.jvm.test;
2+
3+
public class EmployeeV1 {
4+
5+
6+
private String name;
7+
private int age;
8+
9+
public EmployeeV1(String name, int age) {
10+
this.name = name;
11+
this.age = age;
12+
}
13+
14+
public void setName(String name) {
15+
this.name = name;
16+
}
17+
public void setAge(int age){
18+
this.age = age;
19+
}
20+
public void sayHello() {
21+
System.out.println("Hello , this is class Employee ");
22+
}
23+
public static void main(String[] args){
24+
EmployeeV1 p = new EmployeeV1("Andy",29);
25+
p.sayHello();
26+
27+
}
28+
}

0 commit comments

Comments
 (0)