-
Notifications
You must be signed in to change notification settings - Fork 745
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 #4586
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately we're not quite ready to start using Java > 11 features in non-test code internally. I can remove the |
||
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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incidentally I recently learned that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Er, and as a result of that change I'm not sure what the best option is. We could use reflection or MR-JARs to call the method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, nice catch! I haven't looked at the MR-JAR setup yet, but indeed reflection should work. TBD whether I'll find some time today for a new pass. |
||
} | ||
return sb.toString(); | ||
} | ||
return escaped; | ||
return sb.append('"').toString(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a second commit that enables a further optimization by accepting any
CharSequence
; this can be backed out if desired.