Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary use of reflection #965

Merged
merged 1 commit into from
Jan 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
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;
Expand Down Expand Up @@ -41,7 +39,7 @@ public <T> void invokeAll(Collection<Callable<T>> commands) throws GitException,
ExecutorService executorService = null;
try {
if (threads == 1) {
executorService = newExecutorService();
executorService = MoreExecutors.newDirectExecutorService();
} else {
ThreadFactory threadFactory = new ExceptionCatchingThreadFactory(new NamingThreadFactory(new DaemonThreadFactory(), GitCommandsExecutor.class.getSimpleName()));
executorService = Executors.newFixedThreadPool(threads, threadFactory);
Expand All @@ -57,27 +55,6 @@ public <T> void invokeAll(Collection<Callable<T>> 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 <T> void invokeAll(ExecutorService executorService, Collection<Callable<T>> commands) throws InterruptedException {
CompletionService<T> completionService = new ExecutorCompletionService<>(executorService);
Iterator<Callable<T>> remainingCommands = commands.iterator();
Expand Down