From ccf5df1f1e1288b8cfdab568b5f553d2c741871e Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Tue, 5 May 2020 18:43:39 +0200 Subject: [PATCH] new file: ../../../java/bytecode/InheritTest.java modified: ../../../java/bytecode/PutTest.java new file: ../../bytecode/javFiles/Inherit.jav --- src/test/java/bytecode/InheritTest.java | 94 +++++++++++++++++++ src/test/java/bytecode/PutTest.java | 44 --------- .../resources/bytecode/javFiles/Inherit.jav | 29 ++++++ 3 files changed, 123 insertions(+), 44 deletions(-) create mode 100644 src/test/java/bytecode/InheritTest.java create mode 100644 src/test/resources/bytecode/javFiles/Inherit.jav diff --git a/src/test/java/bytecode/InheritTest.java b/src/test/java/bytecode/InheritTest.java new file mode 100644 index 00000000..713d82f4 --- /dev/null +++ b/src/test/java/bytecode/InheritTest.java @@ -0,0 +1,94 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.List; +import java.util.Stack; +import java.util.Vector; + +import org.junit.BeforeClass; +import org.junit.Test; + +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; +import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.typeinference.result.ResultSet; + +public class InheritTest { + private static String path; + private static File fileToTest; + private static JavaTXCompiler compiler; + private static ClassLoader loader; + private static Class classToTest; + private static Class classToTest1; + private static String pathToClassFile; + private static Object instanceOfClass; + private static Object instanceOfClass1; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/Inherit.jav"; + fileToTest = new File(path); + compiler = new JavaTXCompiler(fileToTest); + pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/"; + List typeinferenceResult = compiler.typeInference(); + List simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); + compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); + loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); + classToTest = loader.loadClass("Inherit"); + instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); + classToTest1 = loader.loadClass("A"); + instanceOfClass1 = classToTest1.getDeclaredConstructor().newInstance(); + } + + @Test + public void testInheritClassName() { + assertEquals("Inherit", classToTest.getName()); + } + + @Test + public void testAName() { + assertEquals("A", classToTest1.getName()); + } + /* + @Test + public void testPutElementVector() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Method m = classToTest.getDeclaredMethod("putElement", Object.class, Vector.class); + Vector v_invoke = new Vector<>(); + m.invoke(instanceOfClass, 5, v_invoke); + Vector v = new Vector<>(); + v.add(5); + assertEquals(v, v_invoke); + } + + @Test + public void testPutElementStack() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Method m = classToTest.getDeclaredMethod("putElement", Object.class, Stack.class); + Stack s_invoke = new Stack<>(); + m.invoke(instanceOfClass, 5, s_invoke); + assertEquals(new Integer(5), s_invoke.pop()); + } + + @Test + public void testMainVector() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Method m = classToTest.getDeclaredMethod("main", Object.class, Vector.class); + Vector v_invoke = new Vector<>(); + m.invoke(instanceOfClass, 6, v_invoke); + Vector v = new Vector<>(); + v.add(6); + assertEquals(v, v_invoke); + } + + @Test + public void testMainStack() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Method m = classToTest.getDeclaredMethod("main", Object.class, Stack.class); + Stack s_invoke = new Stack<>(); + m.invoke(instanceOfClass, 6, s_invoke); + assertEquals(new Integer(6), s_invoke.pop()); + } + */ +} diff --git a/src/test/java/bytecode/PutTest.java b/src/test/java/bytecode/PutTest.java index 91ff6f11..b5fd39d1 100644 --- a/src/test/java/bytecode/PutTest.java +++ b/src/test/java/bytecode/PutTest.java @@ -41,8 +41,6 @@ public class PutTest { loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass("Put"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); - classToTest1 = loader.loadClass("OLMain"); - instanceOfClass1 = classToTest1.getDeclaredConstructor().newInstance(); } @Test @@ -85,46 +83,4 @@ public class PutTest { m.invoke(instanceOfClass, 6, s_invoke); assertEquals(new Integer(6), s_invoke.pop()); } - - /* - @Test - public void testmDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { - Method m = classToTest.getDeclaredMethod("m", Double.class); - Double result = (Double) m.invoke(instanceOfClass, 5.0); - assertEquals(new Double(10.0), result); - } - - @Test - public void testmString() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { - Method m = classToTest.getDeclaredMethod("m", String.class); - String result = (String) m.invoke(instanceOfClass, "xxx"); - assertEquals("xxxxxx", result); - } - - @Test - public void testOLMainClassName() { - assertEquals("OLMain", classToTest1.getName()); - } - - @Test - public void testmainInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { - Method main = classToTest1.getDeclaredMethod("main", Integer.class); - Integer result = (Integer) main.invoke(instanceOfClass1, 5); - assertEquals(new Integer(10), result); - } - - @Test - public void testmainDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { - Method main = classToTest1.getDeclaredMethod("main", Double.class); - Double result = (Double) main.invoke(instanceOfClass1, 5.0); - assertEquals(new Double(10.0), result); - } - - @Test - public void testmainString() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { - Method main = classToTest1.getDeclaredMethod("main", String.class); - String result = (String) main.invoke(instanceOfClass1, "xxx"); - assertEquals("xxxxxx", result); - } - */ } diff --git a/src/test/resources/bytecode/javFiles/Inherit.jav b/src/test/resources/bytecode/javFiles/Inherit.jav new file mode 100644 index 00000000..271c4edd --- /dev/null +++ b/src/test/resources/bytecode/javFiles/Inherit.jav @@ -0,0 +1,29 @@ +import java.lang.Integer; +import java.util.Vector; + +public class A { + m(Integer i) { } +} + +public class B extends A { } + +public class C extends B { + m(Integer i) { } +} + +public class D extends C { } + + + +public class Inherit { +/* + main(d, i) { + d.m(1); + } +*/ + + main(v, i) { + var aa = v.elementAt(0); + aa.m(1); + } +} \ No newline at end of file