Skip to content

Commit

Permalink
refactor: simplify java version check logic by extracting internal me…
Browse files Browse the repository at this point in the history
…thod `methodExisted`
  • Loading branch information
oldratlee committed Aug 22, 2024
1 parent e53b92e commit 40d882a
Showing 1 changed file with 13 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4526,53 +4526,26 @@ private static Executor _asyncPool0() {
// region# Internal Java version check logic for compatibility
////////////////////////////////////////////////////////////////////////////////

private static final boolean IS_JAVA9_PLUS;
// `completedStage` is the new method of CompletableFuture since java 9
private static final boolean IS_JAVA9_PLUS = methodExists(() -> CompletableFuture.completedStage(null));

private static final boolean IS_JAVA12_PLUS;
// `exceptionallyCompose` is the new method of CompletableFuture since java 12
private static final boolean IS_JAVA12_PLUS = methodExists(() ->
completedFuture(null).exceptionallyCompose(ex -> null));

private static final boolean IS_JAVA19_PLUS;
// `resultNow` is the new method of CompletableFuture since java 19
private static final boolean IS_JAVA19_PLUS = methodExists(() -> completedFuture(null).resultNow());

private static final boolean IS_JAVA21_PLUS;

static {
boolean b;

try {
// `completedStage` is the new method of CompletableFuture since java 9
CompletableFuture.completedStage(null);
b = true;
} catch (NoSuchMethodError e) {
b = false;
}
IS_JAVA9_PLUS = b;

final CompletableFuture<Integer> cf = completedFuture(42);
try {
// `exceptionallyCompose` is the new method of CompletableFuture since java 12
cf.exceptionallyCompose(v -> cf);
b = true;
} catch (NoSuchMethodError e) {
b = false;
}
IS_JAVA12_PLUS = b;

try {
// `resultNow` is the new method of CompletableFuture since java 19
cf.resultNow();
b = true;
} catch (NoSuchMethodError e) {
b = false;
}
IS_JAVA19_PLUS = b;
// `List.reversed` is the new method since java 21
private static final boolean IS_JAVA21_PLUS = methodExists(() -> new ArrayList<>().reversed());

private static boolean methodExists(Runnable methodCallCheck) {
try {
// `List.reversed` is the new method since java 21
new ArrayList<>().reversed();
b = true;
methodCallCheck.run();
return true;
} catch (NoSuchMethodError e) {
b = false;
return false;
}
IS_JAVA21_PLUS = b;
}

private CompletableFutureUtils() {
Expand Down

0 comments on commit 40d882a

Please sign in to comment.