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

View File

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

View File

@ -731,7 +731,7 @@ public class JavaTXCompiler {
List<ResultSet> typeinferenceResult) throws IOException { List<ResultSet> typeinferenceResult) throws IOException {
try { try {
List<GenericGenratorResultForSourceFile> genericResults = getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); 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); bytecodeGen.visit(sf);
this.writeClassFile(bytecodeGen.getClassFiles(), path); this.writeClassFile(bytecodeGen.getClassFiles(), path);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
@ -792,7 +792,7 @@ public class JavaTXCompiler {
HashMap<JavaClassName, byte[]> classFiles = new HashMap<>(); HashMap<JavaClassName, byte[]> classFiles = new HashMap<>();
SourceFile sf = sourceFiles.get(f); SourceFile sf = sourceFiles.get(f);
BytecodeGen bytecodeGen = new BytecodeGen(classFiles, typeinferenceResult, simplifyResultsForAllSourceFiles, BytecodeGen bytecodeGen = new BytecodeGen(classFiles, typeinferenceResult, simplifyResultsForAllSourceFiles,
sf, path); sf, path, classLoader);
bytecodeGen.visit(sf); bytecodeGen.visit(sf);
if(path == null){ if(path == null){
path = f.getParent(); //Set path to path of the parsed .jav file 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"); Class<?> classToTest = loader.loadClass("de.test.TestClass");
Object instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); 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/"; public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/javFiles/packageTest/de/test/";
@Test @Test
public void testCorrectFolder1File() throws IOException, ClassNotFoundException { public void testCorrectFolder1FileWithWrongPackageName() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"packageNameTestWrongPackage.jav")); JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"packageNameTestWrongPackage.jav"));
compiler.typeInference(); compiler.typeInference();
File f = new File(rootDirectory + "TestClass.class"); 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 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 * Dieser Test wird übersprungen, da der Bytecode-Generator nicht mit zwei Eingabedateien gleichzeitig umgehen kann
@Test @Test

View File

@ -24,6 +24,18 @@ public class ImportTest extends TestCase {
compiler.generateBytecode(rootDirectory + "output/"); compiler.generateBytecode(rootDirectory + "output/");
File f = new File(rootDirectory + "output/de/test/ToImport.class"); File f = new File(rootDirectory + "output/de/test/ToImport.class");
assertTrue(f.exists()); 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 @Test
@ -55,4 +67,20 @@ public class ImportTest extends TestCase {
f = new File(rootDirectory + "ImportTest.class"); f = new File(rootDirectory + "ImportTest.class");
assertTrue(f.exists()); 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(){}
}