3434import java .security .MessageDigest ;
3535import java .security .NoSuchAlgorithmException ;
3636import java .time .Duration ;
37- import java .util .ArrayList ;
38- import java .util .Base64 ;
39- import java .util .List ;
40- import java .util .Optional ;
37+ import java .util .*;
4138import java .util .stream .Collectors ;
4239
40+ import static java .util .Objects .requireNonNull ;
4341import static org .openrewrite .Tree .randomId ;
4442
4543@ Value
@@ -50,7 +48,6 @@ public class UseTextBlocks extends Recipe {
5048 "The default value is true." ,
5149 example = "true" ,
5250 required = false )
53- @ Nullable
5451 boolean convertStringsWithoutNewlines ;
5552
5653 public UseTextBlocks () {
@@ -80,7 +77,7 @@ public String getDescription() {
8077 public TreeVisitor <?, ExecutionContext > getVisitor () {
8178 TreeVisitor <?, ExecutionContext > preconditions = Preconditions .and (
8279 Preconditions .not (new KotlinFileChecker <>()),
83- new HasJavaVersion ("17 " , true ).getVisitor ()
80+ new HasJavaVersion ("[15,) " , null ).getVisitor ()
8481 );
8582 return Preconditions .check (preconditions , new JavaVisitor <ExecutionContext >() {
8683 @ Override
@@ -125,13 +122,13 @@ private J.Literal toTextBlock(J.Binary binary, String content, List<J.Literal> s
125122
126123 StringBuilder sb = new StringBuilder ();
127124 StringBuilder originalContent = new StringBuilder ();
128- stringLiterals = stringLiterals .stream ().filter (s -> !s .getValue ().toString ().isEmpty ()).collect (Collectors .toList ());
125+ stringLiterals = stringLiterals .stream ().filter (s -> s . getValue () != null && !s .getValue ().toString ().isEmpty ()).collect (Collectors .toList ());
129126 for (int i = 0 ; i < stringLiterals .size (); i ++) {
130- String s = stringLiterals .get (i ).getValue ().toString ();
127+ String s = requireNonNull ( stringLiterals .get (i ).getValue () ).toString ();
131128 sb .append (s );
132129 originalContent .append (s );
133130 if (i != stringLiterals .size () - 1 ) {
134- String nextLine = stringLiterals .get (i + 1 ).getValue ().toString ();
131+ String nextLine = requireNonNull ( stringLiterals .get (i + 1 ).getValue () ).toString ();
135132 char nextChar = nextLine .charAt (0 );
136133 if (!s .endsWith ("\n " ) && nextChar != '\n' ) {
137134 sb .append (passPhrase );
@@ -202,7 +199,7 @@ private static boolean flatAdditiveStringLiterals(Expression expression,
202199 } else if (isRegularStringLiteral (expression )) {
203200 J .Literal l = (J .Literal ) expression ;
204201 stringLiterals .add (l );
205- contentSb .append (l .getValue (). toString ( ));
202+ contentSb .append (requireNonNull ( l .getValue ()));
206203 concatenationSb .append (l .getPrefix ().getWhitespace ()).append ("-" );
207204 return true ;
208205 }
@@ -296,13 +293,12 @@ private static int[] shortestPrefixAfterNewline(String concatenation, int tabSiz
296293
297294 private static String generatePassword (String originalStr ) throws NoSuchAlgorithmException {
298295 final String SALT = "kun" ;
299- String password = "" ;
300296 String saltedStr = originalStr + SALT ;
301297
302298 MessageDigest md = MessageDigest .getInstance ("SHA-256" );
303299 byte [] hashBytes = md .digest (saltedStr .getBytes ());
304300
305- password = Base64 .getEncoder ().encodeToString (hashBytes );
301+ String password = Base64 .getEncoder ().encodeToString (hashBytes );
306302
307303 while (originalStr .contains (password )) {
308304 hashBytes = md .digest (password .getBytes ());
0 commit comments