Skip to content

Java: Add CodeQL class (or predicate) for Text Blocks #6619

@Marcono1234

Description

@Marcono1234

In some cases it might be useful to differentiate between regular string literals and Text Blocks (""" ... """).

CodeQL already seems to support Text Blocks and models them as StringLiteral.
Based on the JLS Text Blocks are not string literals (e.g. it says "Unlike in a string literal ..."), however for simplicity it probably makes sense to keep modelling them as StringLiteral anyways. But it would be good to adjust the StringLiteral documentation then.

It would therefore be useful to either have a predicate (e.g. StringLiteral.isTextBlock()) or a separate class TextBlock which extends StringLiteral.
Detection is already possible by checking if StringLiteral.getLiteral() starts with (or ends with) """ (or checking if the literal contains line breaks, which is not possible for regular string literals). However, that is slightly error-prone because (despite being unlikely) the Java source code could use Unicode escapes (which are replaced during pre-processing) for the " and then the getLiteral() check would fail:

// Unicode escapes for """
String s = \u0022\uuuu0022\u0022
    test
    """;

So the sanest solution might be to introduce a database type or predicate eventually.

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    JavaquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions