Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize VisitorState#getConstantExpression #4589

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions check_api/src/main/java/com/google/errorprone/VisitorState.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Convert;
import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.Names;
import com.sun.tools.javac.util.Options;
Expand Down Expand Up @@ -770,24 +771,20 @@ private static final class SharedState {
* Like {@link Elements#getConstantExpression}, but doesn't over-escape single quotes in strings.
*/
public String getConstantExpression(Object value) {
String escaped = getElements().getConstantExpression(value);
if (value instanceof String) {
// Don't escape single-quotes in string literals
StringBuilder sb = new StringBuilder();
for (int i = 0; i < escaped.length(); i++) {
char c = escaped.charAt(i);
if (c == '\\' && i + 1 < escaped.length()) {
char next = escaped.charAt(++i);
if (next != '\'') {
sb.append(c);
}
sb.append(next);
} else {
sb.append(c);
}
if (!(value instanceof CharSequence str)) {
return getElements().getConstantExpression(value);
}

// Don't escape single-quotes in string literals.
StringBuilder sb = new StringBuilder("\"");
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == '\'') {
sb.append('\'');
} else {
sb.append(Convert.quote(c));
}
return sb.toString();
}
return escaped;
return sb.append('"').toString();
}
}
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
Loading