Skip to content

Commit

Permalink
Make ThirdPartyLibrary compatible with -source 8
Browse files Browse the repository at this point in the history
When targeting Java 8, `unnamedModule` is not properly initialized,
causing an NPE when trying to load a class from it. In that context
`noModule` should be used instead.

Resolves #626.
  • Loading branch information
Stephan202 committed May 14, 2023
1 parent f52a93c commit ac56468
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.suppliers.Supplier;
import com.sun.tools.javac.code.ClassFinder;
import com.sun.tools.javac.code.Source;
import com.sun.tools.javac.code.Symbol.CompletionFailure;
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.util.Name;

/**
Expand Down Expand Up @@ -86,9 +89,15 @@ private static boolean isKnownClass(String className, VisitorState state) {

private static boolean canLoadClass(String className, VisitorState state) {
ClassFinder classFinder = ClassFinder.instance(state.context);
Symtab symtab = state.getSymtab();
// XXX: Drop support for targeting Java 8 once the oldest supported JDK drops such support.
ModuleSymbol module =
Source.instance(state.context).compareTo(Source.JDK9) < 0

Check warning on line 95 in error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/util/ThirdPartyLibrary.java

View workflow job for this annotation

GitHub Actions / pitest

2 different changes can be made to line 95 without causing a test to fail

changed conditional boundary (12 tests run ConditionalsBoundaryMutator) removed conditional - replaced comparison check with false (12 tests run RemoveConditionalMutator_ORDER_ELSE)
? symtab.noModule
: symtab.unnamedModule;
Name binaryName = state.binaryNameFromClassname(className);
try {
classFinder.loadClass(state.getSymtab().unnamedModule, binaryName);
classFinder.loadClass(module, binaryName);
return true;
} catch (
@SuppressWarnings("java:S1166" /* Not exceptional. */)
Expand Down

0 comments on commit ac56468

Please sign in to comment.