Skip to content

Commit 2572582

Browse files
java-team-github-botDagger Team
authored andcommitted
Add @NullMarked to dagger package.
RELNOTES=n/a PiperOrigin-RevId: 736471153
1 parent 9852108 commit 2572582

File tree

3 files changed

+57
-29
lines changed

3 files changed

+57
-29
lines changed

dagger-runtime/main/java/dagger/Lazy.java

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@
1616

1717
package dagger;
1818

19+
import org.jspecify.annotations.Nullable;
20+
1921
/**
20-
* A handle to a lazily-computed value. Each {@code Lazy} computes its value on
21-
* the first call to {@link #get()} and remembers that same value for all
22-
* subsequent calls to {@code get()}.
22+
* A handle to a lazily-computed value. Each {@code Lazy} computes its value on the first call to
23+
* {@link #get()} and remembers that same value for all subsequent calls to {@code get()}.
2324
*
2425
* <p>All implementations are expected to be thread-safe and compute their value at most once.
2526
*
2627
* <h2>Example</h2>
27-
* The differences between <strong>direct injection</strong>, <strong>provider
28-
* injection</strong> and <strong>lazy injection</strong> are best demonstrated
29-
* with an example. Start with a module that computes a different integer for
30-
* each use:<pre><code>
28+
*
29+
* The differences between <strong>direct injection</strong>, <strong>provider injection</strong>
30+
* and <strong>lazy injection</strong> are best demonstrated with an example. Start with a module
31+
* that computes a different integer for each use:
32+
*
33+
* <pre><code>
3134
* {@literal @Module}
3235
* final class CounterModule {
3336
* int next = 100;
@@ -40,7 +43,10 @@
4043
* </code></pre>
4144
*
4245
* <h3>Direct Injection</h3>
43-
* This class injects that integer and prints it 3 times:<pre><code>
46+
*
47+
* This class injects that integer and prints it 3 times:
48+
*
49+
* <pre><code>
4450
* final class DirectCounter {
4551
* {@literal @Inject} Integer value;
4652
*
@@ -52,8 +58,11 @@
5258
* }
5359
* }
5460
* </code></pre>
55-
* Injecting a {@code DirectCounter} and invoking {@code print()} reveals that
56-
* the value is computed <i>before</i> it is required:<pre><code>
61+
*
62+
* Injecting a {@code DirectCounter} and invoking {@code print()} reveals that the value is computed
63+
* <i>before</i> it is required:
64+
*
65+
* <pre><code>
5766
* computing...
5867
* printing...
5968
* 100
@@ -62,8 +71,10 @@
6271
* </code></pre>
6372
*
6473
* <h3>Provider Injection</h3>
65-
* This class injects a {@linkplain javax.inject.Provider provider} for the
66-
* integer. It calls {@code Provider.get()} 3 times and prints each result:
74+
*
75+
* This class injects a {@linkplain javax.inject.Provider provider} for the integer. It calls {@code
76+
* Provider.get()} 3 times and prints each result:
77+
*
6778
* <pre><code>
6879
* final class ProviderCounter {
6980
* {@literal @Inject Provider<Integer> provider;}
@@ -76,8 +87,11 @@
7687
* }
7788
* }
7889
* </code></pre>
79-
* Injecting a {@code ProviderCounter} and invoking {@code print()} shows that
80-
* a new value is computed each time {@code Provider.get()} is used:<pre><code>
90+
*
91+
* Injecting a {@code ProviderCounter} and invoking {@code print()} shows that a new value is
92+
* computed each time {@code Provider.get()} is used:
93+
*
94+
* <pre><code>
8195
* printing...
8296
* computing...
8397
* 100
@@ -88,8 +102,11 @@
88102
* </code></pre>
89103
*
90104
* <h3>Lazy Injection</h3>
91-
* This class injects a {@code Lazy} for the integer. Like the provider above,
92-
* it calls {@code Lazy.get()} 3 times and prints each result:<pre><code>
105+
*
106+
* This class injects a {@code Lazy} for the integer. Like the provider above, it calls {@code
107+
* Lazy.get()} 3 times and prints each result:
108+
*
109+
* <pre><code>
93110
* final class LazyCounter {
94111
* {@literal @Inject Lazy<Integer> lazy;}
95112
*
@@ -101,9 +118,11 @@
101118
* }
102119
* }
103120
* </code></pre>
104-
* Injecting a {@code LazyCounter} and invoking {@code print()} shows that a new
105-
* value is computed immediately before it is needed. The same value is returned
106-
* for all subsequent uses:<pre><code>
121+
*
122+
* Injecting a {@code LazyCounter} and invoking {@code print()} shows that a new value is computed
123+
* immediately before it is needed. The same value is returned for all subsequent uses:
124+
*
125+
* <pre><code>
107126
* printing...
108127
* computing...
109128
* 100
@@ -112,9 +131,11 @@
112131
* </code></pre>
113132
*
114133
* <h3>Lazy != Singleton</h3>
115-
* Note that each injected {@code Lazy} is independent, and remembers its value
116-
* in isolation of other {@code Lazy} instances. In this example, two {@code
117-
* LazyCounter} objects are created and {@code print()} is called on each:
134+
*
135+
* Note that each injected {@code Lazy} is independent, and remembers its value in isolation of
136+
* other {@code Lazy} instances. In this example, two {@code LazyCounter} objects are created and
137+
* {@code print()} is called on each:
138+
*
118139
* <pre><code>
119140
* final class LazyCounters {
120141
* {@literal @Inject} LazyCounter counter1;
@@ -126,7 +147,9 @@
126147
* }
127148
* }
128149
* </code></pre>
150+
*
129151
* The output demonstrates that each {@code Lazy} works independently:
152+
*
130153
* <pre><code>
131154
* printing...
132155
* computing...
@@ -139,10 +162,11 @@
139162
* 101
140163
* 101
141164
* </code></pre>
142-
* Use {@link javax.inject.Singleton @Singleton} to share one instance among all
143-
* clients, and {@code Lazy} for lazy computation in a single client.
165+
*
166+
* Use {@link javax.inject.Singleton @Singleton} to share one instance among all clients, and {@code
167+
* Lazy} for lazy computation in a single client.
144168
*/
145-
public interface Lazy<T> {
169+
public interface Lazy<T extends @Nullable Object> {
146170
/**
147171
* Return the underlying value, computing the value if necessary. All calls to
148172
* the same {@code Lazy} instance will return the same result.

dagger-runtime/main/java/dagger/MembersInjector.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@
1616

1717
package dagger;
1818

19+
import org.jspecify.annotations.Nullable;
20+
1921
/**
2022
* Injects dependencies into the fields and methods on instances of type {@code T}. Ignores the
2123
* presence or absence of an injectable constructor.
2224
*
2325
* @param <T> type to inject members of
24-
*
25-
* @since 2.0 (since 1.0 without the provision that {@link #injectMembers} cannot accept
26-
* {@code null})
26+
* @since 2.0 (since 1.0 without the provision that {@link #injectMembers} cannot accept {@code
27+
* null})
2728
*/
28-
public interface MembersInjector<T> {
29+
public interface MembersInjector<T extends @Nullable Object> {
2930

3031
/**
3132
* Injects dependencies into the fields and methods of {@code instance}. Ignores the presence or

dagger-runtime/main/java/dagger/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,7 @@
3030
* service loader} to automatically configure the processor, so explict build configuration
3131
* shouldn't be necessary.
3232
*/
33+
@NullMarked
3334
package dagger;
35+
36+
import org.jspecify.annotations.NullMarked;

0 commit comments

Comments
 (0)