Skip to content

Commit a9f7105

Browse files
committed
AbstractSingletonService: register instances lazily
To be more precise, the objects were previously registered lazily upon the first call to SingletonService#getInstances(). Unfortunately, calling ObjectService#get or ObjectService#getAll before that failed to return the singleton instances, because they had not yet even been instantiated. To address this problem, we now register them using the ObjectIndex's newly minted addLater feature, which lets us register a callback to create objects only when they are needed.
1 parent 28a7e7c commit a9f7105

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

‎src/main/java/org/scijava/plugin/AbstractSingletonService.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535

3636
package org.scijava.plugin;
3737

38+
import java.util.ArrayList;
3839
import java.util.Collections;
3940
import java.util.List;
4041

4142
import org.scijava.log.LogService;
43+
import org.scijava.object.LazyObjects;
4244
import org.scijava.object.ObjectService;
4345

4446
/**
@@ -73,6 +75,20 @@ public List<PT> getInstances() {
7375
return instances;
7476
}
7577

78+
// -- Service methods --
79+
80+
@Override
81+
public void initialize() {
82+
// add singleton instances to the object index... IN THE FUTURE!
83+
objectService.getIndex().addLater(new LazyObjects<Object>() {
84+
85+
@Override
86+
public ArrayList<Object> get() {
87+
return new ArrayList<Object>(getInstances());
88+
}
89+
});
90+
}
91+
7692
// -- Helper methods --
7793

7894
private void createInstances() {

0 commit comments

Comments
 (0)