diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index e17f545b..32570541 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -340,7 +340,7 @@ public class BytecodeGenMethod implements StatementVisitor { } private void doVisitRelOpInsn(Operator op, String typeOfBinary, Label branchLabel, Label endLabel) { - + System.out.println("TypeOfBinary: " + typeOfBinary); switch (typeOfBinary) { case "java/lang/Long": mv.visitInsn(Opcodes.LCMP); @@ -970,6 +970,7 @@ public class BytecodeGenMethod implements StatementVisitor { public void visit(Literal literal) { Object value = literal.value; String typeOfLiteral = getResolvedType(literal.getType()); + System.out.println("typeOfLiteral :=> "+ typeOfLiteral); // Der Wert des Literals wird auf den Stack geladen und // geboxt, wenn es nötig ist. loadValue(typeOfLiteral, value, false); @@ -1118,8 +1119,10 @@ public class BytecodeGenMethod implements StatementVisitor { } private void visitLongLiteral(Long value, boolean isLong) { - if (value < Math.pow(2, 15) || (value >= -Math.pow(2, 15)) && value < -128) { - visitShortLiteral(value.shortValue(), isLong); + if (value == 0) { + mv.visitInsn(Opcodes.LCONST_0); + } else if (value == 1) { + mv.visitInsn(Opcodes.LCONST_1); } else { mv.visitLdcInsn(value); } diff --git a/test/bytecode/BinaryTest.java b/test/bytecode/BinaryTest.java index 07a70d54..543786ad 100644 --- a/test/bytecode/BinaryTest.java +++ b/test/bytecode/BinaryTest.java @@ -3,6 +3,7 @@ package bytecode; import static org.junit.Assert.*; import java.io.File; +import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; @@ -33,8 +34,10 @@ public class BinaryTest { } @Test - public void test() { - fail("Not yet implemented"); + public void test() throws Exception { + Method m2 = classToTest.getDeclaredMethod("m2", Integer.class,Integer.class); + Integer res = (Integer) m2.invoke(instanceOfClass, 2,3); + assertEquals(6, res); } } diff --git a/test/bytecode/FacTest.java b/test/bytecode/FacTest.java index f5dd86ea..387d7387 100644 --- a/test/bytecode/FacTest.java +++ b/test/bytecode/FacTest.java @@ -33,12 +33,12 @@ public class FacTest { classToTest = loader.loadClass("Fac"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); } - + @Test - public void test() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + public void testInteger() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Method getFac = classToTest.getDeclaredMethod("getFac", Integer.class); - Double result = (Double) getFac.invoke(instanceOfClass,3); - assertEquals(result, 6.0); + Integer result = (Integer) getFac.invoke(instanceOfClass,3); + assertEquals(result, 6); } } diff --git a/test/bytecode/GenTest.java b/test/bytecode/GenTest.java index e8ca6938..3ed2f3c3 100644 --- a/test/bytecode/GenTest.java +++ b/test/bytecode/GenTest.java @@ -16,26 +16,15 @@ public class GenTest { 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 { + @Test + public void generateBC() throws Exception { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Gen.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; compiler.generateBytecode(pathToClassFile); - loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); - classToTest = loader.loadClass("Gen"); - instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); - } - - @Test - public void test() { - fail("Not yet implemented"); } } diff --git a/test/bytecode/StaticTest.java b/test/bytecode/StaticTest.java deleted file mode 100644 index 98642b4e..00000000 --- a/test/bytecode/StaticTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package bytecode; - -import static org.junit.Assert.*; - -import java.io.File; -import java.net.URL; -import java.net.URLClassLoader; - -import org.junit.BeforeClass; -import org.junit.Test; - -import de.dhbwstuttgart.core.JavaTXCompiler; - -public class StaticTest { - - 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/StaticM.jav"; - fileToTest = new File(path); - compiler = new JavaTXCompiler(fileToTest); - pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; - compiler.generateBytecode(pathToClassFile); - loader = new URLClassLoader(new URL[] {new URL("file://" + pathToClassFile)}); - classToTest = loader.loadClass("StaticM"); - instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); - } - - @Test - public void test() { - fail("Not yet implemented"); - } - -} diff --git a/test/bytecode/Tph3Test.java b/test/bytecode/Tph3Test.java index 64bfa28f..58bc9e64 100644 --- a/test/bytecode/Tph3Test.java +++ b/test/bytecode/Tph3Test.java @@ -17,53 +17,15 @@ public class Tph3Test { 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 { + @Test + public void generateBC() throws Exception { path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Tph3.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; compiler.generateBytecode(pathToClassFile); - loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); - classToTest = loader.loadClass("Tph3"); - instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); - } - - @Test - public void test1() throws Exception { - Method m = classToTest.getDeclaredMethod("m", Object.class, Object.class); - Object result = m.invoke(instanceOfClass, 1,2); - - assertEquals(1,result); - } - - @Test - public void test2() throws Exception { - Method m = classToTest.getDeclaredMethod("m", Object.class, Object.class); - Object result = m.invoke(instanceOfClass, "sss",2); - - assertEquals("sss",result); - } - - @Test - public void test3() throws Exception { - Method m = classToTest.getDeclaredMethod("m2", Object.class, Object.class); - Object result = m.invoke(instanceOfClass, 1,2); - - assertEquals(2,result); - } - - @Test - public void test4() throws Exception { - Method m = classToTest.getDeclaredMethod("m2", Object.class, Object.class); - Object result = m.invoke(instanceOfClass, 1,"xxx"); - - assertEquals("xxx",result); } } diff --git a/test/bytecode/WhileTest.java b/test/bytecode/WhileTest.java index b3020ffe..67c80c19 100644 --- a/test/bytecode/WhileTest.java +++ b/test/bytecode/WhileTest.java @@ -40,5 +40,19 @@ public class WhileTest { Integer result = (Integer) m.invoke(instanceOfClass, 0); assertEquals(2, result); } + + @Test + public void testDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Method m = classToTest.getDeclaredMethod("m", Double.class); + Double result = (Double) m.invoke(instanceOfClass, 0.0); + assertEquals(2.0, result); + } + + @Test + public void testLong() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Method m = classToTest.getDeclaredMethod("m", Long.class); + Long result = (Long) m.invoke(instanceOfClass, 0l); + assertEquals(2l, result); + } } diff --git a/test/bytecode/javFiles/BinaryInMeth.jav b/test/bytecode/javFiles/BinaryInMeth.jav index f5a11798..c77d7767 100644 --- a/test/bytecode/javFiles/BinaryInMeth.jav +++ b/test/bytecode/javFiles/BinaryInMeth.jav @@ -1,4 +1,6 @@ import java.lang.Integer; +import java.lang.Double; + public class BinaryInMeth { m(a){ diff --git a/test/bytecode/javFiles/Fac.jav b/test/bytecode/javFiles/Fac.jav index eb2e6d61..28650b45 100644 --- a/test/bytecode/javFiles/Fac.jav +++ b/test/bytecode/javFiles/Fac.jav @@ -1,6 +1,5 @@ import java.lang.Integer; -import java.lang.Long; -import java.lang.Double; +//import java.lang.Double; public class Fac { diff --git a/test/bytecode/javFiles/Plus.jav b/test/bytecode/javFiles/Plus.jav index 4d425bf0..1cdecc8c 100644 --- a/test/bytecode/javFiles/Plus.jav +++ b/test/bytecode/javFiles/Plus.jav @@ -1,4 +1,5 @@ import java.lang.Integer; +import java.lang.String; public class Plus { diff --git a/test/bytecode/javFiles/While.jav b/test/bytecode/javFiles/While.jav index f0dc7296..cd2139a6 100644 --- a/test/bytecode/javFiles/While.jav +++ b/test/bytecode/javFiles/While.jav @@ -1,11 +1,9 @@ import java.lang.Integer; -// wenn nur ein Import da steht,wird die Type von -// dem Literal 2 Number berechnet => Deswegen kann -// nicht auf den Stack geladen. import java.lang.Long; +import java.lang.Double; public class While { - Integer m(x) { + m(x) { while(x < 2) { x = x+1; }