diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/cgit/GitCommandsExecutor.java b/src/main/java/org/jenkinsci/plugins/gitclient/cgit/GitCommandsExecutor.java index a57d906495..fae31c820e 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/cgit/GitCommandsExecutor.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/cgit/GitCommandsExecutor.java @@ -1,5 +1,7 @@ package org.jenkinsci.plugins.gitclient.cgit; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.Collection; import java.util.Iterator; import java.util.concurrent.Callable; @@ -39,7 +41,7 @@ public void invokeAll(Collection> commands) throws GitException, ExecutorService executorService = null; try { if (threads == 1) { - executorService = MoreExecutors.sameThreadExecutor(); + executorService = newExecutorService(); } else { ThreadFactory threadFactory = new ExceptionCatchingThreadFactory(new NamingThreadFactory(new DaemonThreadFactory(), GitCommandsExecutor.class.getSimpleName())); executorService = Executors.newFixedThreadPool(threads, threadFactory); @@ -55,6 +57,27 @@ public void invokeAll(Collection> commands) throws GitException, } } + /** + * Returns an {@link ExecutorService} to be used as a parameter in other methods. + * It calls {@code MoreExecutors#sameThreadExecutor} or falls back to {@code MoreExecutors#newDirectExecutorService} + * for compatibility with newer (> 18.0) versions of guava. + */ + private static ExecutorService newExecutorService() { + try { + try { + // guava older than 18 + Method method = MoreExecutors.class.getMethod("sameThreadExecutor"); + return (ExecutorService) method.invoke(null); + } catch (NoSuchMethodException e) { + // TODO invert this to prefer the newer guava method once guava is upgraded in Jenkins core, (or update jenkins.version) + Method method = MoreExecutors.class.getMethod("newDirectExecutorService"); + return (ExecutorService) method.invoke(null); + } + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e ) { + throw new RuntimeException(e); + } + } + private void invokeAll(ExecutorService executorService, Collection> commands) throws InterruptedException { CompletionService completionService = new ExecutorCompletionService<>(executorService); Iterator> remainingCommands = commands.iterator();