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 1 commit
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
18 changes: 13 additions & 5 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 @@ -1143,11 +1144,14 @@ public static List<TopLevelItem> getAllTopLevelItems(ItemGroup root) {
*
* @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 g the ItemGroup used as point of reference for the item.
* If the group is not specified, item's path will be used.
Copy link
Member

Choose a reason for hiding this comment

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

mark it @CheckForNull please

* @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 ItemGroup}.
Copy link
Member Author

Choose a reason for hiding this comment

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

Just documented it while I was around

Copy link
Member

Choose a reason for hiding this comment

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

Huh? A parent by definition is an ItemGroup. I guess you mean is not an Item?

*/
@Nullable
public static String getRelativeNameFrom(Item p, ItemGroup g, boolean useDisplayName) {
Copy link
Member

Choose a reason for hiding this comment

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

Really these methods should have been in Items (cf. #3148). Too late I suppose, unless we deprecate the Functions versions, which seems too intrusive.

if (p == null) return null;
if (g == null) return useDisplayName ? p.getFullDisplayName() : p.getFullName();
Expand Down Expand Up @@ -1182,7 +1186,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 @@ -1194,8 +1198,10 @@ public static String getRelativeNameFrom(Item p, ItemGroup g, boolean useDisplay
* @param p the Item we want the relative display name
* @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 ItemGroup}.
Copy link
Member

Choose a reason for hiding this comment

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

ditto

*/
@Nullable
public static String getRelativeNameFrom(Item p, ItemGroup g) {
return getRelativeNameFrom(p, g, false);
}
Expand All @@ -1208,8 +1214,10 @@ public static String getRelativeNameFrom(Item p, ItemGroup g) {
* @param p the Item we want the relative display name
* @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 ItemGroup}.
Copy link
Member

Choose a reason for hiding this comment

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

ditto

*/
@Nullable
public static String getRelativeDisplayNameFrom(Item p, 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);
}
29 changes: 24 additions & 5 deletions core/src/main/java/hudson/model/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
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;
Expand Down Expand Up @@ -131,18 +133,28 @@ public interface Item extends PersistenceRoot, SearchableModelObject, AccessCont
/**
* Gets the relative name to this item from the specified group.
*
* @param g
* The ItemGroup instance used as context to evaluate the relative name of this AbstractItem
Copy link
Member

Choose a reason for hiding this comment

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

Not necessarily an AbstractItem. Anyway use @link, or plain words.

* @return
* The name of the current item, relative to p. Nested ItemGroups are separated by {@code /} character.
Copy link
Member

Choose a reason for hiding this comment

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

use @link

* @since 1.419
* @return
* String like "../foo/bar"
* String like "../foo/bar".
* {@code null} if item parents is not an {@link ItemGroup}.
Copy link
Member

Choose a reason for hiding this comment

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

probably s/ItemGroup/Item/

*/
String getRelativeNameFrom(ItemGroup g);
default String getRelativeNameFrom(ItemGroup g) {
return Functions.getRelativeNameFrom(this, g);
}

/**
* Short for {@code getRelativeNameFrom(item.getParent())}
*
* @since 1.419
*/
String getRelativeNameFrom(Item item);
default String getRelativeNameFrom(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 +192,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 +224,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