Skip to content

Commit

Permalink
Fix binary compatibility issues when building on JDK 17 and running o…
Browse files Browse the repository at this point in the history
…n JDK 11, and prepare to add test coverage for that configuration

Thanks to Stephan202's suggestion in #3895 (comment)

#3895

PiperOrigin-RevId: 530957571
cushon authored and Error Prone Team committed May 10, 2023
1 parent 7335ba2 commit 5da8e9a
Showing 6 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1141,9 +1141,17 @@ public static List<MethodTree> 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<Symbol> getEnclosedElements(Symbol symbol) {
return symbol.getEnclosedElements();
}

/** Returns the list of all constructors defined in the class. */
public static ImmutableList<MethodSymbol> getConstructors(ClassSymbol classSymbol) {
return classSymbol.getEnclosedElements().stream()
return getEnclosedElements(classSymbol).stream()
.filter(Symbol::isConstructor)
.map(e -> (MethodSymbol) e)
.collect(toImmutableList());
Original file line number Diff line number Diff line change
@@ -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<MethodSymbol> 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(
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<Symbol> parentElements = parentSymbol.getEnclosedElements();
List<Symbol> parentElements = getEnclosedElements(parentSymbol);

Map<Name, VarSymbol> parentMembers =
parentElements.stream()
Original file line number Diff line number Diff line change
@@ -131,7 +131,7 @@ CompilationResult compile() throws IOException {
diagnosticCollector,
javacopts,
/* classes= */ Collections.<String>emptyList(),
fileManager.getJavaFileObjectsFromPaths(sources))
fileManager.getJavaFileObjects(sources.toArray(new Path[0])))
.call();

return CompilationResult.create(
25 changes: 24 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -208,7 +208,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<version>3.1.0</version>
<configuration>
<argLine>
-Xmx1g
@@ -303,5 +303,28 @@
</plugins>
</build>
</profile>
<profile>
<id>custom-test-runtime-version</id>
<activation>
<property>
<name>surefire.jdk-toolchain-version</name>
</property>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<jdkToolchain>
<version>${surefire.jdk-toolchain-version}</version>
</jdkToolchain>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
</project>

0 comments on commit 5da8e9a

Please sign in to comment.