From a785c85d426645a96666d03469084a21bd3395ce Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Wed, 2 May 2018 20:54:07 +0200 Subject: [PATCH] tests --- .../bytecode/BytecodeGenMethod.java | 2 +- .../descriptor/DescriptorToString.java | 6 +- src/de/dhbwstuttgart/core/JavaTXCompiler.java | 2 +- test/bytecode/LambdaTest.java | 23 +++ test/bytecode/OpTest.java | 162 ++---------------- test/bytecode/PlusTest.java | 54 ++++++ test/bytecode/javFiles/LamAssign.jav | 15 -- test/bytecode/javFiles/Lambda.jav | 23 +-- test/bytecode/javFiles/Lambda4.jav | 18 ++ test/bytecode/javFiles/Op.jav | 97 +---------- test/bytecode/javFiles/Plus.jav | 8 + 11 files changed, 132 insertions(+), 278 deletions(-) create mode 100644 test/bytecode/LambdaTest.java create mode 100644 test/bytecode/PlusTest.java delete mode 100644 test/bytecode/javFiles/LamAssign.jav create mode 100644 test/bytecode/javFiles/Lambda4.jav create mode 100644 test/bytecode/javFiles/Plus.jav diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index aa7b586e..6556d364 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -546,7 +546,7 @@ public class BytecodeGenMethod implements StatementVisitor { cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup", Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL); - // generateBCForFunN(lambdaExpression,typeErasure); + generateBCForFunN(lambdaExpression,typeErasure); } private void generateBCForFunN(LambdaExpression lambdaExpression, String methDesc) { diff --git a/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java b/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java index c5f378fe..202b647b 100644 --- a/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java +++ b/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java @@ -50,9 +50,9 @@ public class DescriptorToString implements DescriptorVisitor{ } } //TODO: generate a class java%% ... %% - else if(((RefType) fp.getType()).getParaList().size() > 0){ - desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.toString().replace(".", "%").replace("<", "%%").replace(">", "%%")+ ";"; - } +// else if(((RefType) fp.getType()).getParaList().size() > 0){ +// desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.toString().replace(".", "%").replace("<", "%%").replace(">", "%%")+ ";"; +// } else { desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; } diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index ec74f0da..4aaf23dc 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -150,7 +150,7 @@ public class JavaTXCompiler { for(String name : classFiles.keySet()) { byte[] bytecode = classFiles.get(name); System.out.println("generating "+name+ ".class file ..."); - output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/" +name+".class")); + output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/examples/" +name+".class")); output.write(bytecode); output.close(); System.out.println(name+".class file generated"); diff --git a/test/bytecode/LambdaTest.java b/test/bytecode/LambdaTest.java new file mode 100644 index 00000000..f29690cb --- /dev/null +++ b/test/bytecode/LambdaTest.java @@ -0,0 +1,23 @@ +package bytecode; + +import java.io.File; + +import org.junit.Test; + +import de.dhbwstuttgart.core.JavaTXCompiler; + +public class LambdaTest { + private static String path; + private static File fileToTest; + private static JavaTXCompiler compiler; + + @Test + public void generateBC() throws Exception { + path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Lambda.jav"; + fileToTest = new File(path); + compiler = new JavaTXCompiler(fileToTest); + compiler.generateBytecode(); + } + + +} diff --git a/test/bytecode/OpTest.java b/test/bytecode/OpTest.java index 33708206..39bde2b3 100644 --- a/test/bytecode/OpTest.java +++ b/test/bytecode/OpTest.java @@ -29,165 +29,33 @@ public class OpTest { fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); compiler.generateBytecode(); - pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/examples/"; loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass("Op"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); } @Test - public void testClassname() { - assertEquals("Op", classToTest.getName()); + public void testAddString() throws NoSuchMethodException, SecurityException, IllegalAccessException, + IllegalArgumentException, InvocationTargetException, InstantiationException { + + Method m = classToTest.getDeclaredMethod("m", String.class,String.class); + + String result = (String) m.invoke(instanceOfClass, "Byte","Code"); + + assertEquals("ByteCode", result); } - @Test - public void testClassModifiers() { - assertEquals(Opcodes.ACC_PUBLIC, classToTest.getModifiers()); - } - -// @Test -// public void testNumberOfMethods() { -// int numOfMeth = classToTest.getDeclaredMethods().length; -// assertEquals(5, numOfMeth); -// } -// -// @Test -// public void testAddString() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method addString = classToTest.getDeclaredMethod("addString", String.class,String.class); -// String result = (String) addString.invoke(instanceOfClass, "Byte","Code"); -// assertEquals("ByteCode", result); -// } -// @Test public void testAddInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException { - Method addInt = classToTest.getDeclaredMethod("addInt", Integer.class,Integer.class); - Number result = (Number) addInt.invoke(instanceOfClass, 7,3); + + Method m = classToTest.getDeclaredMethod("m", Integer.class,Integer.class); + + Integer result = (Integer) m.invoke(instanceOfClass, 7,3); + assertEquals(10, result); } -// @Test -// public void testAddLong() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method addLong = classToTest.getDeclaredMethod("addLong", Long.class,Long.class); -// Long result = (Long) addLong.invoke(instanceOfClass, 7L,3L); -// assertEquals(10L, result); -// } -// -// @Test -// public void testAddFloat() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method addFloat = classToTest.getDeclaredMethod("addFloat", Float.class,Float.class); -// Float result = (Float) addFloat.invoke(instanceOfClass, 7f,3f); -// assertEquals(10f, result); -// } -// -// @Test -// public void testAddDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method addDouble = classToTest.getDeclaredMethod("addDouble", Double.class,Double.class); -// Double result = (Double) addDouble.invoke(instanceOfClass, 7.0,3.0); -// assertEquals(10.0, result); -// } - -// @Test -// public void testAddIntLong() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method add = classToTest.getDeclaredMethod("add", Integer.class,Long.class); -// Long result = (Long) add.invoke(instanceOfClass, 7,3L); -// assertEquals(10L, result); -// } - -// @Test -// public void testAddDLong() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method add = classToTest.getDeclaredMethod("add", Double.class,Long.class); -// Double result = (Double) add.invoke(instanceOfClass, 7d,3L); -// assertEquals(10d, result); -// } - -// @Test -// public void testAddIntShort() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method add = classToTest.getDeclaredMethod("add", Integer.class,Short.class); -// Short s = 3; -// Integer result = (Integer) add.invoke(instanceOfClass, 7,s); -// assertEquals(10, result); -// } - -// @Test -// public void testAddIntByte() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method add = classToTest.getDeclaredMethod("add", Integer.class,Byte.class); -// Byte b = 3; -// Integer result = (Integer) add.invoke(instanceOfClass, 7,b); -// assertEquals(10, result); -// } - -// @Test -// public void testAddDFloat() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method add = classToTest.getDeclaredMethod("add", Float.class,Double.class); -// Double result = (Double) add.invoke(instanceOfClass, 7f,3d); -// assertEquals(10d, result); -// } - -// @Test -// public void testAddIntD() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method add = classToTest.getDeclaredMethod("add", Integer.class,Double.class); -// Double result = (Double) add.invoke(instanceOfClass, 7,3d); -// assertEquals(10d, result); -// } - -// @Test -// public void testAddShortD() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method add = classToTest.getDeclaredMethod("add", Short.class,Double.class); -// Short s = 7; -// Double result = (Double) add.invoke(instanceOfClass, s,3d); -// assertEquals(10d, result); -// } - -// @Test -// public void testAddByteD() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method add = classToTest.getDeclaredMethod("add", Byte.class,Double.class); -// Byte b = 7; -// Double result = (Double) add.invoke(instanceOfClass, b,3d); -// assertEquals(10d, result); -// } - -// @Test -// public void testMulInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method mulInt = classToTest.getDeclaredMethod("mulInt", Integer.class,Integer.class); -// Integer result = (Integer) mulInt.invoke(instanceOfClass, 7,3); -// assertEquals(21, result); -// } -// -// @Test -// public void testMulLong() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method mulLong = classToTest.getDeclaredMethod("mulLong", Long.class,Long.class); -// Long result = (Long) mulLong.invoke(instanceOfClass, 7L,3L); -// assertEquals(21L, result); -// } -// -// @Test -// public void testMulFloat() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method mulFloat = classToTest.getDeclaredMethod("mulFloat", Float.class,Float.class); -// Float result = (Float) mulFloat.invoke(instanceOfClass, 7f,3f); -// assertEquals(21f, result); -// } -// -// @Test -// public void testMulDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method mulDouble = classToTest.getDeclaredMethod("mulDouble", Double.class,Double.class); -// Double result = (Double) mulDouble.invoke(instanceOfClass, 7.0,3.0); -// assertEquals(21.0, result); -// } + } diff --git a/test/bytecode/PlusTest.java b/test/bytecode/PlusTest.java new file mode 100644 index 00000000..016cb706 --- /dev/null +++ b/test/bytecode/PlusTest.java @@ -0,0 +1,54 @@ +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 org.junit.BeforeClass; +import org.junit.Test; + +import de.dhbwstuttgart.core.JavaTXCompiler; + +public class PlusTest { + private static String path; + private static File fileToTest; + private static JavaTXCompiler compiler; + private static ClassLoader loader; + private static Class classToTest; + private static String pathToClassFile; + private static Object instanceOfClass; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Plus.jav"; + fileToTest = new File(path); + compiler = new JavaTXCompiler(fileToTest); + compiler.generateBytecode(); + + pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/examples/"; + loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); + classToTest = loader.loadClass("Plus"); + instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); + } + + @Test + public void testAddInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, + IllegalArgumentException, InvocationTargetException, InstantiationException { + Method addInt = classToTest.getDeclaredMethod("m", Integer.class,Integer.class); + Number result = (Number) addInt.invoke(instanceOfClass, 7,3); + assertEquals(10, result); + } + + @Test + public void testAddString() throws NoSuchMethodException, SecurityException, IllegalAccessException, + IllegalArgumentException, InvocationTargetException, InstantiationException { + Method addString = classToTest.getDeclaredMethod("m", String.class,String.class); + String result = (String) addString.invoke(instanceOfClass, "Byte","Code"); + assertEquals("ByteCode", result); + } + +} diff --git a/test/bytecode/javFiles/LamAssign.jav b/test/bytecode/javFiles/LamAssign.jav deleted file mode 100644 index 82bb31b1..00000000 --- a/test/bytecode/javFiles/LamAssign.jav +++ /dev/null @@ -1,15 +0,0 @@ -import java.lang.Integer; - -class LamAssign { - - m () { - var lam1 = (Integer x) -> { - return x; - }; - return lam1; - } -} - -interface Fun1{ - public A apply(B b); -} \ No newline at end of file diff --git a/test/bytecode/javFiles/Lambda.jav b/test/bytecode/javFiles/Lambda.jav index 378eb4d3..3aeded25 100644 --- a/test/bytecode/javFiles/Lambda.jav +++ b/test/bytecode/javFiles/Lambda.jav @@ -1,18 +1,11 @@ -class Lambda{ +import java.lang.Integer; -methode(){ - return ((f) -> f); -} -} -/* -interface Fun0{ - A apply(); -} +public class Lambda { -interface Fun1{ - A apply(B b); + m () { + var lam1 = (Integer x) -> { + return x; + }; + return lam1; + } } -*/ -interface Fun2{ - A apply(B b, C c); -} \ No newline at end of file diff --git a/test/bytecode/javFiles/Lambda4.jav b/test/bytecode/javFiles/Lambda4.jav new file mode 100644 index 00000000..378eb4d3 --- /dev/null +++ b/test/bytecode/javFiles/Lambda4.jav @@ -0,0 +1,18 @@ +class Lambda{ + +methode(){ + return ((f) -> f); +} +} +/* +interface Fun0{ + A apply(); +} + +interface Fun1{ + A apply(B b); +} +*/ +interface Fun2{ + A apply(B b, C c); +} \ No newline at end of file diff --git a/test/bytecode/javFiles/Op.jav b/test/bytecode/javFiles/Op.jav index 4aa039d5..8092d18f 100644 --- a/test/bytecode/javFiles/Op.jav +++ b/test/bytecode/javFiles/Op.jav @@ -9,103 +9,8 @@ import java.lang.Byte; public class Op { - addInt( a, b) { + m(a,b) { var c = a+b; return c; } - - -// addString(String a, String b) { -// String c = a+b; -// return c; -// } -// addLong(Long a, Long b) { -// Long c = a+b; -// return c; -// } -// addFloat(Float a, Float b) { -// Float c = a+b; -// return c; -// } -// addDouble(Double a, Double b) { -// Double c = a+b; -// return c; -// } - -// subInt(Integer a, Integer b) { -// Integer c = a-b; -// return c; -// } -// subLong(Long a, Long b) { -// Long c = a-b; -// return c; -// } -// subFloat(Float a, Float b) { -// Float c = a-b; -// return c; -// } -// subDouble(Double a, Double b) { -// Double c = a-b; -// return c; -// } - -// Long add(Integer a, Long b) { -// Long c = a+b; -// return c; -// } - -// mulInt(Integer a, Integer b) { -// Integer c = a*b; -// return c; -// } -// -// mulLong(Long a, Long b) { -// Long c = a*b; -// return c; -// } -// -// mulFloat(Float a, Float b) { -// Float c = a*b; -// return c; -// } -// -// mulDouble(Double a, Double b) { -// Double c = a*b; -// return c; -// } - -// add(Double a, Long b) { -// Double c = a+b; -// return c; -// } - -// add(Integer a, Short b) { -// Integer c = a+b; -// return c; -// } -// -// add(Integer a, Byte b) { -// Integer c = a+b; -// return c; -// } - -// add(Float a, Double b) { -// Double c = a+b; -// return c; -//} -// -// add(Integer a, Double b) { -// Double c = a+b; -// return c; -// } - -// add(Short a, Double b) { -// Double c = a+b; -// return c; -// } - -// add(Byte a, Double b) { -// Double c = a+b; -// return c; -// } } \ No newline at end of file diff --git a/test/bytecode/javFiles/Plus.jav b/test/bytecode/javFiles/Plus.jav new file mode 100644 index 00000000..4d425bf0 --- /dev/null +++ b/test/bytecode/javFiles/Plus.jav @@ -0,0 +1,8 @@ +import java.lang.Integer; + +public class Plus { + + m(a,b) { + return a+b; + } +} \ No newline at end of file