Skip to content

Commit

Permalink
[FIXED JENKINS-33683] Clarify Action methods can return null
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergondza committed Mar 21, 2016
1 parent e4d7da6 commit eefafb2
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/Functions.java
Expand Up @@ -1540,7 +1540,7 @@ public static String joinPath(String... components) {
*/
public static String getActionUrl(String itUrl,Action action) {
String urlName = action.getUrlName();
if(urlName==null) return null; // to avoid NPE and fail to render the whole page
if(urlName==null) return null; // Should not be displayed
try {
if (new URI(urlName).isAbsolute()) {
return urlName;
Expand Down
12 changes: 8 additions & 4 deletions core/src/main/java/hudson/model/Action.java
Expand Up @@ -25,6 +25,8 @@

import hudson.Functions;

import javax.annotation.CheckForNull;

/**
* Object that contributes additional information, behaviors, and UIs to {@link ModelObject}
* (more specifically {@link Actionable} objects, which most interesting {@link ModelObject}s are.)
Expand Down Expand Up @@ -90,15 +92,17 @@ public interface Action extends ModelObject {
* @see Functions#isAnonymous()
* @see Functions#getIconFilePath(Action)
*/
String getIconFileName();
@CheckForNull String getIconFileName();

/**
* Gets the string to be displayed.
*
* The convention is to capitalize the first letter of each word,
* such as "Test Result".
* such as "Test Result".
*
* @return Can be null in case the action is hidden.
*/
String getDisplayName();
@CheckForNull String getDisplayName();

/**
* Gets the URL path name.
Expand All @@ -124,5 +128,5 @@ public interface Action extends ModelObject {
* (when you do that, be sure to also return null from {@link #getIconFileName()}.
* @see Functions#getActionUrl(String, Action)
*/
String getUrlName();
@CheckForNull String getUrlName();
}
13 changes: 8 additions & 5 deletions core/src/main/java/hudson/model/ManagementLink.java
Expand Up @@ -33,6 +33,9 @@
import java.util.List;
import org.kohsuke.stapler.interceptor.RequirePOST;

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

/**
* Extension point to add icon to <tt>http://server/hudson/manage</tt> page.
*
Expand All @@ -59,7 +62,7 @@ public abstract class ManagementLink implements ExtensionPoint, Action {
* This is useful for defining {@link ManagementLink} that only shows up under
* certain circumstances.
*/
public abstract String getIconFileName();
public abstract @CheckForNull String getIconFileName();

/**
* Returns a short description of what this link does. This text
Expand All @@ -80,7 +83,7 @@ public String getDescription() {
* so relative paths are interpreted against the root {@link Jenkins} object.
*/
@Override
public abstract String getUrlName();
public abstract @CheckForNull String getUrlName();

/**
* Allows implementations to request that this link show a confirmation dialog, and use POST if confirmed.
Expand All @@ -102,16 +105,16 @@ public boolean getRequiresConfirmation() {
public static final List<ManagementLink> LIST = ExtensionListView.createList(ManagementLink.class);

/**
* All regsitered instances.
* All registered instances.
*/
public static ExtensionList<ManagementLink> all() {
public static @Nonnull ExtensionList<ManagementLink> all() {
return ExtensionList.lookup(ManagementLink.class);
}

/**
* @return permission required for user to access this management link, in addition to {@link Jenkins#ADMINISTER}
*/
public Permission getRequiredPermission() {
public @CheckForNull Permission getRequiredPermission() {
return null;
}

Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/hudson/model/User.java
Expand Up @@ -73,6 +73,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand Down Expand Up @@ -903,11 +904,11 @@ public Descriptor getDescriptorByName(String className) {

public Object getDynamic(String token) {
for(Action action: getTransientActions()){
if(action.getUrlName().equals(token))
if(Objects.equals(action.getUrlName(), token))
return action;
}
for(Action action: getPropertyActions()){
if(action.getUrlName().equals(token))
if(Objects.equals(action.getUrlName(), token))
return action;
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/View.java
Expand Up @@ -544,7 +544,7 @@ public Object getDynamic(String token) {
for (Action a : getActions()) {
String url = a.getUrlName();
if (url==null) continue;
if(a.getUrlName().equals(token))
if (url.equals(token))
return a;
}
return null;
Expand Down
8 changes: 6 additions & 2 deletions core/src/main/java/jenkins/model/Jenkins.java
Expand Up @@ -191,6 +191,8 @@
import hudson.views.MyViewsTabBar;
import hudson.views.ViewsTabBar;
import hudson.widgets.Widget;

import java.util.Objects;
import java.util.TimerTask;
import java.util.concurrent.CountDownLatch;
import jenkins.ExtensionComponentSet;
Expand Down Expand Up @@ -3274,7 +3276,7 @@ public Object getDynamic(String token) {
return a;
}
for (Action a : getManagementLinks())
if(a.getUrlName().equals(token))
if (Objects.equals(a.getUrlName(), token))
return a;
return null;
}
Expand Down Expand Up @@ -4292,7 +4294,9 @@ public Collection<String> getUnprotectedRootActions() {
// TODO consider caching (expiring cache when actions changes)
for (Action a : getActions()) {
if (a instanceof UnprotectedRootAction) {
names.add(a.getUrlName());
String url = a.getUrlName();
if (url == null) continue;
names.add(url);
}
}
return names;
Expand Down
2 changes: 0 additions & 2 deletions test/src/test/java/hudson/model/ManagementLinkTest.java
Expand Up @@ -35,9 +35,7 @@
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.WebClient;
import org.jvnet.hudson.test.TestExtension;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.util.List;

/**
Expand Down

0 comments on commit eefafb2

Please sign in to comment.