Skip to content

Commit b5972b6

Browse files
committed
fix some warnings
1 parent e66d818 commit b5972b6

File tree

90 files changed

+201
-211
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+201
-211
lines changed

conventions/src/main/kotlin/otel.errorprone-conventions.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ tasks {
140140
// This check causes too many failures, ignore the ones in tests
141141
disable("OtelCanIgnoreReturnValueSuggester")
142142
disable("OtelInternalJavadoc")
143+
disable("SuppressWarningsWithoutExplanation")
143144
}
144145
}
145146
}

declarative-config-bridge/src/main/java/io/opentelemetry/instrumentation/config/bridge/DeclarativeConfigPropertiesBridge.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.Map;
1717
import java.util.Objects;
1818
import java.util.function.BiFunction;
19+
import java.util.function.Function;
1920
import javax.annotation.Nullable;
2021

2122
/**
@@ -106,18 +107,17 @@ public Duration getDuration(String propertyName) {
106107
return Duration.ofMillis(millis);
107108
}
108109

109-
@SuppressWarnings("unchecked")
110+
@SuppressWarnings("unchecked") // we expect to have only lists of strings in override values
110111
@Override
111112
public List<String> getList(String propertyName) {
112113
List<String> propertyValue =
113114
getPropertyValue(
114115
propertyName,
115-
List.class,
116+
o -> (List<String>) o,
116117
(properties, lastPart) -> properties.getScalarList(lastPart, String.class));
117118
return propertyValue == null ? Collections.emptyList() : propertyValue;
118119
}
119120

120-
@SuppressWarnings("unchecked")
121121
@Override
122122
public Map<String, String> getMap(String propertyName) {
123123
DeclarativeConfigProperties propertyValue =
@@ -147,7 +147,15 @@ private <T> T getPropertyValue(
147147
String property,
148148
Class<T> clazz,
149149
BiFunction<DeclarativeConfigProperties, String, T> extractor) {
150-
T override = clazz.cast(overrideValues.get(property));
150+
return getPropertyValue(property, clazz::cast, extractor);
151+
}
152+
153+
@Nullable
154+
private <T> T getPropertyValue(
155+
String property,
156+
Function<Object, T> converter,
157+
BiFunction<DeclarativeConfigProperties, String, T> extractor) {
158+
T override = converter.apply(overrideValues.get(property));
151159
if (override != null) {
152160
return override;
153161
}

instrumentation-annotations-support/src/main/java/io/opentelemetry/instrumentation/api/annotation/support/AnnotationReflectionHelper.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
package io.opentelemetry.instrumentation.api.annotation.support;
77

88
import java.lang.annotation.Annotation;
9-
import java.lang.invoke.CallSite;
10-
import java.lang.invoke.LambdaMetafactory;
119
import java.lang.invoke.MethodHandle;
1210
import java.lang.invoke.MethodHandles;
1311
import java.lang.invoke.MethodType;
@@ -65,6 +63,7 @@ public static Class<? extends Annotation> forNameOrNull(
6563
* @throws NoSuchMethodException the annotation element method was not found
6664
* @throws Throwable on failing to bind to the
6765
*/
66+
@SuppressWarnings("unchecked") // need to cast the return value for MethodHandle.invoke
6867
public static <A extends Annotation, T> Function<A, T> bindAnnotationElementMethod(
6968
MethodHandles.Lookup lookup,
7069
Class<? extends Annotation> annotationClass,
@@ -75,19 +74,12 @@ public static <A extends Annotation, T> Function<A, T> bindAnnotationElementMeth
7574
MethodHandle valueHandle =
7675
lookup.findVirtual(annotationClass, methodName, MethodType.methodType(returnClass));
7776

78-
CallSite callSite =
79-
LambdaMetafactory.metafactory(
80-
lookup,
81-
"apply",
82-
MethodType.methodType(Function.class),
83-
MethodType.methodType(Object.class, Object.class),
84-
valueHandle,
85-
MethodType.methodType(returnClass, annotationClass));
86-
87-
MethodHandle factory = callSite.getTarget();
88-
89-
@SuppressWarnings("unchecked")
90-
Function<A, T> function = (Function<A, T>) factory.invoke();
91-
return function;
77+
return a -> {
78+
try {
79+
return (T) valueHandle.invoke(a);
80+
} catch (Throwable e) {
81+
throw new IllegalStateException(e);
82+
}
83+
};
9284
}
9385
}

instrumentation-annotations-support/src/main/java/io/opentelemetry/instrumentation/api/annotation/support/AttributeBindingFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private static AttributeBinding arrayBinding(String name, Type type) {
114114
return defaultArrayBinding(name);
115115
}
116116

117-
@SuppressWarnings("unchecked")
117+
@SuppressWarnings("unchecked") // safe because type is checked before casting
118118
private static AttributeBinding listBinding(String name, Type componentType) {
119119
if (componentType == String.class) {
120120
AttributeKey<List<String>> key = AttributeKey.stringArrayKey(name);
@@ -310,7 +310,7 @@ public int size() {
310310
private static <T, U> AttributeBinding mappedListBinding(
311311
AttributeKey<List<U>> key, Function<T, U> mapping) {
312312
return (setter, arg) -> {
313-
@SuppressWarnings("unchecked")
313+
@SuppressWarnings("unchecked") // safe because we only call this method for lists
314314
List<T> list = (List<T>) arg;
315315
List<U> wrapper =
316316
new AbstractList<U>() {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public static <REQUEST, RESPONSE> InstrumenterBuilder<REQUEST, RESPONSE> builder
8585
private final boolean enabled;
8686
private final SpanSuppressor spanSuppressor;
8787

88+
// to allow converting generic lists to arrays with toArray
8889
@SuppressWarnings({"rawtypes", "unchecked"})
8990
Instrumenter(InstrumenterBuilder<REQUEST, RESPONSE> builder) {
9091
this.instrumentationName = builder.instrumentationName;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class UnsafeAttributes extends HashMap<AttributeKey<?>, Object>
2828

2929
// Attributes
3030

31-
@SuppressWarnings("unchecked")
31+
@SuppressWarnings("unchecked") // safe because of the AttributeKey<T> typing
3232
@Override
3333
@Nullable
3434
public <T> T get(AttributeKey<T> key) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static <REQUEST> void setUrlTemplateExtractor(
5555
}
5656
}
5757

58-
@SuppressWarnings({"rawtypes", "unchecked"})
58+
@SuppressWarnings({"rawtypes", "unchecked"}) // we loose the generic type information
5959
public static <REQUEST> void internalSetUrlTemplateExtractor(
6060
BiConsumer<HttpSpanNameExtractorBuilder<REQUEST>, Function<REQUEST, String>>
6161
urlTemplateExtractorSetter) {
@@ -76,7 +76,7 @@ public static <REQUEST, RESPONSE> void addOperationListenerAttributesExtractor(
7676
}
7777
}
7878

79-
@SuppressWarnings({"rawtypes", "unchecked"})
79+
@SuppressWarnings({"rawtypes", "unchecked"}) // we loose the generic type information
8080
public static <REQUEST, RESPONSE> void internalAddOperationListenerAttributesExtractor(
8181
BiConsumer<
8282
InstrumenterBuilder<REQUEST, RESPONSE>,

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,25 @@
2020
* at any time.
2121
*/
2222
public final class InstrumenterContext {
23-
private static final ThreadLocal<InstrumenterContext> instrumenterContext =
24-
new ThreadLocal<InstrumenterContext>() {
25-
@Override
26-
protected InstrumenterContext initialValue() {
27-
return new InstrumenterContext();
28-
}
29-
};
23+
private static final ThreadLocal<InstrumenterContext> instrumenterContext = new ThreadLocal<>();
3024

3125
private final Map<String, Object> map = new HashMap<>();
3226

3327
private InstrumenterContext() {}
3428

35-
@SuppressWarnings("unchecked")
29+
@SuppressWarnings("unchecked") // we expect the caller to use the same type for a given key
3630
public static <T> T computeIfAbsent(String key, Function<String, T> function) {
3731
return (T) get().computeIfAbsent(key, function);
3832
}
3933

4034
// visible for testing
4135
static Map<String, Object> get() {
42-
return instrumenterContext.get().map;
36+
InstrumenterContext context = instrumenterContext.get();
37+
if (context == null) {
38+
context = new InstrumenterContext();
39+
instrumenterContext.set(context);
40+
}
41+
return context.map;
4342
}
4443

4544
public static void reset() {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ private static final class CacheBasedVirtualFieldSupplier implements VirtualFiel
5151
ownerToFieldToImplementationMap = Cache.weak();
5252

5353
@Override
54+
// storing VirtualField instances in a map looses the generic types
5455
@SuppressWarnings("unchecked")
5556
public <U extends T, V extends F, T, F> VirtualField<U, V> find(
5657
Class<T> type, Class<F> fieldType) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public final class ServiceLoaderUtil {
1818

1919
private ServiceLoaderUtil() {}
2020

21+
// we loose the generic type information because of using the loader function
2122
@SuppressWarnings("unchecked")
2223
public static <T> Iterable<T> load(Class<T> clazz) {
2324
return (Iterable<T>) loadFunction.apply(clazz);

0 commit comments

Comments
 (0)