Skip to content

Commit 08e0d10

Browse files
cushonError Prone Team
authored andcommitted
Set up support for Multi-Release jars in Error Prone
and use it to work around a breaking change in `SignatureGenerator`. #3756 PiperOrigin-RevId: 661672308
1 parent b5e6041 commit 08e0d10

File tree

6 files changed

+170
-55
lines changed

6 files changed

+170
-55
lines changed

bnd.bnd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-fixupmessages: "Classes found in the wrong directory"; restrict:=error; is:=warning

check_api/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,30 @@
163163
</path>
164164
</annotationProcessorPaths>
165165
</configuration>
166+
<executions>
167+
<execution>
168+
<id>default-compile</id>
169+
<configuration>
170+
<jdkToolchain>
171+
<version>11</version>
172+
</jdkToolchain>
173+
</configuration>
174+
</execution>
175+
<execution>
176+
<id>java24</id>
177+
<configuration>
178+
<jdkToolchain>
179+
<version>24</version>
180+
</jdkToolchain>
181+
<compileSourceRoots>
182+
<compileSourceRoot>${basedir}/src/main/java24</compileSourceRoot>
183+
</compileSourceRoots>
184+
<!-- multiReleaseOutput requires setting release -->
185+
<outputDirectory>${project.build.outputDirectory}/META-INF/versions/24</outputDirectory>
186+
</configuration>
187+
</execution>
188+
</executions>
189+
166190
</plugin>
167191
</plugins>
168192
</build>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.util;
18+
19+
import com.sun.tools.javac.code.Types;
20+
import com.sun.tools.javac.util.Name;
21+
import com.sun.tools.javac.util.Names;
22+
23+
class ErrorProneSignatureGenerator extends Types.SignatureGenerator {
24+
25+
private final com.sun.tools.javac.util.ByteBuffer buffer =
26+
new com.sun.tools.javac.util.ByteBuffer();
27+
28+
private final Names names;
29+
30+
protected ErrorProneSignatureGenerator(Types types, Names names) {
31+
super(types);
32+
this.names = names;
33+
}
34+
35+
@Override
36+
protected void append(char ch) {
37+
buffer.appendByte(ch);
38+
}
39+
40+
@Override
41+
protected void append(byte[] ba) {
42+
buffer.appendBytes(ba);
43+
}
44+
45+
@Override
46+
protected void append(Name name) {
47+
buffer.appendName(name);
48+
}
49+
50+
@SuppressWarnings("CatchingUnchecked") // handles InvalidUtfException on JDK 21+
51+
@Override
52+
public String toString() {
53+
try {
54+
return buffer.toName(names).toString();
55+
} catch (Exception e) {
56+
throw new AssertionError(e);
57+
}
58+
}
59+
}

check_api/src/main/java/com/google/errorprone/util/Signatures.java

Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -26,76 +26,26 @@
2626
import com.sun.tools.javac.code.Types;
2727
import com.sun.tools.javac.code.Types.DefaultTypeVisitor;
2828
import com.sun.tools.javac.util.Name;
29-
import com.sun.tools.javac.util.Names;
3029

3130
/** Signature generation. */
3231
public final class Signatures {
3332

3433
/** Returns the binary names of the class. */
3534
public static String classDescriptor(Type type, VisitorState state) {
36-
return new Signatures(state).classDescriptor(type);
37-
}
38-
39-
private String classDescriptor(Type type) {
40-
SigGen sig = new SigGen();
35+
Types types = state.getTypes();
36+
ErrorProneSignatureGenerator sig = new ErrorProneSignatureGenerator(types, state.getNames());
4137
sig.assembleClassSig(types.erasure(type));
4238
return sig.toString();
4339
}
4440

4541
/** Returns a JVMS 4.3.3 method descriptor. */
4642
public static String descriptor(Type type, VisitorState state) {
47-
return new Signatures(state).descriptor(type);
48-
}
49-
50-
private String descriptor(Type type) {
51-
SigGen sig = new SigGen();
43+
Types types = state.getTypes();
44+
ErrorProneSignatureGenerator sig = new ErrorProneSignatureGenerator(types, state.getNames());
5245
sig.assembleSig(types.erasure(type));
5346
return sig.toString();
5447
}
5548

56-
final Types types;
57-
final Names names;
58-
59-
private Signatures(VisitorState state) {
60-
this.types = state.getTypes();
61-
this.names = state.getNames();
62-
}
63-
64-
private class SigGen extends Types.SignatureGenerator {
65-
66-
private final com.sun.tools.javac.util.ByteBuffer buffer =
67-
new com.sun.tools.javac.util.ByteBuffer();
68-
69-
protected SigGen() {
70-
super(types);
71-
}
72-
73-
@Override
74-
protected void append(char ch) {
75-
buffer.appendByte(ch);
76-
}
77-
78-
@Override
79-
protected void append(byte[] ba) {
80-
buffer.appendBytes(ba);
81-
}
82-
83-
@Override
84-
protected void append(Name name) {
85-
buffer.appendName(name);
86-
}
87-
88-
@SuppressWarnings("CatchingUnchecked") // handles InvalidUtfException on JDK 21+
89-
@Override
90-
public String toString() {
91-
try {
92-
return buffer.toName(names).toString();
93-
} catch (Exception e) {
94-
throw new AssertionError(e);
95-
}
96-
}
97-
}
98-
9949
/**
10050
* Pretty-prints a method signature for use in diagnostics.
10151
*
@@ -171,4 +121,6 @@ public String visitType(Type t, Void s) {
171121
return t.toString();
172122
}
173123
};
124+
125+
private Signatures() {}
174126
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.util;
18+
19+
import com.sun.tools.javac.code.Types;
20+
import com.sun.tools.javac.util.Name;
21+
import com.sun.tools.javac.util.Names;
22+
23+
class ErrorProneSignatureGenerator extends Types.SignatureGenerator {
24+
25+
private final com.sun.tools.javac.util.ByteBuffer buffer =
26+
new com.sun.tools.javac.util.ByteBuffer();
27+
28+
private final Names names;
29+
30+
protected ErrorProneSignatureGenerator(Types types, Names names) {
31+
types.super();
32+
this.names = names;
33+
}
34+
35+
@Override
36+
protected void append(char ch) {
37+
buffer.appendByte(ch);
38+
}
39+
40+
@Override
41+
protected void append(byte[] ba) {
42+
buffer.appendBytes(ba);
43+
}
44+
45+
@Override
46+
protected void append(Name name) {
47+
buffer.appendName(name);
48+
}
49+
50+
@SuppressWarnings("CatchingUnchecked") // handles InvalidUtfException on JDK 21+
51+
@Override
52+
public String toString() {
53+
try {
54+
return buffer.toName(names).toString();
55+
} catch (Exception e) {
56+
throw new AssertionError(e);
57+
}
58+
}
59+
}

pom.xml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@
180180
<plugin>
181181
<groupId>org.apache.maven.plugins</groupId>
182182
<artifactId>maven-compiler-plugin</artifactId>
183-
<version>3.10.1</version>
183+
<version>3.13.0</version>
184184
<configuration>
185185
<source>11</source>
186186
<target>11</target>
@@ -264,6 +264,26 @@
264264
<trimStackTrace>false</trimStackTrace>
265265
</configuration>
266266
</plugin>
267+
<plugin>
268+
<groupId>org.apache.maven.plugins</groupId>
269+
<artifactId>maven-toolchains-plugin</artifactId>
270+
<version>3.2.0</version>
271+
<configuration>
272+
<toolchains>
273+
<jdk>
274+
<version>11</version>
275+
<version>24</version>
276+
</jdk>
277+
</toolchains>
278+
</configuration>
279+
<executions>
280+
<execution>
281+
<goals>
282+
<goal>toolchain</goal>
283+
</goals>
284+
</execution>
285+
</executions>
286+
</plugin>
267287
</plugins>
268288
</build>
269289

0 commit comments

Comments
 (0)