From 907ab2fcec6e92e970f5c2f865e67ef2ee19c969 Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Fri, 8 May 2020 10:19:00 +0200 Subject: [PATCH] modified: ../../../java/bytecode/InheritTest.java modified: ../../bytecode/javFiles/Inherit.jav --- src/test/java/bytecode/InheritTest.java | 63 +++++++++++-------- .../resources/bytecode/javFiles/Inherit.jav | 2 + 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/test/java/bytecode/InheritTest.java b/src/test/java/bytecode/InheritTest.java index 5d9bb231..86181bf6 100644 --- a/src/test/java/bytecode/InheritTest.java +++ b/src/test/java/bytecode/InheritTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.*; import java.io.File; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; @@ -30,14 +31,16 @@ public class InheritTest { private static JavaTXCompiler compiler; private static ClassLoader loader; private static Class classToTest; - private static Class classToTest1, classToTest2, classToTest3, classToTest4; + private static Class classToTestAA, classToTestBB, classToTestCC, classToTestDD; private static String pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/";; + //private static String pathToClassFile1 = System.getProperty("user.dir") + "/src/test/resources/testBytecode/generatedBC/"; private static Object instanceOfClass; - private static Object instanceOfClass1, instanceOfClass2, instanceOfClass3, instanceOfClass4; + private static Object instanceOfClassAA, instanceOfClassBB, instanceOfClassCC, instanceOfClassDD; private static HashMap, Method> hm = new HashMap<>(); @BeforeClass public static void setUpBeforeClass() throws Exception { + path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/AA.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); @@ -45,9 +48,10 @@ public class InheritTest { List simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); - classToTest1 = loader.loadClass("AA"); - instanceOfClass1 = classToTest1.getDeclaredConstructor().newInstance(); + classToTestAA = loader.loadClass("AA"); + instanceOfClassAA = classToTestAA.getDeclaredConstructor().newInstance(); + /* path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/BB.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); @@ -55,8 +59,8 @@ public class InheritTest { simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); - classToTest2 = loader.loadClass("BB"); - instanceOfClass2 = classToTest1.getDeclaredConstructor().newInstance(); + classToTestBB = loader.loadClass("BB"); + instanceOfClassBB = classToTestBB.getDeclaredConstructor().newInstance(); path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/CC.jav"; fileToTest = new File(path); @@ -65,8 +69,8 @@ public class InheritTest { simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); - classToTest3 = loader.loadClass("CC"); - instanceOfClass3 = classToTest1.getDeclaredConstructor().newInstance(); + classToTestCC = loader.loadClass("CC"); + instanceOfClassCC = classToTestCC.getDeclaredConstructor().newInstance(); path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/DD.jav"; fileToTest = new File(path); @@ -75,15 +79,18 @@ public class InheritTest { simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); - classToTest4 = loader.loadClass("DD"); - instanceOfClass4 = classToTest1.getDeclaredConstructor().newInstance(); + classToTestDD = loader.loadClass("DD"); + instanceOfClassDD = classToTestDD.getDeclaredConstructor().newInstance(); + */ path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/Inherit.jav"; fileToTest = new File(path); + compiler = new JavaTXCompiler(fileToTest); + /* compiler = new JavaTXCompiler( Lists.newArrayList(fileToTest), - Lists.newArrayList(new File(pathToClassFile))); - //compiler = new JavaTXCompiler(fileToTest); + Lists.newArrayList(new File(pathToClassFile1))); + */ typeinferenceResult = compiler.typeInference(); simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); @@ -91,6 +98,7 @@ public class InheritTest { classToTest = loader.loadClass("Inherit"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); + /* for (Method m: classToTest.getMethods()) { ArrayList param = Arrays.stream(m.getParameterTypes()).map(x -> x.getName()).collect(Collectors.toCollection(ArrayList::new)); ArrayList nameParam = new ArrayList<>(); @@ -98,6 +106,7 @@ public class InheritTest { nameParam.addAll(param); hm.put(nameParam, m); } + */ } @Test @@ -105,33 +114,37 @@ public class InheritTest { assertEquals("Inherit", classToTest.getName()); } - @Test - public void testAAName() { - assertEquals("AA", classToTest1.getName()); - } + @Test + public void testAAName() { + assertEquals("AA", classToTestAA.getName()); + } + + /* @Test public void testBBName() { - assertEquals("BB", classToTest2.getName()); + assertEquals("BB", classToTestBB.getName()); } @Test public void testCCName() { - assertEquals("CC", classToTest3.getName()); + assertEquals("CC", classToTestCC.getName()); } @Test public void testDDName() { - assertEquals("DD", classToTest4.getName()); + assertEquals("DD", classToTestDD.getName()); } - + */ @Test - public void testmainAA() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException { - String[] x = {"main", "AA", "java.lang.Integer"}; - ArrayList nameParam = new ArrayList<>(Arrays.asList(x)); - Method m = hm.get(nameParam); - assertEquals(m.invoke(classToTest1.getConstructor().newInstance(), 5), "AA"); + public void testmainAA() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException { + loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); + classToTest = loader.loadClass("Inherit"); + Method m = classToTestAA.getDeclaredMethod("m", Integer.class); + assertEquals(m.invoke(instanceOfClassAA, 5), "AA"); + Method main = classToTest.getDeclaredMethod("main", classToTestAA, Integer.class); + assertEquals(main.invoke(instanceOfClass, instanceOfClassAA, 5), "AA"); } /* Vector v_invoke = new Vector<>(); diff --git a/src/test/resources/bytecode/javFiles/Inherit.jav b/src/test/resources/bytecode/javFiles/Inherit.jav index bf677af9..0ef9c020 100644 --- a/src/test/resources/bytecode/javFiles/Inherit.jav +++ b/src/test/resources/bytecode/javFiles/Inherit.jav @@ -6,6 +6,8 @@ import java.lang.String; public class Inherit { + //m(Integer i) { return "AA"; } + main(d, i) { return d.m(i); }