diff --git a/check_api/pom.xml b/check_api/pom.xml index 061e83e0e6b..78c7f0c3a25 100644 --- a/check_api/pom.xml +++ b/check_api/pom.xml @@ -181,6 +181,22 @@ + + java23 + + compile + + + + 23 + + + ${basedir}/src/main/java23 + + + ${project.build.outputDirectory}/META-INF/versions/23 + + java24 @@ -197,6 +213,17 @@ + + org.apache.maven.plugins + maven-jar-plugin + + + + true + + + + diff --git a/check_api/src/main/java/com/google/errorprone/ConstantStringExpressions.java b/check_api/src/main/java/com/google/errorprone/ConstantStringExpressions.java new file mode 100644 index 00000000000..9b988cfb44e --- /dev/null +++ b/check_api/src/main/java/com/google/errorprone/ConstantStringExpressions.java @@ -0,0 +1,45 @@ +/* + * 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; + +/** + * @see VisitorState#getConstantExpression(Object) + */ +final class ConstantStringExpressions { + private ConstantStringExpressions() {} + + static String toConstantStringExpression(Object value, VisitorState state) { + if (!(value instanceof CharSequence)) { + return state.getElements().getConstantExpression(value); + } + + // Don't escape single-quotes in string literals. + CharSequence str = (CharSequence) value; + 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(); + } +} diff --git a/check_api/src/main/java/com/google/errorprone/VisitorState.java b/check_api/src/main/java/com/google/errorprone/VisitorState.java index e385d0a1466..7d769a3799e 100644 --- a/check_api/src/main/java/com/google/errorprone/VisitorState.java +++ b/check_api/src/main/java/com/google/errorprone/VisitorState.java @@ -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; @@ -51,7 +52,6 @@ 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; @@ -758,23 +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) { - 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.append('"').toString(); + return toConstantStringExpression(value, this); } } diff --git a/check_api/src/main/java23/com/google/errorprone/ConstantStringExpressions.java b/check_api/src/main/java23/com/google/errorprone/ConstantStringExpressions.java new file mode 100644 index 00000000000..449e4c4f0bb --- /dev/null +++ b/check_api/src/main/java23/com/google/errorprone/ConstantStringExpressions.java @@ -0,0 +1,34 @@ +/* + * 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; + +/** + * @see VisitorState#getConstantExpression(Object) + */ +final class ConstantStringExpressions { + private ConstantStringExpressions() {} + + static String toConstantStringExpression(Object value, VisitorState state) { + return state + .getElements() + .getConstantExpression( + value instanceof CharSequence charSequence ? charSequence.toString() : value); + } +}