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

View File

@ -12,41 +12,40 @@ import java.net.URLClassLoader;
public class Bytecode extends TestCase { 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 @Test
public void testSetPackageNameInBytecode() throws Exception { 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(); compiler.typeInference();
File f = new File(rootDirectory + "de/test/TestClass.class"); File f = new File(rootDirectory + "de/test/TestClass.class");
if(f.exists() && !f.isDirectory()) { if (f.exists() && !f.isDirectory()) {
f.delete(); f.delete();
} }
compiler.generateBytecode(); compiler.generateBytecode();
f = new File(rootDirectory + "de/test/TestClass.class"); f = new File(rootDirectory + "de/test/TestClass.class");
assertTrue(f.exists()); 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"); Class<?> classToTest = loader.loadClass("de.test.TestClass");
Object instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); Object instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
} }
@Test @Test
public void testSetPackageNameInBytecodeAndOutputFolder() throws Exception { 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(); compiler.typeInference();
File f = new File(rootDirectory + "de/test/output/de/test/TestClass.class"); File f = new File(rootDirectory + "de/test/output/de/test/TestClass.class");
if(f.exists() && !f.isDirectory()) { if (f.exists() && !f.isDirectory()) {
f.delete(); f.delete();
} }
compiler.generateBytecode(rootDirectory + "de/test/output/"); compiler.generateBytecode(rootDirectory + "de/test/output/");
f = new File(rootDirectory + "de/test/output/de/test/TestClass.class"); f = new File(rootDirectory + "de/test/output/de/test/TestClass.class");
assertTrue(f.exists()); 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"); Class<?> classToTest = loader.loadClass("de.test.TestClass");
Object instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); Object instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
} }
} }

View File

@ -12,69 +12,51 @@ import java.util.Arrays;
public class CheckPackageFolder extends TestCase { 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 @Test
public void testCorrectFolder1FileWithWrongPackageName() throws IOException, ClassNotFoundException { public void testCorrectFolder1FileWithWrongPackageName() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"packageNameTestWrongPackage.jav")); JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory + "packageNameTestWrongPackage.jav"));
compiler.typeInference(); compiler.typeInference();
File f = new File(rootDirectory + "TestClass.class"); File f = new File(rootDirectory + "TestClass.class");
if(f.exists() && !f.isDirectory()) { if (f.exists() && !f.isDirectory()) {
f.delete(); f.delete();
} }
compiler.generateBytecode(); compiler.generateBytecode();
f = new File(rootDirectory + "TestClass.class"); 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 @Test
public void testCorrectFolder1File() throws IOException, ClassNotFoundException { 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(); compiler.typeInference();
File f = new File(rootDirectory + "TestClass.class"); File f = new File(rootDirectory + "TestClass.class");
if(f.exists() && !f.isDirectory()) { if (f.exists() && !f.isDirectory()) {
f.delete(); f.delete();
} }
compiler.generateBytecode(); compiler.generateBytecode();
f = new File(rootDirectory + "TestClass.class"); 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 @Test
public void testCorrectFolder1FileAndOutputDirectory() throws IOException, ClassNotFoundException { 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(); compiler.typeInference();
File f = new File(rootDirectory + "output/de/test/TestClass.class"); File f = new File(rootDirectory + "output/de/test/TestClass.class");
if(f.exists() && !f.isDirectory()) { if (f.exists() && !f.isDirectory()) {
f.delete(); f.delete();
} }
compiler.generateBytecode(rootDirectory+"output/"); compiler.generateBytecode(rootDirectory + "output/");
f = new File(rootDirectory + "output/de/test/TestClass.class"); 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 * Dieser Test wird übersprungen, da der Bytecode-Generator nicht mit zwei Eingabedateien gleichzeitig umgehen kann
@Test *
public void testCorrectFolder2Files() throws IOException, ClassNotFoundException { * @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()) {
JavaTXCompiler compiler = new JavaTXCompiler(Arrays.asList( * f.delete(); } compiler.generateBytecode(); f = new File(rootDirectory + "subpackage1/Test1.class"); f2 = new File(rootDirectory + "subpackage2/Test2.class"); assertTrue(f.exists()); assertTrue(f2.exists()); }
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 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 @Test
public void testCompileSingleJavFile() throws Exception { public void testCompileSingleJavFile() throws Exception {
File f = new File(rootDirectory + "de/test/TestClass.class"); File f = new File(rootDirectory + "de/test/TestClass.class");
if(f.exists() && !f.isDirectory()) { if (f.exists() && !f.isDirectory()) {
f.delete(); 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"); f = new File(rootDirectory + "de/test/TestClass.class");
assertTrue(f.exists()); assertTrue(f.exists());
@ -29,11 +29,11 @@ public class ConsoleInterfaceTest extends TestCase {
@Test @Test
public void testCompileSingleJavFileWithOutputDirectory() throws Exception { public void testCompileSingleJavFileWithOutputDirectory() throws Exception {
File f = new File(rootDirectory + "de/test/output/de/test/TestClass.class"); File f = new File(rootDirectory + "de/test/output/de/test/TestClass.class");
if(f.exists() && !f.isDirectory()) { if (f.exists() && !f.isDirectory()) {
f.delete(); 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"); f = new File(rootDirectory + "de/test/output/de/test/TestClass.class");
assertTrue(f.exists()); assertTrue(f.exists());
@ -41,18 +41,18 @@ public class ConsoleInterfaceTest extends TestCase {
@Test @Test
public void testCpNotEndsWithSlash() throws Exception { 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.typeInference();
compiler.generateBytecode(rootDirectory + "output/"); compiler.generateBytecode(rootDirectory + "output/");
File f = new File(rootDirectory + "output/de/test/ToImport.class"); File f = new File(rootDirectory + "output/de/test/ToImport.class");
assertTrue(f.exists()); assertTrue(f.exists());
f = new File(rootDirectory + "de/test/ImportTest.class"); f = new File(rootDirectory + "de/test/ImportTest.class");
if(f.exists() && !f.isDirectory()) { if (f.exists() && !f.isDirectory()) {
f.delete(); 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"); f = new File(rootDirectory + "de/test/ImportTest.class");
assertTrue(f.exists()); assertTrue(f.exists());
@ -61,11 +61,11 @@ public class ConsoleInterfaceTest extends TestCase {
@Test @Test
public void testOutputDirNotEndsWithSlash() throws Exception { public void testOutputDirNotEndsWithSlash() throws Exception {
File f = new File(rootDirectory + "de/test/output/de/test/TestClass.class"); File f = new File(rootDirectory + "de/test/output/de/test/TestClass.class");
if(f.exists() && !f.isDirectory()) { if (f.exists() && !f.isDirectory()) {
f.delete(); 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"); f = new File(rootDirectory + "de/test/output/de/test/TestClass.class");
assertTrue(f.exists()); assertTrue(f.exists());
@ -73,18 +73,18 @@ public class ConsoleInterfaceTest extends TestCase {
@Test @Test
public void testCompileSingleJavFileWithClassPath() throws Exception { 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.typeInference();
compiler.generateBytecode(rootDirectory + "output/"); compiler.generateBytecode(rootDirectory + "output/");
File f = new File(rootDirectory + "output/de/test/ToImport.class"); File f = new File(rootDirectory + "output/de/test/ToImport.class");
assertTrue(f.exists()); assertTrue(f.exists());
f = new File(rootDirectory + "de/test/ImportTest.class"); f = new File(rootDirectory + "de/test/ImportTest.class");
if(f.exists() && !f.isDirectory()) { if (f.exists() && !f.isDirectory()) {
f.delete(); 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"); f = new File(rootDirectory + "de/test/ImportTest.class");
assertTrue(f.exists()); assertTrue(f.exists());
@ -92,19 +92,18 @@ public class ConsoleInterfaceTest extends TestCase {
@Test @Test
public void testCompileSingleJavFileWithMultipleClassPath() throws Exception { 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.typeInference();
compiler.generateBytecode(rootDirectory + "output/"); compiler.generateBytecode(rootDirectory + "output/");
File f = new File(rootDirectory + "output/de/test/ToImport.class"); File f = new File(rootDirectory + "output/de/test/ToImport.class");
assertTrue(f.exists()); assertTrue(f.exists());
f = new File(rootDirectory + "de/test/ImportTest.class"); f = new File(rootDirectory + "de/test/ImportTest.class");
if(f.exists() && !f.isDirectory()) { if (f.exists() && !f.isDirectory()) {
f.delete(); f.delete();
} }
ConsoleInterface.main(new String[]{"-cp", rootDirectory + "de/test/output/:"+rootDirectory+"de", ConsoleInterface.main(new String[] { "-cp", rootDirectory + "de/test/output/:" + rootDirectory + "de", rootDirectory + "de/test/ImportTest.jav" });
rootDirectory + "de/test/ImportTest.jav"});
f = new File(rootDirectory + "de/test/ImportTest.class"); f = new File(rootDirectory + "de/test/ImportTest.class");
assertTrue(f.exists()); assertTrue(f.exists());
@ -112,19 +111,18 @@ public class ConsoleInterfaceTest extends TestCase {
@Test @Test
public void testCompileSingleJavFileWithClassPathAndOutputDir() throws Exception { 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.typeInference();
compiler.generateBytecode(rootDirectory + "output/"); compiler.generateBytecode(rootDirectory + "output/");
File f = new File(rootDirectory + "output/de/test/ToImport.class"); File f = new File(rootDirectory + "output/de/test/ToImport.class");
assertTrue(f.exists()); assertTrue(f.exists());
f = new File(rootDirectory + "de/test/output/de/test/ImportTest.class"); f = new File(rootDirectory + "de/test/output/de/test/ImportTest.class");
if(f.exists() && !f.isDirectory()) { if (f.exists() && !f.isDirectory()) {
f.delete(); f.delete();
} }
ConsoleInterface.main(new String[]{"-cp", rootDirectory + "de/test/output/", ConsoleInterface.main(new String[] { "-cp", rootDirectory + "de/test/output/", "-d" + rootDirectory + "de/test/output/", rootDirectory + "de/test/ImportTest.jav" });
"-d"+rootDirectory + "de/test/output/" ,rootDirectory + "de/test/ImportTest.jav"});
f = new File(rootDirectory + "de/test/output/de/test/ImportTest.class"); f = new File(rootDirectory + "de/test/output/de/test/ImportTest.class");
assertTrue(f.exists()); assertTrue(f.exists());

View File

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

View File

@ -13,7 +13,7 @@ import java.net.URLClassLoader;
public class LoadDefaultPackageClassesTest extends TestCase { 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 { public LoadDefaultPackageClassesTest() throws ClassNotFoundException, IOException {
/* /*
@ -35,14 +35,8 @@ public class LoadDefaultPackageClassesTest extends TestCase {
cl.loadClass("Gen"); cl.loadClass("Gen");
} }
/* /*
* public void testE2E() throws IOException, ClassNotFoundException { * 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());
* 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 = new JavaTXCompiler(new File(rootDirectory+"OLMain.jav")); compiler.typeInference(); compiler.generateBytecode(); f = new File(rootDirectory + "OLMain.class"); assertTrue(f.exists()); }
* 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 instanceOfClass1;
private static Object instanceOfClass2; 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 @BeforeClass
public static void setUpBeforeClass() throws Exception { public static void setUpBeforeClass() throws Exception {
path = rootDirectory +"OLOneFile.jav"; path = rootDirectory + "OLOneFile.jav";
fileToTest = new File(path); fileToTest = new File(path);
compiler = new JavaTXCompiler( compiler = new JavaTXCompiler(Lists.newArrayList(fileToTest), Lists.newArrayList(new File(rootDirectory + "de/test/output/")));
Lists.newArrayList(fileToTest), pathToClassFile = System.getProperty("user.dir") + "/src/test/resources/javFiles/packageTest/";
Lists.newArrayList(new File(rootDirectory+"de/test/output/")));
pathToClassFile = System.getProperty("user.dir")+"/resources/javFiles/packageTest/";
List<ResultSet> typeinferenceResult = compiler.typeInference(); List<ResultSet> typeinferenceResult = compiler.typeInference();
//List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); // List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult);
//compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); // compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles);
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); loader = new URLClassLoader(new URL[] { new URL("file://" + pathToClassFile) });
classToTest = loader.loadClass("OLOneFile"); classToTest = loader.loadClass("OLOneFile");
instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
classToTest1 = loader.loadClass("OLextendsOneFile"); classToTest1 = loader.loadClass("OLextendsOneFile");
@ -52,78 +50,78 @@ public class OLOneFileTest {
classToTest2 = loader.loadClass("OLMainOneFile"); classToTest2 = loader.loadClass("OLMainOneFile");
instanceOfClass2 = classToTest2.getDeclaredConstructor().newInstance(); instanceOfClass2 = classToTest2.getDeclaredConstructor().newInstance();
} }
@Test @Test
public void testOLClassName() { public void testOLClassName() {
assertEquals("OLOneFile", classToTest.getName()); assertEquals("OLOneFile", classToTest.getName());
} }
@Test @Test
public void testmInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { public void testmInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method m = classToTest.getDeclaredMethod("m2", Integer.class); Method m = classToTest.getDeclaredMethod("m2", Integer.class);
Integer result = (Integer) m.invoke(instanceOfClass, 5); Integer result = (Integer) m.invoke(instanceOfClass, 5);
assertEquals(new Integer(10), result); assertEquals(new Integer(10), result);
} }
@Test @Test
public void testmDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { public void testmDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method m = classToTest.getDeclaredMethod("m2", Double.class); Method m = classToTest.getDeclaredMethod("m2", Double.class);
Double result = (Double) m.invoke(instanceOfClass, 5.0); Double result = (Double) m.invoke(instanceOfClass, 5.0);
assertEquals(new Double(10.0), result); assertEquals(new Double(10.0), result);
} }
@Test @Test
public void testmString() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { public void testmString() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method m = classToTest.getDeclaredMethod("m2", String.class); Method m = classToTest.getDeclaredMethod("m2", String.class);
String result = (String) m.invoke(instanceOfClass, "xxx"); String result = (String) m.invoke(instanceOfClass, "xxx");
assertEquals("xxxxxx", result); assertEquals("xxxxxx", result);
} }
@Test @Test
public void testOLextendsClassName() { public void testOLextendsClassName() {
assertEquals("OLextendsOneFile", classToTest1.getName()); assertEquals("OLextendsOneFile", classToTest1.getName());
} }
@Test @Test
public void testextendsInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { public void testextendsInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method main = classToTest1.getMethod("m2", Integer.class); Method main = classToTest1.getMethod("m2", Integer.class);
Integer result = (Integer) main.invoke(instanceOfClass1, 5); Integer result = (Integer) main.invoke(instanceOfClass1, 5);
assertEquals(new Integer(10), result); assertEquals(new Integer(10), result);
} }
@Test @Test
public void testextendsDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { public void testextendsDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method main = classToTest1.getMethod("m2", Double.class); Method main = classToTest1.getMethod("m2", Double.class);
Double result = (Double) main.invoke(instanceOfClass1, 5.0); Double result = (Double) main.invoke(instanceOfClass1, 5.0);
assertEquals(new Double(10.0), result); assertEquals(new Double(10.0), result);
} }
@Test @Test
public void testextendsString() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { public void testextendsString() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method main = classToTest1.getMethod("m2", String.class); Method main = classToTest1.getMethod("m2", String.class);
String result = (String) main.invoke(instanceOfClass1, "xxx"); String result = (String) main.invoke(instanceOfClass1, "xxx");
assertEquals("xxxxxx", result); assertEquals("xxxxxx", result);
} }
@Test @Test
public void testOLMainClassName() { public void testOLMainClassName() {
assertEquals("OLMainOneFile", classToTest2.getName()); assertEquals("OLMainOneFile", classToTest2.getName());
} }
@Test @Test
public void testmainInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { public void testmainInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method main = classToTest2.getDeclaredMethod("main", Integer.class); Method main = classToTest2.getDeclaredMethod("main", Integer.class);
Integer result = (Integer) main.invoke(instanceOfClass2, 5); Integer result = (Integer) main.invoke(instanceOfClass2, 5);
assertEquals(new Integer(10), result); assertEquals(new Integer(10), result);
} }
@Test @Test
public void testmainDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { public void testmainDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method main = classToTest2.getDeclaredMethod("main", Double.class); Method main = classToTest2.getDeclaredMethod("main", Double.class);
Double result = (Double) main.invoke(instanceOfClass2, 5.0); Double result = (Double) main.invoke(instanceOfClass2, 5.0);
assertEquals(new Double(10.0), result); assertEquals(new Double(10.0), result);
} }
@Test @Test
public void testmainString() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { public void testmainString() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method main = classToTest2.getDeclaredMethod("main", String.class); Method main = classToTest2.getDeclaredMethod("main", String.class);

View File

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

View File

@ -9,11 +9,12 @@ import java.io.IOException;
public class ParsePackageName { 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 @Test
public void parsePackage() throws IOException, ClassNotFoundException { public void parsePackage() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"TestClass.jav")); JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory + "TestClass.jav"));
for(File f : compiler.sourceFiles.keySet()){ for (File f : compiler.sourceFiles.keySet()) {
SourceFile sf = compiler.sourceFiles.get(f); SourceFile sf = compiler.sourceFiles.get(f);
assert sf.getPkgName().equals("de.test"); assert sf.getPkgName().equals("de.test");
} }

View File

@ -12,7 +12,7 @@ import java.net.URL;
public class mathStrucMatrixOPTest extends TestCase { 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 { public mathStrucMatrixOPTest() throws ClassNotFoundException, IOException {

View File

@ -12,37 +12,30 @@ import java.net.URL;
public class mathStrucVectorTest extends TestCase { 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 { 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")); * 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());
compiler.typeInference(); */
compiler.generateBytecode(rootDirectory + "output/"); JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory + "vectorAdd.jav"));
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.typeInference();
compiler.generateBytecode(rootDirectory + "output/"); compiler.generateBytecode(rootDirectory + "output/");
File f = new File(rootDirectory + "output/de/test/vectorAdd.class"); File f = new File(rootDirectory + "output/de/test/vectorAdd.class");
assertTrue(f.exists()); assertTrue(f.exists());
} }
@Test @Test
public void testSetPackageNameInBytecodeAndOutputFolder() throws IOException, ClassNotFoundException { public void testSetPackageNameInBytecodeAndOutputFolder() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler( JavaTXCompiler compiler = new JavaTXCompiler(Lists.newArrayList(new File(rootDirectory + "mathStrucVector.jav")), Lists.newArrayList(new File(rootDirectory + "output/")));
Lists.newArrayList(new File(rootDirectory+"mathStrucVector.jav")),
Lists.newArrayList(new File(rootDirectory+"output/")));
compiler.typeInference(); compiler.typeInference();
File f = new File(rootDirectory + "output/de/test/mathStrucVector.class"); File f = new File(rootDirectory + "output/de/test/mathStrucVector.class");
if(f.exists() && !f.isDirectory()) { if (f.exists() && !f.isDirectory()) {
f.delete(); f.delete();
} }
compiler.generateBytecode(rootDirectory + "output/"); compiler.generateBytecode(rootDirectory + "output/");
@ -50,5 +43,4 @@ public class mathStrucVectorTest extends TestCase {
assertTrue(f.exists()); assertTrue(f.exists());
} }
} }

View File

@ -24,9 +24,8 @@ import static org.junit.Assert.*;
public class ASTToTypedTargetAST { public class ASTToTypedTargetAST {
@Test @Test
public void emptyClass(){ 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()), 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());
new RefType(new JavaClassName("Object"), new NullToken()), false, new ArrayList<>(), new NullToken());
ResultSet emptyResultSet = new ResultSet(new HashSet<>()); ResultSet emptyResultSet = new ResultSet(new HashSet<>());
TargetClass emptyTargetClass = new ASTToTargetAST(List.of(emptyResultSet)).convert(emptyClass); TargetClass emptyTargetClass = new ASTToTargetAST(List.of(emptyResultSet)).convert(emptyClass);
assert emptyTargetClass.getName().equals("EmptyClass"); assert emptyTargetClass.getName().equals("EmptyClass");
@ -36,7 +35,7 @@ public class ASTToTypedTargetAST {
@Test @Test
public void overloading() throws Exception { 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 compiler = new JavaTXCompiler(file);
var resultSet = compiler.typeInference(); var resultSet = compiler.typeInference();
var converter = new ASTToTargetAST(resultSet); var converter = new ASTToTargetAST(resultSet);
@ -58,7 +57,7 @@ public class ASTToTypedTargetAST {
@Test @Test
public void tphsAndGenerics() throws Exception { 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 compiler = new JavaTXCompiler(file);
var resultSet = compiler.typeInference(); var resultSet = compiler.typeInference();
var converter = new ASTToTargetAST(resultSet); var converter = new ASTToTargetAST(resultSet);
@ -69,7 +68,7 @@ public class ASTToTypedTargetAST {
@Test @Test
public void cycles() throws Exception { 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 compiler = new JavaTXCompiler(file);
var resultSet = compiler.typeInference(); var resultSet = compiler.typeInference();
var converter = new ASTToTargetAST(resultSet); var converter = new ASTToTargetAST(resultSet);
@ -80,7 +79,7 @@ public class ASTToTypedTargetAST {
@Test @Test
public void infimum() throws Exception { 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 compiler = new JavaTXCompiler(file);
var resultSet = compiler.typeInference(); var resultSet = compiler.typeInference();
var converter = new ASTToTargetAST(resultSet); var converter = new ASTToTargetAST(resultSet);
@ -91,7 +90,7 @@ public class ASTToTypedTargetAST {
@Test @Test
public void gen() throws Exception { 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 compiler = new JavaTXCompiler(file);
var resultSet = compiler.typeInference(); var resultSet = compiler.typeInference();
var converter = new ASTToTargetAST(resultSet); var converter = new ASTToTargetAST(resultSet);
@ -101,12 +100,13 @@ public class ASTToTypedTargetAST {
var m = generics.getDeclaredMethod("m", Vector.class); var m = generics.getDeclaredMethod("m", Vector.class);
var mReturnType = m.getGenericReturnType(); var mReturnType = m.getGenericReturnType();
assertEquals(mReturnType, m.getParameters()[0].getParameterizedType()); assertEquals(mReturnType, m.getParameters()[0].getParameterizedType());
assertEquals(mReturnType, new TypeToken<Vector<Integer>>(){}.getType()); assertEquals(mReturnType, new TypeToken<Vector<Integer>>() {
}.getType());
} }
@Test @Test
public void definedGenerics() throws Exception { 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 compiler = new JavaTXCompiler(file);
var resultSet = compiler.typeInference(); var resultSet = compiler.typeInference();
var converter = new ASTToTargetAST(resultSet); var converter = new ASTToTargetAST(resultSet);
@ -124,7 +124,7 @@ public class ASTToTypedTargetAST {
@Test @Test
public void definedGenerics2() throws Exception { 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 compiler = new JavaTXCompiler(file);
var resultSet = compiler.typeInference(); var resultSet = compiler.typeInference();
var converter = new ASTToTargetAST(resultSet); var converter = new ASTToTargetAST(resultSet);
@ -140,7 +140,7 @@ public class ASTToTypedTargetAST {
@Test @Test
@Ignore("Not implemented") @Ignore("Not implemented")
public void definedGenerics3() throws Exception { 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 compiler = new JavaTXCompiler(file);
var resultSet = compiler.typeInference(); var resultSet = compiler.typeInference();
var converter = new ASTToTargetAST(resultSet); var converter = new ASTToTargetAST(resultSet);
@ -151,7 +151,7 @@ public class ASTToTypedTargetAST {
@Test @Test
public void definedGenerics4() throws Exception { 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 compiler = new JavaTXCompiler(file);
var resultSet = compiler.typeInference(); var resultSet = compiler.typeInference();
var converter = new ASTToTargetAST(resultSet); var converter = new ASTToTargetAST(resultSet);
@ -159,8 +159,8 @@ public class ASTToTypedTargetAST {
var generics4 = TestCodegen.generateClass(converter.convert(classes.get(0)), new ByteArrayClassLoader()); var generics4 = TestCodegen.generateClass(converter.convert(classes.get(0)), new ByteArrayClassLoader());
//var instance = generics4.getDeclaredConstructor().newInstance(); // var instance = generics4.getDeclaredConstructor().newInstance();
//var method = generics4.getDeclaredMethod("m2", Object.class); // var method = generics4.getDeclaredMethod("m2", Object.class);
//method.invoke(instance, new Object()); // method.invoke(instance, new Object());
} }
} }

View File

@ -25,341 +25,178 @@ import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class TestCodegen { public class TestCodegen {
private static void writeClassFile(String name, byte[] code) throws IOException { private static void writeClassFile(String name, byte[] code) throws IOException {
var path = Path.of(System.getProperty("user.dir"), "src/test/resources/target/"); var path = Path.of(System.getProperty("user.dir"), "src/test/resources/target/");
Files.createDirectories(path); Files.createDirectories(path);
Files.write(path.resolve(name + ".class"), code); 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());
} }
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 static Map<String, ? extends Class<?>> generateClassFiles(String filename, IByteArrayClassLoader classLoader) throws IOException, ClassNotFoundException {
public void testEmptyClass() throws Exception { var file = Path.of(System.getProperty("user.dir"), "/src/test/resources/bytecode/javFiles/", filename).toFile();
var clazz = new TargetClass(Opcodes.ACC_PUBLIC, "Empty"); var compiler = new JavaTXCompiler(List.of(file), List.of(file.getParentFile()));
clazz.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "main", List.of(), null, new TargetBlock(List.of())); var resultSet = compiler.typeInference();
generateClass(clazz, new ByteArrayClassLoader()).getDeclaredMethod("main").invoke(null);
}
@Test var sourceFile = compiler.sourceFiles.get(file);
public void testArithmetic() throws Exception { var converter = new ASTToTargetAST(resultSet, sourceFile, classLoader);
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "Arithmetic"); var classes = compiler.sourceFiles.get(file).getClasses();
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "add", var result = classes.stream().map(cli -> {
List.of(new MethodParameter(TargetType.Integer, "a"), new MethodParameter(TargetType.Integer, "b")), try {
TargetType.Integer, return generateClass(converter.convert(cli), classLoader);
new TargetBlock(List.of(new TargetReturn( } catch (IOException exception) {
new TargetBinaryOp.Add(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "a"), new TargetLocalVar(TargetType.Integer, "b"))) throw new RuntimeException(exception);
)) }
); }).collect(Collectors.toMap(Class::getName, Function.identity()));
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")))
))
);
for (var entry : converter.auxiliaries.entrySet()) {
writeClassFile(entry.getKey(), entry.getValue());
}
var clazz = generateClass(targetClass, new ByteArrayClassLoader()); return result;
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 @Test
public void testUnary() throws Exception { public void testEmptyClass() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "Unary"); 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", @Test
List.of(new MethodParameter(TargetType.Integer, "a")), public void testArithmetic() throws Exception {
TargetType.Integer, var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "Arithmetic");
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()); 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"))))));
assertEquals(clazz.getDeclaredMethod("not", Integer.class).invoke(null, 10), -11); 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"))))));
assertEquals(clazz.getDeclaredMethod("neg", Integer.class).invoke(null, 10), -10); 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"))))));
assertEquals(clazz.getDeclaredMethod("add", Integer.class).invoke(null, 10), 10); 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 @Test
public void testConditional() throws Exception { public void testUnary() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "Conditional"); var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "Unary");
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "and", 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"))))));
List.of(new MethodParameter(TargetType.Boolean, "a"), new MethodParameter(TargetType.Boolean, "b")), 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"))))));
TargetType.Boolean, 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"))))));
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")))
))
);
var clazz = generateClass(targetClass, new ByteArrayClassLoader()); var clazz = generateClass(targetClass, new ByteArrayClassLoader());
var and = clazz.getDeclaredMethod("and", Boolean.class, Boolean.class); assertEquals(clazz.getDeclaredMethod("not", Integer.class).invoke(null, 10), -11);
var or = clazz.getDeclaredMethod("or", Boolean.class, Boolean.class); assertEquals(clazz.getDeclaredMethod("neg", Integer.class).invoke(null, 10), -10);
assertEquals(and.invoke(null, true, false), false); assertEquals(clazz.getDeclaredMethod("add", Integer.class).invoke(null, 10), 10);
assertEquals(and.invoke(null, true, true), true);
assertEquals(or.invoke(null, false, false), false);
assertEquals(or.invoke(null, true, false), true);
}
// 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 @Test
public void testMethodCall() throws Exception { public void testConditional() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "HelloWorld"); var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "Conditional");
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
)))
);
var clazz = generateClass(targetClass, new ByteArrayClassLoader()); 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"))))));
clazz.getDeclaredMethod("helloWorld").invoke(null); 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 var clazz = generateClass(targetClass, new ByteArrayClassLoader());
public void testIfStatement() throws Exception { var and = clazz.getDeclaredMethod("and", Boolean.class, Boolean.class);
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "IfStmt"); var or = clazz.getDeclaredMethod("or", Boolean.class, Boolean.class);
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "ifStmt", assertEquals(and.invoke(null, true, false), false);
List.of(new MethodParameter(TargetType.Integer, "val")), assertEquals(and.invoke(null, true, true), true);
TargetType.Integer, assertEquals(or.invoke(null, false, false), false);
new TargetBlock(List.of(new TargetIf( assertEquals(or.invoke(null, true, false), true);
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);
}
@Test // When adding two numbers and the return type is Long it needs to convert both values to Long
public void testFor() throws Exception { @Test
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "For"); public void testArithmeticConvert() throws Exception {
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "forLoop", List.of(), TargetType.Integer, var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "ArithmeticConvert");
new TargetBlock(List.of( 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))))));
new TargetVarDecl(TargetType.Integer, "sum", new TargetLiteral.IntLiteral(0)), var clazz = generateClass(targetClass, new ByteArrayClassLoader());
new TargetFor( assertEquals(clazz.getDeclaredMethod("add").invoke(null), (long) 30);
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);
}
@Test @Test
public void testWhile() throws Exception { public void testMethodCall() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "While"); var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "HelloWorld");
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "whileLoop", List.of(), TargetType.Integer, 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))));
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 var clazz = generateClass(targetClass, new ByteArrayClassLoader());
public void testNew() throws Exception { clazz.getDeclaredMethod("helloWorld").invoke(null);
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 mainTarget = new TargetClass(Opcodes.ACC_PUBLIC, "New"); @Test
mainTarget.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "makePoint", public void testIfStatement() throws Exception {
List.of(new MethodParameter(TargetType.Integer, "x"), new MethodParameter(TargetType.Integer, "y")), pointType, var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, "IfStmt");
new TargetBlock(List.of( 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)))))));
new TargetReturn(new TargetNew(pointType, List.of( var clazz = generateClass(targetClass, new ByteArrayClassLoader());
new TargetLocalVar(TargetType.Integer, "x"), var ifStmt = clazz.getDeclaredMethod("ifStmt", Integer.class);
new TargetLocalVar(TargetType.Integer, "y") assertEquals(ifStmt.invoke(null, 10), 1);
))) assertEquals(ifStmt.invoke(null, 3), 2);
)) assertEquals(ifStmt.invoke(null, 20), 3);
); }
var classLoader = new ByteArrayClassLoader(); @Test
var pointClass = generateClass(pointTarget, classLoader); public void testFor() throws Exception {
var mainClass = generateClass(mainTarget, classLoader); 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); @Test
assertEquals(point.getClass().getDeclaredField("x").get(point), 10); public void testWhile() throws Exception {
assertEquals(point.getClass().getDeclaredField("y").get(point), 20); 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 @Test
@Ignore("The lambda class is not generated because we don't call ASTToTargetAST") public void testNew() throws Exception {
public void testLambda() throws Exception { var pointType = new TargetRefType("Point");
var classLoader = new ByteArrayClassLoader(); var pointTarget = new TargetClass(Opcodes.ACC_PUBLIC, "Point");
//var fun = classLoader.loadClass(Path.of(System.getProperty("user.dir"), "src/test/java/targetast/Fun1$$.class")); pointTarget.addField(Opcodes.ACC_PUBLIC, TargetType.Integer, "x");
var interfaceType = new TargetFunNType(1, List.of(TargetType.Integer)); 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"); var mainTarget = new TargetClass(Opcodes.ACC_PUBLIC, "New");
targetClass.addConstructor(Opcodes.ACC_PUBLIC, List.of(), new TargetBlock(List.of( 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")))))));
new TargetMethodCall(null, new TargetSuper(TargetType.Object), List.of(), TargetType.Object, "<init>", false, false)
))); var classLoader = new ByteArrayClassLoader();
targetClass.addMethod(Opcodes.ACC_PUBLIC, "lambda", List.of(), TargetType.Integer, var pointClass = generateClass(pointTarget, classLoader);
new TargetBlock(List.of( var mainClass = generateClass(mainTarget, classLoader);
new TargetVarDecl(interfaceType, "by2",
new TargetLambdaExpression(interfaceType, List.of(), List.of(new MethodParameter(TargetType.Integer, "num")), TargetType.Integer, var point = mainClass.getDeclaredMethod("makePoint", Integer.class, Integer.class).invoke(null, 10, 20);
new TargetBlock(List.of( assertEquals(point.getClass().getDeclaredField("x").get(point), 10);
new TargetReturn(new TargetBinaryOp.Mul(TargetType.Integer, assertEquals(point.getClass().getDeclaredField("y").get(point), 20);
new TargetLocalVar(TargetType.Integer, "num"), }
new TargetLiteral.IntLiteral(2)
)) @Test
) @Ignore("The lambda class is not generated because we don't call ASTToTargetAST")
)) public void testLambda() throws Exception {
), var classLoader = new ByteArrayClassLoader();
new TargetReturn(new TargetCast(TargetType.Integer, new TargetMethodCall(TargetType.Object, TargetType.Object, List.of(TargetType.Object), new TargetLocalVar(interfaceType, "by2"), List.of( // var fun = classLoader.loadClass(Path.of(System.getProperty("user.dir"), "src/test/java/targetast/Fun1$$.class"));
new TargetLiteral.IntLiteral(10) var interfaceType = new TargetFunNType(1, List.of(TargetType.Integer));
), interfaceType, "apply", false, true)))
)) 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))));
var clazz = generateClass(targetClass, classLoader); 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 instance = clazz.getConstructor().newInstance(); var clazz = generateClass(targetClass, classLoader);
assertEquals(clazz.getDeclaredMethod("lambda").invoke(instance), 20); 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.ArrayList;
import java.util.List; import java.util.List;
public class TestGenerics { 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 static final String bytecodeDirectory = System.getProperty("user.dir") + "/src/test/resources/testBytecode/generatedBC/";
private record Result(List<GenericsResult> genericsResults, ClassOrInterface clazz) { private record Result(List<GenericsResult> genericsResults, ClassOrInterface clazz) {
@ -122,7 +121,7 @@ public class TestGenerics {
assertEquals(2, generics.get(m).size()); assertEquals(2, generics.get(m).size());
var R = generics.getBounds(a.getType(), result.clazz); 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); assertEquals(R, RChain);
var O = generics.getBounds(id.getParameterList().getParameterAt(0).getType(), result.clazz, id); var O = generics.getBounds(id.getParameterList().getParameterAt(0).getType(), result.clazz, id);
@ -176,13 +175,13 @@ public class TestGenerics {
assertEquals(AK, AKChain); assertEquals(AK, AKChain);
// TODO Shouldn't AK and AK2 be the same and on the class? // 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 AK2 = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m);
var Y = 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(AK2, AKChain);
assertEquals(Y, AKChain); assertEquals(Y, AKChain);
var AF = generics.getBounds(main.getParameterList().getParameterAt(0).getType(), 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); var AG = generics.getBounds(main.getParameterList().getParameterAt(1).getType(), result.clazz, main);
var AK3 = generics.getBounds(main.getReturnType(), 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)); var AFChain = new BoundsList(new Bound(true, TypePlaceholder.of("AG")), new Bound(true, ASTToTargetAST.OBJECT));
assertEquals(AF, AFChain); assertEquals(AF, AFChain);
@ -266,12 +265,7 @@ public class TestGenerics {
var generics = result.genericsResults.get(0); var generics = result.genericsResults.get(0);
var AO = generics.getBounds(a.getType(), result.clazz); var AO = generics.getBounds(a.getType(), result.clazz);
var AOBound = new BoundsList( 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));
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); assertEquals(AO, AOBound);
var S = generics.getBounds(setA.getParameterList().getParameterAt(0).getType(), result.clazz, setA); 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))); assertEquals(AE, new BoundsList(new Bound(false, ASTToTargetAST.OBJECT)));
// TODO main seems to change between runs // 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 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 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 @Test

View File

@ -23,155 +23,175 @@ import java.util.Set;
public class JavaTXCompilerTest { 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 @Test
public void finiteClosure() throws IOException, ClassNotFoundException { public void finiteClosure() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"fc.jav")); 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"));
} }
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 { public TestResultSet execute(File fileToTest) throws IOException, ClassNotFoundException {
//filesToTest.add(new File(rootDirectory+"fc.jav")); // filesToTest.add(new File(rootDirectory+"fc.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda.jav")); // filesToTest.add(new File(rootDirectory+"Lambda.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda2.jav")); // filesToTest.add(new File(rootDirectory+"Lambda2.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda3.jav")); // filesToTest.add(new File(rootDirectory+"Lambda3.jav"));
//filesToTest.add(new File(rootDirectory+"Vector.jav")); // filesToTest.add(new File(rootDirectory+"Vector.jav"));
//filesToTest.add(new File(rootDirectory+"Generics.jav")); // filesToTest.add(new File(rootDirectory+"Generics.jav"));
//filesToTest.add(new File(rootDirectory+"MethodsEasy.jav")); // filesToTest.add(new File(rootDirectory+"MethodsEasy.jav"));
//filesToTest.add(new File(rootDirectory+"Matrix.jav")); // filesToTest.add(new File(rootDirectory+"Matrix.jav"));
//filesToTest.add(new File(rootDirectory+"Import.jav")); // filesToTest.add(new File(rootDirectory+"Import.jav"));
//filesToTest.add(new File(rootDirectory+"Faculty.jav")); // filesToTest.add(new File(rootDirectory+"Faculty.jav"));
//filesToTest.add(new File(rootDirectory+"mathStruc.jav")); // filesToTest.add(new File(rootDirectory+"mathStruc.jav"));
//filesToTest.add(new File(rootDirectory+"test.jav")); // filesToTest.add(new File(rootDirectory+"test.jav"));
JavaTXCompiler compiler = new JavaTXCompiler(fileToTest); JavaTXCompiler compiler = new JavaTXCompiler(fileToTest);
for(File f : compiler.sourceFiles.keySet()){ for (File f : compiler.sourceFiles.keySet()) {
SourceFile sf = compiler.sourceFiles.get(f); SourceFile sf = compiler.sourceFiles.get(f);
System.out.println(ASTTypePrinter.print(sf)); System.out.println(ASTTypePrinter.print(sf));
System.out.println(ASTPrinter.print(sf)); System.out.println(ASTPrinter.print(sf));
} }
List<ResultSet> results = compiler.typeInference(); List<ResultSet> results = compiler.typeInference();
List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(results); List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(results);
//compiler.generateBytecode(rootDirectory+"xxx.class", results, simplifyResultsForAllSourceFiles); // compiler.generateBytecode(rootDirectory+"xxx.class", results, simplifyResultsForAllSourceFiles);
for(File f : compiler.sourceFiles.keySet()){ for (File f : compiler.sourceFiles.keySet()) {
SourceFile sf = compiler.sourceFiles.get(f); SourceFile sf = compiler.sourceFiles.get(f);
System.out.println(ASTTypePrinter.print(sf)); System.out.println(ASTTypePrinter.print(sf));
System.out.println(ASTPrinter.print(sf)); System.out.println(ASTPrinter.print(sf));
//List<ResultSet> results = compiler.typeInference(); PL 2017-10-03 vor die For-Schleife gezogen // List<ResultSet> results = compiler.typeInference(); PL 2017-10-03 vor die For-Schleife gezogen
assert results.size()>0; assert results.size() > 0;
Set<String> insertedTypes = new HashSet<>(); Set<String> insertedTypes = new HashSet<>();
for(ResultSet resultSet : results){ for (ResultSet resultSet : results) {
Set<TypeInsert> result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet, results, simplifyResultsForAllSourceFiles); 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); String content = readFile(f.getPath(), StandardCharsets.UTF_8);
for(TypeInsert tip : result){ for (TypeInsert tip : result) {
insertedTypes.add(tip.insert(content)); insertedTypes.add(tip.insert(content));
} }
} }
for(String s : insertedTypes){ for (String s : insertedTypes) {
System.out.println(s); System.out.println(s);
} }
} }
return new TestResultSet(); return new TestResultSet();
} }
static String readFile(String path, Charset encoding) static String readFile(String path, Charset encoding) throws IOException {
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path)); byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding); return new String(encoded, encoding);
} }
} }

View File

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

View File

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

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