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 default implementations of deprecated methods of BuilableItem and Item. #3142

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
31 changes: 21 additions & 10 deletions core/src/main/java/hudson/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
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;
Expand Down Expand Up @@ -1142,13 +1143,17 @@ public static List<TopLevelItem> getAllTopLevelItems(ItemGroup root) {
* 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 ? " » " : "/";
Expand Down Expand Up @@ -1182,7 +1187,7 @@ public static String getRelativeNameFrom(Item p, ItemGroup g, boolean useDisplay

if (gr instanceof Item)
i = (Item)gr;
else
else // Parent is a group, but not an item
return null;
}
}
Expand All @@ -1192,11 +1197,14 @@ public static String getRelativeNameFrom(Item p, ItemGroup g, boolean useDisplay
*
* @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);
}

Expand All @@ -1205,12 +1213,15 @@ public static String getRelativeNameFrom(Item p, ItemGroup g) {
* 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);
}

Expand Down
21 changes: 2 additions & 19 deletions core/src/main/java/hudson/model/AbstractItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -381,21 +381,6 @@ public String getRelativeNameFromGroup(ItemGroup p) {
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.
Expand Down Expand Up @@ -470,12 +455,10 @@ public String getSearchUrl() {
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();
}

/**
Expand Down
10 changes: 8 additions & 2 deletions core/src/main/java/hudson/model/BuildableItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@ public interface BuildableItem extends Item, Task {
* Use {@link #scheduleBuild(Cause)}. Since 1.283
*/
@Deprecated
boolean scheduleBuild();
default boolean scheduleBuild() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tabs vs spaces :( Should I use tabs or refactor the methods?

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);
}
37 changes: 32 additions & 5 deletions core/src/main/java/hudson/model/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,6 +42,9 @@
import hudson.security.AccessControlled;
import hudson.util.Secret;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
* Basic configuration unit in Hudson.
*
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -207,7 +232,9 @@ public interface Item extends PersistenceRoot, SearchableModelObject, AccessCont
*
* @since 1.374
*/
default void onCreatedFromScratch() {}
default void onCreatedFromScratch() {
// do nothing by default
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would there not be default no-op impls of onLoad and onCopiedFrom then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about that, but I decided not to touch it for now since I was unable to find noop implementations.

}

/**
* Save the settings to a file.
Expand Down
18 changes: 0 additions & 18 deletions core/src/main/java/jenkins/model/ParameterizedJobMixIn.java
Original file line number Diff line number Diff line change
Expand Up @@ -377,29 +377,11 @@ default String getBuildNowText() {
*/
Map<TriggerDescriptor,Trigger<?>> 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);
Expand Down