Skip to content

Commit 765d6ef

Browse files
committed
ObjectService: ensure getName() returns non-null
1 parent a5d0fc0 commit 765d6ef

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/main/java/org/scijava/object/DefaultObjectService.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,16 @@ public void removeObject(final Object obj) {
110110

111111
@Override
112112
public String getName(Object obj) {
113-
String name = objectIndex.getName(obj);
114-
if (name != null) {
115-
return name;
116-
}
113+
if (obj == null) throw new NullPointerException();
114+
final String name = objectIndex.getName(obj);
115+
if (name != null) return name;
117116
if (obj instanceof Named) {
118-
return ((Named) obj).getName();
117+
final String n = ((Named) obj).getName();
118+
if (n != null) return n;
119119
}
120-
return obj.toString();
120+
final String s = obj.toString();
121+
if (s != null) return s;
122+
return obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode());
121123
}
122124

123125
// -- Service methods --

src/main/java/org/scijava/object/ObjectService.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ default EventService eventService() {
5757

5858
/**
5959
* Gets the name belonging to a given object.
60-
*
60+
* <p>
6161
* If no explicit name was provided at registration time, the name will be
62-
* derived from {@link Named#getName()} if the object implements {@link Named},
63-
* or from the {@link Object#toString()} otherwise
64-
**/
62+
* derived from {@link Named#getName()} if the object implements
63+
* {@link Named}, or from the {@link Object#toString()} otherwise. It is
64+
* guaranteed that this method will not return {@code null}.
65+
* </p>
66+
*/
6567
String getName(Object obj);
6668

6769
/** Registers an object with the object service. */

0 commit comments

Comments
 (0)