From 05214235bd37631218d80d552bda25e204b4c9b5 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Wed, 10 May 2023 09:29:16 -0700 Subject: [PATCH] Fix binary compatibility issues when building on JDK 17 and running on JDK 11, and prepare to add test coverage for that configuration Thanks to Stephan202's suggestion in https://github.com/google/error-prone/issues/3895#issuecomment-1542294317 https://github.com/google/error-prone/issues/3895 PiperOrigin-RevId: 530929411 --- .../google/errorprone/util/ASTHelpers.java | 10 +++++++- .../ChainedAssertionLosesContext.java | 5 ++-- ...FuturesGetCheckedIllegalExceptionType.java | 3 ++- .../errorprone/bugpatterns/HidingField.java | 3 ++- .../apidiff/CompilationBuilderHelpers.java | 2 +- pom.xml | 25 ++++++++++++++++++- 6 files changed, 41 insertions(+), 7 deletions(-) diff --git a/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java b/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java index 9a4764d097d7..71e069fceeab 100644 --- a/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java +++ b/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java @@ -1141,9 +1141,17 @@ public static List getConstructors(ClassTree classTree) { return constructors; } + /** + * A wrapper for {@link Symbol#getEnclosedElements} to avoid binary compatibility issues for + * covariant overrides in subtypes of {@link Symbol}. + */ + public static List getEnclosedElements(Symbol symbol) { + return symbol.getEnclosedElements(); + } + /** Returns the list of all constructors defined in the class. */ public static ImmutableList getConstructors(ClassSymbol classSymbol) { - return classSymbol.getEnclosedElements().stream() + return getEnclosedElements(classSymbol).stream() .filter(Symbol::isConstructor) .map(e -> (MethodSymbol) e) .collect(toImmutableList()); diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/ChainedAssertionLosesContext.java b/core/src/main/java/com/google/errorprone/bugpatterns/ChainedAssertionLosesContext.java index c31461b31667..226c050ee27c 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/ChainedAssertionLosesContext.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/ChainedAssertionLosesContext.java @@ -25,6 +25,7 @@ import static com.google.errorprone.matchers.Matchers.instanceMethod; import static com.google.errorprone.matchers.Matchers.staticMethod; import static com.google.errorprone.util.ASTHelpers.getDeclaredSymbol; +import static com.google.errorprone.util.ASTHelpers.getEnclosedElements; import static com.google.errorprone.util.ASTHelpers.getSymbol; import static com.google.errorprone.util.ASTHelpers.isSubtype; import static com.sun.source.tree.Tree.Kind.CLASS; @@ -212,9 +213,9 @@ private static FactoryMethodName tryFindFactory( ImmutableSet factories = concat( // The class that assertThat is declared in: - assertThatSymbol.owner.getEnclosedElements().stream(), + getEnclosedElements(assertThatSymbol.owner).stream(), // The Subject class (possibly the same; if so, toImmutableSet() will deduplicate): - assertThatSymbol.getReturnType().asElement().getEnclosedElements().stream()) + getEnclosedElements(assertThatSymbol.getReturnType().asElement()).stream()) .filter(s -> s instanceof MethodSymbol) .map(s -> (MethodSymbol) s) .filter( diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/FuturesGetCheckedIllegalExceptionType.java b/core/src/main/java/com/google/errorprone/bugpatterns/FuturesGetCheckedIllegalExceptionType.java index 3902861b68ec..13c53003105c 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/FuturesGetCheckedIllegalExceptionType.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/FuturesGetCheckedIllegalExceptionType.java @@ -20,6 +20,7 @@ import static com.google.errorprone.matchers.Matchers.argument; import static com.google.errorprone.matchers.Matchers.classLiteral; import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod; +import static com.google.errorprone.util.ASTHelpers.getEnclosedElements; import static com.google.errorprone.util.ASTHelpers.getSymbol; import static com.google.errorprone.util.ASTHelpers.getType; import static com.google.errorprone.util.ASTHelpers.isSameType; @@ -123,7 +124,7 @@ public boolean matches(ExpressionTree tree, VisitorState state) { return true; } - for (Symbol enclosedSymbol : classSymbol.getEnclosedElements()) { + for (Symbol enclosedSymbol : getEnclosedElements(classSymbol)) { if (!enclosedSymbol.isConstructor()) { continue; } diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/HidingField.java b/core/src/main/java/com/google/errorprone/bugpatterns/HidingField.java index 96112a022c95..35bb501cd332 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/HidingField.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/HidingField.java @@ -17,6 +17,7 @@ import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; import static com.google.errorprone.util.ASTHelpers.enclosingPackage; +import static com.google.errorprone.util.ASTHelpers.getEnclosedElements; import static com.google.errorprone.util.ASTHelpers.getSymbol; import static java.util.stream.Collectors.toCollection; @@ -70,7 +71,7 @@ public Description matchClass(ClassTree classTree, VisitorState visitorState) { while (!classSymbol.getSuperclass().getKind().equals(TypeKind.NONE)) { TypeSymbol parentSymbol = classSymbol.getSuperclass().asElement(); - List parentElements = parentSymbol.getEnclosedElements(); + List parentElements = getEnclosedElements(parentSymbol); Map parentMembers = parentElements.stream() diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/apidiff/CompilationBuilderHelpers.java b/core/src/test/java/com/google/errorprone/bugpatterns/apidiff/CompilationBuilderHelpers.java index 14191e9a3a20..c93e697e5930 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/apidiff/CompilationBuilderHelpers.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/apidiff/CompilationBuilderHelpers.java @@ -131,7 +131,7 @@ CompilationResult compile() throws IOException { diagnosticCollector, javacopts, /* classes= */ Collections.emptyList(), - fileManager.getJavaFileObjectsFromPaths(sources)) + fileManager.getJavaFileObjects(sources.toArray(new Path[0]))) .call(); return CompilationResult.create( diff --git a/pom.xml b/pom.xml index 412ceb5bf858..01bfcaf1f649 100644 --- a/pom.xml +++ b/pom.xml @@ -208,7 +208,7 @@ org.apache.maven.plugins maven-surefire-plugin - 2.22.2 + 3.1.0 -Xmx1g @@ -303,5 +303,28 @@ + + custom-test-runtime-version + + + surefire.jdk-toolchain-version + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + ${surefire.jdk-toolchain-version} + + + + + + +