From 7f3c1686ec9a9597906254a87675844103c1729d Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Mon, 25 Mar 2024 15:10:47 +0100 Subject: [PATCH] Fix #303 --- resources/bytecode/javFiles/Literal.jav | 2 ++ .../SyntaxTreeGenerator/StatementGenerator.java | 11 +++++++++-- src/test/java/TestComplete.java | 2 ++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/resources/bytecode/javFiles/Literal.jav b/resources/bytecode/javFiles/Literal.jav index eee2caa8..ae0d1a06 100644 --- a/resources/bytecode/javFiles/Literal.jav +++ b/resources/bytecode/javFiles/Literal.jav @@ -3,4 +3,6 @@ import java.lang.Character; public class Literal { public m() { return null; } public m2() { return 'C'; } + public m3() { return 10L; } + public m4() { return 10.5F; } } \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 829ba9e6..26c9728c 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -1044,10 +1044,17 @@ public class StatementGenerator { private Expression convert(Java17Parser.LiteralContext literal) { switch (literal) { case IntLiteralContext intliteral: - Number value = Integer.parseInt(intliteral.getText()); + Number value; + if (intliteral.getText().endsWith("l") || intliteral.getText().endsWith("L")) + value = Long.parseLong(intliteral.getText().substring(0, intliteral.getText().length() - 1)); + else value = Integer.parseInt(intliteral.getText()); return new Literal(TypePlaceholder.fresh(literal.getStart()), value, intliteral.getStart()); case FltLiteralContext floatliteral: - value = Double.parseDouble(floatliteral.getText()); + if (floatliteral.getText().endsWith("f") || floatliteral.getText().endsWith("F")) + value = Float.parseFloat(floatliteral.getText().substring(0, floatliteral.getText().length() - 1)); + else if (floatliteral.getText().endsWith("d") || floatliteral.getText().endsWith("D")) + value = Double.parseDouble(floatliteral.getText().substring(0, floatliteral.getText().length() - 1)); + else value = Double.parseDouble(floatliteral.getText()); return new Literal(TypePlaceholder.fresh(literal.getStart()), value, floatliteral.getStart()); case CharLiteralContext charliteral: RefType type = new RefType(reg.getName("java.lang.Character"), charliteral.getStart()); diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index a9191989..06a99ca7 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -825,6 +825,8 @@ public class TestComplete { assertNull(clazz.getDeclaredMethod("m").invoke(instance)); assertEquals(clazz.getDeclaredMethod("m2").invoke(instance), 'C'); + assertEquals(clazz.getDeclaredMethod("m3").invoke(instance), 10L); + assertEquals(clazz.getDeclaredMethod("m4").invoke(instance), 10.5F); } @Test