Tests anfügen und Bugs fixen

This commit is contained in:
JanUlrich 2019-12-22 22:52:55 +01:00
parent 306af907c5
commit d0b6dbcd30
9 changed files with 87 additions and 8 deletions

View File

@ -119,22 +119,24 @@ public class BytecodeGen implements ASTVisitor {
private GenericsGeneratorResultForClass generatedGenerics;
private Resolver resolver;
private final ClassLoader classLoader;
public BytecodeGen(HashMap<JavaClassName, byte[]> classFiles, Collection<ResultSet> listOfResultSets, List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles, SourceFile sf,
String path) {
String path, ClassLoader classLoader) {
this.classFiles = classFiles;
this.listOfResultSets = listOfResultSets;
this.simplifyResultsForAllSourceFiles = simplifyResultsForAllSourceFiles;
this.sf = sf;
this.path = path;
this.pkgName = sf.getPkgName();
this.classLoader = classLoader;
}
@Override
public void visit(SourceFile sourceFile) {
for (ClassOrInterface cl : sourceFile.getClasses()) {
System.out.println("in Class: " + cl.getClassName().toString());
BytecodeGen classGen = new BytecodeGen(classFiles, listOfResultSets, simplifyResultsForAllSourceFiles, sf, path);
BytecodeGen classGen = new BytecodeGen(classFiles, listOfResultSets, simplifyResultsForAllSourceFiles, sf, path, classLoader);
cl.accept(classGen);
classGen.writeClass(cl.getClassName());
}
@ -270,7 +272,7 @@ public class BytecodeGen implements ASTVisitor {
constructorPos += 1;
BytecodeGenMethod gen = new BytecodeGenMethod(className, superClass, resultSet, field, mv, paramsAndLocals, cw,
genericsAndBoundsMethod, genericsAndBounds, isInterface, classFiles, sf, path, block, constructorPos, ClassLoader.getSystemClassLoader());
genericsAndBoundsMethod, genericsAndBounds, isInterface, classFiles, sf, path, block, constructorPos, classLoader);
if (!field.getParameterList().iterator().hasNext()
&& !(field.block.statements.get(field.block.statements.size() - 1) instanceof ReturnVoid)) {
mv.visitInsn(Opcodes.RETURN);
@ -351,7 +353,7 @@ public class BytecodeGen implements ASTVisitor {
mv.visitCode();
BytecodeGenMethod gen = new BytecodeGenMethod(className, superClass, resultSet, method, mv, paramsAndLocals, cw,
genericsAndBoundsMethod, genericsAndBounds, isInterface, classFiles, sf, path, ClassLoader.getSystemClassLoader());
genericsAndBoundsMethod, genericsAndBounds, isInterface, classFiles, sf, path, classLoader);
mv.visitMaxs(0, 0);
mv.visitEnd();

View File

@ -814,7 +814,7 @@ public class BytecodeGenMethod implements StatementVisitor {
// mDesc = helper.generateBCForFunN(methCallType,typesOfParams);
}else {
try {
cLoader2 = new URLClassLoader(new URL[] {new URL("file://"+path)});
cLoader2 = new URLClassLoader(new URL[] {new URL("file://"+path)}, classLoader);
java.lang.reflect.Method[] methods = cLoader2.loadClass(clazz).getMethods();
System.out.println("Methods of " + receiverName + " ");
for(int i = 0; i<methods.length; i++) {

View File

@ -731,7 +731,7 @@ public class JavaTXCompiler {
List<ResultSet> typeinferenceResult) throws IOException {
try {
List<GenericGenratorResultForSourceFile> genericResults = getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult);
BytecodeGen bytecodeGen = new BytecodeGen(classFiles,typeinferenceResult, genericResults, sf,path);
BytecodeGen bytecodeGen = new BytecodeGen(classFiles,typeinferenceResult, genericResults, sf,path, classLoader);
bytecodeGen.visit(sf);
this.writeClassFile(bytecodeGen.getClassFiles(), path);
} catch (ClassNotFoundException e) {
@ -792,7 +792,7 @@ public class JavaTXCompiler {
HashMap<JavaClassName, byte[]> classFiles = new HashMap<>();
SourceFile sf = sourceFiles.get(f);
BytecodeGen bytecodeGen = new BytecodeGen(classFiles, typeinferenceResult, simplifyResultsForAllSourceFiles,
sf, path);
sf, path, classLoader);
bytecodeGen.visit(sf);
if(path == null){
path = f.getParent(); //Set path to path of the parsed .jav file

View File

@ -47,4 +47,6 @@ public class Bytecode extends TestCase {
Class<?> classToTest = loader.loadClass("de.test.TestClass");
Object instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
}
}

View File

@ -14,7 +14,7 @@ public class CheckPackageFolder extends TestCase {
public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/javFiles/packageTest/de/test/";
@Test
public void testCorrectFolder1File() throws IOException, ClassNotFoundException {
public void testCorrectFolder1FileWithWrongPackageName() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"packageNameTestWrongPackage.jav"));
compiler.typeInference();
File f = new File(rootDirectory + "TestClass.class");
@ -26,6 +26,32 @@ public class CheckPackageFolder extends TestCase {
assertTrue(f.exists()); //Es ist erlaubt falsche package Namen zu verwenden. Warnung wäre optional
}
@Test
public void testCorrectFolder1File() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"TestClass.jav"));
compiler.typeInference();
File f = new File(rootDirectory + "TestClass.class");
if(f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode();
f = new File(rootDirectory + "TestClass.class");
assertTrue(f.exists()); //Es ist erlaubt falsche package Namen zu verwenden. Warnung wäre optional
}
@Test
public void testCorrectFolder1FileAndOutputDirectory() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"TestClass.jav"));
compiler.typeInference();
File f = new File(rootDirectory + "output/de/test/TestClass.class");
if(f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode(rootDirectory+"output/");
f = new File(rootDirectory + "output/de/test/TestClass.class");
assertTrue(f.exists()); //Es ist erlaubt falsche package Namen zu verwenden. Warnung wäre optional
}
/*
* Dieser Test wird übersprungen, da der Bytecode-Generator nicht mit zwei Eingabedateien gleichzeitig umgehen kann
@Test

View File

@ -24,6 +24,18 @@ public class ImportTest extends TestCase {
compiler.generateBytecode(rootDirectory + "output/");
File f = new File(rootDirectory + "output/de/test/ToImport.class");
assertTrue(f.exists());
compiler = new JavaTXCompiler(new File(rootDirectory+"subpackage1/ToImport2.jav"));
compiler.typeInference();
compiler.generateBytecode(rootDirectory + "output/");
f = new File(rootDirectory + "output/de/test/subpackage1/ToImport2.class");
assertTrue(f.exists());
compiler = new JavaTXCompiler(new File(rootDirectory+"subpackage2/ToImport3.jav"));
compiler.typeInference();
compiler.generateBytecode(rootDirectory + "output/");
f = new File(rootDirectory + "output/de/test/subpackage2/ToImport3.class");
assertTrue(f.exists());
}
@Test
@ -55,4 +67,20 @@ public class ImportTest extends TestCase {
f = new File(rootDirectory + "ImportTest.class");
assertTrue(f.exists());
}
@Test
public void testImportTwoClasses() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(
Lists.newArrayList(new File(rootDirectory+"ImportTest2.jav")),
Lists.newArrayList(new URL("file://"+rootDirectory+"output/")));
compiler.typeInference();
File f = new File(rootDirectory + "ImportTest2.class");
if(f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode(null);
f = new File(rootDirectory + "ImportTest2.class");
assertTrue(f.exists());
}
}

View File

@ -0,0 +1,11 @@
package de.test;
import de.test.subpackage1.ToImport2;
import de.test.subpackage2.ToImport3;
class ImportTest2{
void methode(){
new ToImport2().m1();
new ToImport3().m2();
}
}

View File

@ -0,0 +1,5 @@
package de.test.subpackage1;
class ToImport2{
void m1(){}
}

View File

@ -0,0 +1,5 @@
package de.test.subpackage2;
class ToImport3{
void m2(){}
}