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 2 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
19 changes: 11 additions & 8 deletions core/src/main/java/hudson/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -1143,16 +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 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.
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".
* {@code null} if item is null or if one of its parents is not an {@link ItemGroup}.
* {@code null} if item is null or if one of its parents is not an {@link Item}.
*/
@Nullable
public static String getRelativeNameFrom(Item p, ItemGroup g, boolean useDisplayName) {
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 @@ -1196,13 +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".
* {@code null} if the item is {@code null} or if one of its parents is not an {@link ItemGroup}.
* {@code null} if the item is {@code null} or if one of its parents is not an {@link Item}.
*/
@Nullable
public static String getRelativeNameFrom(Item p, ItemGroup g) {
public static String getRelativeNameFrom(@CheckForNull Item p, @CheckForNull ItemGroup g) {
return getRelativeNameFrom(p, g, false);
}

Expand All @@ -1211,14 +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".
* {@code null} if the item is {@code null} or if one of its parents is not an {@link ItemGroup}.
* {@code null} if the item is {@code null} or if one of its parents is not an {@link Item}.
*/
@Nullable
public static String getRelativeDisplayNameFrom(Item p, ItemGroup g) {
public static String getRelativeDisplayNameFrom(@CheckForNull Item p, @CheckForNull ItemGroup g) {
return getRelativeNameFrom(p, g, true);
}

Expand Down
18 changes: 13 additions & 5 deletions core/src/main/java/hudson/model/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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 @@ -41,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 @@ -134,24 +138,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
* 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 ItemGroups are separated by {@code /} character.
* 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".
* {@code null} if item parents is not an {@link ItemGroup}.
* {@code null} if one of item parents is not an {@link Item}.
*/
default String getRelativeNameFrom(ItemGroup g) {
@Nullable
Copy link
Member

Choose a reason for hiding this comment

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

Not actually sure why these are all @Nullable rather than @CheckForNull. Is there some circumstance where a caller would statically know the return value cannot be null?

Copy link
Member Author

@oleg-nenashev oleg-nenashev Nov 27, 2017

Choose a reason for hiding this comment

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

Yes, generally "in all sane cases" assuming you do not resolve names for things like Promoted Builds plugin PromotionProcess instances or pass nulls. I can add @CheckForNull, but it may become just another Jenkins.getInstance()

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
*/
default String getRelativeNameFrom(Item item) {
@Nullable
default String getRelativeNameFrom(@Nonnull Item item) {
return getRelativeNameFrom(item.getParent());

}
Expand Down