Moved test resources to src/test and refactored tests

This commit is contained in:
luca9913 2023-06-05 09:28:55 +02:00
parent e3edd0410a
commit 63c5cb8390
213 changed files with 583 additions and 861 deletions

View File

@ -1 +0,0 @@

View File

@ -1,5 +1,4 @@
import static org.junit.Assert.*;
import java.io.File;
@ -24,51 +23,48 @@ public class AllgemeinTest {
private static Class<?> classToTest;
private static String pathToClassFile;
private static Object instanceOfClass;
@Test
public void test() throws Exception {
//String className = "GenTest";
//String className = "Overloading_Generics";
//String className = "Generics";
//String className = "OverloadingMain";
//String className = "OverrideMain";
//String className = "OverrideMainRet";
//String className = "FCTest1";
//String className = "FCTest2";
//String className = "Pair";
//String className = "FCTest3";
//String className = "Var";
//String className = "Put";
//String className = "Twice";
//String className = "TestSubTypless";
//String className = "addList";
//String className = "M";
//String className = "Wildcard_Andi";
//String className = "Box";
//String className = "Box_Main";
//String className = "wildcardPair";
//String className = "VectorConstAdd";
// String className = "GenTest";
// String className = "Overloading_Generics";
// String className = "Generics";
// String className = "OverloadingMain";
// String className = "OverrideMain";
// String className = "OverrideMainRet";
// String className = "FCTest1";
// String className = "FCTest2";
// String className = "Pair";
// String className = "FCTest3";
// String className = "Var";
// String className = "Put";
// String className = "Twice";
// String className = "TestSubTypless";
// String className = "addList";
// String className = "M";
// String className = "Wildcard_Andi";
// String className = "Box";
// String className = "Box_Main";
// String className = "wildcardPair";
// String className = "VectorConstAdd";
String className = "VectorNotObject";
//String className = "WildcardCaptureConversionTest";
//PL 2019-10-24: genutzt fuer unterschiedliche Tests
path = System.getProperty("user.dir")+"/resources/AllgemeinTest/" + className + ".jav";
//path = System.getProperty("user.dir")+"/src/test/resources/AllgemeinTest/Overloading_Generics.jav";
//path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/mathStrucInteger.jav";
//compiler = new JavaTXCompiler(Lists.newArrayList(new File(System.getProperty("user.dir")+"/src/test/resources/AllgemeinTest/Overloading_Generics.jav")));
///*
compiler = new JavaTXCompiler(
Lists.newArrayList(new File(path)),
Lists.newArrayList(new File(System.getProperty("user.dir")+"/resources/testBytecode/generatedBC/")));
//*/
compiler.generateBytecode(System.getProperty("user.dir")+"/resources/testBytecode/generatedBC/");
pathToClassFile = System.getProperty("user.dir")+"/resources/testBytecode/generatedBC/";
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
// String className = "WildcardCaptureConversionTest";
// PL 2019-10-24: genutzt fuer unterschiedliche Tests
path = System.getProperty("user.dir") + "/src/test/resources/AllgemeinTest/" + className + ".jav";
// path = System.getProperty("user.dir")+"/src/test/src/test/resources/AllgemeinTest/Overloading_Generics.jav";
// path = System.getProperty("user.dir")+"/src/test/src/test/resources/bytecode/javFiles/mathStrucInteger.jav";
// compiler = new JavaTXCompiler(Lists.newArrayList(new File(System.getProperty("user.dir")+"/src/test/src/test/resources/AllgemeinTest/Overloading_Generics.jav")));
/// *
compiler = new JavaTXCompiler(Lists.newArrayList(new File(path)), Lists.newArrayList(new File(System.getProperty("user.dir") + "/src/test/resources/testBytecode/generatedBC/")));
// */
compiler.generateBytecode(System.getProperty("user.dir") + "/src/test/resources/testBytecode/generatedBC/");
pathToClassFile = System.getProperty("user.dir") + "/src/test/resources/testBytecode/generatedBC/";
loader = new URLClassLoader(new URL[] { new URL("file://" + pathToClassFile) });
classToTest = loader.loadClass(className);
//classToTest = loader.loadClass("Overloading_Generics");
//instanceOfClass = classToTest.getDeclaredConstructor().newInstance("A");
//classToTest = loader.loadClass("Overloading_Generics1");
//instanceOfClass = classToTest.getDeclaredConstructor(Object.class).newInstance("B");
// classToTest = loader.loadClass("Overloading_Generics");
// instanceOfClass = classToTest.getDeclaredConstructor().newInstance("A");
// classToTest = loader.loadClass("Overloading_Generics1");
// instanceOfClass = classToTest.getDeclaredConstructor(Object.class).newInstance("B");
}
}

View File

@ -12,41 +12,40 @@ import java.net.URLClassLoader;
public class Bytecode extends TestCase {
public static final String rootDirectory = System.getProperty("user.dir")+"/resources/javFiles/packageTest/";
public static final String rootDirectory = System.getProperty("user.dir") + "/src/test/resources/javFiles/packageTest/";
@Test
public void testSetPackageNameInBytecode() throws Exception {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"de/test/TestClass.jav"));
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory + "de/test/TestClass.jav"));
compiler.typeInference();
File f = new File(rootDirectory + "de/test/TestClass.class");
if(f.exists() && !f.isDirectory()) {
if (f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode();
f = new File(rootDirectory + "de/test/TestClass.class");
assertTrue(f.exists());
URLClassLoader loader = new URLClassLoader(new URL[]{new URL("file://" + rootDirectory)});
URLClassLoader loader = new URLClassLoader(new URL[] { new URL("file://" + rootDirectory) });
Class<?> classToTest = loader.loadClass("de.test.TestClass");
Object instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
}
@Test
public void testSetPackageNameInBytecodeAndOutputFolder() throws Exception {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"de/test/TestClass.jav"));
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory + "de/test/TestClass.jav"));
compiler.typeInference();
File f = new File(rootDirectory + "de/test/output/de/test/TestClass.class");
if(f.exists() && !f.isDirectory()) {
if (f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode(rootDirectory + "de/test/output/");
f = new File(rootDirectory + "de/test/output/de/test/TestClass.class");
assertTrue(f.exists());
URLClassLoader loader = new URLClassLoader(new URL[]{new URL("file://" + rootDirectory + "de/test/output/")});
URLClassLoader loader = new URLClassLoader(new URL[] { new URL("file://" + rootDirectory + "de/test/output/") });
Class<?> classToTest = loader.loadClass("de.test.TestClass");
Object instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
}
}

View File

@ -12,69 +12,51 @@ import java.util.Arrays;
public class CheckPackageFolder extends TestCase {
public static final String rootDirectory = System.getProperty("user.dir")+"/resources/javFiles/packageTest/de/test/";
public static final String rootDirectory = System.getProperty("user.dir") + "/src/test/resources/javFiles/packageTest/de/test/";
@Test
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();
File f = new File(rootDirectory + "TestClass.class");
if(f.exists() && !f.isDirectory()) {
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
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"));
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory + "TestClass.jav"));
compiler.typeInference();
File f = new File(rootDirectory + "TestClass.class");
if(f.exists() && !f.isDirectory()) {
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
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"));
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()) {
if (f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode(rootDirectory+"output/");
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
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
public void testCorrectFolder2Files() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(Arrays.asList(
new File(rootDirectory+"subpackage1/Test1.jav"),
new File(rootDirectory+"subpackage2/Test2.jav")
));
compiler.typeInference();
File f = new File(rootDirectory + "subpackage1/Test1.class");
if(f.exists() && !f.isDirectory()) {
f.delete();
}
File f2 = new File(rootDirectory + "subpackage2/Test2.class");
if(f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode();
f = new File(rootDirectory + "subpackage1/Test1.class");
f2 = new File(rootDirectory + "subpackage2/Test2.class");
assertTrue(f.exists());
assertTrue(f2.exists());
}
* Dieser Test wird übersprungen, da der Bytecode-Generator nicht mit zwei Eingabedateien gleichzeitig umgehen kann
*
* @Test public void testCorrectFolder2Files() throws IOException, ClassNotFoundException { JavaTXCompiler compiler = new JavaTXCompiler(Arrays.asList( new File(rootDirectory+"subpackage1/Test1.jav"), new File(rootDirectory+"subpackage2/Test2.jav") )); compiler.typeInference(); File f = new File(rootDirectory + "subpackage1/Test1.class"); if(f.exists() && !f.isDirectory()) { f.delete(); } File f2 = new File(rootDirectory + "subpackage2/Test2.class"); if(f.exists() && !f.isDirectory()) {
* f.delete(); } compiler.generateBytecode(); f = new File(rootDirectory + "subpackage1/Test1.class"); f2 = new File(rootDirectory + "subpackage2/Test2.class"); assertTrue(f.exists()); assertTrue(f2.exists()); }
*/
}

View File

@ -11,16 +11,16 @@ import java.net.URLClassLoader;
public class ConsoleInterfaceTest extends TestCase {
public static final String rootDirectory = System.getProperty("user.dir")+"/resources/javFiles/packageTest/";
public static final String rootDirectory = System.getProperty("user.dir") + "/src/test/resources/javFiles/packageTest/";
@Test
public void testCompileSingleJavFile() throws Exception {
File f = new File(rootDirectory + "de/test/TestClass.class");
if(f.exists() && !f.isDirectory()) {
if (f.exists() && !f.isDirectory()) {
f.delete();
}
ConsoleInterface.main(new String[]{rootDirectory + "de/test/TestClass.jav"});
ConsoleInterface.main(new String[] { rootDirectory + "de/test/TestClass.jav" });
f = new File(rootDirectory + "de/test/TestClass.class");
assertTrue(f.exists());
@ -29,11 +29,11 @@ public class ConsoleInterfaceTest extends TestCase {
@Test
public void testCompileSingleJavFileWithOutputDirectory() throws Exception {
File f = new File(rootDirectory + "de/test/output/de/test/TestClass.class");
if(f.exists() && !f.isDirectory()) {
if (f.exists() && !f.isDirectory()) {
f.delete();
}
ConsoleInterface.main(new String[]{"-d", rootDirectory + "de/test/output/" ,rootDirectory + "de/test/TestClass.jav"});
ConsoleInterface.main(new String[] { "-d", rootDirectory + "de/test/output/", rootDirectory + "de/test/TestClass.jav" });
f = new File(rootDirectory + "de/test/output/de/test/TestClass.class");
assertTrue(f.exists());
@ -41,18 +41,18 @@ public class ConsoleInterfaceTest extends TestCase {
@Test
public void testCpNotEndsWithSlash() throws Exception {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"/de/test/ToImport.jav"));
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory + "/de/test/ToImport.jav"));
compiler.typeInference();
compiler.generateBytecode(rootDirectory + "output/");
File f = new File(rootDirectory + "output/de/test/ToImport.class");
assertTrue(f.exists());
f = new File(rootDirectory + "de/test/ImportTest.class");
if(f.exists() && !f.isDirectory()) {
if (f.exists() && !f.isDirectory()) {
f.delete();
}
ConsoleInterface.main(new String[]{"-cp", rootDirectory + "de/test/output" , rootDirectory + "de/test/ImportTest.jav"});
ConsoleInterface.main(new String[] { "-cp", rootDirectory + "de/test/output", rootDirectory + "de/test/ImportTest.jav" });
f = new File(rootDirectory + "de/test/ImportTest.class");
assertTrue(f.exists());
@ -61,11 +61,11 @@ public class ConsoleInterfaceTest extends TestCase {
@Test
public void testOutputDirNotEndsWithSlash() throws Exception {
File f = new File(rootDirectory + "de/test/output/de/test/TestClass.class");
if(f.exists() && !f.isDirectory()) {
if (f.exists() && !f.isDirectory()) {
f.delete();
}
ConsoleInterface.main(new String[]{"-d", rootDirectory + "de/test/output" ,rootDirectory + "de/test/TestClass.jav"});
ConsoleInterface.main(new String[] { "-d", rootDirectory + "de/test/output", rootDirectory + "de/test/TestClass.jav" });
f = new File(rootDirectory + "de/test/output/de/test/TestClass.class");
assertTrue(f.exists());
@ -73,18 +73,18 @@ public class ConsoleInterfaceTest extends TestCase {
@Test
public void testCompileSingleJavFileWithClassPath() throws Exception {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"/de/test/ToImport.jav"));
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory + "/de/test/ToImport.jav"));
compiler.typeInference();
compiler.generateBytecode(rootDirectory + "output/");
File f = new File(rootDirectory + "output/de/test/ToImport.class");
assertTrue(f.exists());
f = new File(rootDirectory + "de/test/ImportTest.class");
if(f.exists() && !f.isDirectory()) {
if (f.exists() && !f.isDirectory()) {
f.delete();
}
ConsoleInterface.main(new String[]{"-cp", rootDirectory + "de/test/output/" ,rootDirectory + "de/test/ImportTest.jav"});
ConsoleInterface.main(new String[] { "-cp", rootDirectory + "de/test/output/", rootDirectory + "de/test/ImportTest.jav" });
f = new File(rootDirectory + "de/test/ImportTest.class");
assertTrue(f.exists());
@ -92,19 +92,18 @@ public class ConsoleInterfaceTest extends TestCase {
@Test
public void testCompileSingleJavFileWithMultipleClassPath() throws Exception {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"/de/test/ToImport.jav"));
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory + "/de/test/ToImport.jav"));
compiler.typeInference();
compiler.generateBytecode(rootDirectory + "output/");
File f = new File(rootDirectory + "output/de/test/ToImport.class");
assertTrue(f.exists());
f = new File(rootDirectory + "de/test/ImportTest.class");
if(f.exists() && !f.isDirectory()) {
if (f.exists() && !f.isDirectory()) {
f.delete();
}
ConsoleInterface.main(new String[]{"-cp", rootDirectory + "de/test/output/:"+rootDirectory+"de",
rootDirectory + "de/test/ImportTest.jav"});
ConsoleInterface.main(new String[] { "-cp", rootDirectory + "de/test/output/:" + rootDirectory + "de", rootDirectory + "de/test/ImportTest.jav" });
f = new File(rootDirectory + "de/test/ImportTest.class");
assertTrue(f.exists());
@ -112,19 +111,18 @@ public class ConsoleInterfaceTest extends TestCase {
@Test
public void testCompileSingleJavFileWithClassPathAndOutputDir() throws Exception {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"/de/test/ToImport.jav"));
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory + "/de/test/ToImport.jav"));
compiler.typeInference();
compiler.generateBytecode(rootDirectory + "output/");
File f = new File(rootDirectory + "output/de/test/ToImport.class");
assertTrue(f.exists());
f = new File(rootDirectory + "de/test/output/de/test/ImportTest.class");
if(f.exists() && !f.isDirectory()) {
if (f.exists() && !f.isDirectory()) {
f.delete();
}
ConsoleInterface.main(new String[]{"-cp", rootDirectory + "de/test/output/",
"-d"+rootDirectory + "de/test/output/" ,rootDirectory + "de/test/ImportTest.jav"});
ConsoleInterface.main(new String[] { "-cp", rootDirectory + "de/test/output/", "-d" + rootDirectory + "de/test/output/", rootDirectory + "de/test/ImportTest.jav" });
f = new File(rootDirectory + "de/test/output/de/test/ImportTest.class");
assertTrue(f.exists());

View File

@ -12,32 +12,31 @@ import java.net.URL;
public class ImportTest extends TestCase {
public static final String rootDirectory = System.getProperty("user.dir")+"/resources/javFiles/packageTest/de/test/";
public static final String rootDirectory = System.getProperty("user.dir") + "/src/test/resources/javFiles/packageTest/de/test/";
public ImportTest() throws ClassNotFoundException, IOException {
/*
Generate ToImport class in rootDirectory and in output-Directory
* Generate ToImport class in rootDirectory and in output-Directory
*/
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"ToImport.jav"));
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory + "ToImport.jav"));
compiler.typeInference();
compiler.generateBytecode(rootDirectory + "output/");
File f = new File(rootDirectory + "output/de/test/ToImport.class");
assertTrue(f.exists());
compiler = new JavaTXCompiler(new File(rootDirectory+"ToImport.jav"));
compiler = new JavaTXCompiler(new File(rootDirectory + "ToImport.jav"));
compiler.typeInference();
compiler.generateBytecode();
f = new File(rootDirectory + "ToImport.class");
assertTrue(f.exists());
compiler = new JavaTXCompiler(new File(rootDirectory+"subpackage1/ToImport2.jav"));
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 = new JavaTXCompiler(new File(rootDirectory + "subpackage2/ToImport3.jav"));
compiler.typeInference();
compiler.generateBytecode(rootDirectory + "output/");
f = new File(rootDirectory + "output/de/test/subpackage2/ToImport3.class");
@ -46,12 +45,10 @@ public class ImportTest extends TestCase {
@Test
public void testSetPackageNameInBytecodeAndOutputFolder() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(
Lists.newArrayList(new File(rootDirectory+"ImportTest.jav")),
Lists.newArrayList(new File(rootDirectory+"output/")));
JavaTXCompiler compiler = new JavaTXCompiler(Lists.newArrayList(new File(rootDirectory + "ImportTest.jav")), Lists.newArrayList(new File(rootDirectory + "output/")));
compiler.typeInference();
File f = new File(rootDirectory + "output/de/test/ImportTest.class");
if(f.exists() && !f.isDirectory()) {
if (f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode(rootDirectory + "output/");
@ -61,12 +58,10 @@ public class ImportTest extends TestCase {
@Test
public void testSetPackageNameInBytecodeAndStandardOutputFolder() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(
Lists.newArrayList(new File(rootDirectory+"ImportTest.jav")),
Lists.newArrayList(new File(rootDirectory+"output/")));
JavaTXCompiler compiler = new JavaTXCompiler(Lists.newArrayList(new File(rootDirectory + "ImportTest.jav")), Lists.newArrayList(new File(rootDirectory + "output/")));
compiler.typeInference();
File f = new File(rootDirectory + "ImportTest.class");
if(f.exists() && !f.isDirectory()) {
if (f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode();
@ -74,15 +69,12 @@ public class ImportTest extends TestCase {
assertTrue(f.exists());
}
@Test
public void testImportTwoClasses() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(
Lists.newArrayList(new File(rootDirectory+"ImportTest2.jav")),
Lists.newArrayList(new File(rootDirectory+"output/")));
JavaTXCompiler compiler = new JavaTXCompiler(Lists.newArrayList(new File(rootDirectory + "ImportTest2.jav")), Lists.newArrayList(new File(rootDirectory + "output/")));
compiler.typeInference();
File f = new File(rootDirectory + "ImportTest2.class");
if(f.exists() && !f.isDirectory()) {
if (f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode();
@ -92,11 +84,10 @@ public class ImportTest extends TestCase {
@Test
public void testImportDefaultPackage() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(
Lists.newArrayList(new File(rootDirectory+"ImportTestDefault.jav")));
JavaTXCompiler compiler = new JavaTXCompiler(Lists.newArrayList(new File(rootDirectory + "ImportTestDefault.jav")));
compiler.typeInference();
File f = new File(rootDirectory + "ImportTestDefault.class");
if(f.exists() && !f.isDirectory()) {
if (f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode();
@ -104,5 +95,4 @@ public class ImportTest extends TestCase {
assertTrue(f.exists());
}
}

View File

@ -13,7 +13,7 @@ import java.net.URLClassLoader;
public class LoadDefaultPackageClassesTest extends TestCase {
public static final String rootDirectory = System.getProperty("user.dir") + "/resources/javFiles/packageTest/";
public static final String rootDirectory = System.getProperty("user.dir") + "/src/test/resources/javFiles/packageTest/";
public LoadDefaultPackageClassesTest() throws ClassNotFoundException, IOException {
/*
@ -35,14 +35,8 @@ public class LoadDefaultPackageClassesTest extends TestCase {
cl.loadClass("Gen");
}
/*
* public void testE2E() throws IOException, ClassNotFoundException {
* JavaTXCompiler compiler = new JavaTXCompiler(new
* File(rootDirectory+"OL.jav")); compiler.typeInference();
* compiler.generateBytecode(); File f = new File(rootDirectory + "OL.class");
* assertTrue(f.exists());
* public void testE2E() throws IOException, ClassNotFoundException { JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"OL.jav")); compiler.typeInference(); compiler.generateBytecode(); File f = new File(rootDirectory + "OL.class"); assertTrue(f.exists());
*
* compiler = new JavaTXCompiler(new File(rootDirectory+"OLMain.jav"));
* compiler.typeInference(); compiler.generateBytecode(); f = new
* File(rootDirectory + "OLMain.class"); assertTrue(f.exists()); }
* compiler = new JavaTXCompiler(new File(rootDirectory+"OLMain.jav")); compiler.typeInference(); compiler.generateBytecode(); f = new File(rootDirectory + "OLMain.class"); assertTrue(f.exists()); }
*/
}

View File

@ -31,20 +31,18 @@ public class OLOneFileTest {
private static Object instanceOfClass1;
private static Object instanceOfClass2;
public static final String rootDirectory = System.getProperty("user.dir")+"/resources/javFiles/packageTest/";
public static final String rootDirectory = System.getProperty("user.dir") + "/src/test/resources/javFiles/packageTest/";
@BeforeClass
public static void setUpBeforeClass() throws Exception {
path = rootDirectory +"OLOneFile.jav";
path = rootDirectory + "OLOneFile.jav";
fileToTest = new File(path);
compiler = new JavaTXCompiler(
Lists.newArrayList(fileToTest),
Lists.newArrayList(new File(rootDirectory+"de/test/output/")));
pathToClassFile = System.getProperty("user.dir")+"/resources/javFiles/packageTest/";
compiler = new JavaTXCompiler(Lists.newArrayList(fileToTest), Lists.newArrayList(new File(rootDirectory + "de/test/output/")));
pathToClassFile = System.getProperty("user.dir") + "/src/test/resources/javFiles/packageTest/";
List<ResultSet> typeinferenceResult = compiler.typeInference();
//List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult);
//compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles);
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
// List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult);
// compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles);
loader = new URLClassLoader(new URL[] { new URL("file://" + pathToClassFile) });
classToTest = loader.loadClass("OLOneFile");
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
classToTest1 = loader.loadClass("OLextendsOneFile");
@ -52,78 +50,78 @@ public class OLOneFileTest {
classToTest2 = loader.loadClass("OLMainOneFile");
instanceOfClass2 = classToTest2.getDeclaredConstructor().newInstance();
}
@Test
public void testOLClassName() {
assertEquals("OLOneFile", classToTest.getName());
}
@Test
public void testmInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method m = classToTest.getDeclaredMethod("m2", Integer.class);
Integer result = (Integer) m.invoke(instanceOfClass, 5);
assertEquals(new Integer(10), result);
}
@Test
public void testmDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method m = classToTest.getDeclaredMethod("m2", Double.class);
Double result = (Double) m.invoke(instanceOfClass, 5.0);
assertEquals(new Double(10.0), result);
}
@Test
public void testmString() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method m = classToTest.getDeclaredMethod("m2", String.class);
String result = (String) m.invoke(instanceOfClass, "xxx");
assertEquals("xxxxxx", result);
}
@Test
public void testOLextendsClassName() {
assertEquals("OLextendsOneFile", classToTest1.getName());
}
@Test
public void testextendsInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method main = classToTest1.getMethod("m2", Integer.class);
Integer result = (Integer) main.invoke(instanceOfClass1, 5);
assertEquals(new Integer(10), result);
}
@Test
public void testextendsDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method main = classToTest1.getMethod("m2", Double.class);
Double result = (Double) main.invoke(instanceOfClass1, 5.0);
assertEquals(new Double(10.0), result);
}
@Test
public void testextendsString() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method main = classToTest1.getMethod("m2", String.class);
String result = (String) main.invoke(instanceOfClass1, "xxx");
assertEquals("xxxxxx", result);
}
@Test
public void testOLMainClassName() {
assertEquals("OLMainOneFile", classToTest2.getName());
}
@Test
public void testmainInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method main = classToTest2.getDeclaredMethod("main", Integer.class);
Integer result = (Integer) main.invoke(instanceOfClass2, 5);
assertEquals(new Integer(10), result);
}
@Test
public void testmainDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method main = classToTest2.getDeclaredMethod("main", Double.class);
Double result = (Double) main.invoke(instanceOfClass2, 5.0);
assertEquals(new Double(10.0), result);
}
@Test
public void testmainString() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method main = classToTest2.getDeclaredMethod("main", String.class);

View File

@ -30,114 +30,110 @@ public class OLTest {
private static Object instanceOfClass;
private static Object instanceOfClass1;
private static Object instanceOfClass2;
public static final String rootDirectory = System.getProperty("user.dir")+"/resources/javFiles/packageTest";
public static final String rootDirectory = System.getProperty("user.dir") + "/src/test/resources/javFiles/packageTest";
@BeforeClass
public static void setUpBeforeClass() throws Exception {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"/de/test/OL.jav"));
compiler.typeInference();
compiler.generateBytecode(rootDirectory + "/de/test/output/");
loader = new URLClassLoader(new URL[] {new URL("file://"+ rootDirectory + "/de/test/output/")});
classToTest = loader.loadClass("de.test.OL");
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
path = System.getProperty("user.dir")+"/resources/javFiles/packageTest/OLextends.jav";
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory + "/de/test/OL.jav"));
compiler.typeInference();
compiler.generateBytecode(rootDirectory + "/de/test/output/");
loader = new URLClassLoader(new URL[] { new URL("file://" + rootDirectory + "/de/test/output/") });
classToTest = loader.loadClass("de.test.OL");
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
path = System.getProperty("user.dir") + "/src/test/resources/javFiles/packageTest/OLextends.jav";
fileToTest = new File(path);
compiler = new JavaTXCompiler(
Lists.newArrayList(new File(rootDirectory+"/OLextends.jav")),
Lists.newArrayList(new File(rootDirectory+"/de/test/output/")));
//compiler = new JavaTXCompiler(fileToTest);
pathToClassFile = System.getProperty("user.dir")+"/resources/javFiles/packageTest/";
compiler = new JavaTXCompiler(Lists.newArrayList(new File(rootDirectory + "/OLextends.jav")), Lists.newArrayList(new File(rootDirectory + "/de/test/output/")));
// compiler = new JavaTXCompiler(fileToTest);
pathToClassFile = System.getProperty("user.dir") + "/src/test/resources/javFiles/packageTest/";
compiler.generateBytecode(pathToClassFile);
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile), new URL("file://"+ rootDirectory + "/de/test/output/")});
loader = new URLClassLoader(new URL[] { new URL("file://" + pathToClassFile), new URL("file://" + rootDirectory + "/de/test/output/") });
classToTest1 = loader.loadClass("OLextends");
instanceOfClass1 = classToTest1.getDeclaredConstructor().newInstance();
path = System.getProperty("user.dir")+"/resources/javFiles/packageTest/OLMain.jav";
path = System.getProperty("user.dir") + "/src/test/resources/javFiles/packageTest/OLMain.jav";
fileToTest = new File(path);
compiler = new JavaTXCompiler(
Lists.newArrayList(new File(rootDirectory+"/OLMain.jav")),
Lists.newArrayList(new File(rootDirectory+"/de/test/output/")));
//compiler = new JavaTXCompiler(fileToTest);
pathToClassFile = System.getProperty("user.dir")+"/resources/javFiles/packageTest/";
compiler = new JavaTXCompiler(Lists.newArrayList(new File(rootDirectory + "/OLMain.jav")), Lists.newArrayList(new File(rootDirectory + "/de/test/output/")));
// compiler = new JavaTXCompiler(fileToTest);
pathToClassFile = System.getProperty("user.dir") + "/src/test/resources/javFiles/packageTest/";
compiler.generateBytecode(pathToClassFile);
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile), new URL("file://"+ rootDirectory + "/de/test/output/")});
loader = new URLClassLoader(new URL[] { new URL("file://" + pathToClassFile), new URL("file://" + rootDirectory + "/de/test/output/") });
classToTest2 = loader.loadClass("OLMain");
instanceOfClass2 = classToTest2.getDeclaredConstructor().newInstance();
}
@Test
public void testOLClassName() {
assertEquals("de.test.OL", classToTest.getName());
}
@Test
public void testmInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method m = classToTest.getDeclaredMethod("m", Integer.class);
Integer result = (Integer) m.invoke(instanceOfClass, 5);
assertEquals(new Integer(10), result);
}
@Test
public void testmDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method m = classToTest.getDeclaredMethod("m", Double.class);
Double result = (Double) m.invoke(instanceOfClass, 5.0);
assertEquals(new Double(10.0), result);
}
@Test
public void testmString() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method m = classToTest.getDeclaredMethod("m", String.class);
String result = (String) m.invoke(instanceOfClass, "xxx");
assertEquals("xxxxxx", result);
}
@Test
public void testOLextendsClassName() {
assertEquals("OLextends", classToTest1.getName());
}
@Test
public void testextendsInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method main = classToTest1.getMethod("m", Integer.class);
Integer result = (Integer) main.invoke(instanceOfClass1, 5);
assertEquals(new Integer(10), result);
}
@Test
public void testextendsDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method main = classToTest1.getMethod("m", Double.class);
Double result = (Double) main.invoke(instanceOfClass1, 5.0);
assertEquals(new Double(10.0), result);
}
@Test
public void testextendsString() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method main = classToTest1.getMethod("m", String.class);
String result = (String) main.invoke(instanceOfClass1, "xxx");
assertEquals("xxxxxx", result);
}
@Test
public void testOLMainClassName() {
assertEquals("OLMain", classToTest2.getName());
}
@Test
public void testmainInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method main = classToTest2.getDeclaredMethod("main", Integer.class);
Integer result = (Integer) main.invoke(instanceOfClass2, 5);
assertEquals(new Integer(10), result);
}
@Test
public void testmainDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method main = classToTest2.getDeclaredMethod("main", Double.class);
Double result = (Double) main.invoke(instanceOfClass2, 5.0);
assertEquals(new Double(10.0), result);
}
@Test
public void testmainString() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method main = classToTest2.getDeclaredMethod("main", String.class);

View File

@ -9,11 +9,12 @@ import java.io.IOException;
public class ParsePackageName {
public static final String rootDirectory = System.getProperty("user.dir")+"/resources/javFiles/packageTest/de/test/";
public static final String rootDirectory = System.getProperty("user.dir") + "/src/test/resources/javFiles/packageTest/de/test/";
@Test
public void parsePackage() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"TestClass.jav"));
for(File f : compiler.sourceFiles.keySet()){
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory + "TestClass.jav"));
for (File f : compiler.sourceFiles.keySet()) {
SourceFile sf = compiler.sourceFiles.get(f);
assert sf.getPkgName().equals("de.test");
}

View File

@ -12,7 +12,7 @@ import java.net.URL;
public class mathStrucMatrixOPTest extends TestCase {
public static final String rootDirectory = System.getProperty("user.dir")+"/resources/javFiles/packageTest/de/test/";
public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/javFiles/packageTest/de/test/";
public mathStrucMatrixOPTest() throws ClassNotFoundException, IOException {

View File

@ -12,37 +12,30 @@ import java.net.URL;
public class mathStrucVectorTest extends TestCase {
public static final String rootDirectory = System.getProperty("user.dir")+"/resources/javFiles/packageTest/de/test/";
public static final String rootDirectory = System.getProperty("user.dir") + "/src/test/resources/javFiles/packageTest/de/test/";
public mathStrucVectorTest() throws ClassNotFoundException, IOException {
/*
Generate ToImport class in rootDirectory and in output-Directory
* Generate ToImport class in rootDirectory and in output-Directory
*/
/* PL 2020-01-07 kann z.Zt. nicht erzeugt werden (siehe Bug 170, http://bugzilla.ba-horb.de/show_bug.cgi?id=170)
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"mathStruc.jav"));
compiler.typeInference();
compiler.generateBytecode(rootDirectory + "output/");
File f = new File(rootDirectory + "output/de/test/mathStruc.class");
assertTrue(f.exists());
*/
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"vectorAdd.jav"));
/*
* PL 2020-01-07 kann z.Zt. nicht erzeugt werden (siehe Bug 170, http://bugzilla.ba-horb.de/show_bug.cgi?id=170) JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"mathStruc.jav")); compiler.typeInference(); compiler.generateBytecode(rootDirectory + "output/"); File f = new File(rootDirectory + "output/de/test/mathStruc.class"); assertTrue(f.exists());
*/
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory + "vectorAdd.jav"));
compiler.typeInference();
compiler.generateBytecode(rootDirectory + "output/");
File f = new File(rootDirectory + "output/de/test/vectorAdd.class");
assertTrue(f.exists());
}
@Test
public void testSetPackageNameInBytecodeAndOutputFolder() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(
Lists.newArrayList(new File(rootDirectory+"mathStrucVector.jav")),
Lists.newArrayList(new File(rootDirectory+"output/")));
JavaTXCompiler compiler = new JavaTXCompiler(Lists.newArrayList(new File(rootDirectory + "mathStrucVector.jav")), Lists.newArrayList(new File(rootDirectory + "output/")));
compiler.typeInference();
File f = new File(rootDirectory + "output/de/test/mathStrucVector.class");
if(f.exists() && !f.isDirectory()) {
if (f.exists() && !f.isDirectory()) {
f.delete();
}
compiler.generateBytecode(rootDirectory + "output/");
@ -50,5 +43,4 @@ public class mathStrucVectorTest extends TestCase {
assertTrue(f.exists());
}
}

View File

@ -24,9 +24,8 @@ import static org.junit.Assert.*;
public class ASTToTypedTargetAST {
@Test
public void emptyClass(){
ClassOrInterface emptyClass = new ClassOrInterface(0, new JavaClassName("EmptyClass"), new ArrayList<>(), java.util.Optional.empty(), new ArrayList<>(), new ArrayList<>(), new GenericDeclarationList(new ArrayList<>(), new NullToken()),
new RefType(new JavaClassName("Object"), new NullToken()), false, new ArrayList<>(), new NullToken());
public void emptyClass() {
ClassOrInterface emptyClass = new ClassOrInterface(0, new JavaClassName("EmptyClass"), new ArrayList<>(), java.util.Optional.empty(), new ArrayList<>(), new ArrayList<>(), new GenericDeclarationList(new ArrayList<>(), new NullToken()), new RefType(new JavaClassName("Object"), new NullToken()), false, new ArrayList<>(), new NullToken());
ResultSet emptyResultSet = new ResultSet(new HashSet<>());
TargetClass emptyTargetClass = new ASTToTargetAST(List.of(emptyResultSet)).convert(emptyClass);
assert emptyTargetClass.getName().equals("EmptyClass");
@ -36,7 +35,7 @@ public class ASTToTypedTargetAST {
@Test
public void overloading() throws Exception {
var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Overloading.jav").toFile();
var file = Path.of(System.getProperty("user.dir"), "/src/test/resources/bytecode/javFiles/Overloading.jav").toFile();
var compiler = new JavaTXCompiler(file);
var resultSet = compiler.typeInference();
var converter = new ASTToTargetAST(resultSet);
@ -58,7 +57,7 @@ public class ASTToTypedTargetAST {
@Test
public void tphsAndGenerics() throws Exception {
var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Tph2.jav").toFile();
var file = Path.of(System.getProperty("user.dir"), "/src/test/resources/bytecode/javFiles/Tph2.jav").toFile();
var compiler = new JavaTXCompiler(file);
var resultSet = compiler.typeInference();
var converter = new ASTToTargetAST(resultSet);
@ -69,7 +68,7 @@ public class ASTToTypedTargetAST {
@Test
public void cycles() throws Exception {
var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Cycle.jav").toFile();
var file = Path.of(System.getProperty("user.dir"), "/src/test/resources/bytecode/javFiles/Cycle.jav").toFile();
var compiler = new JavaTXCompiler(file);
var resultSet = compiler.typeInference();
var converter = new ASTToTargetAST(resultSet);
@ -80,7 +79,7 @@ public class ASTToTypedTargetAST {
@Test
public void infimum() throws Exception {
var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Infimum.jav").toFile();
var file = Path.of(System.getProperty("user.dir"), "/src/test/resources/bytecode/javFiles/Infimum.jav").toFile();
var compiler = new JavaTXCompiler(file);
var resultSet = compiler.typeInference();
var converter = new ASTToTargetAST(resultSet);
@ -91,7 +90,7 @@ public class ASTToTypedTargetAST {
@Test
public void gen() throws Exception {
var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Gen.jav").toFile();
var file = Path.of(System.getProperty("user.dir"), "/src/test/resources/bytecode/javFiles/Gen.jav").toFile();
var compiler = new JavaTXCompiler(file);
var resultSet = compiler.typeInference();
var converter = new ASTToTargetAST(resultSet);
@ -101,12 +100,13 @@ public class ASTToTypedTargetAST {
var m = generics.getDeclaredMethod("m", Vector.class);
var mReturnType = m.getGenericReturnType();
assertEquals(mReturnType, m.getParameters()[0].getParameterizedType());
assertEquals(mReturnType, new TypeToken<Vector<Integer>>(){}.getType());
assertEquals(mReturnType, new TypeToken<Vector<Integer>>() {
}.getType());
}
@Test
public void definedGenerics() throws Exception {
var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Generics.jav").toFile();
var file = Path.of(System.getProperty("user.dir"), "/src/test/resources/bytecode/javFiles/Generics.jav").toFile();
var compiler = new JavaTXCompiler(file);
var resultSet = compiler.typeInference();
var converter = new ASTToTargetAST(resultSet);
@ -124,7 +124,7 @@ public class ASTToTypedTargetAST {
@Test
public void definedGenerics2() throws Exception {
var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Generics2.jav").toFile();
var file = Path.of(System.getProperty("user.dir"), "/src/test/resources/bytecode/javFiles/Generics2.jav").toFile();
var compiler = new JavaTXCompiler(file);
var resultSet = compiler.typeInference();
var converter = new ASTToTargetAST(resultSet);
@ -140,7 +140,7 @@ public class ASTToTypedTargetAST {
@Test
@Ignore("Not implemented")
public void definedGenerics3() throws Exception {
var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Generics3.jav").toFile();
var file = Path.of(System.getProperty("user.dir"), "/src/test/resources/bytecode/javFiles/Generics3.jav").toFile();
var compiler = new JavaTXCompiler(file);
var resultSet = compiler.typeInference();
var converter = new ASTToTargetAST(resultSet);
@ -151,7 +151,7 @@ public class ASTToTypedTargetAST {
@Test
public void definedGenerics4() throws Exception {
var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Generics4.jav").toFile();
var file = Path.of(System.getProperty("user.dir"), "/src/test/resources/bytecode/javFiles/Generics4.jav").toFile();
var compiler = new JavaTXCompiler(file);
var resultSet = compiler.typeInference();
var converter = new ASTToTargetAST(resultSet);
@ -159,8 +159,8 @@ public class ASTToTypedTargetAST {
var generics4 = TestCodegen.generateClass(converter.convert(classes.get(0)), new ByteArrayClassLoader());
//var instance = generics4.getDeclaredConstructor().newInstance();
//var method = generics4.getDeclaredMethod("m2", Object.class);
//method.invoke(instance, new Object());
// var instance = generics4.getDeclaredConstructor().newInstance();
// var method = generics4.getDeclaredMethod("m2", Object.class);
// method.invoke(instance, new Object());
}
}

View File

@ -25,341 +25,178 @@ import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class TestCodegen {
private static void writeClassFile(String name, byte[] code) throws IOException {
var path = Path.of(System.getProperty("user.dir"), "src/test/resources/target/");
Files.createDirectories(path);
Files.write(path.resolve(name + ".class"), code);
}
public static Class<?> generateClass(TargetClass clazz, IByteArrayClassLoader classLoader) throws IOException {
var codegen = new Codegen(clazz);
var code = codegen.generate();
writeClassFile(clazz.qualifiedName(), code);
return classLoader.loadClass(code);
}
public static Map<String, ? extends Class<?>> generateClassFiles(String filename, IByteArrayClassLoader classLoader) throws IOException, ClassNotFoundException {
var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/", filename).toFile();
var compiler = new JavaTXCompiler(List.of(file), List.of(file.getParentFile()));
var resultSet = compiler.typeInference();
var sourceFile = compiler.sourceFiles.get(file);
var converter = new ASTToTargetAST(resultSet, sourceFile, classLoader);
var classes = compiler.sourceFiles.get(file).getClasses();
var result = classes.stream().map(cli -> {
try {
return generateClass(converter.convert(cli), classLoader);
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}).collect(Collectors.toMap(Class::getName, Function.identity()));
for (var entry : converter.auxiliaries.entrySet()) {
writeClassFile(entry.getKey(), entry.getValue());
private static void writeClassFile(String name, byte[] code) throws IOException {
var path = Path.of(System.getProperty("user.dir"), "src/test/resources/target/");
Files.createDirectories(path);
Files.write(path.resolve(name + ".class"), code);
}
return result;
}
public static Class<?> generateClass(TargetClass clazz, IByteArrayClassLoader classLoader) throws IOException {
var codegen = new Codegen(clazz);
var code = codegen.generate();
writeClassFile(clazz.qualifiedName(), code);
return classLoader.loadClass(code);
}
@Test
public void testEmptyClass() throws Exception {
var clazz = new TargetClass(Opcodes.ACC_PUBLIC, "Empty");
clazz.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "main", List.of(), null, new TargetBlock(List.of()));
generateClass(clazz, new ByteArrayClassLoader()).getDeclaredMethod("main").invoke(null);
}
public static Map<String, ? extends Class<?>> generateClassFiles(String filename, IByteArrayClassLoader classLoader) throws IOException, ClassNotFoundException {
var file = Path.of(System.getProperty("user.dir"), "/src/test/resources/bytecode/javFiles/", filename).toFile();
var compiler = new JavaTXCompiler(List.of(file), List.of(file.getParentFile()));
var resultSet = compiler.typeInference();
@Test
public void testArithmetic() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "Arithmetic");
var sourceFile = compiler.sourceFiles.get(file);
var converter = new ASTToTargetAST(resultSet, sourceFile, classLoader);
var classes = compiler.sourceFiles.get(file).getClasses();
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "add",
List.of(new MethodParameter(TargetType.Integer, "a"), new MethodParameter(TargetType.Integer, "b")),
TargetType.Integer,
new TargetBlock(List.of(new TargetReturn(
new TargetBinaryOp.Add(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a"), new TargetLocalVar(TargetType.Integer, "b")))
))
);
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "sub",
List.of(new MethodParameter(TargetType.Integer, "a"), new MethodParameter(TargetType.Integer, "b")),
TargetType.Integer,
new TargetBlock(List.of(new TargetReturn(
new TargetBinaryOp.Sub(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a"), new TargetLocalVar(TargetType.Integer, "b")))
))
);
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "div",
List.of(new MethodParameter(TargetType.Integer, "a"), new MethodParameter(TargetType.Integer, "b")),
TargetType.Integer,
new TargetBlock(List.of(new TargetReturn(
new TargetBinaryOp.Div(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a"), new TargetLocalVar(TargetType.Integer, "b")))
))
);
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "mul",
List.of(new MethodParameter(TargetType.Integer, "a"), new MethodParameter(TargetType.Integer, "b")),
TargetType.Integer,
new TargetBlock(List.of(new TargetReturn(
new TargetBinaryOp.Mul(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a"), new TargetLocalVar(TargetType.Integer, "b")))
))
);
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "rem",
List.of(new MethodParameter(TargetType.Integer, "a"), new MethodParameter(TargetType.Integer, "b")),
TargetType.Integer,
new TargetBlock(List.of(new TargetReturn(
new TargetBinaryOp.Rem(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a"), new TargetLocalVar(TargetType.Integer, "b")))
))
);
var result = classes.stream().map(cli -> {
try {
return generateClass(converter.convert(cli), classLoader);
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}).collect(Collectors.toMap(Class::getName, Function.identity()));
for (var entry : converter.auxiliaries.entrySet()) {
writeClassFile(entry.getKey(), entry.getValue());
}
var clazz = generateClass(targetClass, new ByteArrayClassLoader());
assertEquals(clazz.getDeclaredMethod("add", Integer.class, Integer.class).invoke(null, 10, 10), 20);
assertEquals(clazz.getDeclaredMethod("sub", Integer.class, Integer.class).invoke(null, 20, 10), 10);
assertEquals(clazz.getDeclaredMethod("div", Integer.class, Integer.class).invoke(null, 20, 10), 2);
assertEquals(clazz.getDeclaredMethod("mul", Integer.class, Integer.class).invoke(null, 20, 10), 200);
assertEquals(clazz.getDeclaredMethod("rem", Integer.class, Integer.class).invoke(null, 10, 3), 1);
}
return result;
}
@Test
public void testUnary() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "Unary");
@Test
public void testEmptyClass() throws Exception {
var clazz = new TargetClass(Opcodes.ACC_PUBLIC, "Empty");
clazz.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "main", List.of(), null, new TargetBlock(List.of()));
generateClass(clazz, new ByteArrayClassLoader()).getDeclaredMethod("main").invoke(null);
}
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "not",
List.of(new MethodParameter(TargetType.Integer, "a")),
TargetType.Integer,
new TargetBlock(List.of(new TargetReturn(
new TargetUnaryOp.Not(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a")))
))
);
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "neg",
List.of(new MethodParameter(TargetType.Integer, "a")),
TargetType.Integer,
new TargetBlock(List.of(new TargetReturn(
new TargetUnaryOp.Negate(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a")))
))
);
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "add",
List.of(new MethodParameter(TargetType.Integer, "a")),
TargetType.Integer,
new TargetBlock(List.of(new TargetReturn(
new TargetUnaryOp.Add(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a")))
))
);
@Test
public void testArithmetic() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "Arithmetic");
var clazz = generateClass(targetClass, new ByteArrayClassLoader());
assertEquals(clazz.getDeclaredMethod("not", Integer.class).invoke(null, 10), -11);
assertEquals(clazz.getDeclaredMethod("neg", Integer.class).invoke(null, 10), -10);
assertEquals(clazz.getDeclaredMethod("add", Integer.class).invoke(null, 10), 10);
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "add", List.of(new MethodParameter(TargetType.Integer, "a"), new MethodParameter(TargetType.Integer, "b")), TargetType.Integer, new TargetBlock(List.of(new TargetReturn(new TargetBinaryOp.Add(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a"), new TargetLocalVar(TargetType.Integer, "b"))))));
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "sub", List.of(new MethodParameter(TargetType.Integer, "a"), new MethodParameter(TargetType.Integer, "b")), TargetType.Integer, new TargetBlock(List.of(new TargetReturn(new TargetBinaryOp.Sub(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a"), new TargetLocalVar(TargetType.Integer, "b"))))));
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "div", List.of(new MethodParameter(TargetType.Integer, "a"), new MethodParameter(TargetType.Integer, "b")), TargetType.Integer, new TargetBlock(List.of(new TargetReturn(new TargetBinaryOp.Div(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a"), new TargetLocalVar(TargetType.Integer, "b"))))));
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "mul", List.of(new MethodParameter(TargetType.Integer, "a"), new MethodParameter(TargetType.Integer, "b")), TargetType.Integer, new TargetBlock(List.of(new TargetReturn(new TargetBinaryOp.Mul(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a"), new TargetLocalVar(TargetType.Integer, "b"))))));
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "rem", List.of(new MethodParameter(TargetType.Integer, "a"), new MethodParameter(TargetType.Integer, "b")), TargetType.Integer, new TargetBlock(List.of(new TargetReturn(new TargetBinaryOp.Rem(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a"), new TargetLocalVar(TargetType.Integer, "b"))))));
}
var clazz = generateClass(targetClass, new ByteArrayClassLoader());
assertEquals(clazz.getDeclaredMethod("add", Integer.class, Integer.class).invoke(null, 10, 10), 20);
assertEquals(clazz.getDeclaredMethod("sub", Integer.class, Integer.class).invoke(null, 20, 10), 10);
assertEquals(clazz.getDeclaredMethod("div", Integer.class, Integer.class).invoke(null, 20, 10), 2);
assertEquals(clazz.getDeclaredMethod("mul", Integer.class, Integer.class).invoke(null, 20, 10), 200);
assertEquals(clazz.getDeclaredMethod("rem", Integer.class, Integer.class).invoke(null, 10, 3), 1);
}
@Test
public void testConditional() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "Conditional");
@Test
public void testUnary() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "Unary");
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "and",
List.of(new MethodParameter(TargetType.Boolean, "a"), new MethodParameter(TargetType.Boolean, "b")),
TargetType.Boolean,
new TargetBlock(List.of(new TargetReturn(
new TargetBinaryOp.And(TargetType.Boolean, new TargetLocalVar(TargetType.Boolean, "a"), new TargetLocalVar(TargetType.Boolean, "b")))
))
);
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "or",
List.of(new MethodParameter(TargetType.Boolean, "a"), new MethodParameter(TargetType.Boolean, "b")),
TargetType.Boolean,
new TargetBlock(List.of(new TargetReturn(
new TargetBinaryOp.Or(TargetType.Boolean, new TargetLocalVar(TargetType.Boolean, "a"), new TargetLocalVar(TargetType.Boolean, "b")))
))
);
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "not", List.of(new MethodParameter(TargetType.Integer, "a")), TargetType.Integer, new TargetBlock(List.of(new TargetReturn(new TargetUnaryOp.Not(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a"))))));
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "neg", List.of(new MethodParameter(TargetType.Integer, "a")), TargetType.Integer, new TargetBlock(List.of(new TargetReturn(new TargetUnaryOp.Negate(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a"))))));
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "add", List.of(new MethodParameter(TargetType.Integer, "a")), TargetType.Integer, new TargetBlock(List.of(new TargetReturn(new TargetUnaryOp.Add(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a"))))));
var clazz = generateClass(targetClass, new ByteArrayClassLoader());
var and = clazz.getDeclaredMethod("and", Boolean.class, Boolean.class);
var or = clazz.getDeclaredMethod("or", Boolean.class, Boolean.class);
assertEquals(and.invoke(null, true, false), false);
assertEquals(and.invoke(null, true, true), true);
assertEquals(or.invoke(null, false, false), false);
assertEquals(or.invoke(null, true, false), true);
}
var clazz = generateClass(targetClass, new ByteArrayClassLoader());
assertEquals(clazz.getDeclaredMethod("not", Integer.class).invoke(null, 10), -11);
assertEquals(clazz.getDeclaredMethod("neg", Integer.class).invoke(null, 10), -10);
assertEquals(clazz.getDeclaredMethod("add", Integer.class).invoke(null, 10), 10);
// When adding two numbers and the return type is Long it needs to convert both values to Long
@Test
public void testArithmeticConvert() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "ArithmeticConvert");
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "add", List.of(), TargetType.Long,
new TargetBlock(List.of(new TargetReturn(
new TargetBinaryOp.Add(TargetType.Long, new TargetLiteral.CharLiteral((char)10), new TargetLiteral.LongLiteral((long)20))
)))
);
var clazz = generateClass(targetClass, new ByteArrayClassLoader());
assertEquals(clazz.getDeclaredMethod("add").invoke(null), (long)30);
}
}
@Test
public void testMethodCall() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "HelloWorld");
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "helloWorld", List.of(), null,
new TargetBlock(List.of(new TargetMethodCall(null,
new TargetFieldVar(
new TargetRefType("java.io.PrintStream"),
new TargetRefType("java.lang.System"),
true,
new TargetClassName(new TargetRefType("java.lang.System")),
"out"
),
List.of(new TargetLiteral.StringLiteral("Hello World!")),
new TargetRefType("java.io.PrintStream"),
"println",
false, false
)))
);
@Test
public void testConditional() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "Conditional");
var clazz = generateClass(targetClass, new ByteArrayClassLoader());
clazz.getDeclaredMethod("helloWorld").invoke(null);
}
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "and", List.of(new MethodParameter(TargetType.Boolean, "a"), new MethodParameter(TargetType.Boolean, "b")), TargetType.Boolean, new TargetBlock(List.of(new TargetReturn(new TargetBinaryOp.And(TargetType.Boolean, new TargetLocalVar(TargetType.Boolean, "a"), new TargetLocalVar(TargetType.Boolean, "b"))))));
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "or", List.of(new MethodParameter(TargetType.Boolean, "a"), new MethodParameter(TargetType.Boolean, "b")), TargetType.Boolean, new TargetBlock(List.of(new TargetReturn(new TargetBinaryOp.Or(TargetType.Boolean, new TargetLocalVar(TargetType.Boolean, "a"), new TargetLocalVar(TargetType.Boolean, "b"))))));
@Test
public void testIfStatement() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "IfStmt");
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "ifStmt",
List.of(new MethodParameter(TargetType.Integer, "val")),
TargetType.Integer,
new TargetBlock(List.of(new TargetIf(
new TargetBinaryOp.Equal(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "val"), new TargetLiteral.IntLiteral(10)),
new TargetReturn(new TargetLiteral.IntLiteral(1)),
new TargetIf(
new TargetBinaryOp.Less(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "val"), new TargetLiteral.IntLiteral(5)),
new TargetReturn(new TargetLiteral.IntLiteral(2)),
new TargetReturn(new TargetLiteral.IntLiteral(3))
)
)))
);
var clazz = generateClass(targetClass, new ByteArrayClassLoader());
var ifStmt = clazz.getDeclaredMethod("ifStmt", Integer.class);
assertEquals(ifStmt.invoke(null, 10), 1);
assertEquals(ifStmt.invoke(null, 3), 2);
assertEquals(ifStmt.invoke(null, 20), 3);
}
var clazz = generateClass(targetClass, new ByteArrayClassLoader());
var and = clazz.getDeclaredMethod("and", Boolean.class, Boolean.class);
var or = clazz.getDeclaredMethod("or", Boolean.class, Boolean.class);
assertEquals(and.invoke(null, true, false), false);
assertEquals(and.invoke(null, true, true), true);
assertEquals(or.invoke(null, false, false), false);
assertEquals(or.invoke(null, true, false), true);
}
@Test
public void testFor() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "For");
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "forLoop", List.of(), TargetType.Integer,
new TargetBlock(List.of(
new TargetVarDecl(TargetType.Integer, "sum", new TargetLiteral.IntLiteral(0)),
new TargetFor(
new TargetVarDecl(TargetType.Integer, "i", new TargetLiteral.IntLiteral(0)),
new TargetBinaryOp.Less(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "i"), new TargetLiteral.IntLiteral(10)),
new TargetAssign(TargetType.Integer,
new TargetLocalVar(TargetType.Integer, "i"),
new TargetBinaryOp.Add(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "i"), new TargetLiteral.IntLiteral(1))),
new TargetBlock(List.of(
new TargetAssign(TargetType.Integer,
new TargetLocalVar(TargetType.Integer, "sum"),
new TargetBinaryOp.Add(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "sum"), new TargetLocalVar(TargetType.Integer, "i"))
)
))
),
new TargetReturn(new TargetLocalVar(TargetType.Integer, "sum"))
))
);
var clazz = generateClass(targetClass, new ByteArrayClassLoader());
assertEquals(clazz.getDeclaredMethod("forLoop").invoke(null), 45);
}
// When adding two numbers and the return type is Long it needs to convert both values to Long
@Test
public void testArithmeticConvert() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "ArithmeticConvert");
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "add", List.of(), TargetType.Long, new TargetBlock(List.of(new TargetReturn(new TargetBinaryOp.Add(TargetType.Long, new TargetLiteral.CharLiteral((char) 10), new TargetLiteral.LongLiteral((long) 20))))));
var clazz = generateClass(targetClass, new ByteArrayClassLoader());
assertEquals(clazz.getDeclaredMethod("add").invoke(null), (long) 30);
}
@Test
public void testWhile() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "While");
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "whileLoop", List.of(), TargetType.Integer,
new TargetBlock(List.of(
new TargetVarDecl(TargetType.Integer, "i", new TargetLiteral.IntLiteral(0)),
new TargetWhile(
new TargetBinaryOp.Less(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "i"), new TargetLiteral.IntLiteral(10)),
new TargetBlock(List.of(
new TargetAssign(TargetType.Integer,
new TargetLocalVar(TargetType.Integer, "i"),
new TargetBinaryOp.Add(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "i"), new TargetLiteral.IntLiteral(1))
)
))
),
new TargetReturn(new TargetLocalVar(TargetType.Integer, "i"))
))
);
var clazz = generateClass(targetClass, new ByteArrayClassLoader());
assertEquals(clazz.getDeclaredMethod("whileLoop").invoke(null), 10);
}
@Test
public void testMethodCall() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "HelloWorld");
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "helloWorld", List.of(), null, new TargetBlock(List.of(new TargetMethodCall(null, new TargetFieldVar(new TargetRefType("java.io.PrintStream"), new TargetRefType("java.lang.System"), true, new TargetClassName(new TargetRefType("java.lang.System")), "out"), List.of(new TargetLiteral.StringLiteral("Hello World!")), new TargetRefType("java.io.PrintStream"), "println", false, false))));
@Test
public void testNew() throws Exception {
var pointType = new TargetRefType("Point");
var pointTarget = new TargetClass(Opcodes.ACC_PUBLIC, "Point");
pointTarget.addField(Opcodes.ACC_PUBLIC, TargetType.Integer, "x");
pointTarget.addField(Opcodes.ACC_PUBLIC, TargetType.Integer, "y");
pointTarget.addConstructor(Opcodes.ACC_PUBLIC,
List.of(new MethodParameter(TargetType.Integer, "x"), new MethodParameter(TargetType.Integer, "y")),
new TargetBlock(List.of(
new TargetMethodCall(null, new TargetSuper(TargetType.Object), List.of(), TargetType.Object, "<init>", false, false),
new TargetAssign(TargetType.Integer,
new TargetFieldVar(TargetType.Integer, pointType, false, new TargetThis(pointType), "x"),
new TargetLocalVar(TargetType.Integer, "x")
),
new TargetAssign(TargetType.Integer,
new TargetFieldVar(TargetType.Integer, pointType, false, new TargetThis(pointType), "y"),
new TargetLocalVar(TargetType.Integer, "y")
)
))
);
var clazz = generateClass(targetClass, new ByteArrayClassLoader());
clazz.getDeclaredMethod("helloWorld").invoke(null);
}
var mainTarget = new TargetClass(Opcodes.ACC_PUBLIC, "New");
mainTarget.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "makePoint",
List.of(new MethodParameter(TargetType.Integer, "x"), new MethodParameter(TargetType.Integer, "y")), pointType,
new TargetBlock(List.of(
new TargetReturn(new TargetNew(pointType, List.of(
new TargetLocalVar(TargetType.Integer, "x"),
new TargetLocalVar(TargetType.Integer, "y")
)))
))
);
@Test
public void testIfStatement() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "IfStmt");
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "ifStmt", List.of(new MethodParameter(TargetType.Integer, "val")), TargetType.Integer, new TargetBlock(List.of(new TargetIf(new TargetBinaryOp.Equal(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "val"), new TargetLiteral.IntLiteral(10)), new TargetReturn(new TargetLiteral.IntLiteral(1)), new TargetIf(new TargetBinaryOp.Less(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "val"), new TargetLiteral.IntLiteral(5)), new TargetReturn(new TargetLiteral.IntLiteral(2)), new TargetReturn(new TargetLiteral.IntLiteral(3)))))));
var clazz = generateClass(targetClass, new ByteArrayClassLoader());
var ifStmt = clazz.getDeclaredMethod("ifStmt", Integer.class);
assertEquals(ifStmt.invoke(null, 10), 1);
assertEquals(ifStmt.invoke(null, 3), 2);
assertEquals(ifStmt.invoke(null, 20), 3);
}
var classLoader = new ByteArrayClassLoader();
var pointClass = generateClass(pointTarget, classLoader);
var mainClass = generateClass(mainTarget, classLoader);
@Test
public void testFor() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "For");
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "forLoop", List.of(), TargetType.Integer, new TargetBlock(List.of(new TargetVarDecl(TargetType.Integer, "sum", new TargetLiteral.IntLiteral(0)), new TargetFor(new TargetVarDecl(TargetType.Integer, "i", new TargetLiteral.IntLiteral(0)), new TargetBinaryOp.Less(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "i"), new TargetLiteral.IntLiteral(10)), new TargetAssign(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "i"), new TargetBinaryOp.Add(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "i"), new TargetLiteral.IntLiteral(1))), new TargetBlock(List.of(new TargetAssign(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "sum"), new TargetBinaryOp.Add(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "sum"), new TargetLocalVar(TargetType.Integer, "i")))))), new TargetReturn(new TargetLocalVar(TargetType.Integer, "sum")))));
var clazz = generateClass(targetClass, new ByteArrayClassLoader());
assertEquals(clazz.getDeclaredMethod("forLoop").invoke(null), 45);
}
var point = mainClass.getDeclaredMethod("makePoint", Integer.class, Integer.class).invoke(null, 10, 20);
assertEquals(point.getClass().getDeclaredField("x").get(point), 10);
assertEquals(point.getClass().getDeclaredField("y").get(point), 20);
}
@Test
public void testWhile() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "While");
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "whileLoop", List.of(), TargetType.Integer, new TargetBlock(List.of(new TargetVarDecl(TargetType.Integer, "i", new TargetLiteral.IntLiteral(0)), new TargetWhile(new TargetBinaryOp.Less(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "i"), new TargetLiteral.IntLiteral(10)), new TargetBlock(List.of(new TargetAssign(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "i"), new TargetBinaryOp.Add(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "i"), new TargetLiteral.IntLiteral(1)))))), new TargetReturn(new TargetLocalVar(TargetType.Integer, "i")))));
var clazz = generateClass(targetClass, new ByteArrayClassLoader());
assertEquals(clazz.getDeclaredMethod("whileLoop").invoke(null), 10);
}
@Test
@Ignore("The lambda class is not generated because we don't call ASTToTargetAST")
public void testLambda() throws Exception {
var classLoader = new ByteArrayClassLoader();
//var fun = classLoader.loadClass(Path.of(System.getProperty("user.dir"), "src/test/java/targetast/Fun1$$.class"));
var interfaceType = new TargetFunNType(1, List.of(TargetType.Integer));
@Test
public void testNew() throws Exception {
var pointType = new TargetRefType("Point");
var pointTarget = new TargetClass(Opcodes.ACC_PUBLIC, "Point");
pointTarget.addField(Opcodes.ACC_PUBLIC, TargetType.Integer, "x");
pointTarget.addField(Opcodes.ACC_PUBLIC, TargetType.Integer, "y");
pointTarget.addConstructor(Opcodes.ACC_PUBLIC, List.of(new MethodParameter(TargetType.Integer, "x"), new MethodParameter(TargetType.Integer, "y")), new TargetBlock(List.of(new TargetMethodCall(null, new TargetSuper(TargetType.Object), List.of(), TargetType.Object, "<init>", false, false), new TargetAssign(TargetType.Integer, new TargetFieldVar(TargetType.Integer, pointType, false, new TargetThis(pointType), "x"), new TargetLocalVar(TargetType.Integer, "x")), new TargetAssign(TargetType.Integer, new TargetFieldVar(TargetType.Integer, pointType, false, new TargetThis(pointType), "y"), new TargetLocalVar(TargetType.Integer, "y")))));
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "CGLambda");
targetClass.addConstructor(Opcodes.ACC_PUBLIC, List.of(), new TargetBlock(List.of(
new TargetMethodCall(null, new TargetSuper(TargetType.Object), List.of(), TargetType.Object, "<init>", false, false)
)));
targetClass.addMethod(Opcodes.ACC_PUBLIC, "lambda", List.of(), TargetType.Integer,
new TargetBlock(List.of(
new TargetVarDecl(interfaceType, "by2",
new TargetLambdaExpression(interfaceType, List.of(), List.of(new MethodParameter(TargetType.Integer, "num")), TargetType.Integer,
new TargetBlock(List.of(
new TargetReturn(new TargetBinaryOp.Mul(TargetType.Integer,
new TargetLocalVar(TargetType.Integer, "num"),
new TargetLiteral.IntLiteral(2)
))
)
))
),
new TargetReturn(new TargetCast(TargetType.Integer, new TargetMethodCall(TargetType.Object, TargetType.Object, List.of(TargetType.Object), new TargetLocalVar(interfaceType, "by2"), List.of(
new TargetLiteral.IntLiteral(10)
), interfaceType, "apply", false, true)))
))
);
var clazz = generateClass(targetClass, classLoader);
var instance = clazz.getConstructor().newInstance();
assertEquals(clazz.getDeclaredMethod("lambda").invoke(instance), 20);
}
var mainTarget = new TargetClass(Opcodes.ACC_PUBLIC, "New");
mainTarget.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "makePoint", List.of(new MethodParameter(TargetType.Integer, "x"), new MethodParameter(TargetType.Integer, "y")), pointType, new TargetBlock(List.of(new TargetReturn(new TargetNew(pointType, List.of(new TargetLocalVar(TargetType.Integer, "x"), new TargetLocalVar(TargetType.Integer, "y")))))));
var classLoader = new ByteArrayClassLoader();
var pointClass = generateClass(pointTarget, classLoader);
var mainClass = generateClass(mainTarget, classLoader);
var point = mainClass.getDeclaredMethod("makePoint", Integer.class, Integer.class).invoke(null, 10, 20);
assertEquals(point.getClass().getDeclaredField("x").get(point), 10);
assertEquals(point.getClass().getDeclaredField("y").get(point), 20);
}
@Test
@Ignore("The lambda class is not generated because we don't call ASTToTargetAST")
public void testLambda() throws Exception {
var classLoader = new ByteArrayClassLoader();
// var fun = classLoader.loadClass(Path.of(System.getProperty("user.dir"), "src/test/java/targetast/Fun1$$.class"));
var interfaceType = new TargetFunNType(1, List.of(TargetType.Integer));
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "CGLambda");
targetClass.addConstructor(Opcodes.ACC_PUBLIC, List.of(), new TargetBlock(List.of(new TargetMethodCall(null, new TargetSuper(TargetType.Object), List.of(), TargetType.Object, "<init>", false, false))));
targetClass.addMethod(Opcodes.ACC_PUBLIC, "lambda", List.of(), TargetType.Integer, new TargetBlock(List.of(new TargetVarDecl(interfaceType, "by2", new TargetLambdaExpression(interfaceType, List.of(), List.of(new MethodParameter(TargetType.Integer, "num")), TargetType.Integer, new TargetBlock(List.of(new TargetReturn(new TargetBinaryOp.Mul(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "num"), new TargetLiteral.IntLiteral(2))))))), new TargetReturn(new TargetCast(TargetType.Integer, new TargetMethodCall(TargetType.Object, TargetType.Object, List.of(TargetType.Object), new TargetLocalVar(interfaceType, "by2"), List.of(new TargetLiteral.IntLiteral(10)), interfaceType, "apply", false, true))))));
var clazz = generateClass(targetClass, classLoader);
var instance = clazz.getConstructor().newInstance();
assertEquals(clazz.getDeclaredMethod("lambda").invoke(instance), 20);
}
}

View File

@ -20,10 +20,9 @@ import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
public class TestGenerics {
private static final String rootDirectory = System.getProperty("user.dir") + "/resources/insertGenericsJav/";
private static final String rootDirectory = System.getProperty("user.dir") + "/src/test/resources/insertGenericsJav/";
private static final String bytecodeDirectory = System.getProperty("user.dir") + "/src/test/resources/testBytecode/generatedBC/";
private record Result(List<GenericsResult> genericsResults, ClassOrInterface clazz) {
@ -122,7 +121,7 @@ public class TestGenerics {
assertEquals(2, generics.get(m).size());
var R = generics.getBounds(a.getType(), result.clazz);
var RChain = new BoundsList(new Bound(false, ASTToTargetAST.OBJECT));
var RChain = new BoundsList(new Bound(false, ASTToTargetAST.OBJECT));
assertEquals(R, RChain);
var O = generics.getBounds(id.getParameterList().getParameterAt(0).getType(), result.clazz, id);
@ -176,13 +175,13 @@ public class TestGenerics {
assertEquals(AK, AKChain);
// TODO Shouldn't AK and AK2 be the same and on the class?
var AK2 = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m);
var Y = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m);
var AK2 = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m);
var Y = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m);
assertEquals(AK2, AKChain);
assertEquals(Y, AKChain);
var AF = generics.getBounds(main.getParameterList().getParameterAt(0).getType(), result.clazz, main);
var AG= generics.getBounds(main.getParameterList().getParameterAt(1).getType(), result.clazz, main);
var AG = generics.getBounds(main.getParameterList().getParameterAt(1).getType(), result.clazz, main);
var AK3 = generics.getBounds(main.getReturnType(), result.clazz, main);
var AFChain = new BoundsList(new Bound(true, TypePlaceholder.of("AG")), new Bound(true, ASTToTargetAST.OBJECT));
assertEquals(AF, AFChain);
@ -266,12 +265,7 @@ public class TestGenerics {
var generics = result.genericsResults.get(0);
var AO = generics.getBounds(a.getType(), result.clazz);
var AOBound = new BoundsList(
new Bound(false, TypePlaceholder.of("Y")),
new Bound(false, TypePlaceholder.of("AK")),
new Bound(false, TypePlaceholder.of("AE")),
new Bound(false, ASTToTargetAST.OBJECT)
);
var AOBound = new BoundsList(new Bound(false, TypePlaceholder.of("Y")), new Bound(false, TypePlaceholder.of("AK")), new Bound(false, TypePlaceholder.of("AE")), new Bound(false, ASTToTargetAST.OBJECT));
assertEquals(AO, AOBound);
var S = generics.getBounds(setA.getParameterList().getParameterAt(0).getType(), result.clazz, setA);
@ -288,12 +282,9 @@ public class TestGenerics {
assertEquals(AE, new BoundsList(new Bound(false, ASTToTargetAST.OBJECT)));
// TODO main seems to change between runs
/*var AE2 = generics.getBounds(main.getReturnType(), result.clazz, main);
var AF = generics.getBounds(main.getParameterList().getParameterAt(0).getType(), result.clazz, main);
var AG = generics.getBounds(main.getParameterList().getParameterAt(1).getType(), result.clazz, main);
assertEquals(AE, AE2));
assertEquals(AF, new BoundsList(new Bound(true, TypePlaceholder.of("AK")), new Bound(true, TypePlaceholder.of("AE")), new Bound(false, ASTToTargetAST.OBJECT))));
assertEquals(AG, SChain));*/
/*
* var AE2 = generics.getBounds(main.getReturnType(), result.clazz, main); var AF = generics.getBounds(main.getParameterList().getParameterAt(0).getType(), result.clazz, main); var AG = generics.getBounds(main.getParameterList().getParameterAt(1).getType(), result.clazz, main); assertEquals(AE, AE2)); assertEquals(AF, new BoundsList(new Bound(true, TypePlaceholder.of("AK")), new Bound(true, TypePlaceholder.of("AE")), new Bound(false, ASTToTargetAST.OBJECT)))); assertEquals(AG, SChain));
*/
}
@Test

View File

@ -23,155 +23,175 @@ import java.util.Set;
public class JavaTXCompilerTest {
public static final String rootDirectory = System.getProperty("user.dir")+"/resources/javFiles/";
public static final String rootDirectory = System.getProperty("user.dir") + "/src/test/resources/javFiles/";
@Test
public void finiteClosure() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"fc.jav"));
}
@Test
public void importTest() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Import.jav"));
}
@Test
public void fieldTest() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"FieldAccess.jav"));
}
@Test
public void lambda() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Lambda.jav"));
}
@Test
public void lambda2() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Lambda2.jav"));
}
@Test
public void lambda3() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Lambda3.jav"));
}
@Test
public void lambdaField() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"LambdaField.jav"));
}
@Test
public void mathStruc() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"mathStruc.jav"));
}
@Test
public void generics() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Generics.jav"));
}
@Test
public void genericsMethodCall() throws IOException, ClassNotFoundException {
TestResultSet result = execute(new File(rootDirectory+"MethodCallGenerics.jav"));
//TODO: Hier sollte der Rückgabetyp der Methode String sein
}
@Test
public void faculty() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Faculty.jav"));
}
@Test
public void facultyIf() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"FacultyIf.jav"));
}
@Test
public void facultyTyped() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"FacultyTyped.jav"));
}
@Test
public void matrix() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Matrix.jav"));
}
@Test
public void packageTests() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Package.jav"));
}
@Test
public void vector() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Vector.jav"));
}
@Test
public void lambdaRunnable() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"LambdaRunnable.jav"));
}
@Test
public void expressions() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Expressions.jav"));
}
@Test
public void addLong() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"AddLong.jav"));
}
@Test
public void fields() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Fields.jav"));
}
@Test
public void ifStatement() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"IfTest.jav"));
}
@Test
public void multipleSolutions() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Sorting.jav"));
}
@Test
public void listenerTest() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"ListenerOverload.jav"));
execute(new File(rootDirectory + "fc.jav"));
}
private static class TestResultSet{
@Test
public void importTest() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "Import.jav"));
}
@Test
public void fieldTest() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "FieldAccess.jav"));
}
@Test
public void lambda() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "Lambda.jav"));
}
@Test
public void lambda2() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "Lambda2.jav"));
}
@Test
public void lambda3() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "Lambda3.jav"));
}
@Test
public void lambdaField() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "LambdaField.jav"));
}
@Test
public void mathStruc() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "mathStruc.jav"));
}
@Test
public void generics() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "Generics.jav"));
}
@Test
public void genericsMethodCall() throws IOException, ClassNotFoundException {
TestResultSet result = execute(new File(rootDirectory + "MethodCallGenerics.jav"));
// TODO: Hier sollte der Rückgabetyp der Methode String sein
}
@Test
public void faculty() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "Faculty.jav"));
}
@Test
public void facultyIf() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "FacultyIf.jav"));
}
@Test
public void facultyTyped() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "FacultyTyped.jav"));
}
@Test
public void matrix() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "Matrix.jav"));
}
@Test
public void packageTests() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "Package.jav"));
}
@Test
public void vector() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "Vector.jav"));
}
@Test
public void lambdaRunnable() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "LambdaRunnable.jav"));
}
@Test
public void expressions() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "Expressions.jav"));
}
@Test
public void addLong() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "AddLong.jav"));
}
@Test
public void fields() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "Fields.jav"));
}
@Test
public void ifStatement() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "IfTest.jav"));
}
@Test
public void multipleSolutions() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "Sorting.jav"));
}
@Test
public void listenerTest() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory + "ListenerOverload.jav"));
}
private static class TestResultSet {
}
public TestResultSet execute(File fileToTest) throws IOException, ClassNotFoundException {
//filesToTest.add(new File(rootDirectory+"fc.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda2.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda3.jav"));
//filesToTest.add(new File(rootDirectory+"Vector.jav"));
//filesToTest.add(new File(rootDirectory+"Generics.jav"));
//filesToTest.add(new File(rootDirectory+"MethodsEasy.jav"));
//filesToTest.add(new File(rootDirectory+"Matrix.jav"));
//filesToTest.add(new File(rootDirectory+"Import.jav"));
//filesToTest.add(new File(rootDirectory+"Faculty.jav"));
//filesToTest.add(new File(rootDirectory+"mathStruc.jav"));
//filesToTest.add(new File(rootDirectory+"test.jav"));
// filesToTest.add(new File(rootDirectory+"fc.jav"));
// filesToTest.add(new File(rootDirectory+"Lambda.jav"));
// filesToTest.add(new File(rootDirectory+"Lambda2.jav"));
// filesToTest.add(new File(rootDirectory+"Lambda3.jav"));
// filesToTest.add(new File(rootDirectory+"Vector.jav"));
// filesToTest.add(new File(rootDirectory+"Generics.jav"));
// filesToTest.add(new File(rootDirectory+"MethodsEasy.jav"));
// filesToTest.add(new File(rootDirectory+"Matrix.jav"));
// filesToTest.add(new File(rootDirectory+"Import.jav"));
// filesToTest.add(new File(rootDirectory+"Faculty.jav"));
// filesToTest.add(new File(rootDirectory+"mathStruc.jav"));
// filesToTest.add(new File(rootDirectory+"test.jav"));
JavaTXCompiler compiler = new JavaTXCompiler(fileToTest);
for(File f : compiler.sourceFiles.keySet()){
for (File f : compiler.sourceFiles.keySet()) {
SourceFile sf = compiler.sourceFiles.get(f);
System.out.println(ASTTypePrinter.print(sf));
System.out.println(ASTPrinter.print(sf));
}
List<ResultSet> results = compiler.typeInference();
List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(results);
//compiler.generateBytecode(rootDirectory+"xxx.class", results, simplifyResultsForAllSourceFiles);
for(File f : compiler.sourceFiles.keySet()){
// compiler.generateBytecode(rootDirectory+"xxx.class", results, simplifyResultsForAllSourceFiles);
for (File f : compiler.sourceFiles.keySet()) {
SourceFile sf = compiler.sourceFiles.get(f);
System.out.println(ASTTypePrinter.print(sf));
System.out.println(ASTPrinter.print(sf));
//List<ResultSet> results = compiler.typeInference(); PL 2017-10-03 vor die For-Schleife gezogen
assert results.size()>0;
// List<ResultSet> results = compiler.typeInference(); PL 2017-10-03 vor die For-Schleife gezogen
assert results.size() > 0;
Set<String> insertedTypes = new HashSet<>();
for(ResultSet resultSet : results){
for (ResultSet resultSet : results) {
Set<TypeInsert> result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet, results, simplifyResultsForAllSourceFiles);
assert result.size()>0;
assert result.size() > 0;
String content = readFile(f.getPath(), StandardCharsets.UTF_8);
for(TypeInsert tip : result){
for (TypeInsert tip : result) {
insertedTypes.add(tip.insert(content));
}
}
for(String s : insertedTypes){
for (String s : insertedTypes) {
System.out.println(s);
}
}
return new TestResultSet();
}
static String readFile(String path, Charset encoding)
throws IOException
{
static String readFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}

View File

@ -23,84 +23,58 @@ import java.util.Set;
public class Meth_GenTest {
public static final String rootDirectory = System.getProperty("user.dir")+"/resources/javFiles/";
/*
@Test
public void finiteClosure() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"fc.jav"));
}
@Test
public void lambda() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Lambda.jav"));
}
@Test
public void lambda2() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Lambda2.jav"));
}
@Test
public void lambda3() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Lambda3.jav"));
}
@Test
public void mathStruc() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"mathStruc.jav"));
}
@Test
public void generics() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Generics.jav"));
}
public static final String rootDirectory = System.getProperty("user.dir") + "/src/test/resources/javFiles/";
@Test
public void faculty() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Faculty.jav"));
}
@Test
public void facultyTyped() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"FacultyTyped.jav"));
}
*/
/*
* @Test public void finiteClosure() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"fc.jav")); }
*
* @Test public void lambda() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Lambda.jav")); }
*
* @Test public void lambda2() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Lambda2.jav")); }
*
* @Test public void lambda3() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Lambda3.jav")); }
*
* @Test public void mathStruc() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"mathStruc.jav")); }
*
* @Test public void generics() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Generics.jav")); }
*
* @Test public void faculty() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Faculty.jav")); }
*
* @Test public void facultyTyped() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"FacultyTyped.jav")); }
*/
@Test
public void matrix() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Meth_Gen.jav"));
execute(new File(rootDirectory + "Meth_Gen.jav"));
}
/*
@Test
public void vector() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Vector.jav"));
}
@Test
public void lambdaRunnable() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"LambdaRunnable.jav"));
}
@Test
public void expressions() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Expressions.jav"));
}
@Test
public void matrixFC() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"FC_Matrix.jav"));
}
*/
private static class TestResultSet{
/*
* @Test public void vector() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Vector.jav")); }
*
* @Test public void lambdaRunnable() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"LambdaRunnable.jav")); }
*
* @Test public void expressions() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Expressions.jav")); }
*
* @Test public void matrixFC() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"FC_Matrix.jav")); }
*/
private static class TestResultSet {
}
public TestResultSet execute(File fileToTest) throws IOException, ClassNotFoundException {
//filesToTest.add(new File(rootDirectory+"fc.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda2.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda3.jav"));
//filesToTest.add(new File(rootDirectory+"Vector.jav"));
//filesToTest.add(new File(rootDirectory+"Generics.jav"));
//filesToTest.add(new File(rootDirectory+"MethodsEasy.jav"));
//filesToTest.add(new File(rootDirectory+"Matrix.jav"));
//filesToTest.add(new File(rootDirectory+"Import.jav"));
// //filesToTest.add(new File(rootDirectory+"Faculty.jav"));
// //filesToTest.add(new File(rootDirectory+"mathStruc.jav"));
// //filesToTest.add(new File(rootDirectory+"test.jav"));
// filesToTest.add(new File(rootDirectory+"fc.jav"));
// filesToTest.add(new File(rootDirectory+"Lambda.jav"));
// filesToTest.add(new File(rootDirectory+"Lambda2.jav"));
// filesToTest.add(new File(rootDirectory+"Lambda3.jav"));
// filesToTest.add(new File(rootDirectory+"Vector.jav"));
// filesToTest.add(new File(rootDirectory+"Generics.jav"));
// filesToTest.add(new File(rootDirectory+"MethodsEasy.jav"));
// filesToTest.add(new File(rootDirectory+"Matrix.jav"));
// filesToTest.add(new File(rootDirectory+"Import.jav"));
// //filesToTest.add(new File(rootDirectory+"Faculty.jav"));
// //filesToTest.add(new File(rootDirectory+"mathStruc.jav"));
// //filesToTest.add(new File(rootDirectory+"test.jav"));
JavaTXCompiler compiler = new JavaTXCompiler(fileToTest);
for(File f : compiler.sourceFiles.keySet()){
for (File f : compiler.sourceFiles.keySet()) {
SourceFile sf = compiler.sourceFiles.get(f);
System.out.println(ASTTypePrinter.print(sf));
System.out.println(ASTPrinter.print(sf));
@ -108,34 +82,31 @@ public class Meth_GenTest {
List<ResultSet> results = compiler.typeInference();
List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(results);
for(File f : compiler.sourceFiles.keySet()){
for (File f : compiler.sourceFiles.keySet()) {
SourceFile sf = compiler.sourceFiles.get(f);
System.out.println(ASTTypePrinter.print(sf));
System.out.println(ASTPrinter.print(sf));
//List<ResultSet> results = compiler.typeInference(); PL 2017-10-03 vor die For-Schleife gezogen
assert results.size()>0;
// List<ResultSet> results = compiler.typeInference(); PL 2017-10-03 vor die For-Schleife gezogen
assert results.size() > 0;
Set<String> insertedTypes = new HashSet<>();
for(ResultSet resultSet : results){
for (ResultSet resultSet : results) {
Set<TypeInsert> result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet, results, simplifyResultsForAllSourceFiles);
assert result.size()>0;
assert result.size() > 0;
String content = readFile(f.getPath(), StandardCharsets.UTF_8);
for(TypeInsert tip : result){
for (TypeInsert tip : result) {
insertedTypes.add(tip.insert(content));
}
}
for(String s : insertedTypes){
for (String s : insertedTypes) {
System.out.println(s);
}
}
return new TestResultSet();
}
static String readFile(String path, Charset encoding)
throws IOException
{
static String readFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}

View File

@ -23,110 +23,71 @@ import java.util.Set;
public class UnifyTest {
public static final String rootDirectory = System.getProperty("user.dir")+"/resources/javFiles/";
/*
@Test
public void finiteClosure() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"fc.jav"));
}
@Test
public void lambda() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Lambda.jav"));
}
*/
public static final String rootDirectory = System.getProperty("user.dir") + "/src/test/resources/javFiles/";
/*
@Test
public void vector() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Vector.jav"));
}
*/
* @Test public void finiteClosure() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"fc.jav")); }
*
* @Test public void lambda() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Lambda.jav")); }
*
*/
/*
* @Test public void vector() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Vector.jav")); }
*/
/*
@Test
public void lambda2() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Lambda2.jav"));
}
*/
/*
@Test
public void lambda3() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Lambda3.jav"));
}
@Test
public void lambdafield() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"LambdaField.jav"));
}
@Test
public void mathStruc() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"mathStruc.jav"));
}
@Test
public void generics() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Generics.jav"));
}
*/
/*
@Test
public void faculty() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Faculty.jav"));
}
*/
/*
@Test
public void facultyTyped() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"FacultyTyped.jav"));
}
*/
* @Test public void lambda2() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Lambda2.jav")); }
*/
/*
* @Test public void lambda3() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Lambda3.jav")); }
*
* @Test public void lambdafield() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"LambdaField.jav")); }
*
* @Test public void mathStruc() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"mathStruc.jav")); }
*
* @Test public void generics() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Generics.jav")); }
*/
/*
* @Test public void faculty() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Faculty.jav")); }
*/
/*
* @Test public void facultyTyped() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"FacultyTyped.jav")); }
*/
@Test
public void matrix() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Matrix.jav"));
//JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"Matrix.jav"));
//compiler.generateBytecode();
execute(new File(rootDirectory + "Matrix.jav"));
// JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"Matrix.jav"));
// compiler.generateBytecode();
}
/*
@Test
public void vector() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Vector.jav"));
}
@Test
public void lambdaRunnable() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"LambdaRunnable.jav"));
}
@Test
public void expressions() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Expressions.jav"));
}
@Test
public void matrixFC() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"FC_Matrix.jav"));
}
*/
private static class TestResultSet{
/*
* @Test public void vector() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Vector.jav")); }
*
* @Test public void lambdaRunnable() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"LambdaRunnable.jav")); }
*
* @Test public void expressions() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Expressions.jav")); }
*
* @Test public void matrixFC() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"FC_Matrix.jav")); }
*/
private static class TestResultSet {
}
public TestResultSet execute(File fileToTest) throws IOException, ClassNotFoundException {
//filesToTest.add(new File(rootDirectory+"fc.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda2.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda3.jav"));
//filesToTest.add(new File(rootDirectory+"Vector.jav"));
//filesToTest.add(new File(rootDirectory+"Generics.jav"));
//filesToTest.add(new File(rootDirectory+"MethodsEasy.jav"));
//filesToTest.add(new File(rootDirectory+"Matrix.jav"));
//filesToTest.add(new File(rootDirectory+"Import.jav"));
// //filesToTest.add(new File(rootDirectory+"Faculty.jav"));
// //filesToTest.add(new File(rootDirectory+"mathStruc.jav"));
// //filesToTest.add(new File(rootDirectory+"test.jav"));
// filesToTest.add(new File(rootDirectory+"fc.jav"));
// filesToTest.add(new File(rootDirectory+"Lambda.jav"));
// filesToTest.add(new File(rootDirectory+"Lambda2.jav"));
// filesToTest.add(new File(rootDirectory+"Lambda3.jav"));
// filesToTest.add(new File(rootDirectory+"Vector.jav"));
// filesToTest.add(new File(rootDirectory+"Generics.jav"));
// filesToTest.add(new File(rootDirectory+"MethodsEasy.jav"));
// filesToTest.add(new File(rootDirectory+"Matrix.jav"));
// filesToTest.add(new File(rootDirectory+"Import.jav"));
// //filesToTest.add(new File(rootDirectory+"Faculty.jav"));
// //filesToTest.add(new File(rootDirectory+"mathStruc.jav"));
// //filesToTest.add(new File(rootDirectory+"test.jav"));
JavaTXCompiler compiler = new JavaTXCompiler(fileToTest);
for(File f : compiler.sourceFiles.keySet()){
for (File f : compiler.sourceFiles.keySet()) {
SourceFile sf = compiler.sourceFiles.get(f);
System.out.println(ASTTypePrinter.print(sf));
System.out.println(ASTPrinter.print(sf));
@ -134,34 +95,31 @@ public class UnifyTest {
List<ResultSet> results = compiler.typeInference();
List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(results);
for(File f : compiler.sourceFiles.keySet()){
for (File f : compiler.sourceFiles.keySet()) {
SourceFile sf = compiler.sourceFiles.get(f);
System.out.println(ASTTypePrinter.print(sf));
System.out.println(ASTPrinter.print(sf));
//List<ResultSet> results = compiler.typeInference(); PL 2017-10-03 vor die For-Schleife gezogen
assert results.size()>0;
// List<ResultSet> results = compiler.typeInference(); PL 2017-10-03 vor die For-Schleife gezogen
assert results.size() > 0;
Set<String> insertedTypes = new HashSet<>();
for(ResultSet resultSet : results){
for (ResultSet resultSet : results) {
Set<TypeInsert> result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet, results, simplifyResultsForAllSourceFiles);
assert result.size()>0;
assert result.size() > 0;
String content = readFile(f.getPath(), StandardCharsets.UTF_8);
for(TypeInsert tip : result){
for (TypeInsert tip : result) {
insertedTypes.add(tip.insert(content));
}
}
for(String s : insertedTypes){
for (String s : insertedTypes) {
System.out.println(s);
}
}
return new TestResultSet();
}
static String readFile(String path, Charset encoding)
throws IOException
{
static String readFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}

Some files were not shown because too many files have changed in this diff Show More