diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTLiteralExpression.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTLiteralExpression.java index b3011ff47a..047d4b413c 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTLiteralExpression.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTLiteralExpression.java @@ -4,6 +4,8 @@ package net.sourceforge.pmd.lang.apex.ast; +import net.sourceforge.pmd.lang.document.Chars; + import com.google.summit.ast.expression.LiteralExpression; import com.google.summit.ast.expression.VariableExpression; @@ -38,7 +40,12 @@ public final class ASTLiteralExpression extends AbstractApexNode.Single -1 || source.indexOf('D') > -1) { + return LiteralType.DOUBLE; + } else { + return LiteralType.DECIMAL; + } } else if (node instanceof LiteralExpression.LongVal) { return LiteralType.LONG; /* TODO(b/239648780): the parser must distinguish decimal vs. double @@ -88,8 +95,8 @@ public final class ASTLiteralExpression extends AbstractApexNode.Single - * For example, in the Apex code + * + *

For example, in the Apex code *

{@code
      * new X(a = 1, b = 2)
      * }
diff --git a/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/ast/ASTLiteralExpressionTest.java b/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/ast/ASTLiteralExpressionTest.java new file mode 100644 index 0000000000..c249c8df5a --- /dev/null +++ b/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/ast/ASTLiteralExpressionTest.java @@ -0,0 +1,47 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.apex.ast; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +class ASTLiteralExpressionTest extends ApexParserTestBase { + + @Test + void doubleLiteral() { + ASTLiteralExpression literal = createLiteral("1.2d"); + assertTrue(literal.isDouble()); + assertEquals(ASTLiteralExpression.LiteralType.DOUBLE, literal.getLiteralType()); + assertEquals("1.2", literal.getImage()); + + literal = createLiteral("1.2D"); + assertTrue(literal.isDouble()); + assertEquals(ASTLiteralExpression.LiteralType.DOUBLE, literal.getLiteralType()); + } + + @Test + void decimalLiteral() { + ASTLiteralExpression literal = createLiteral("1.2"); + assertTrue(literal.isDecimal()); + assertEquals(ASTLiteralExpression.LiteralType.DECIMAL, literal.getLiteralType()); + assertEquals("1.2", literal.getImage()); + } + + @Test + void stringLiteral() { + ASTLiteralExpression literal = createLiteral("'foo'"); + assertTrue(literal.isString()); + assertEquals(ASTLiteralExpression.LiteralType.STRING, literal.getLiteralType()); + assertEquals("foo", literal.getImage()); + } + + private ASTLiteralExpression createLiteral(String code) { + return parse("class Foo { object field = " + code + "; }") + .descendants(ASTLiteralExpression.class) + .first(); + } +}