Inline WidgetFactory#createWidgetsFor and be a bit more lenient.

This commit is contained in:
Vincent Latombe committed 2023-05-23 05:26:41 +02:00
1 parent 815d07625a
commit 5e44136ffb
2 files changed
+16 -22

No files matched your search

@@ -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<Widget> result = new ArrayList<>();
WidgetFactory
.factoriesFor(getClass(), Widget.class)
.forEach(wf -> result.addAll(wf.createWidgetsFor(this)));
.forEach(wf -> {
try {
Collection<Widget> 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 <code>null</code> if it does not exist.
*
* Defaults to iterating on widgets and filtering based on the defined <code>urlName</code>.
*
* @param name the name of the widget within the current context.
@@ -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<T extends HasWidgets, W extends Widget> 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<T extends HasWidgets, W extends Widget> impl
return result;
}
public Collection<W> createWidgetsFor(HasWidgets hasWidgets) {
try {
Collection<W> 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();
}
}
}