Skip to content

Commit

Permalink
Add ExtensionList.lookupFirst convenience method. (#8735)
Browse files Browse the repository at this point in the history
* Add `ExtensionList.lookupFirst` convenience method.

When introducing an extension point that is meant to allow overriding a default behaviour with another implementation,
generally the caller only cares about looking up only one implementation, the one with the highest ordinal.

* Fix javadoc

Co-authored-by: Jesse Glick <[email protected]>

---------

Co-authored-by: Jesse Glick <[email protected]>
  • Loading branch information
Vlatombe and jglick authored Dec 5, 2023
1 parent 62d22f3 commit 0f0d81b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
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

0 comments on commit 0f0d81b

Please sign in to comment.