Skip to content

Commit

Permalink
Support JDK 23+
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Dec 28, 2024
1 parent 361ee85 commit 45f3401
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 17 deletions.
27 changes: 27 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>
<execution>
<id>java24</id>
<configuration>
Expand All @@ -197,6 +213,17 @@
</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>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
21 changes: 4 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 @@ -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 @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 45f3401

Please sign in to comment.