Skip to content

Commit 5bc086e

Browse files
committed
add nullaway
1 parent fd74eb8 commit 5bc086e

17 files changed

+51
-21
lines changed

instrumentation-api/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ plugins {
77
id("otel.japicmp-conventions")
88
id("otel.publish-conventions")
99
id("otel.jmh-conventions")
10+
id("otel.nullaway-conventions")
1011
}
1112

1213
group = "io.opentelemetry.instrumentation"

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/UnsafeAttributes.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.HashMap;
1313
import java.util.Map;
1414
import java.util.function.BiConsumer;
15+
import javax.annotation.Nullable;
1516

1617
/**
1718
* The {@link AttributesBuilder} and {@link Attributes} used by the instrumentation API. We are able
@@ -29,6 +30,7 @@ final class UnsafeAttributes extends HashMap<AttributeKey<?>, Object>
2930

3031
@SuppressWarnings("unchecked")
3132
@Override
33+
@Nullable
3234
public <T> T get(AttributeKey<T> key) {
3335
return (T) super.get(key);
3436
}
@@ -62,7 +64,7 @@ public <T> AttributesBuilder put(AttributeKey<Long> key, int value) {
6264

6365
@Override
6466
@CanIgnoreReturnValue
65-
public <T> AttributesBuilder put(AttributeKey<T> key, T value) {
67+
public <T> AttributesBuilder put(AttributeKey<T> key, @Nullable T value) {
6668
super.put(key, value);
6769
return this;
6870
}

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/ContextPropagationDebug.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public final class ContextPropagationDebug {
4545
private final Context sourceContext;
4646
private final List<Propagation> locations;
4747
// context after adding debug locations
48-
private Context wrappedContext;
48+
@Nullable private Context wrappedContext;
4949

5050
private ContextPropagationDebug(Context sourceContext) {
5151
this.sourceContext = sourceContext;

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/HttpProtocolUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
*/
1414
public final class HttpProtocolUtil {
1515

16+
@Nullable
1617
public static String getProtocol(@Nullable String protocol) {
1718
if (protocol != null && protocol.startsWith("HTTP/")) {
1819
return "http";
1920
}
2021
return null;
2122
}
2223

24+
@Nullable
2325
public static String getVersion(@Nullable String protocol) {
2426
if (protocol != null && protocol.startsWith("HTTP/")) {
2527
return normalizeHttpVersion(protocol.substring("HTTP/".length()));

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/HttpRouteState.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ public static HttpRouteState create(
4040

4141
// this method is used reflectively from InstrumentationApiContextBridging
4242
public static HttpRouteState create(
43-
@Nullable String method, @Nullable String route, int updatedBySourceOrder, Span span) {
43+
@Nullable String method,
44+
@Nullable String route,
45+
int updatedBySourceOrder,
46+
@Nullable Span span) {
4447
return new HttpRouteState(method, route, updatedBySourceOrder, span);
4548
}
4649

@@ -50,7 +53,10 @@ public static HttpRouteState create(
5053
@Nullable private volatile Span span;
5154

5255
private HttpRouteState(
53-
@Nullable String method, @Nullable String route, int updatedBySourceOrder, Span span) {
56+
@Nullable String method,
57+
@Nullable String route,
58+
int updatedBySourceOrder,
59+
@Nullable Span span) {
5460
this.method = method;
5561
this.updatedBySourceOrder = updatedBySourceOrder;
5662
this.route = route;

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/InstrumenterUtil.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
2020
* any time.
2121
*/
22+
@SuppressWarnings("NullAway") // setters called in static initializer
2223
public final class InstrumenterUtil {
2324

2425
private static InstrumenterAccess instrumenterAccess;

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/cache/WeakLockFreeCache.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import io.opentelemetry.instrumentation.api.internal.cache.weaklockfree.WeakConcurrentMap;
99
import java.util.function.Function;
10+
import javax.annotation.Nullable;
1011

1112
final class WeakLockFreeCache<K, V> implements Cache<K, V> {
1213

@@ -22,6 +23,7 @@ public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction
2223
}
2324

2425
@Override
26+
@Nullable
2527
public V get(K key) {
2628
return delegate.getIfPresent(key);
2729
}

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/cache/weaklockfree/AbstractWeakConcurrentMap.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
package io.opentelemetry.instrumentation.api.internal.cache.weaklockfree;
2727

28+
import static java.util.Objects.requireNonNull;
29+
2830
import java.lang.ref.Reference;
2931
import java.lang.ref.ReferenceQueue;
3032
import java.lang.ref.WeakReference;
@@ -81,6 +83,7 @@ protected AbstractWeakConcurrentMap(ConcurrentMap<WeakKey<K>, V> target) {
8183
* @param key The key of the entry.
8284
* @return The value of the entry or the default value if it did not exist.
8385
*/
86+
@Nullable
8487
public V get(K key) {
8588
if (key == null) {
8689
throw new NullPointerException();
@@ -108,6 +111,7 @@ public V get(K key) {
108111
* @param key The key of the entry.
109112
* @return The value of the entry or null if it did not exist.
110113
*/
114+
@Nullable
111115
public V getIfPresent(K key) {
112116
if (key == null) {
113117
throw new NullPointerException();
@@ -225,6 +229,7 @@ public void clear() {
225229
* @return The default value for a key without value or {@code null} for not defining a default
226230
* value.
227231
*/
232+
@Nullable
228233
protected V defaultValue(K key) {
229234
return null;
230235
}
@@ -332,7 +337,7 @@ public boolean equals(@Nullable Object other) {
332337
if (other instanceof WeakKey<?>) {
333338
return ((WeakKey<?>) other).get() == get();
334339
} else {
335-
return other.equals(this);
340+
return requireNonNull(other).equals(this);
336341
}
337342
}
338343

@@ -346,9 +351,9 @@ private class EntryIterator implements Iterator<Map.Entry<K, V>> {
346351

347352
private final Iterator<Map.Entry<WeakKey<K>, V>> iterator;
348353

349-
private Map.Entry<WeakKey<K>, V> nextEntry;
354+
@Nullable private Map.Entry<WeakKey<K>, V> nextEntry;
350355

351-
private K nextKey;
356+
@Nullable private K nextKey;
352357

353358
private EntryIterator(Iterator<Map.Entry<WeakKey<K>, V>> iterator) {
354359
this.iterator = iterator;
@@ -378,7 +383,7 @@ public Map.Entry<K, V> next() {
378383
throw new NoSuchElementException();
379384
}
380385
try {
381-
return new SimpleEntry(nextKey, nextEntry);
386+
return new SimpleEntry(requireNonNull(nextKey), requireNonNull(nextEntry));
382387
} finally {
383388
findNext();
384389
}

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/cache/weaklockfree/WeakConcurrentMap.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
package io.opentelemetry.instrumentation.api.internal.cache.weaklockfree;
2727

28+
import static java.util.Objects.requireNonNull;
29+
2830
import java.util.Iterator;
2931
import java.util.Map;
3032
import java.util.concurrent.ConcurrentHashMap;
@@ -142,7 +144,7 @@ protected void resetLookupKey(LookupKey<K> lookupKey) {
142144
// can't use AutoClosable/try-with-resources as this project still supports Java 6
143145
static final class LookupKey<K> {
144146

145-
private K key;
147+
@Nullable private K key;
146148
private int hashCode;
147149

148150
@SuppressWarnings("OtelCanIgnoreReturnValueSuggester")
@@ -163,7 +165,7 @@ public boolean equals(@Nullable Object other) {
163165
if (other instanceof WeakConcurrentMap.LookupKey<?>) {
164166
return ((LookupKey<?>) other).key == key;
165167
} else {
166-
return ((AbstractWeakConcurrentMap.WeakKey<?>) other).get() == key;
168+
return ((AbstractWeakConcurrentMap.WeakKey<?>) requireNonNull(other)).get() == key;
167169
}
168170
}
169171

@@ -183,12 +185,14 @@ public int hashCode() {
183185
public static class WithInlinedExpunction<K, V> extends WeakConcurrentMap<K, V> {
184186

185187
@Override
188+
@Nullable
186189
public V get(K key) {
187190
expungeStaleEntries();
188191
return super.get(key);
189192
}
190193

191194
@Override
195+
@Nullable
192196
public V getIfPresent(K key) {
193197
expungeStaleEntries();
194198
return super.getIfPresent(key);

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/cache/weaklockfree/WeakConcurrentMapCleaner.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66
package io.opentelemetry.instrumentation.api.internal.cache.weaklockfree;
77

8+
import javax.annotation.Nullable;
9+
810
/**
911
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
1012
* any time.
1113
*/
1214
public final class WeakConcurrentMapCleaner {
13-
private static Thread thread;
15+
@Nullable private static Thread thread;
1416

1517
private WeakConcurrentMapCleaner() {}
1618

0 commit comments

Comments
 (0)