Changed to take ".class" files again :)

This commit is contained in:
KingJulian 2024-06-19 17:05:54 +02:00
parent eaac9898f4
commit 46ac49576e
3 changed files with 47 additions and 19 deletions

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

View File

@ -2,21 +2,16 @@ package ByteCode;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; 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.io.InputStream;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarFile; import java.util.jar.JarFile;
public class FileClassLoader extends ClassLoader { public class JarFileLoader extends ClassLoader {
private final String jarFilePath; private final String jarFilePath;
public FileClassLoader(String jarFilePath) { public JarFileLoader(String jarFilePath) {
this.jarFilePath = jarFilePath; this.jarFilePath = jarFilePath;
System.out.println("Jar File Path: " + jarFilePath); System.out.println("Jar File Path: " + jarFilePath);
} }

View File

@ -7,16 +7,13 @@ import gen.DecafParser;
import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.ParseTree;
import org.junit.Test; import org.junit.Test;
import javax.rmi.ssl.SslRMIClientSocketFactory;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List;
import ASTs.emptyClassAST; import ASTs.emptyClassAST;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -25,7 +22,6 @@ import static org.junit.Assert.fail;
public class TestAll { public class TestAll {
CompareByteCodeBehaviour byteCodeBehaviourComparer; CompareByteCodeBehaviour byteCodeBehaviourComparer;
private final String codeGenOutputLocation = "output.jar";
public TestAll(){ public TestAll(){
byteCodeBehaviourComparer = new CompareByteCodeBehaviour(); byteCodeBehaviourComparer = new CompareByteCodeBehaviour();
} }
@ -41,8 +37,8 @@ public class TestAll {
try { try {
FileClassLoader classLoader1 = new FileClassLoader(classPath); ClassFileLoader classLoader1 = new ClassFileLoader(classPath);
FileClassLoader classLoader2 = new FileClassLoader(codeGenOutputLocation); ClassFileLoader classLoader2 = new ClassFileLoader(className + ".class");
Class<?> class1 = classLoader1.findClass(className); Class<?> class1 = classLoader1.findClass(className);
Class<?> class2 = classLoader2.findClass(className); Class<?> class2 = classLoader2.findClass(className);
@ -92,8 +88,8 @@ public class TestAll {
} }
try { try {
FileClassLoader classLoader1 = new FileClassLoader(correctClassFilePath); ClassFileLoader classLoader1 = new ClassFileLoader(correctClassFilePath);
FileClassLoader classLoader2 = new FileClassLoader(codeGenOutputLocation); ClassFileLoader classLoader2 = new ClassFileLoader(className + ".class");
Class<?> class1 = classLoader1.findClass(className); Class<?> class1 = classLoader1.findClass(className);
Class<?> class2 = classLoader2.findClass(className); Class<?> class2 = classLoader2.findClass(className);
@ -108,12 +104,12 @@ public class TestAll {
@Test @Test
public void testEmptyClass() { public void testEmptyClass() {
String classPath = "src/test/resources/basicClasses/emptyClass.jar"; String classPath = "src/test/resources/basicClasses/emptyClass.class";
Program ast = emptyClassAST.getEmptyProgramm(); Program ast = emptyClassAST.getEmptyProgramm();
String className = "emptyClass"; String className = "emptyClass";
String javacode = "src/test/resources/basicClasses/emptyClass.java"; String javacode = "src/test/resources/basicClasses/emptyClass.java";
//testByteCodeFromAst(classPath, ast ,className); testByteCodeFromAst(classPath, ast ,className);
testByteCodeFromScratch(classPath, javacode, className); //testByteCodeFromScratch(classPath, javacode, className);
} }