Commit 2cb16a6
committed
Add default behavior for service accessors
It is easy and reasonable to fall back to asking the context directly
for the service. However, there is a downside: services which do not
override these methods to return their injected field values may have
subtle bugs relating to service dependencies and initialization order.
For example, InputService has a method EventService, which is intended
to never return null. In other words: it is intended that InputService
implementations (at least by default) _depend_ on the EventService.
If you write:
Context ctx = new Context(InputService.class);
When DefaultInputService is on the classpath, it will be instantiated,
its @parameter EventService field will be noted, and the
highest-priority concrete EventService implementation (i.e.,
DefaultEventService) will then be recursively instantiated and
initialized, and so forth. So you will end up with a Context containing
a DefaultInputService _and_ all of its service dependencies.
But if DefaultInputService neglects to declare an @parameter
EventService, the way the service loader is coded right now, the
EventService dependency will not be noticed, and the Context will
contain only a DefaultInputService, whose eventService() method will
return null, resulting in NPE when calling other methods which use it.
There is one vital reason to define these default method behaviors
anyway, though: to avoid breaking SPI compatibility with downstream
components. For example, the net.imagej:imagej-legacy component
implements a LegacyConsoleService (ConsoleService is a
SingletonService), which was forced to implement the new
objectService() method when upgrading its version of scijava-common.
Unfortunately, the ImageJ Updater (net.imagej:imagej-updater) has a
serious design flaw whereby it updates itself and its dependencies
(including scijava-common) first, without updating any downstream
libraries (e.g., imagej-legacy). So we end up with a situation where
scijava-common is at 2.58.2, after the addition of the newly required
objectService() method, while imagej-legacy remains at its old version
which does not implement that method. And the Context fails to spin up.
Adding these default method behaviors is a simple way around the issue.
We should consider whether to remove these default implementations in
SJC3. And we should definitely consider how best to fix SciJava Common
so that downstream JAR skew cannot taint the Context startup so easily.
But for now, it is safest to provide these methods, in case of JAR skew.1 parent fd0a289 commit 2cb16a6
File tree
7 files changed
+33
-11
lines changed- src/main/java/org/scijava
- command
- display
- input
- object
- platform
- plugin
7 files changed
+33
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | | - | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
68 | 70 | | |
69 | | - | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
70 | 74 | | |
71 | 75 | | |
72 | 76 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
54 | 56 | | |
55 | | - | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
56 | 60 | | |
57 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
58 | 64 | | |
59 | 65 | | |
60 | 66 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
48 | | - | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
49 | 51 | | |
50 | 52 | | |
51 | 53 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
47 | 49 | | |
48 | 50 | | |
49 | 51 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
55 | | - | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
56 | 58 | | |
57 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
58 | 62 | | |
59 | 63 | | |
60 | 64 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
90 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
91 | 93 | | |
92 | 94 | | |
93 | 95 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | | - | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
61 | 63 | | |
62 | 64 | | |
63 | 65 | | |
| |||
0 commit comments