implement Wildcard Entry

This commit is contained in:
Imgaojp 2017-02-17 15:57:51 +08:00
parent bfd2ffa3a5
commit ee30abbf2d
7 changed files with 225 additions and 13 deletions

View File

@ -27,18 +27,18 @@ package com.gabongao.jvm.classpath;
public abstract class Entry {
static String pathListSeparator = System.getProperty("path.separator");//system path separator
// public Entry newEntry(String path) {
public static Entry newEntry(String path) {
// if (path.contains(pathListSeparator)) {
// return newCompositeEntry(path);
// }
// if (path.contains("*")) {
// return newWildcardEntry(path);
// return new WildcardEntry(path);
// }
// if (path.contains(".jar") || path.contains(".zip") || path.contains(".JAR") || path.contains(".ZIP")) {
// return newZipEntry(path);
// }
// return newDirEntry(path);
// }
if (path.contains(".jar") || path.contains(".zip") || path.contains(".JAR") || path.contains(".ZIP")) {
return new Entry_Zip(path);
}
return new Entry_Dir(path);
}
/**
* read class from file

View File

@ -1,5 +1,8 @@
package com.gabongao.jvm.classpath;
import java.util.ArrayList;
import java.util.List;
/**
*            + +
*         + +
@ -25,14 +28,39 @@ package com.gabongao.jvm.classpath;
* Created by Imgaojp on 2017/2/17.
*/
public class Entry_Composite extends Entry {
List<Entry> entries=new ArrayList<>();
public Entry_Composite() {
}
public Entry_Composite(String pathList) {
for (String str : pathList.split(super.pathListSeparator)
) {
Entry entry = Entry.newEntry(str);
entries.add(entry);
}
}
@Override
public byte[] readClass(String className) {
return new byte[0];
for (Entry entry:entries
) {
byte[] bytes = entry.readClass(className);
if (bytes != null) {
return bytes;
}
}
return null;
}
@Override
public String toString() {
return null;
StringBuilder sb = new StringBuilder();
for (Entry entry:entries
) {
sb.append(entry.toString()+"\t");
}
return sb.toString();
}
}

View File

@ -61,14 +61,19 @@ public class Entry_Dir extends Entry {
) {
System.out.printf("%x",b);
}
System.out.println();
return bos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Class "+className+" not found"+" in "+absDir);
try {
inputStream.close();
try {
inputStream.close();
} catch (Exception e1) {
System.out.println("inputStream does not exist");
}
bos.close();
} catch (Exception e1) {
e1.printStackTrace();
System.out.println("bos close error");
}
}
return null;

View File

@ -0,0 +1,71 @@
package com.gabongao.jvm.classpath;
import java.io.File;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
/**
*            + +
*         + +
*               
*               ++ + + +
*        +
*                +
*              
*                + +
*           
*             
*              + + + +
*                 Code is far away from bug with the animal protecting
*              +     神兽保佑,代码无bug
*             
*               +
*               + +
*                  
*                  
*           + + + +
*            
*            + + + +
* Created by Imgaojp on 2017/2/17.
*/
public class Entry_Wildcard extends Entry {
String baseDir = "";
List<Entry> entryList = new ArrayList<>();
public Entry_Wildcard(String path) {
File file = new File(path.substring(0, path.length() - 1));
baseDir = file.getAbsolutePath();
}
@Override
public byte[] readClass(String className) {
Path dir = Paths.get(baseDir);
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path e:stream
) {
String fileName = e.getFileName().toString();
if ((!e.toFile().isDirectory()) && (fileName.contains(".jar") || fileName.contains(".JAR"))) {
entryList.add(new Entry_Zip(e.getParent()+"\\"+fileName));
}
}
} catch (Exception e) {
e.printStackTrace();
}
for (Entry entry : entryList
) {
entry.readClass(className);
}
return null;
}
@Override
public String toString() {
return baseDir;
}
}

View File

@ -63,12 +63,13 @@ public class Entry_Zip extends Entry {
) {
System.out.printf("%x",b);
}
System.out.println();
return bos.toByteArray();
}
entry = jarInput.getNextJarEntry();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Class "+className+" not found"+" in "+absPath);
}
return null;
}

View File

@ -0,0 +1,54 @@
package test.com.gabongao.jvm.classpath;
import com.gabongao.jvm.classpath.Entry_Composite;
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.framework.TestCase;
/**
* Entry_Composite Tester.
*
* @author <Authors name>
* @since <pre>02/17/2017</pre>
* @version 1.0
*/
public class Entry_CompositeTest extends TestCase {
public Entry_CompositeTest(String name) {
super(name);
}
public void setUp() throws Exception {
super.setUp();
}
public void tearDown() throws Exception {
super.tearDown();
}
/**
*
* Method: readClass(String className)
*
*/
public void testReadClass() throws Exception {
//TODO: Test goes here...
Entry_Composite entry_composite = new Entry_Composite("D:\\Programming\\Java\\JVM\\src\\com\\gabongao\\jvm\\classpath;D:\\Programming\\Java\\JVM\\out\\production\\JVM\\com\\gabongao\\jvm\\;D:\\Programming\\Java\\JVM\\lib\\commons-cli-1.3.1;C:\\Program Files\\Java\\jdk1.8.0_77\\jre\\lib\\jfxswt.jar");
entry_composite.readClass("javafx/embed/swt/CustomTransfer.class");
System.out.println(entry_composite.toString());
}
/**
*
* Method: toString()
*
*/
public void testToString() throws Exception {
//TODO: Test goes here...
}
public static Test suite() {
return new TestSuite(Entry_CompositeTest.class);
}
}

View File

@ -0,0 +1,53 @@
package test.com.gabongao.jvm.classpath;
import com.gabongao.jvm.classpath.Entry_Wildcard;
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.framework.TestCase;
/**
* Entry_Wildcard Tester.
*
* @author <Authors name>
* @since <pre>02/17/2017</pre>
* @version 1.0
*/
public class Entry_WildcardTest extends TestCase {
public Entry_WildcardTest(String name) {
super(name);
}
public void setUp() throws Exception {
super.setUp();
}
public void tearDown() throws Exception {
super.tearDown();
}
/**
*
* Method: readClass(String className)
*
*/
public void testReadClass() throws Exception {
//TODO: Test goes here...
Entry_Wildcard entry_wildcard = new Entry_Wildcard("C:\\Program Files\\Java\\jdk1.8.0_77\\jre\\lib\\*");
entry_wildcard.readClass("javafx/embed/swt/CustomTransfer.class");
}
/**
*
* Method: toString()
*
*/
public void testToString() throws Exception {
//TODO: Test goes here...
}
public static Test suite() {
return new TestSuite(Entry_WildcardTest.class);
}
}