Skip to content

Commit 0f8bc1f

Browse files
committed
Introduce Nameable, FullyNameable, FullyNameableModelObject interfaces
These correspond to methods `getName`, `getFullName`, `getFullDisplayName` that did not have any common interface to call them, causing some inconvenient usages of switch statements or even reflection in CloudBees CI HA.
1 parent 95da76d commit 0f8bc1f

File tree

16 files changed

+102
-61
lines changed

16 files changed

+102
-61
lines changed

core/src/main/java/hudson/model/AbstractItem.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ protected AbstractItem(ItemGroup parent, String name) {
129129
doSetName(name);
130130
}
131131

132+
@NonNull
132133
@Override
133134
@Exported(visibility = 999)
134135
public String getName() {
@@ -470,6 +471,7 @@ public void movedTo(DirectlyModifiableTopLevelItemGroup destination, AbstractIte
470471
@Override
471472
public abstract Collection<? extends Job> getAllJobs();
472473

474+
@NonNull
473475
@Override
474476
@Exported
475477
public final String getFullName() {

core/src/main/java/hudson/model/Item.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@
3838
import hudson.util.Secret;
3939
import java.io.IOException;
4040
import java.util.Collection;
41+
import jenkins.model.FullyNameable;
42+
import jenkins.model.FullyNameableModelObject;
4143
import jenkins.model.Jenkins;
44+
import jenkins.model.Nameable;
4245
import jenkins.search.SearchGroup;
4346
import jenkins.util.SystemProperties;
4447
import jenkins.util.io.OnMaster;
@@ -73,7 +76,7 @@
7376
* @see Items
7477
* @see ItemVisitor
7578
*/
76-
public interface Item extends PersistenceRoot, SearchableModelObject, AccessControlled, OnMaster {
79+
public interface Item extends PersistenceRoot, FullyNameableModelObject, SearchableModelObject, FullyNameable, Nameable, AccessControlled, OnMaster {
7780
/**
7881
* Gets the parent that contains this item.
7982
*/
@@ -97,6 +100,8 @@ public interface Item extends PersistenceRoot, SearchableModelObject, AccessCont
97100
*
98101
* @see #getFullName()
99102
*/
103+
@NonNull
104+
@Override
100105
String getName();
101106

102107
/**
@@ -110,6 +115,8 @@ public interface Item extends PersistenceRoot, SearchableModelObject, AccessCont
110115
*
111116
* @see jenkins.model.Jenkins#getItemByFullName(String,Class)
112117
*/
118+
@NonNull
119+
@Override
113120
String getFullName();
114121

115122
/**
@@ -127,13 +134,6 @@ public interface Item extends PersistenceRoot, SearchableModelObject, AccessCont
127134
@Override
128135
String getDisplayName();
129136

130-
/**
131-
* Works like {@link #getDisplayName()} but return
132-
* the full path that includes all the display names
133-
* of the ancestors.
134-
*/
135-
String getFullDisplayName();
136-
137137
/**
138138
* Gets the relative name to this item from the specified group.
139139
*

core/src/main/java/hudson/model/ItemGroup.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import java.util.function.Predicate;
3434
import java.util.stream.Collectors;
3535
import java.util.stream.Stream;
36+
import jenkins.model.FullyNameable;
37+
import jenkins.model.FullyNameableModelObject;
3638
import org.springframework.security.access.AccessDeniedException;
3739

3840
/**
@@ -41,19 +43,7 @@
4143
* @author Kohsuke Kawaguchi
4244
* @see ItemGroupMixIn
4345
*/
44-
public interface ItemGroup<T extends Item> extends PersistenceRoot, ModelObject {
45-
/**
46-
* Gets the full name of this {@link ItemGroup}.
47-
*
48-
* @see Item#getFullName()
49-
*/
50-
String getFullName();
51-
52-
/**
53-
* @see Item#getFullDisplayName()
54-
*/
55-
String getFullDisplayName();
56-
46+
public interface ItemGroup<T extends Item> extends FullyNameable, FullyNameableModelObject, PersistenceRoot {
5747
/**
5848
* Gets all the items in this collection in a read-only view.
5949
*/

core/src/main/java/hudson/model/Queue.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
import java.util.logging.Logger;
110110
import java.util.stream.Collectors;
111111
import jenkins.console.WithConsoleUrl;
112+
import jenkins.model.FullyNameableModelObject;
112113
import jenkins.model.Jenkins;
113114
import jenkins.model.queue.AsynchronousExecution;
114115
import jenkins.model.queue.CompositeCauseOfBlockage;
@@ -1882,7 +1883,7 @@ public interface NonBlockingTask extends Task {}
18821883
* design, a {@link Task} must have at least one sub-task.)
18831884
* Most of the time, the primary subtask is the only sub task.
18841885
*/
1885-
public interface Task extends ModelObject, SubTask {
1886+
public interface Task extends FullyNameableModelObject, SubTask {
18861887
/**
18871888
* Returns true if the execution should be blocked
18881889
* for temporary reasons.
@@ -1928,11 +1929,6 @@ default CauseOfBlockage getCauseOfBlockage() {
19281929
*/
19291930
String getName();
19301931

1931-
/**
1932-
* @see hudson.model.Item#getFullDisplayName()
1933-
*/
1934-
String getFullDisplayName();
1935-
19361932
/**
19371933
* Returns task-specific key which is used by the {@link LoadBalancer} to choose one particular executor
19381934
* amongst all the free executors on all possibly suitable nodes.

core/src/main/java/hudson/model/queue/MappingWorksheet.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static java.lang.Math.max;
2828

2929
import com.google.common.collect.Iterables;
30+
import edu.umd.cs.findbugs.annotations.NonNull;
3031
import hudson.model.Computer;
3132
import hudson.model.Executor;
3233
import hudson.model.Label;
@@ -48,6 +49,7 @@
4849
import java.util.List;
4950
import java.util.Map;
5051
import java.util.logging.Logger;
52+
import jenkins.model.Nameable;
5153

5254
/**
5355
* Defines a mapping problem for answering "where do we execute this task?"
@@ -114,7 +116,7 @@ public int size() {
114116
}
115117
}
116118

117-
public final class ExecutorChunk extends ReadOnlyList<ExecutorSlot> {
119+
public final class ExecutorChunk extends ReadOnlyList<ExecutorSlot> implements Nameable {
118120
public final int index;
119121
public final Computer computer;
120122
public final Node node;
@@ -150,6 +152,8 @@ public boolean canAccept(WorkChunk c) {
150152
/**
151153
* Node name.
152154
*/
155+
@NonNull
156+
@Override
153157
public String getName() {
154158
return node.getNodeName();
155159
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package jenkins.model;
2+
3+
import edu.umd.cs.findbugs.annotations.NonNull;
4+
5+
/**
6+
* An interface for objects that have a name and a parent, so exposing a full name.
7+
*/
8+
public interface FullyNameable {
9+
/**
10+
* Returns the full name of this object, which is a qualified name
11+
* that includes the names of all its ancestors, separated by '/'.
12+
*
13+
* @return the full name of this object.
14+
*/
15+
@NonNull
16+
String getFullName();
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package jenkins.model;
2+
3+
import hudson.model.ModelObject;
4+
import jenkins.security.stapler.StaplerAccessibleType;
5+
6+
/**
7+
* A model object that has a human-readable full name. This is usually valid when nested as part of an object hierarchy.
8+
*
9+
* <p>
10+
* This interface is used to mark objects that can be qualified in the context of a Jenkins instance.
11+
* It is typically used for objects that are part of the Jenkins model and can be referenced by their names.
12+
*
13+
* @see ModelObject
14+
*/
15+
@StaplerAccessibleType
16+
public interface FullyNameableModelObject extends ModelObject {
17+
/**
18+
* Works like {@link #getDisplayName()} but return
19+
* the full path that includes all the display names
20+
* of the ancestors.
21+
*/
22+
String getFullDisplayName();
23+
}

core/src/main/java/jenkins/model/HistoricalBuild.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import hudson.markup.MarkupFormatter;
3030
import hudson.model.BallColor;
3131
import hudson.model.BuildBadgeAction;
32-
import hudson.model.ModelObject;
3332
import hudson.model.ParameterValue;
3433
import hudson.model.ParametersAction;
3534
import hudson.model.Queue;
@@ -48,7 +47,7 @@
4847
* @since 2.477
4948
*/
5049
@Restricted(Beta.class)
51-
public interface HistoricalBuild extends ModelObject {
50+
public interface HistoricalBuild extends FullyNameableModelObject {
5251

5352
/**
5453
* @return A build number
@@ -74,12 +73,6 @@ public interface HistoricalBuild extends ModelObject {
7473
@CheckForNull
7574
String getDescription();
7675

77-
/**
78-
* @return a human-readable full display name of this build.
79-
*/
80-
@NonNull
81-
String getFullDisplayName();
82-
8376
/**
8477
* Get the {@link Queue.Item#getId()} of the original queue item from where this {@link HistoricalBuild} instance
8578
* originated.

core/src/main/java/jenkins/model/IComputer.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import edu.umd.cs.findbugs.annotations.NonNull;
2828
import hudson.Util;
2929
import hudson.model.Computer;
30-
import hudson.model.Node;
30+
import hudson.model.ModelObject;
3131
import hudson.security.ACL;
3232
import hudson.security.AccessControlled;
3333
import java.util.List;
@@ -45,13 +45,7 @@
4545
* @since 2.480
4646
*/
4747
@Restricted(Beta.class)
48-
public interface IComputer extends AccessControlled, IconSpec {
49-
/**
50-
* Returns {@link Node#getNodeName() the name of the node}.
51-
*/
52-
@NonNull
53-
String getName();
54-
48+
public interface IComputer extends AccessControlled, IconSpec, ModelObject, Nameable {
5549
/**
5650
* Used to render the list of executors.
5751
* @return a snapshot of the executor display information
@@ -64,12 +58,6 @@ public interface IComputer extends AccessControlled, IconSpec {
6458
*/
6559
boolean isOffline();
6660

67-
/**
68-
* @return the node name for UI purposes.
69-
*/
70-
@NonNull
71-
String getDisplayName();
72-
7361
/**
7462
* Returns {@code true} if the computer is accepting tasks. Needed to allow agents programmatic suspension of task
7563
* scheduling that does not overlap with being offline.

core/src/main/java/jenkins/model/IDisplayExecutor.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package jenkins.model;
2626

2727
import edu.umd.cs.findbugs.annotations.NonNull;
28+
import hudson.model.ModelObject;
2829
import org.kohsuke.accmod.Restricted;
2930
import org.kohsuke.accmod.restrictions.Beta;
3031

@@ -34,13 +35,7 @@
3435
* @since 2.480
3536
*/
3637
@Restricted(Beta.class)
37-
public interface IDisplayExecutor {
38-
/**
39-
* @return The UI label for this executor.
40-
*/
41-
@NonNull
42-
String getDisplayName();
43-
38+
public interface IDisplayExecutor extends ModelObject {
4439
/**
4540
* @return the URL where to reach specifically this executor, relative to Jenkins URL.
4641
*/

0 commit comments

Comments
 (0)