Changed to take ".class" files again :)
This commit is contained in:
parent
eaac9898f4
commit
46ac49576e
37
src/test/java/ByteCode/ClassFileLoader.java
Normal file
37
src/test/java/ByteCode/ClassFileLoader.java
Normal file
@ -0,0 +1,37 @@
|
||||
package ByteCode;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ClassFileLoader extends ClassLoader {
|
||||
private final String classFilePath;
|
||||
|
||||
public ClassFileLoader(String classFilePath) {
|
||||
this.classFilePath = classFilePath;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> findClass(String name) throws ClassNotFoundException {
|
||||
try {
|
||||
byte[] classData = loadClassData();
|
||||
return defineClass(name, classData, 0, classData.length);
|
||||
} catch (IOException e) {
|
||||
throw new ClassNotFoundException("Class not found", e);
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] loadClassData() throws IOException {
|
||||
File file = new File(classFilePath);
|
||||
try (FileInputStream fis = new FileInputStream(file);
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead;
|
||||
while ((bytesRead = fis.read(buffer)) != -1) {
|
||||
baos.write(buffer, 0, bytesRead);
|
||||
}
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
||||
}
|
@ -2,21 +2,16 @@ package ByteCode;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
|
||||
public class FileClassLoader extends ClassLoader {
|
||||
public class JarFileLoader extends ClassLoader {
|
||||
private final String jarFilePath;
|
||||
|
||||
public FileClassLoader(String jarFilePath) {
|
||||
public JarFileLoader(String jarFilePath) {
|
||||
this.jarFilePath = jarFilePath;
|
||||
System.out.println("Jar File Path: " + jarFilePath);
|
||||
}
|
@ -7,16 +7,13 @@ import gen.DecafParser;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
import org.antlr.v4.runtime.CharStreams;
|
||||
import org.antlr.v4.runtime.CommonTokenStream;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.rmi.ssl.SslRMIClientSocketFactory;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import ASTs.emptyClassAST;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
@ -25,7 +22,6 @@ import static org.junit.Assert.fail;
|
||||
|
||||
public class TestAll {
|
||||
CompareByteCodeBehaviour byteCodeBehaviourComparer;
|
||||
private final String codeGenOutputLocation = "output.jar";
|
||||
public TestAll(){
|
||||
byteCodeBehaviourComparer = new CompareByteCodeBehaviour();
|
||||
}
|
||||
@ -41,8 +37,8 @@ public class TestAll {
|
||||
|
||||
|
||||
try {
|
||||
FileClassLoader classLoader1 = new FileClassLoader(classPath);
|
||||
FileClassLoader classLoader2 = new FileClassLoader(codeGenOutputLocation);
|
||||
ClassFileLoader classLoader1 = new ClassFileLoader(classPath);
|
||||
ClassFileLoader classLoader2 = new ClassFileLoader(className + ".class");
|
||||
|
||||
Class<?> class1 = classLoader1.findClass(className);
|
||||
Class<?> class2 = classLoader2.findClass(className);
|
||||
@ -92,8 +88,8 @@ public class TestAll {
|
||||
}
|
||||
|
||||
try {
|
||||
FileClassLoader classLoader1 = new FileClassLoader(correctClassFilePath);
|
||||
FileClassLoader classLoader2 = new FileClassLoader(codeGenOutputLocation);
|
||||
ClassFileLoader classLoader1 = new ClassFileLoader(correctClassFilePath);
|
||||
ClassFileLoader classLoader2 = new ClassFileLoader(className + ".class");
|
||||
|
||||
Class<?> class1 = classLoader1.findClass(className);
|
||||
Class<?> class2 = classLoader2.findClass(className);
|
||||
@ -108,12 +104,12 @@ public class TestAll {
|
||||
|
||||
@Test
|
||||
public void testEmptyClass() {
|
||||
String classPath = "src/test/resources/basicClasses/emptyClass.jar";
|
||||
String classPath = "src/test/resources/basicClasses/emptyClass.class";
|
||||
Program ast = emptyClassAST.getEmptyProgramm();
|
||||
String className = "emptyClass";
|
||||
String javacode = "src/test/resources/basicClasses/emptyClass.java";
|
||||
//testByteCodeFromAst(classPath, ast ,className);
|
||||
testByteCodeFromScratch(classPath, javacode, className);
|
||||
testByteCodeFromAst(classPath, ast ,className);
|
||||
//testByteCodeFromScratch(classPath, javacode, className);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user