Uploaded image for project: 'SonarJava'
  1. SonarJava
  2. SONARJAVA-3736

Support Text Block in rules relying on String literals from expressions


    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 6.15
    • Component/s: Rules
    • Labels:


      Text blocks (added in Java 15) can be used anywhere you would expect a String literal. It makes sense for our rules to support this as well. As an example for RSPEC-5547, we can write code as follows:

      private static final String DES_STRING = "DES";
      private static final String DES_TEXT_BLOCK = """
      // ...
      Cipher.getInstance(DES_STRING); // Raises an issue
      Cipher.getInstance(DES_TEXT_BLOCK); // Does not raise an issue, despite the fact that it is equivalent to the line above!

      While it does not make sense to support Text block in all possible situations (it is a huge effort with only little value because most of the time using a string literal makes more sense, and RSPEC-5663 will report an intermediate issue if needed), supporting it for rules relying on asConstant(String.class) is a quick win.
      For these rules, we should make sure to report an issue independently of the way you declared the String.


      In many rules, we rely on .asConstant(String.class) method to recover the value as a String from an expression. It's a powerful method since the expression can be a literal or an identifier from a final or effectively final variable. It makes sense to extend it to support returning the value of the text block. By doing this, we will transparently support text blocks in all rules relying on this method.


          Issue Links



              • Assignee:
                quentin.jaquier Quentin Jaquier
                quentin.jaquier Quentin Jaquier
              • Votes:
                0 Vote for this issue
                1 Start watching this issue


                • Due: