Fix #303
All checks were successful
Build and Test with Maven / Build-and-test-with-Maven (push) Successful in 2m37s

This commit is contained in:
Daniel Holle 2024-03-25 15:10:47 +01:00
parent 43da2ffbdc
commit 7f3c1686ec
3 changed files with 13 additions and 2 deletions

View File

@ -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; }
}

View File

@ -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());

View File

@ -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