Skip to content

Commit

Permalink
Merge pull request #33 from szlele/master
Browse files Browse the repository at this point in the history
3月26日作业
  • Loading branch information
zhijiaxinyu authored Apr 9, 2017
2 parents d537cd6 + 4ebc4d0 commit 6d21660
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 0 deletions.
69 changes: 69 additions & 0 deletions group10/595128841/src/main/java/org/le/d/ClassFileLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.le.d;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;



public class ClassFileLoader {

private List<String> clzPaths = new ArrayList<String>();

public byte[] readBinaryCode(String className) {
File file = null;
final String classAllName = className.replace(".", File.separator) + ".class";
for(String path : clzPaths){
File filePath = new File(path);
if(filePath.isDirectory()){
File[] fileList = filePath.listFiles(new FilenameFilter(){
@Override
public boolean accept(File dir, String name) {
String filePath = dir.getPath()+File.separator+name;
return filePath.contains(classAllName);
}
});
if(fileList.length > 0){
file = fileList[0];
break;
}
}
}
if(file != null){
try (
DataInputStream in = new DataInputStream(new FileInputStream(file));
){
byte[] buf = new byte[(int) file.length()];
in.readFully(buf);
return buf;
} catch (Exception e) {
e.printStackTrace();
}
}
return null;


}

public void addClassPath(String path) {
clzPaths.add(path);
}



public String getClassPath(){
StringBuilder str = new StringBuilder();
for(String s : clzPaths){
str.append(';').append(s);
}
return str.length() > 0 ?str.substring(1):"";
}





}
87 changes: 87 additions & 0 deletions group10/595128841/src/main/java/org/le/d/ClassFileloaderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.le.d;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;


public class ClassFileloaderTest {


static String path1 = "C:\\work\\workspace\\coding2017\\coding2017\\group10\\595128841\\src\\main\\java\\org\\le\\d";
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 = "org.le.d.EmployeeV1";

byte[] byteCodes = loader.readBinaryCode(className);

// 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大
Assert.assertEquals(801, byteCodes.length);

}


@Test
public void testMagicNumber(){
ClassFileLoader loader = new ClassFileLoader();
loader.addClassPath(path1);
String className = "org.le.d.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<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();
}

}
28 changes: 28 additions & 0 deletions group10/595128841/src/main/java/org/le/d/EmployeeV1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.le.d;

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();

}
}
25 changes: 25 additions & 0 deletions group10/595128841/src/main/java/org/le/d/LRUMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
*
*/
package org.le.d;

import java.util.LinkedHashMap;
/**
* @author yue
* @time 2017年4月5日
*/
public class LRUMap<K,V> extends LinkedHashMap<K, V>{

private static final long serialVersionUID = -4435712528205829896L;
private int maxElements;

public LRUMap(int maxElements){
super(maxElements, 0.75F, true);
this.maxElements = maxElements;
}

@Override
protected boolean removeEldestEntry(java.util.Map.Entry<K, V> eldest) {
return size() > this.maxElements;
}
}

0 comments on commit 6d21660

Please sign in to comment.