Skip to content

Commit

Permalink
Remove some unnecessary reflection now that the formatter only suppor…
Browse files Browse the repository at this point in the history
…ts JDK 17

PiperOrigin-RevId: 699223994
  • Loading branch information
cushon authored and google-java-format Team committed Nov 22, 2024
1 parent a4991be commit 0e2ff5f
Showing 1 changed file with 1 addition and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import com.sun.tools.javac.util.Position;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.ArrayDeque;
import java.util.ArrayList;
Expand All @@ -61,7 +60,6 @@
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardLocation;
import org.jspecify.annotations.Nullable;

/** Wraps string literals that exceed the column limit. */
public final class StringWrapper {
Expand Down Expand Up @@ -204,7 +202,7 @@ private void indentTextBlocks(
// The first line of the text block is always """, and it does not affect incidental
// whitespace.
ImmutableList<String> initialLines = text.lines().collect(toImmutableList());
String stripped = stripIndent(initialLines.stream().skip(1).collect(joining(separator)));
String stripped = initialLines.stream().skip(1).collect(joining(separator)).stripIndent();
ImmutableList<String> lines = stripped.lines().collect(toImmutableList());
int deindent =
getLast(initialLines).stripTrailing().length()
Expand Down Expand Up @@ -280,30 +278,6 @@ private void wrapLongStrings(
}
}

private static final Method STRIP_INDENT = getStripIndent();

private static @Nullable Method getStripIndent() {
if (Runtime.version().feature() < 15) {
return null;
}
try {
return String.class.getMethod("stripIndent");
} catch (NoSuchMethodException e) {
throw new LinkageError(e.getMessage(), e);
}
}

private static String stripIndent(String input) {
if (STRIP_INDENT == null) {
return input;
}
try {
return (String) STRIP_INDENT.invoke(input);
} catch (ReflectiveOperationException e) {
throw new LinkageError(e.getMessage(), e);
}
}

/**
* Returns the source text of the given string literal trees, excluding the leading and trailing
* double-quotes and the `+` operator.
Expand Down

0 comments on commit 0e2ff5f

Please sign in to comment.