1717import java .io .Serializable ;
1818import java .lang .ref .Reference ;
1919import java .lang .ref .WeakReference ;
20+ import java .lang .reflect .InvocationTargetException ;
21+ import java .lang .reflect .Method ;
2022import java .util .List ;
2123import java .util .UUID ;
2224import java .util .concurrent .ExecutionException ;
25+ import java .util .concurrent .ExecutorService ;
2326import java .util .concurrent .ScheduledFuture ;
2427import java .util .concurrent .TimeUnit ;
2528import 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