From c363cc8b4c9adef22fa9a238f2b272dc88f16733 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Mu=C3=B1iz?= Date: Thu, 4 Mar 2021 17:29:03 +0100 Subject: [PATCH] Fallback to old guava API if needed and avoid core bump --- pom.xml | 21 +-------- .../workflow/flow/FlowExecutionList.java | 47 +++++++++++++------ .../workflow/graphanalysis/ForkScanner.java | 2 +- .../graphanalysis/NodeStepNamePredicate.java | 2 +- .../graphanalysis/NodeStepTypePredicate.java | 2 +- 5 files changed, 37 insertions(+), 37 deletions(-) diff --git a/pom.xml b/pom.xml index 2e351f07..e6e28a3e 100644 --- a/pom.xml +++ b/pom.xml @@ -64,7 +64,7 @@ 2.42 -SNAPSHOT - 2.273-rc30689.09147672b85b + 2.176.4 8 false true @@ -105,11 +105,6 @@ workflow-basic-steps test - - org.jenkins-ci.plugins - trilead-api - test - org.jenkins-ci.plugins.workflow @@ -139,18 +134,4 @@ test - - - - - org.apache.maven.plugins - maven-surefire-plugin - - - - InjectedTest - - - - diff --git a/src/main/java/org/jenkinsci/plugins/workflow/flow/FlowExecutionList.java b/src/main/java/org/jenkinsci/plugins/workflow/flow/FlowExecutionList.java index f70c77de..cb80b72b 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/flow/FlowExecutionList.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/flow/FlowExecutionList.java @@ -21,10 +21,13 @@ import java.io.File; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.concurrent.CancellationException; +import java.util.concurrent.ExecutorService; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.TimeUnit; import java.util.logging.Level; @@ -181,11 +184,9 @@ public void onLoaded() { Futures.addCallback(e.getCurrentExecutions(false), new FutureCallback>() { @Override public void onSuccess(List result) { - if (result != null) { - LOGGER.log(FINE, "Will resume {0}", result); - for (StepExecution se : result) { - se.onResume(); - } + LOGGER.log(FINE, "Will resume {0}", result); + for (StepExecution se : result) { + se.onResume(); } } @@ -197,7 +198,7 @@ public void onFailure(Throwable t) { LOGGER.log(WARNING, "Failed to load " + e, t); } } - }, MoreExecutors.newDirectExecutorService()); + }, newExecutorService()); } } } @@ -220,13 +221,11 @@ public ListenableFuture apply(final Function f) { Futures.addCallback(execs,new FutureCallback>() { @Override public void onSuccess(List result) { - if (result != null) { - for (StepExecution e : result) { - try { - f.apply(e); - } catch (RuntimeException x) { - LOGGER.log(Level.WARNING, null, x); - } + for (StepExecution e : result) { + try { + f.apply(e); + } catch (RuntimeException x) { + LOGGER.log(Level.WARNING, null, x); } } } @@ -235,7 +234,7 @@ public void onSuccess(List result) { public void onFailure(Throwable t) { LOGGER.log(Level.WARNING, null, t); } - }, MoreExecutors.newDirectExecutorService()); + }, newExecutorService()); } return Futures.allAsList(all); @@ -259,4 +258,24 @@ public void onFailure(Throwable t) { executor.awaitTermination(1, TimeUnit.MINUTES); } + /** + * Returns an {@link ExecutorService} to be used as a parameter in other methods. + * It calls {@code MoreExecutors#newDirectExecutorService} or falls back to {@code MoreExecutors#sameThreadExecutor} + * for compatibility with older (< 18.0) versions of guava. + */ + private static ExecutorService newExecutorService() { + try { + try { + Method method = MoreExecutors.class.getMethod("newDirectExecutorService"); + return (ExecutorService) method.invoke(null); + } catch (NoSuchMethodException e) { + // guava older than 18, fallback to `sameThreadExecutor` + Method method = MoreExecutors.class.getMethod("sameThreadExecutor"); + return (ExecutorService) method.invoke(null); + } + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e ) { + throw new RuntimeException(e); + } + } + } diff --git a/src/main/java/org/jenkinsci/plugins/workflow/graphanalysis/ForkScanner.java b/src/main/java/org/jenkinsci/plugins/workflow/graphanalysis/ForkScanner.java index 322fd8d4..b161deb1 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/graphanalysis/ForkScanner.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/graphanalysis/ForkScanner.java @@ -26,7 +26,6 @@ import com.google.common.base.Predicate; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import org.checkerframework.checker.nullness.qual.Nullable; import org.jenkinsci.plugins.workflow.actions.ThreadNameAction; import org.jenkinsci.plugins.workflow.actions.TimingAction; import org.jenkinsci.plugins.workflow.graph.BlockEndNode; @@ -37,6 +36,7 @@ import javax.annotation.CheckForNull; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import javax.annotation.concurrent.NotThreadSafe; import java.util.ArrayDeque; import java.util.ArrayList; diff --git a/src/main/java/org/jenkinsci/plugins/workflow/graphanalysis/NodeStepNamePredicate.java b/src/main/java/org/jenkinsci/plugins/workflow/graphanalysis/NodeStepNamePredicate.java index b4ae91bd..9890a8e1 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/graphanalysis/NodeStepNamePredicate.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/graphanalysis/NodeStepNamePredicate.java @@ -24,7 +24,6 @@ package org.jenkinsci.plugins.workflow.graphanalysis; import com.google.common.base.Predicate; -import org.checkerframework.checker.nullness.qual.Nullable; import org.jenkinsci.plugins.workflow.graph.FlowEndNode; import org.jenkinsci.plugins.workflow.graph.FlowNode; import org.jenkinsci.plugins.workflow.graph.FlowStartNode; @@ -32,6 +31,7 @@ import org.jenkinsci.plugins.workflow.steps.StepDescriptor; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; diff --git a/src/main/java/org/jenkinsci/plugins/workflow/graphanalysis/NodeStepTypePredicate.java b/src/main/java/org/jenkinsci/plugins/workflow/graphanalysis/NodeStepTypePredicate.java index 472504e5..c7ae8d7b 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/graphanalysis/NodeStepTypePredicate.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/graphanalysis/NodeStepTypePredicate.java @@ -24,7 +24,6 @@ package org.jenkinsci.plugins.workflow.graphanalysis; import com.google.common.base.Predicate; -import org.checkerframework.checker.nullness.qual.Nullable; import org.jenkinsci.plugins.workflow.graph.FlowEndNode; import org.jenkinsci.plugins.workflow.graph.FlowNode; import org.jenkinsci.plugins.workflow.graph.FlowStartNode; @@ -32,6 +31,7 @@ import org.jenkinsci.plugins.workflow.steps.StepDescriptor; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;