From 7d4ad636ff25df6d3264311815d2861dc7b2d583 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Wed, 14 Aug 2024 17:10:02 -0700 Subject: [PATCH] Set up support for Multi-Release jars in Error Prone and use it to work around a breaking change in `SignatureGenerator`. https://github.com/google/error-prone/issues/3756 PiperOrigin-RevId: 663105454 --- bnd.bnd | 1 + check_api/pom.xml | 24 ++++++++ .../util/ErrorProneSignatureGenerator.java | 59 ++++++++++++++++++ .../google/errorprone/util/Signatures.java | 60 ++----------------- .../util/ErrorProneSignatureGenerator.java | 59 ++++++++++++++++++ pom.xml | 22 ++++++- 6 files changed, 170 insertions(+), 55 deletions(-) create mode 100644 bnd.bnd create mode 100644 check_api/src/main/java/com/google/errorprone/util/ErrorProneSignatureGenerator.java create mode 100644 check_api/src/main/java24/com/google/errorprone/util/ErrorProneSignatureGenerator.java diff --git a/bnd.bnd b/bnd.bnd new file mode 100644 index 00000000000..8f44eafbfc8 --- /dev/null +++ b/bnd.bnd @@ -0,0 +1 @@ +-fixupmessages: "Classes found in the wrong directory"; restrict:=error; is:=warning diff --git a/check_api/pom.xml b/check_api/pom.xml index 21c7a05fcd5..0824e406034 100644 --- a/check_api/pom.xml +++ b/check_api/pom.xml @@ -163,6 +163,30 @@ + + + default-compile + + + 11 + + + + + java24 + + + 24 + + + ${basedir}/src/main/java24 + + + ${project.build.outputDirectory}/META-INF/versions/24 + + + + diff --git a/check_api/src/main/java/com/google/errorprone/util/ErrorProneSignatureGenerator.java b/check_api/src/main/java/com/google/errorprone/util/ErrorProneSignatureGenerator.java new file mode 100644 index 00000000000..54a1fd8d12e --- /dev/null +++ b/check_api/src/main/java/com/google/errorprone/util/ErrorProneSignatureGenerator.java @@ -0,0 +1,59 @@ +/* + * 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.util; + +import com.sun.tools.javac.code.Types; +import com.sun.tools.javac.util.Name; +import com.sun.tools.javac.util.Names; + +class ErrorProneSignatureGenerator extends Types.SignatureGenerator { + + private final com.sun.tools.javac.util.ByteBuffer buffer = + new com.sun.tools.javac.util.ByteBuffer(); + + private final Names names; + + protected ErrorProneSignatureGenerator(Types types, Names names) { + super(types); + this.names = names; + } + + @Override + protected void append(char ch) { + buffer.appendByte(ch); + } + + @Override + protected void append(byte[] ba) { + buffer.appendBytes(ba); + } + + @Override + protected void append(Name name) { + buffer.appendName(name); + } + + @SuppressWarnings("CatchingUnchecked") // handles InvalidUtfException on JDK 21+ + @Override + public String toString() { + try { + return buffer.toName(names).toString(); + } catch (Exception e) { + throw new AssertionError(e); + } + } +} diff --git a/check_api/src/main/java/com/google/errorprone/util/Signatures.java b/check_api/src/main/java/com/google/errorprone/util/Signatures.java index ab8ad1a68b5..b2244bd0b6e 100644 --- a/check_api/src/main/java/com/google/errorprone/util/Signatures.java +++ b/check_api/src/main/java/com/google/errorprone/util/Signatures.java @@ -26,76 +26,26 @@ import com.sun.tools.javac.code.Types; import com.sun.tools.javac.code.Types.DefaultTypeVisitor; import com.sun.tools.javac.util.Name; -import com.sun.tools.javac.util.Names; /** Signature generation. */ public final class Signatures { /** Returns the binary names of the class. */ public static String classDescriptor(Type type, VisitorState state) { - return new Signatures(state).classDescriptor(type); - } - - private String classDescriptor(Type type) { - SigGen sig = new SigGen(); + Types types = state.getTypes(); + ErrorProneSignatureGenerator sig = new ErrorProneSignatureGenerator(types, state.getNames()); sig.assembleClassSig(types.erasure(type)); return sig.toString(); } /** Returns a JVMS 4.3.3 method descriptor. */ public static String descriptor(Type type, VisitorState state) { - return new Signatures(state).descriptor(type); - } - - private String descriptor(Type type) { - SigGen sig = new SigGen(); + Types types = state.getTypes(); + ErrorProneSignatureGenerator sig = new ErrorProneSignatureGenerator(types, state.getNames()); sig.assembleSig(types.erasure(type)); return sig.toString(); } - final Types types; - final Names names; - - private Signatures(VisitorState state) { - this.types = state.getTypes(); - this.names = state.getNames(); - } - - private class SigGen extends Types.SignatureGenerator { - - private final com.sun.tools.javac.util.ByteBuffer buffer = - new com.sun.tools.javac.util.ByteBuffer(); - - protected SigGen() { - super(types); - } - - @Override - protected void append(char ch) { - buffer.appendByte(ch); - } - - @Override - protected void append(byte[] ba) { - buffer.appendBytes(ba); - } - - @Override - protected void append(Name name) { - buffer.appendName(name); - } - - @SuppressWarnings("CatchingUnchecked") // handles InvalidUtfException on JDK 21+ - @Override - public String toString() { - try { - return buffer.toName(names).toString(); - } catch (Exception e) { - throw new AssertionError(e); - } - } - } - /** * Pretty-prints a method signature for use in diagnostics. * @@ -171,4 +121,6 @@ public String visitType(Type t, Void s) { return t.toString(); } }; + + private Signatures() {} } diff --git a/check_api/src/main/java24/com/google/errorprone/util/ErrorProneSignatureGenerator.java b/check_api/src/main/java24/com/google/errorprone/util/ErrorProneSignatureGenerator.java new file mode 100644 index 00000000000..e8a350938cb --- /dev/null +++ b/check_api/src/main/java24/com/google/errorprone/util/ErrorProneSignatureGenerator.java @@ -0,0 +1,59 @@ +/* + * 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.util; + +import com.sun.tools.javac.code.Types; +import com.sun.tools.javac.util.Name; +import com.sun.tools.javac.util.Names; + +class ErrorProneSignatureGenerator extends Types.SignatureGenerator { + + private final com.sun.tools.javac.util.ByteBuffer buffer = + new com.sun.tools.javac.util.ByteBuffer(); + + private final Names names; + + protected ErrorProneSignatureGenerator(Types types, Names names) { + types.super(); + this.names = names; + } + + @Override + protected void append(char ch) { + buffer.appendByte(ch); + } + + @Override + protected void append(byte[] ba) { + buffer.appendBytes(ba); + } + + @Override + protected void append(Name name) { + buffer.appendName(name); + } + + @SuppressWarnings("CatchingUnchecked") // handles InvalidUtfException on JDK 21+ + @Override + public String toString() { + try { + return buffer.toName(names).toString(); + } catch (Exception e) { + throw new AssertionError(e); + } + } +} diff --git a/pom.xml b/pom.xml index 1a2615bb86d..2992590fe2c 100644 --- a/pom.xml +++ b/pom.xml @@ -180,7 +180,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.10.1 + 3.13.0 11 11 @@ -264,6 +264,26 @@ false + + org.apache.maven.plugins + maven-toolchains-plugin + 3.2.0 + + + + 11 + 24 + + + + + + + toolchain + + + +