56 lines
1.5 KiB
Plaintext
56 lines
1.5 KiB
Plaintext
In Java 15 Text Blocks are official and can be used just like an ordinary String. However, when they are used to represent a big chunk of text, they should not be used directly in complex expressions, as it decreases the readability. In this case, it is better to extract the text block into a variable or a field.
|
|
|
|
|
|
This rule reports an issue when a text block longer than a number of lines given as a parameter is directly used within a lambda expression.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
listOfString.stream()
|
|
.map(str -> !"""
|
|
<project>
|
|
<modelVersion>4.0.0</modelVersion>
|
|
<parent>
|
|
<groupId>com.mycompany.app</groupId>
|
|
<artifactId>my-app</artifactId>
|
|
<version>1</version>
|
|
</parent>
|
|
|
|
<groupId>com.mycompany.app</groupId>
|
|
<artifactId>my-module</artifactId>
|
|
<version>1</version>
|
|
</project>
|
|
""".equals(str));
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
String myTextBlock = """
|
|
<project>
|
|
<modelVersion>4.0.0</modelVersion>
|
|
<parent>
|
|
<groupId>com.mycompany.app</groupId>
|
|
<artifactId>my-app</artifactId>
|
|
<version>1</version>
|
|
</parent>
|
|
|
|
<groupId>com.mycompany.app</groupId>
|
|
<artifactId>my-module</artifactId>
|
|
<version>1</version>
|
|
</project>
|
|
""";
|
|
|
|
listOfString.stream()
|
|
.map(str -> !myTextBlock.equals(str));
|
|
----
|
|
|
|
|
|
== See
|
|
|
|
* https://openjdk.java.net/jeps/378[JEP 378: Text Blocks]
|
|
* https://cr.openjdk.java.net/~jlaskey/Strings/TextBlocksGuide_v9.html[Programmer's Guide To Text Blocks], by Jim Laskey and Stuart Marks
|
|
|