Skip to content

Commit 45f3401

Browse files
committed
Support JDK 23+
1 parent 361ee85 commit 45f3401

File tree

4 files changed

+110
-17
lines changed

4 files changed

+110
-17
lines changed

check_api/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,22 @@
181181
</jdkToolchain>
182182
</configuration>
183183
</execution>
184+
<execution>
185+
<id>java23</id>
186+
<goals>
187+
<goal>compile</goal>
188+
</goals>
189+
<configuration>
190+
<jdkToolchain>
191+
<version>23</version>
192+
</jdkToolchain>
193+
<compileSourceRoots>
194+
<compileSourceRoot>${basedir}/src/main/java23</compileSourceRoot>
195+
</compileSourceRoots>
196+
<!-- multiReleaseOutput requires setting release -->
197+
<outputDirectory>${project.build.outputDirectory}/META-INF/versions/23</outputDirectory>
198+
</configuration>
199+
</execution>
184200
<execution>
185201
<id>java24</id>
186202
<configuration>
@@ -197,6 +213,17 @@
197213
</executions>
198214

199215
</plugin>
216+
<plugin>
217+
<groupId>org.apache.maven.plugins</groupId>
218+
<artifactId>maven-jar-plugin</artifactId>
219+
<configuration>
220+
<archive>
221+
<manifestEntries>
222+
<Multi-Release>true</Multi-Release>
223+
</manifestEntries>
224+
</archive>
225+
</configuration>
226+
</plugin>
200227
</plugins>
201228
</build>
202229

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2024 The Error Prone Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.errorprone;
18+
19+
import com.sun.tools.javac.util.Convert;
20+
21+
/**
22+
* @see VisitorState#getConstantExpression(Object)
23+
*/
24+
final class ConstantStringExpressions {
25+
private ConstantStringExpressions() {}
26+
27+
static String toConstantStringExpression(Object value, VisitorState state) {
28+
if (!(value instanceof CharSequence)) {
29+
return state.getElements().getConstantExpression(value);
30+
}
31+
32+
// Don't escape single-quotes in string literals.
33+
CharSequence str = (CharSequence) value;
34+
StringBuilder sb = new StringBuilder("\"");
35+
for (int i = 0; i < str.length(); i++) {
36+
char c = str.charAt(i);
37+
if (c == '\'') {
38+
sb.append('\'');
39+
} else {
40+
sb.append(Convert.quote(c));
41+
}
42+
}
43+
return sb.append('"').toString();
44+
}
45+
}

check_api/src/main/java/com/google/errorprone/VisitorState.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.common.base.Preconditions.checkArgument;
2020
import static com.google.common.base.Preconditions.checkNotNull;
21+
import static com.google.errorprone.ConstantStringExpressions.toConstantStringExpression;
2122
import static com.google.errorprone.util.ASTHelpers.getStartPosition;
2223

2324
import com.google.common.annotations.VisibleForTesting;
@@ -51,7 +52,6 @@
5152
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
5253
import com.sun.tools.javac.tree.TreeMaker;
5354
import com.sun.tools.javac.util.Context;
54-
import com.sun.tools.javac.util.Convert;
5555
import com.sun.tools.javac.util.Name;
5656
import com.sun.tools.javac.util.Names;
5757
import com.sun.tools.javac.util.Options;
@@ -758,23 +758,10 @@ private static final class SharedState {
758758

759759
/**
760760
* Returns the Java source code for a constant expression representing the given constant value.
761-
* Like {@link Elements#getConstantExpression}, but doesn't over-escape single quotes in strings.
761+
* Like {@link Elements#getConstantExpression}, but (a) before JDK 23, doesn't over-escape single
762+
* quotes in strings and (b) treats any {@link CharSequence} as a {@link String}.
762763
*/
763764
public String getConstantExpression(Object value) {
764-
if (!(value instanceof CharSequence str)) {
765-
return getElements().getConstantExpression(value);
766-
}
767-
768-
// Don't escape single-quotes in string literals.
769-
StringBuilder sb = new StringBuilder("\"");
770-
for (int i = 0; i < str.length(); i++) {
771-
char c = str.charAt(i);
772-
if (c == '\'') {
773-
sb.append('\'');
774-
} else {
775-
sb.append(Convert.quote(c));
776-
}
777-
}
778-
return sb.append('"').toString();
765+
return toConstantStringExpression(value, this);
779766
}
780767
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2024 The Error Prone Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.errorprone;
18+
19+
import com.google.errorprone.VisitorState;
20+
import com.sun.tools.javac.util.Convert;
21+
22+
/**
23+
* @see VisitorState#getConstantExpression(Object)
24+
*/
25+
final class ConstantStringExpressions {
26+
private ConstantStringExpressions() {}
27+
28+
static String toConstantStringExpression(Object value, VisitorState state) {
29+
return state
30+
.getElements()
31+
.getConstantExpression(
32+
value instanceof CharSequence charSequence ? charSequence.toString() : value);
33+
}
34+
}

0 commit comments

Comments
 (0)