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

Add ExtensionList.lookupFirst convenience method. #8735

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
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
25 changes: 25 additions & 0 deletions core/src/main/java/hudson/ExtensionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,31 @@ public static <T> ExtensionList<T> create(Jenkins jenkins, Class<T> type) {
return all.get(0);
}

/**
* Convenience method allowing lookup of the instance of a given type with the highest ordinal.
* Equivalent to {@code ExtensionList.lookup(type).get(0)} if there is at least one instance,
* and throws an {@link IllegalStateException} otherwise if no instance could be found.
*
* @param type The type to look up.
* @return the singleton instance of the given type in its list.
* @throws IllegalStateException if there are no instances
*
* @since TODO
*/
public static @NonNull <U> U lookupFirst(Class<U> type) {
var all = lookup(type);
if (!all.isEmpty()) {
return all.get(0);
} else {
if (Main.isUnitTest) {
throw new IllegalStateException("Found no instances of " + type.getName() +
" registered (possible annotation processor issue); try using `mvn clean test -Dtest=…` rather than an IDE test runner");
} else {
throw new IllegalStateException("Found no instances of " + type.getName() + " registered");
}
}
}

/**
* Places to store static-scope legacy instances.
*/
Expand Down
5 changes: 3 additions & 2 deletions test/src/test/java/lib/form/OptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import hudson.ExtensionList;
import hudson.model.RootAction;
import org.htmlunit.html.DomElement;
import org.htmlunit.html.DomNodeList;
Expand Down Expand Up @@ -216,7 +217,7 @@ private void checkJelly(int mode, String msgToInject,
String bodyContainsExpected, String valueContainsExpected,
boolean checkExactCharacters,
boolean withValueTrue, boolean withValueFalse) throws Exception {
UsingJellyView view = j.jenkins.getExtensionList(UsingJellyView.class).get(0);
UsingJellyView view = ExtensionList.lookupFirst(UsingJellyView.class);
view.setMode(mode);
view.setInjection(msgToInject);

Expand All @@ -242,7 +243,7 @@ private void checkGroovy(int mode, String msgToInject,
String bodyContainsExpected, String valueContainsExpected,
boolean checkExactCharacters,
boolean withValueTrue, boolean withValueFalse) throws Exception {
UsingGroovyView view = j.jenkins.getExtensionList(UsingGroovyView.class).get(0);
UsingGroovyView view = ExtensionList.lookupFirst(UsingGroovyView.class);
view.setMode(mode);
view.setInjection(msgToInject);

Expand Down