Skip to content

Commit

Permalink
Optional additional performance improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Sep 21, 2024
1 parent b401790 commit ff02145
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ private static final class SharedState {
* Like {@link Elements#getConstantExpression}, but doesn't over-escape single quotes in strings.
*/
public String getConstantExpression(Object value) {
if (!(value instanceof String str)) {
if (!(value instanceof CharSequence str)) {
return getElements().getConstantExpression(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
MethodSymbol symbol = getSymbol(parent);
String argReplacement =
Streams.concat(
Stream.of(state.getConstantExpression(symbol.getSimpleName().toString())),
Stream.of(state.getConstantExpression(symbol.getSimpleName())),
Streams.zip(
symbol.getParameters().stream(),
parent.getArguments().stream(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ Description unwrapArguments(
if (!fixed) {
return NO_MATCH;
}
fix.replace(tree.getArguments().get(0), state.getConstantExpression(sb.toString()));
fix.replace(tree.getArguments().get(0), state.getConstantExpression(sb));
return describeMatch(tree, fix.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected Void defaultAction(Tree tree, Void unused) {
tree,
SuggestedFix.replace(
argument,
state.getConstantExpression(formatString.toString())
state.getConstantExpression(formatString)
+ ", "
+ formatArguments.stream().map(state::getSourceForNode).collect(joining(", "))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public void getConstantExpression() {
assertThat(visitorState.getConstantExpression("hello \n world"))
.isEqualTo("\"hello \\n world\"");
assertThat(visitorState.getConstantExpression('\'')).isEqualTo("'\\''");
assertThat(visitorState.getConstantExpression(new StringBuilder("hello ' world")))
.isEqualTo("\"hello ' world\"");
}

// The following is taken from ErrorProneJavacPluginTest. There may be an easier way.
Expand Down

0 comments on commit ff02145

Please sign in to comment.