From 8824857752f89ebaddf7a814c65b31b9c155ab2f Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Fri, 1 Dec 2017 10:00:49 +0300 Subject: [PATCH] Add default implementations of deprecated methods of BuilableItem and Item. (#3142) * Add default implementations to deprecated methods of BuilableItem and Item. Currently the interface requires the API user to implement already deprecated methods. It does not make much sense, and the API could be simplified. * Address comments from @jglick --- core/src/main/java/hudson/Functions.java | 31 +++++++++++----- .../main/java/hudson/model/AbstractItem.java | 21 +---------- .../main/java/hudson/model/BuildableItem.java | 10 ++++- core/src/main/java/hudson/model/Item.java | 37 ++++++++++++++++--- .../jenkins/model/ParameterizedJobMixIn.java | 18 --------- 5 files changed, 63 insertions(+), 54 deletions(-) diff --git a/core/src/main/java/hudson/Functions.java b/core/src/main/java/hudson/Functions.java index 9203efd03c..c9c5e8545b 100644 --- a/core/src/main/java/hudson/Functions.java +++ b/core/src/main/java/hudson/Functions.java @@ -125,6 +125,7 @@ import java.util.logging.Logger; import java.util.logging.SimpleFormatter; import java.util.regex.Pattern; +import javax.annotation.Nullable; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; @@ -1142,13 +1143,17 @@ public class Functions { * Gets the relative name or display name to the given item from the specified group. * * @since 1.515 - * @param p the Item we want the relative display name - * @param g the ItemGroup used as point of reference for the item + * @param p the Item we want the relative display name. + * If {@code null}, a {@code null} will be returned by the method + * @param g the ItemGroup used as point of reference for the item. + * If the group is not specified, item's path will be used. * @param useDisplayName if true, returns a display name, otherwise returns a name * @return - * String like "foo » bar" + * String like "foo » bar". + * {@code null} if item is null or if one of its parents is not an {@link Item}. */ - public static String getRelativeNameFrom(Item p, ItemGroup g, boolean useDisplayName) { + @Nullable + public static String getRelativeNameFrom(@CheckForNull Item p, @CheckForNull ItemGroup g, boolean useDisplayName) { if (p == null) return null; if (g == null) return useDisplayName ? p.getFullDisplayName() : p.getFullName(); String separationString = useDisplayName ? " » " : "/"; @@ -1182,7 +1187,7 @@ public class Functions { if (gr instanceof Item) i = (Item)gr; - else + else // Parent is a group, but not an item return null; } } @@ -1192,11 +1197,14 @@ public class Functions { * * @since 1.515 * @param p the Item we want the relative display name + * If {@code null}, the method will immediately return {@code null}. * @param g the ItemGroup used as point of reference for the item * @return - * String like "foo/bar" + * String like "foo/bar". + * {@code null} if the item is {@code null} or if one of its parents is not an {@link Item}. */ - public static String getRelativeNameFrom(Item p, ItemGroup g) { + @Nullable + public static String getRelativeNameFrom(@CheckForNull Item p, @CheckForNull ItemGroup g) { return getRelativeNameFrom(p, g, false); } @@ -1205,12 +1213,15 @@ public class Functions { * Gets the relative display name to the given item from the specified group. * * @since 1.512 - * @param p the Item we want the relative display name + * @param p the Item we want the relative display name. + * If {@code null}, the method will immediately return {@code null}. * @param g the ItemGroup used as point of reference for the item * @return - * String like "Foo » Bar" + * String like "Foo » Bar". + * {@code null} if the item is {@code null} or if one of its parents is not an {@link Item}. */ - public static String getRelativeDisplayNameFrom(Item p, ItemGroup g) { + @Nullable + public static String getRelativeDisplayNameFrom(@CheckForNull Item p, @CheckForNull ItemGroup g) { return getRelativeNameFrom(p, g, true); } diff --git a/core/src/main/java/hudson/model/AbstractItem.java b/core/src/main/java/hudson/model/AbstractItem.java index 01ef3a0bbf..4c026d5f31 100644 --- a/core/src/main/java/hudson/model/AbstractItem.java +++ b/core/src/main/java/hudson/model/AbstractItem.java @@ -381,21 +381,6 @@ public abstract class AbstractItem extends Actionable implements Item, HttpDelet return getRelativeNameFrom(p); } - /** - * @param p - * The ItemGroup instance used as context to evaluate the relative name of this AbstractItem - * @return - * The name of the current item, relative to p. - * Nested ItemGroups are separated by / character. - */ - public String getRelativeNameFrom(ItemGroup p) { - return Functions.getRelativeNameFrom(this, p); - } - - public String getRelativeNameFrom(Item item) { - return getRelativeNameFrom(item.getParent()); - } - /** * Called right after when a {@link Item} is loaded from disk. * This is an opportunity to do a post load processing. @@ -470,12 +455,10 @@ public abstract class AbstractItem extends Actionable implements Item, HttpDelet return getShortUrl(); } + @Override @Exported(visibility=999,name="url") public final String getAbsoluteUrl() { - String r = Jenkins.getInstance().getRootUrl(); - if(r==null) - throw new IllegalStateException("Root URL isn't configured yet. Cannot compute absolute URL."); - return Util.encode(r+getUrl()); + return Item.super.getAbsoluteUrl(); } /** diff --git a/core/src/main/java/hudson/model/BuildableItem.java b/core/src/main/java/hudson/model/BuildableItem.java index ef845e1175..0d65214e43 100644 --- a/core/src/main/java/hudson/model/BuildableItem.java +++ b/core/src/main/java/hudson/model/BuildableItem.java @@ -40,13 +40,19 @@ public interface BuildableItem extends Item, Task { * Use {@link #scheduleBuild(Cause)}. Since 1.283 */ @Deprecated - boolean scheduleBuild(); + default boolean scheduleBuild() { + return scheduleBuild(new Cause.LegacyCodeCause()); + } + boolean scheduleBuild(Cause c); /** * @deprecated * Use {@link #scheduleBuild(int, Cause)}. Since 1.283 */ @Deprecated - boolean scheduleBuild(int quietPeriod); + default boolean scheduleBuild(int quietPeriod) { + return scheduleBuild(quietPeriod, new Cause.LegacyCodeCause()); + } + boolean scheduleBuild(int quietPeriod, Cause c); } diff --git a/core/src/main/java/hudson/model/Item.java b/core/src/main/java/hudson/model/Item.java index d0c438744d..8187d608bd 100644 --- a/core/src/main/java/hudson/model/Item.java +++ b/core/src/main/java/hudson/model/Item.java @@ -25,9 +25,12 @@ package hudson.model; import hudson.Functions; +import hudson.Util; +import jenkins.model.Jenkins; import jenkins.util.SystemProperties; import hudson.security.PermissionScope; import jenkins.util.io.OnMaster; +import jline.internal.Nullable; import org.kohsuke.stapler.StaplerRequest; import java.io.IOException; @@ -39,6 +42,9 @@ import hudson.security.PermissionGroup; import hudson.security.AccessControlled; import hudson.util.Secret; +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; + /** * Basic configuration unit in Hudson. * @@ -131,18 +137,32 @@ public interface Item extends PersistenceRoot, SearchableModelObject, AccessCont /** * Gets the relative name to this item from the specified group. * + * @param g + * The {@link ItemGroup} instance used as context to evaluate the relative name of this item + * @return + * The name of the current item, relative to p. Nested {@link ItemGroup}s are separated by {@code /} character. * @since 1.419 * @return - * String like "../foo/bar" + * String like "../foo/bar". + * {@code null} if one of item parents is not an {@link Item}. */ - String getRelativeNameFrom(ItemGroup g); + @Nullable + default String getRelativeNameFrom(@CheckForNull ItemGroup g) { + return Functions.getRelativeNameFrom(this, g); + } /** * Short for {@code getRelativeNameFrom(item.getParent())} * + * @return String like "../foo/bar". + * {@code null} if one of item parents is not an {@link Item}. * @since 1.419 */ - String getRelativeNameFrom(Item item); + @Nullable + default String getRelativeNameFrom(@Nonnull Item item) { + return getRelativeNameFrom(item.getParent()); + + } /** * Returns the URL of this item relative to the context root of the application. @@ -180,7 +200,12 @@ public interface Item extends PersistenceRoot, SearchableModelObject, AccessCont * (even this won't work for the same reason, which should be fixed.) */ @Deprecated - String getAbsoluteUrl(); + default String getAbsoluteUrl() { + String r = Jenkins.getInstance().getRootUrl(); + if(r==null) + throw new IllegalStateException("Root URL isn't configured yet. Cannot compute absolute URL."); + return Util.encode(r+getUrl()); + } /** * Called right after when a {@link Item} is loaded from disk. @@ -207,7 +232,9 @@ public interface Item extends PersistenceRoot, SearchableModelObject, AccessCont * * @since 1.374 */ - default void onCreatedFromScratch() {} + default void onCreatedFromScratch() { + // do nothing by default + } /** * Save the settings to a file. diff --git a/core/src/main/java/jenkins/model/ParameterizedJobMixIn.java b/core/src/main/java/jenkins/model/ParameterizedJobMixIn.java index 7cfbc23baf..55301e4f86 100644 --- a/core/src/main/java/jenkins/model/ParameterizedJobMixIn.java +++ b/core/src/main/java/jenkins/model/ParameterizedJobMixIn.java @@ -377,29 +377,11 @@ public abstract class ParameterizedJobMixIn & Param */ Map> getTriggers(); - /** - * @deprecated use {@link #scheduleBuild(Cause)} - */ - @Deprecated - @Override - default boolean scheduleBuild() { - return getParameterizedJobMixIn().scheduleBuild(); - } - @Override default boolean scheduleBuild(Cause c) { return getParameterizedJobMixIn().scheduleBuild(c); } - /** - * @deprecated use {@link #scheduleBuild(int, Cause)} - */ - @Deprecated - @Override - default boolean scheduleBuild(int quietPeriod) { - return getParameterizedJobMixIn().scheduleBuild(quietPeriod); - } - @Override default boolean scheduleBuild(int quietPeriod, Cause c) { return getParameterizedJobMixIn().scheduleBuild(quietPeriod, c);