Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ThirdPartyLibrary compatible with -source 8 #627

Merged
merged 1 commit into from
May 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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;
Comment on lines +93 to +97
Copy link
Member Author

@Stephan202 Stephan202 May 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unconditionally using noModule seems to work as well, but:

  1. The documentation claims it should only be used with -source 8.
  2. I suspect (but did not test) that if using noModule when targeting Java 9+, the requested classes may be loaded a second time. That's not very nice/efficient.

Copy link
Member Author

@Stephan202 Stephan202 May 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naturally Pitest complains that one of the branches isn't covered. We can't prevent that, but it does raise the question whether we should have a build targeting JDK 8. With Jabel introduced in #603 this would in theory be possible, but (a) it'd require a number of workarounds and (b) that setup doesn't seem capable of reproducing the NPE reported in #626 (not sure why; didn't deep-dive). So all-in-all not worth the hassle.

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
Loading