diff --git a/rewrite-core/src/main/java/org/openrewrite/internal/StringUtils.java b/rewrite-core/src/main/java/org/openrewrite/internal/StringUtils.java index 08c50b3d28e..c84f3fe7c0e 100644 --- a/rewrite-core/src/main/java/org/openrewrite/internal/StringUtils.java +++ b/rewrite-core/src/main/java/org/openrewrite/internal/StringUtils.java @@ -103,13 +103,11 @@ public static String trimIndent(String text) { if (end == start) { end++; } - char[] charArray = text.substring(start, end).toCharArray(); - StringBuilder trimmed = new StringBuilder(); - for (int i = 0; i < charArray.length; i++) { + for (int i = start; i < end; i++) { int j = i; - for (; j < charArray.length; j++) { - char c = charArray[j]; + for (; j < end; j++) { + char c = text.charAt(j); if (c == '\r' || c == '\n') { trimmed.append(c); break; @@ -139,7 +137,8 @@ private static int minCommonIndentLevel(String text) { int minIndent = Integer.MAX_VALUE; int whiteSpaceCount = 0; boolean contentEncountered = false; - for (char c : text.toCharArray()) { + for (int i = 0; i < text.length(); i++) { + char c = text.charAt(i); if (c == '\n' || c == '\r') { if (contentEncountered) { minIndent = Math.min(whiteSpaceCount, minIndent); @@ -565,7 +564,8 @@ private static boolean different(boolean caseSensitive, char ch, char other) { public static String indent(String text) { StringBuilder indent = new StringBuilder(); - for (char c : text.toCharArray()) { + for (int i = 0; i < text.length(); i++) { + char c = text.charAt(i); if (c == '\n' || c == '\r') { return indent.toString(); } else if (Character.isWhitespace(c)) { diff --git a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java index ef5aea9f48f..5152b98fb7c 100644 --- a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java +++ b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java @@ -88,8 +88,6 @@ public ReloadableJava17JavadocVisitor(Context context, TreePath scope, Reloadabl } private void init() { - char[] sourceArr = source.toCharArray(); - StringBuilder firstPrefixBuilder = new StringBuilder(); StringBuilder javadocContent = new StringBuilder(); StringBuilder marginBuilder = null; @@ -98,12 +96,12 @@ private void init() { // skip past the opening '/**' int i = 3; - for (; i < sourceArr.length; i++) { - char c = sourceArr[i]; + for (; i < source.length(); i++) { + char c = source.charAt(i); if (inFirstPrefix) { // '*' characters are considered a part of the margin until a non '*' is parsed. if (Character.isWhitespace(c) || c == '*' && isPrefixAsterisk) { - if (isPrefixAsterisk && i + 1 <= sourceArr.length - 1 && sourceArr[i + 1] != '*') { + if (isPrefixAsterisk && i + 1 <= source.length() - 1 && source.charAt(i + 1) != '*') { isPrefixAsterisk = false; } firstPrefixBuilder.append(c); @@ -118,30 +116,31 @@ private void init() { } if (c == '\n') { + char prev = source.charAt(i - 1); if (inFirstPrefix) { firstPrefix = firstPrefixBuilder.toString(); inFirstPrefix = false; } else { // Handle consecutive new lines. - if ((sourceArr[i - 1] == '\n' || - sourceArr[i - 1] == '\r' && sourceArr[i - 2] == '\n')) { - String prevLineLine = sourceArr[i - 1] == '\n' ? "\n" : "\r\n"; + if ((prev == '\n' || + prev == '\r' && source.charAt(i - 2) == '\n')) { + String prevLineLine = prev == '\n' ? "\n" : "\r\n"; lineBreaks.put(javadocContent.length(), new Javadoc.LineBreak(randomId(), prevLineLine, Markers.EMPTY)); } else if (marginBuilder != null) { // A new line with no '*' that only contains whitespace. - String newLine = sourceArr[i - 1] == '\r' ? "\r\n" : "\n"; + String newLine = prev == '\r' ? "\r\n" : "\n"; lineBreaks.put(javadocContent.length(), new Javadoc.LineBreak(randomId(), newLine, Markers.EMPTY)); javadocContent.append(marginBuilder.substring(marginBuilder.indexOf("\n") + 1)); } javadocContent.append(c); } - String newLine = sourceArr[i - 1] == '\r' ? "\r\n" : "\n"; + String newLine = prev == '\r' ? "\r\n" : "\n"; marginBuilder = new StringBuilder(newLine); } else if (marginBuilder != null) { if (!Character.isWhitespace(c)) { if (c == '*') { // '*' characters are considered a part of the margin until a non '*' is parsed. marginBuilder.append(c); - if (i + 1 <= sourceArr.length - 1 && sourceArr[i + 1] != '*') { + if (i + 1 <= source.length() - 1 && source.charAt(i + 1) != '*') { lineBreaks.put(javadocContent.length(), new Javadoc.LineBreak(randomId(), marginBuilder.toString(), Markers.EMPTY)); marginBuilder = null; @@ -152,7 +151,7 @@ private void init() { marginBuilder.toString(), Markers.EMPTY)); javadocContent.append(c); } else { - String newLine = marginBuilder.toString().startsWith("\r") ? "\r\n" : "\n"; + String newLine = marginBuilder.charAt(0) == '\r' ? "\r\n" : "\n"; lineBreaks.put(javadocContent.length(), new Javadoc.LineBreak(randomId(), newLine, Markers.EMPTY)); String margin = marginBuilder.toString(); @@ -864,9 +863,9 @@ public List visitText(String node) { node = node.stripLeading(); } - char[] textArr = node.toCharArray(); StringBuilder text = new StringBuilder(); - for (char c : textArr) { + for (int i = 0; i < node.length(); i++) { + char c = node.charAt(i); cursor++; if (c == '\n') { if (text.length() > 0) { diff --git a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java index 0ddd7bd2493..778dec66d3a 100644 --- a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java +++ b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java @@ -835,11 +835,10 @@ public J visitLiteral(LiteralTree node, Space fmt) { List unicodeEscapes = null; int i = 0; - char[] valueSourceArr = valueSource.toCharArray(); - for (int j = 0; j < valueSourceArr.length; j++) { - char c = valueSourceArr[j]; - if (c == '\\' && j < valueSourceArr.length - 1 && (j == 0 || valueSourceArr[j - 1] != '\\')) { - if (valueSourceArr[j + 1] == 'u' && j < valueSource.length() - 5) { + for (int j = 0; j < valueSource.length(); j++) { + char c = valueSource.charAt(j); + if (c == '\\' && j < valueSource.length() - 1 && (j == 0 || valueSource.charAt(j - 1) != '\\')) { + if (valueSource.charAt(j + 1) == 'u' && j < valueSource.length() - 5) { String codePoint = valueSource.substring(j + 2, j + 6); int codePointNumeric = Integer.parseInt(codePoint, 16); if (codePointNumeric >= SURR_FIRST && codePointNumeric <= SURR_LAST) { diff --git a/rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java b/rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java index 9764f0856ff..db21d010b73 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java @@ -707,8 +707,9 @@ public J visitLiteral(Literal literal, PrintOutputCapture

p) { } } - char[] valueSourceArr = literal.getValueSource().toCharArray(); - for (char c : valueSourceArr) { + String valueSource = literal.getValueSource(); + for (int j = 0; j < valueSource.length(); j++) { + char c = valueSource.charAt(j); p.append(c); if (surrogate != null && surrogate.getValueSourceIndex() == ++i) { while (surrogate != null && surrogate.getValueSourceIndex() == i) { diff --git a/rewrite-java/src/main/java/org/openrewrite/java/JavadocPrinter.java b/rewrite-java/src/main/java/org/openrewrite/java/JavadocPrinter.java index af78d3099c6..242f4485648 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/JavadocPrinter.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/JavadocPrinter.java @@ -438,8 +438,10 @@ public Space visitSpace(Space space, Space.Location loc, PrintOutputCapture

p List lineBreaks = getCursor().getNearestMessage("JAVADOC_LINE_BREAKS"); Integer index = getCursor().getNearestMessage("JAVADOC_LINE_BREAK_INDEX"); - if (lineBreaks != null && !lineBreaks.isEmpty() && index != null && space.getWhitespace().contains("\n")) { - for (char c : space.getWhitespace().toCharArray()) { + String whitespace = space.getWhitespace(); + if (lineBreaks != null && !lineBreaks.isEmpty() && index != null && whitespace.contains("\n")) { + for (int i = 0; i < whitespace.length(); i++) { + char c = whitespace.charAt(i); // The Space from a JavaDoc will not contain a CR because the JavaDoc parser // filters out other new line characters. CRLF is detected through the source // and only exists through LineBreaks. @@ -452,7 +454,7 @@ public Space visitSpace(Space space, Space.Location loc, PrintOutputCapture

p } getCursor().putMessageOnFirstEnclosing(Javadoc.DocComment.class, "JAVADOC_LINE_BREAK_INDEX", index); } else { - p.append(space.getWhitespace()); + p.append(whitespace); } return space; } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/BlankLinesVisitor.java b/rewrite-java/src/main/java/org/openrewrite/java/format/BlankLinesVisitor.java index 7f1e7c94b00..42d84f12a05 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/BlankLinesVisitor.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/BlankLinesVisitor.java @@ -302,7 +302,8 @@ private String minimumLines(String whitespace, int min) { private static int getNewLineCount(String whitespace) { int newLineCount = 0; - for (char c : whitespace.toCharArray()) { + for (int i = 0; i < whitespace.length(); i++) { + char c = whitespace.charAt(i); if (c == '\n') { newLineCount++; } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeLineBreaksVisitor.java b/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeLineBreaksVisitor.java index d5992948934..4ac95c1f162 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeLineBreaksVisitor.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeLineBreaksVisitor.java @@ -71,9 +71,8 @@ private static String normalizeNewLines(String text, boolean useCrlf) { return text; } StringBuilder normalized = new StringBuilder(); - char[] charArray = text.toCharArray(); - for (int i = 0; i < charArray.length; i++) { - char c = charArray[i]; + for (int i = 0; i < text.length(); i++) { + char c = text.charAt(i); if (useCrlf && c == '\n' && (i == 0 || text.charAt(i - 1) != '\r')) { normalized.append('\r').append('\n'); } else if (useCrlf || c != '\r') { diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeTabsOrSpacesVisitor.java b/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeTabsOrSpacesVisitor.java index ee86d4410bc..01b916751e6 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeTabsOrSpacesVisitor.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/NormalizeTabsOrSpacesVisitor.java @@ -83,10 +83,9 @@ private String normalize(String text, boolean isComment) { StringBuilder textBuilder = new StringBuilder(); int consecutiveSpaces = 0; boolean inMargin = true; - char[] charArray = text.toCharArray(); outer: - for (int i = 0; i < charArray.length; i++) { - char c = charArray[i]; + for (int i = 0; i < text.length(); i++) { + char c = text.charAt(i); if (c == '\n' || c == '\r') { inMargin = true; consecutiveSpaces = 0; @@ -94,10 +93,10 @@ private String normalize(String text, boolean isComment) { } else if (!inMargin) { textBuilder.append(c); } else if (style.getUseTabCharacter() && c == ' ' && (!isComment || - (i + 1 < charArray.length && charArray[i + 1] != '*'))) { + (i + 1 < text.length() && text.charAt(i + 1) != '*'))) { int j = i + 1; - for (; j < charArray.length && j < style.getTabSize(); j++) { - if (charArray[j] != ' ') { + for (; j < text.length() && j < style.getTabSize(); j++) { + if (text.charAt(j) != ' ') { continue outer; } } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/RemoveTrailingWhitespaceVisitor.java b/rewrite-java/src/main/java/org/openrewrite/java/format/RemoveTrailingWhitespaceVisitor.java index 2ea7d7c2710..77651a80a81 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/RemoveTrailingWhitespaceVisitor.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/RemoveTrailingWhitespaceVisitor.java @@ -40,7 +40,8 @@ public RemoveTrailingWhitespaceVisitor() { public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, P p) { String eof = cu.getEof().getWhitespace(); StringBuilder builder = new StringBuilder(); - for (char c : eof.toCharArray()) { + for (int i = 0; i < eof.length(); i++) { + char c = eof.charAt(i); if (c == '\n' || c == '\r') { builder.appendCodePoint(c); } @@ -53,13 +54,13 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, P p) { @Override public Space visitSpace(Space space, Space.Location loc, P p) { Space s = space; - int lastNewline = s.getWhitespace().lastIndexOf('\n'); + String whitespace = s.getWhitespace(); + int lastNewline = whitespace.lastIndexOf('\n'); // Skip import prefixes, leave those up to OrderImports which better understands that domain if (lastNewline > 0 && loc != Space.Location.IMPORT_PREFIX) { StringBuilder ws = new StringBuilder(); - char[] charArray = s.getWhitespace().toCharArray(); - for (int i = 0; i < charArray.length; i++) { - char c = charArray[i]; + for (int i = 0; i < whitespace.length(); i++) { + char c = whitespace.charAt(i); if (i >= lastNewline) { ws.append(c); } else if (c == ',' || c == '\r' || c == '\n') { diff --git a/rewrite-java/src/main/java/org/openrewrite/java/format/TabsAndIndentsVisitor.java b/rewrite-java/src/main/java/org/openrewrite/java/format/TabsAndIndentsVisitor.java index 0ce41b00117..a4effda558d 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/format/TabsAndIndentsVisitor.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/format/TabsAndIndentsVisitor.java @@ -475,7 +475,8 @@ private Space indentTo(Space space, int column, Space.Location spaceLocation) { private Comment indentComment(Comment comment, String priorSuffix, int column) { if (comment instanceof TextComment) { TextComment textComment = (TextComment) comment; - if (!textComment.getText().contains("\n")) { + String text = textComment.getText(); + if (!text.contains("\n")) { return comment; } @@ -489,9 +490,8 @@ private Comment indentComment(Comment comment, String priorSuffix, int column) { String newMargin = indent(margin, shift); if (textComment.isMultiline()) { StringBuilder multiline = new StringBuilder(); - char[] chars = textComment.getText().toCharArray(); - for (int i = 0; i < chars.length; i++) { - char c = chars[i]; + for (int i = 0; i < text.length(); i++) { + char c = text.charAt(i); if (c == '\n') { multiline.append(c).append(newMargin); i += margin.length(); @@ -504,12 +504,11 @@ private Comment indentComment(Comment comment, String priorSuffix, int column) { } else if (shift < 0) { if (textComment.isMultiline()) { StringBuilder multiline = new StringBuilder(); - char[] chars = textComment.getText().toCharArray(); - for (int i = 0; i < chars.length; i++) { - char c = chars[i]; + for (int i = 0; i < text.length(); i++) { + char c = text.charAt(i); if (c == '\n') { multiline.append(c); - for (int j = 0; j < Math.abs(shift) && i+j+1 < chars.length && (chars[j + i + 1] == ' ' || chars[j + i + 1] == '\t'); j++) { + for (int j = 0; j < Math.abs(shift) && i+j+1 < text.length() && (text.charAt(j + i + 1) == ' ' || text.charAt(j + i + 1) == '\t'); j++) { i++; } } else { @@ -585,7 +584,8 @@ private int getLengthOfWhitespace(@Nullable String whitespace) { } int size = 0; - for (char c : whitespace.toCharArray()) { + for (int i = 0; i < whitespace.length(); i++) { + char c = whitespace.charAt(i); size += c == '\t' ? style.getTabSize() : 1; if (c == '\n' || c == '\r') { size = 0; @@ -604,7 +604,9 @@ private int forInitColumn() { int column = 0; boolean afterInitStart = false; - for (char c : alignTo.print(getCursor()).toCharArray()) { + String print = alignTo.print(getCursor()); + for (int i = 0; i < print.length(); i++) { + char c = print.charAt(i); if (c == '(') { afterInitStart = true; } else if (afterInitStart && !Character.isWhitespace(c)) { diff --git a/rewrite-java/src/main/java/org/openrewrite/java/style/Autodetect.java b/rewrite-java/src/main/java/org/openrewrite/java/style/Autodetect.java index e68f2ce229b..0f736fbed2f 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/style/Autodetect.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/style/Autodetect.java @@ -293,12 +293,11 @@ private static class FindLineFormatJavaVisitor extends JavaIsoVisitor 0) { mixed = true; @@ -843,11 +843,9 @@ private String longestCommonPrefix(String pkg, @Nullable String lcp) { return pkg; } - char[] p1 = pkg.toCharArray(); - char[] p2 = lcp.toCharArray(); int i = 0; - for (; i < p1.length && i < p2.length; i++) { - if (p1[i] != p2[i]) { + for (; i < pkg.length() && i < lcp.length(); i++) { + if (pkg.charAt(i) != lcp.charAt(i)) { break; } }