Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ tasks {
// This check causes too many failures, ignore the ones in tests
disable("OtelCanIgnoreReturnValueSuggester")
disable("OtelInternalJavadoc")
disable("SuppressWarningsWithoutExplanation")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Function;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -106,18 +107,17 @@ public Duration getDuration(String propertyName) {
return Duration.ofMillis(millis);
}

@SuppressWarnings("unchecked")
@SuppressWarnings("unchecked") // we expect to have only lists of strings in override values
@Override
public List<String> getList(String propertyName) {
List<String> propertyValue =
getPropertyValue(
propertyName,
List.class,
o -> (List<String>) o,
(properties, lastPart) -> properties.getScalarList(lastPart, String.class));
return propertyValue == null ? Collections.emptyList() : propertyValue;
}

@SuppressWarnings("unchecked")
@Override
public Map<String, String> getMap(String propertyName) {
DeclarativeConfigProperties propertyValue =
Expand Down Expand Up @@ -147,7 +147,15 @@ private <T> T getPropertyValue(
String property,
Class<T> clazz,
BiFunction<DeclarativeConfigProperties, String, T> extractor) {
T override = clazz.cast(overrideValues.get(property));
return getPropertyValue(property, clazz::cast, extractor);
}

@Nullable
private <T> T getPropertyValue(
String property,
Function<Object, T> converter,
BiFunction<DeclarativeConfigProperties, String, T> extractor) {
T override = converter.apply(overrideValues.get(property));
if (override != null) {
return override;
}
Expand Down
2 changes: 1 addition & 1 deletion dependencyManagement/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ val DEPENDENCY_BOMS = listOf(

val autoServiceVersion = "1.1.1"
val autoValueVersion = "1.11.1"
val errorProneVersion = "2.43.0"
val errorProneVersion = "2.44.0"
val byteBuddyVersion = "1.18.1"
val asmVersion = "9.9"
val jmhVersion = "1.37"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
package io.opentelemetry.instrumentation.api.annotation.support;

import java.lang.annotation.Annotation;
import java.lang.invoke.CallSite;
import java.lang.invoke.LambdaMetafactory;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
Expand Down Expand Up @@ -65,6 +63,7 @@ public static Class<? extends Annotation> forNameOrNull(
* @throws NoSuchMethodException the annotation element method was not found
* @throws Throwable on failing to bind to the
*/
@SuppressWarnings("unchecked") // need to cast the return value for MethodHandle.invoke
public static <A extends Annotation, T> Function<A, T> bindAnnotationElementMethod(
MethodHandles.Lookup lookup,
Class<? extends Annotation> annotationClass,
Expand All @@ -75,19 +74,12 @@ public static <A extends Annotation, T> Function<A, T> bindAnnotationElementMeth
MethodHandle valueHandle =
lookup.findVirtual(annotationClass, methodName, MethodType.methodType(returnClass));

CallSite callSite =
LambdaMetafactory.metafactory(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't see what there is to gain from directly using the LambdaMetafactory

lookup,
"apply",
MethodType.methodType(Function.class),
MethodType.methodType(Object.class, Object.class),
valueHandle,
MethodType.methodType(returnClass, annotationClass));

MethodHandle factory = callSite.getTarget();

@SuppressWarnings("unchecked")
Function<A, T> function = (Function<A, T>) factory.invoke();
return function;
return a -> {
try {
return (T) valueHandle.invoke(a);
} catch (Throwable e) {
throw new IllegalStateException(e);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private static AttributeBinding arrayBinding(String name, Type type) {
return defaultArrayBinding(name);
}

@SuppressWarnings("unchecked")
@SuppressWarnings("unchecked") // safe because type is checked before casting
private static AttributeBinding listBinding(String name, Type componentType) {
if (componentType == String.class) {
AttributeKey<List<String>> key = AttributeKey.stringArrayKey(name);
Expand Down Expand Up @@ -310,7 +310,7 @@ public int size() {
private static <T, U> AttributeBinding mappedListBinding(
AttributeKey<List<U>> key, Function<T, U> mapping) {
return (setter, arg) -> {
@SuppressWarnings("unchecked")
@SuppressWarnings("unchecked") // safe because we only call this method for lists
List<T> list = (List<T>) arg;
List<U> wrapper =
new AbstractList<U>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public static <REQUEST, RESPONSE> InstrumenterBuilder<REQUEST, RESPONSE> builder
private final boolean enabled;
private final SpanSuppressor spanSuppressor;

// to allow converting generic lists to arrays with toArray
@SuppressWarnings({"rawtypes", "unchecked"})
Instrumenter(InstrumenterBuilder<REQUEST, RESPONSE> builder) {
this.instrumentationName = builder.instrumentationName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class UnsafeAttributes extends HashMap<AttributeKey<?>, Object>

// Attributes

@SuppressWarnings("unchecked")
@SuppressWarnings("unchecked") // safe because of the AttributeKey<T> typing
@Override
@Nullable
public <T> T get(AttributeKey<T> key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static <REQUEST> void setUrlTemplateExtractor(
}
}

@SuppressWarnings({"rawtypes", "unchecked"})
@SuppressWarnings({"rawtypes", "unchecked"}) // we loose the generic type information
public static <REQUEST> void internalSetUrlTemplateExtractor(
BiConsumer<HttpSpanNameExtractorBuilder<REQUEST>, Function<REQUEST, String>>
urlTemplateExtractorSetter) {
Expand All @@ -76,7 +76,7 @@ public static <REQUEST, RESPONSE> void addOperationListenerAttributesExtractor(
}
}

@SuppressWarnings({"rawtypes", "unchecked"})
@SuppressWarnings({"rawtypes", "unchecked"}) // we loose the generic type information
public static <REQUEST, RESPONSE> void internalAddOperationListenerAttributesExtractor(
BiConsumer<
InstrumenterBuilder<REQUEST, RESPONSE>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,25 @@
* at any time.
*/
public final class InstrumenterContext {
private static final ThreadLocal<InstrumenterContext> instrumenterContext =
new ThreadLocal<InstrumenterContext>() {
@Override
protected InstrumenterContext initialValue() {
return new InstrumenterContext();
}
};
private static final ThreadLocal<InstrumenterContext> instrumenterContext = new ThreadLocal<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an unrelated change. I think that in our api jars we shouldn't use subclasses of ThreadLocal because these will leak the class loader. Can't use TheadLocal.withInitial because of android.


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

private InstrumenterContext() {}

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

// visible for testing
static Map<String, Object> get() {
return instrumenterContext.get().map;
InstrumenterContext context = instrumenterContext.get();
if (context == null) {
context = new InstrumenterContext();
instrumenterContext.set(context);
}
return context.map;
}

public static void reset() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private static final class CacheBasedVirtualFieldSupplier implements VirtualFiel
ownerToFieldToImplementationMap = Cache.weak();

@Override
// storing VirtualField instances in a map looses the generic types
@SuppressWarnings("unchecked")
public <U extends T, V extends F, T, F> VirtualField<U, V> find(
Class<T> type, Class<F> fieldType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public final class ServiceLoaderUtil {

private ServiceLoaderUtil() {}

// we loose the generic type information because of using the loader function
@SuppressWarnings("unchecked")
public static <T> Iterable<T> load(Class<T> clazz) {
return (Iterable<T>) loadFunction.apply(clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"HashCodeToString",
"MissingSummary",
"UngroupedOverloads",
"FieldMissingNullable"
"FieldMissingNullable",
"SuppressWarningsWithoutExplanation"
})
public class WeakConcurrentMap<K, V>
extends AbstractWeakConcurrentMap<K, V, WeakConcurrentMap.LookupKey<K>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static <T> void update(
HttpServerRouteSource source,
HttpServerRouteGetter<T> httpRouteGetter,
T arg1) {
update(context, source, OneArgAdapter.getInstance(), arg1, httpRouteGetter);
update(context, source, OneArgAdapter::get, arg1, httpRouteGetter);
}

/**
Expand Down Expand Up @@ -149,19 +149,11 @@ static String get(Context context) {
return httpRouteState == null ? null : httpRouteState.getRoute();
}

private static final class OneArgAdapter<T>
implements HttpServerRouteBiGetter<T, HttpServerRouteGetter<T>> {
private static final class OneArgAdapter {

private static final OneArgAdapter<Object> INSTANCE = new OneArgAdapter<>();

@SuppressWarnings("unchecked")
static <T> OneArgAdapter<T> getInstance() {
return (OneArgAdapter<T>) INSTANCE;
}

@Override
@Nullable
public String get(Context context, @Nullable T arg, HttpServerRouteGetter<T> httpRouteGetter) {
static <T> String get(
Context context, @Nullable T arg, HttpServerRouteGetter<T> httpRouteGetter) {
return httpRouteGetter.get(context, arg);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private static String getDocumentationDisableList(HttpClient client)
throw new IOException("Failed to fetch disable list: " + response);
}

@SuppressWarnings("unchecked")
@SuppressWarnings("unchecked") // for casting yaml parsed objects
public static List<String> parseInstrumentationList(String fileContent) {
List<String> instrumentationList = new ArrayList<>();
Yaml yaml = new Yaml();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
@SuppressWarnings("unchecked")
@SuppressWarnings("unchecked") // to allow mocking of generic HttpResponse
class SupportedLibrariesAuditorTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
@SuppressWarnings("unchecked")
@SuppressWarnings("unchecked") // to allow mocking of generic HttpResponse
class SuppressionListAuditorTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ enum DubboHeadersGetter implements TextMapGetter<DubboRequest> {
}

@Override
@SuppressWarnings("unchecked")
@SuppressWarnings("unchecked") // casting MethodHandle.invoke result
public Iterable<String> keys(DubboRequest request) {
RpcInvocation invocation = request.invocation();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected TracingRequestWrapperBase(BiFunction<I, Class<?>, Object> parameterMap
}

@Override
@SuppressWarnings("unchecked")
@SuppressWarnings("unchecked") // casting reflection result
protected O doHandleRequest(I input, Context context) {
Object[] parameters = LambdaParameters.toArray(targetMethod, input, context, parameterMapper);
O result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected TracingRequestWrapperBase(BiFunction<I, Class<?>, Object> parameterMap
}

@Override
@SuppressWarnings("unchecked")
@SuppressWarnings("unchecked") // casting reflection result
protected O doHandleRequest(I input, Context context) {
Object[] parameters = LambdaParameters.toArray(targetMethod, input, context, parameterMapper);
O result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ private static <T> PojoSerializer<T> createSerializer(Class<T> clazz) {
}
}

@SuppressWarnings("unchecked")
public static <T> PojoSerializer<T> getSerializer(Class<T> clazz) {
@SuppressWarnings("unchecked") // we expect the caller to provide the correct class
private static <T> PojoSerializer<T> getSerializer(Class<?> clazz) {
return (PojoSerializer<T>) serializerCache.get(clazz);
}

Expand All @@ -61,20 +61,18 @@ public static <T> T fromJson(InputStream inputStream, Class<T> clazz) {
return serializer.fromJson(inputStream);
}

@SuppressWarnings("unchecked")
public static <T> void toJson(OutputStream outputStream, T obj) {
if (obj != null) {
PojoSerializer<T> serializer = getSerializer((Class<T>) obj.getClass());
PojoSerializer<T> serializer = getSerializer(obj.getClass());
serializer.toJson(obj, outputStream);
}
}

@SuppressWarnings("unchecked")
public static <T> String toJson(T obj) {
if (obj == null) {
return null;
}
PojoSerializer<T> serializer = getSerializer((Class<T>) obj.getClass());
PojoSerializer<T> serializer = getSerializer(obj.getClass());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE);
serializer.toJson(obj, outputStream);
return new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ final class AwsJsonProtocolFactoryAccess {
// AwsJsonProtocolFactory requires any URI to be present
.option(SdkClientOption.ENDPOINT, URI.create("http://empty"))
.build());
@SuppressWarnings("rawtypes")
Class awsJsonProtocolClass =
Class.forName("software.amazon.awssdk.protocols.json.AwsJsonProtocol");
@SuppressWarnings("unchecked")
@SuppressWarnings("rawtypes") // fine
Class<? extends Enum> awsJsonProtocolClass =
Class.forName("software.amazon.awssdk.protocols.json.AwsJsonProtocol")
.asSubclass(Enum.class);
@SuppressWarnings("unchecked") // fine
Object awsJsonProtocol = Enum.valueOf(awsJsonProtocolClass, "AWS_JSON");
awsJsonProtocolFactoryBuilder
.getClass()
Expand All @@ -69,7 +70,7 @@ final class AwsJsonProtocolFactoryAccess {
INVOKE_CREATE_PROTOCOL_MARSHALLER = invokeCreateProtocolMarshaller;
}

@SuppressWarnings("unchecked")
@SuppressWarnings("unchecked") // casting reflection result
@Nullable
static ProtocolMarshaller<SdkHttpFullRequest> createMarshaller() {
if (INVOKE_CREATE_PROTOCOL_MARSHALLER == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ AttributeType getAttributeType() {
return attributeKey.getType();
}

@SuppressWarnings("unchecked")
@SuppressWarnings("unchecked") // we expect the caller to check the attribute type
<T> AttributeKey<T> getAttributeKey() {
return (AttributeKey<T>) attributeKey;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private static Field getProxyField(Class<?> clazz, String fieldName) {
}
}

@SuppressWarnings("unchecked")
@SuppressWarnings("unchecked") // casting reflection result
private static Instrumenter<ElasticsearchRestRequest, Response> getInstrumenter(Object proxy)
throws IllegalAccessException {
Supplier<Instrumenter<ElasticsearchRestRequest, Response>> supplier =
Expand Down
Loading
Loading