Skip to content

Commit 0016470

Browse files
committed
Make workflow-basic-steps compatible with Guava 21.0 and newer
1 parent 474cea2 commit 0016470

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

src/main/java/org/jenkinsci/plugins/workflow/steps/TimeoutStepExecution.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
import java.io.Serializable;
1818
import java.lang.ref.Reference;
1919
import java.lang.ref.WeakReference;
20+
import java.lang.reflect.InvocationTargetException;
21+
import java.lang.reflect.Method;
2022
import java.util.List;
2123
import java.util.UUID;
2224
import java.util.concurrent.ExecutionException;
25+
import java.util.concurrent.ExecutorService;
2326
import java.util.concurrent.ScheduledFuture;
2427
import java.util.concurrent.TimeUnit;
2528
import java.util.concurrent.atomic.AtomicBoolean;
@@ -188,7 +191,7 @@ private void cancel() {
188191
LOGGER.log(Level.WARNING, null, x);
189192
}
190193
}
191-
}, MoreExecutors.sameThreadExecutor());
194+
}, newExecutorService());
192195
}
193196
} else {
194197
listener().getLogger().println("Cancelling nested steps due to timeout");
@@ -199,6 +202,30 @@ private void cancel() {
199202
}
200203
}
201204

205+
/**
206+
* Returns an {@link ExecutorService} to be used as a parameter in other methods. It calls
207+
* {@code MoreExecutors#newDirectExecutorService} or falls back to {@code
208+
* MoreExecutors#sameThreadExecutor} for compatibility with older (< 18.0) versions of Guava.
209+
*
210+
* @since TODO
211+
*/
212+
private static ExecutorService newExecutorService() {
213+
try {
214+
try {
215+
// Guava older than 18
216+
Method method = MoreExecutors.class.getMethod("sameThreadExecutor");
217+
return (ExecutorService) method.invoke(null);
218+
} catch (NoSuchMethodException e) {
219+
// TODO Invert this to prefer the newer Guava method once Guava is upgraded in
220+
// Jenkins core.
221+
Method method = MoreExecutors.class.getMethod("newDirectExecutorService");
222+
return (ExecutorService) method.invoke(null);
223+
}
224+
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
225+
throw new RuntimeException(e);
226+
}
227+
}
228+
202229
@Override public String getStatus() {
203230
if (killer == null) {
204231
return "killer task nowhere to be found";

0 commit comments

Comments
 (0)