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: 530929411
  • Loading branch information
cushon authored and Error Prone Team committed May 10, 2023
1 parent 7335ba2 commit 047e1e3
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
25 changes: 24 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 047e1e3

Please sign in to comment.