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 #4586

Open
wants to merge 6 commits 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
43 changes: 43 additions & 0 deletions check_api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,22 @@
</jdkToolchain>
</configuration>
</execution>
<execution>
<id>java23</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<jdkToolchain>
<version>23</version>
</jdkToolchain>
<compileSourceRoots>
<compileSourceRoot>${basedir}/src/main/java23</compileSourceRoot>
</compileSourceRoots>
<!-- multiReleaseOutput requires setting release -->
<outputDirectory>${project.build.outputDirectory}/META-INF/versions/23</outputDirectory>
</configuration>
</execution>
Comment on lines +184 to +199
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few things are of note here:

  • Without <goal>compile</goal> this execution is skipped. This also means that the java24 execution below is currently a no-op.
  • This configuration doesn't override the inherited <source>17</source> and <target>17</target> settings, though it could. (If so, we should do the same below.)
  • I'm not sure each of these executions should use an exactly-matching toolchain JDK version. Perhaps it's better to use the latest JDK whenever possible, so as to minimize the number of JDKs required.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also means that the java24 execution below is currently a no-op.

Huh, thanks for pointing that out. This is also used with an Bazel-based build that does the multi-release jar stuff differently, so maybe there are no uses (or test coverage) for the maven build and ErrorProneSignatureGenerator that require JDK 24 support.

I'm not sure each of these executions should use an exactly-matching toolchain JDK version. Perhaps it's better to use the latest JDK whenever possible, so as to minimize the number of JDKs required.

The answer might be 'it depends'. For javac API changes, it's probably best to try to use an exact match, but that does require installing extra JDKs for the github actions setup. If the API changes once and is stable on newer versions we'd get away with a newer version, though.

<execution>
<id>java24</id>
<configuration>
Expand All @@ -194,9 +210,36 @@
<outputDirectory>${project.build.outputDirectory}/META-INF/versions/24</outputDirectory>
</configuration>
</execution>
<execution>
<!-- This final no-op compilation step resets the output directory associated with this
module. This is important for Maven reactor builds that skip the `install` phase,
because in those the compilation of a module `B` happens with reference to the output
directory of a module `A` on which it depends, rather than `A`'s installed JAR. -->
<id>reset-output-directory</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<compileSourceRoots>
<compileSourceRoot>${basedir}/nonexistent-directory</compileSourceRoot>
</compileSourceRoots>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>

</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Multi-Release>true</Multi-Release>
</manifestEntries>
</archive>
</configuration>
</plugin>
Comment on lines +232 to +242
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also had to add this to make sure that the class file bundled under META-INF/versions/23 is picked up.

</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2024 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.errorprone;

import com.sun.tools.javac.util.Convert;
import javax.lang.model.util.Elements;

/**
* @see VisitorState#getConstantExpression(Object)
*/
final class ConstantStringExpressions {
private ConstantStringExpressions() {}
Comment on lines +22 to +26
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly open to approaches other than a special-purpose static utility class named com.google.errorprone.ConstantStringExpressions.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems good for now, thanks.


static String toConstantStringExpression(Object value, Elements elements) {
if (!(value instanceof CharSequence)) {
return elements.getConstantExpression(value);
}

// Don't escape single-quotes in string literals.
CharSequence str = (CharSequence) value;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped the instanceof pattern matching as suggested here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're finally able to use Java 21 features here internally, the earlier comment is obsolete.

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.append('"').toString();
}
}
24 changes: 4 additions & 20 deletions check_api/src/main/java/com/google/errorprone/VisitorState.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.errorprone.ConstantStringExpressions.toConstantStringExpression;
import static com.google.errorprone.util.ASTHelpers.getStartPosition;

import com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -757,27 +758,10 @@ private static final class SharedState {

/**
* Returns the Java source code for a constant expression representing the given constant value.
* Like {@link Elements#getConstantExpression}, but doesn't over-escape single quotes in strings.
* Like {@link Elements#getConstantExpression}, but (a) before JDK 23, doesn't over-escape single
* quotes in strings and (b) treats any {@link CharSequence} as a {@link String}.
*/
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);
}
}
return sb.toString();
}
return escaped;
return toConstantStringExpression(value, getElements());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2024 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.errorprone;

import com.google.errorprone.VisitorState;
import com.sun.tools.javac.util.Convert;
import javax.lang.model.util.Elements;

/**
* @see VisitorState#getConstantExpression(Object)
*/
final class ConstantStringExpressions {
private ConstantStringExpressions() {}

static String toConstantStringExpression(Object value, Elements elements) {
return elements.getConstantExpression(
value instanceof CharSequence charSequence ? charSequence.toString() : value);
}
}
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 @@ -352,7 +352,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