From 5e44136ffb013d3eb75ef19daa3da7f83a39a307 Mon Sep 17 00:00:00 2001 From: Vincent Latombe Date: Tue, 23 May 2023 05:26:41 +0200 Subject: [PATCH] Inline WidgetFactory#createWidgetsFor and be a bit more lenient. --- .../main/java/jenkins/widgets/HasWidgets.java | 18 +++++++++++++++-- .../java/jenkins/widgets/WidgetFactory.java | 20 ------------------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/jenkins/widgets/HasWidgets.java b/core/src/main/java/jenkins/widgets/HasWidgets.java index f9ec88306c..3e2342b48a 100644 --- a/core/src/main/java/jenkins/widgets/HasWidgets.java +++ b/core/src/main/java/jenkins/widgets/HasWidgets.java @@ -27,8 +27,10 @@ package jenkins.widgets; import edu.umd.cs.findbugs.annotations.CheckForNull; import hudson.widgets.Widget; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.logging.Level; import java.util.logging.Logger; import jenkins.security.stapler.StaplerAccessibleType; import jenkins.security.stapler.StaplerDispatchable; @@ -48,13 +50,25 @@ public interface HasWidgets { List result = new ArrayList<>(); WidgetFactory .factoriesFor(getClass(), Widget.class) - .forEach(wf -> result.addAll(wf.createWidgetsFor(this))); + .forEach(wf -> { + try { + Collection wfResult = wf.createFor(wf.type().cast(this)); + for (Widget w : wfResult) { + if (wf.widgetType().isInstance(w)) { + result.add(w); + } else { + LOGGER.log(Level.WARNING, "Widget from {0} for {1} included {2} not assignable to {3}", new Object[] {wf, this, w, wf.widgetType()}); + } + } + } catch (RuntimeException e) { + LOGGER.log(Level.WARNING, "Could not load all widgets from " + wf + " for " + this, e); + } + }); return Collections.unmodifiableList(result); } /** * Returns the named widget, or null if it does not exist. - * * Defaults to iterating on widgets and filtering based on the defined urlName. * * @param name the name of the widget within the current context. diff --git a/core/src/main/java/jenkins/widgets/WidgetFactory.java b/core/src/main/java/jenkins/widgets/WidgetFactory.java index 73c9adcd99..932d8de97d 100644 --- a/core/src/main/java/jenkins/widgets/WidgetFactory.java +++ b/core/src/main/java/jenkins/widgets/WidgetFactory.java @@ -30,11 +30,8 @@ import hudson.ExtensionPoint; import hudson.widgets.Widget; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.List; import java.util.Set; -import java.util.logging.Level; -import java.util.logging.Logger; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.NoExternalUse; @@ -45,8 +42,6 @@ import org.kohsuke.accmod.restrictions.NoExternalUse; * @since TODO */ public abstract class WidgetFactory implements ExtensionPoint { - private static final Logger LOGGER = Logger.getLogger(WidgetFactory.class.getName()); - /** * The type of object this factory cares about. * Declared separately, rather than by having {@link #createFor} do a check-cast, @@ -84,19 +79,4 @@ public abstract class WidgetFactory impl return result; } - public Collection createWidgetsFor(HasWidgets hasWidgets) { - try { - Collection result = createFor(type().cast(hasWidgets)); - for (W w : result) { - if (!widgetType().isInstance(w)) { - LOGGER.log(Level.WARNING, "Widgets from {0} for {1} included {2} not assignable to {3}", new Object[] {this, hasWidgets, w, widgetType()}); - return Collections.emptySet(); - } - } - return result; - } catch (RuntimeException e) { - LOGGER.log(Level.WARNING, "Could not load widgets from " + this + " for " + hasWidgets, e); - return Collections.emptySet(); - } - } }