Skip to content

Commit 9eff522

Browse files
committed
fix(spring): Resolve the Error Prone and javac 21 findings in the Spring modules
1 parent fef4772 commit 9eff522

29 files changed

Lines changed: 58 additions & 55 deletions

‎sentry-spring-7/src/main/java/io/sentry/spring7/HttpServletRequestSentryUserProvider.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ public HttpServletRequestSentryUserProvider(final @NotNull SentryOptions options
2525
public @Nullable User provideUser() {
2626
if (options.getDataCollectionResolver().isUserInfo()) {
2727
final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
28-
if (requestAttributes instanceof ServletRequestAttributes) {
29-
final ServletRequestAttributes servletRequestAttributes =
30-
(ServletRequestAttributes) requestAttributes;
28+
if (requestAttributes instanceof ServletRequestAttributes servletRequestAttributes) {
29+
3130
final HttpServletRequest request = servletRequestAttributes.getRequest();
3231

3332
final User user = new User();

‎sentry-spring-7/src/main/java/io/sentry/spring7/SentryInitBeanPostProcessor.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ public SentryInitBeanPostProcessor() {
4242
@SuppressWarnings({"unchecked", "deprecation"})
4343
public @NotNull Object postProcessAfterInitialization(
4444
final @NotNull Object bean, @NotNull final String beanName) throws BeansException {
45-
if (bean instanceof SentryOptions) {
46-
final SentryOptions options = (SentryOptions) bean;
45+
if (bean instanceof SentryOptions options) {
4746

4847
if (applicationContext != null) {
4948
applicationContext

‎sentry-spring-7/src/main/java/io/sentry/spring7/cache/SentryCacheBeanPostProcessor.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public final class SentryCacheBeanPostProcessor implements BeanPostProcessor, Pr
1616
@Override
1717
public @NotNull Object postProcessAfterInitialization(
1818
final @NotNull Object bean, final @NotNull String beanName) throws BeansException {
19-
if (bean instanceof CacheManager && !(bean instanceof SentryCacheManagerWrapper)) {
20-
return new SentryCacheManagerWrapper((CacheManager) bean, ScopesAdapter.getInstance());
19+
if (bean instanceof CacheManager cacheManager && !(bean instanceof SentryCacheManagerWrapper)) {
20+
return new SentryCacheManagerWrapper(cacheManager, ScopesAdapter.getInstance());
2121
}
2222
return bean;
2323
}

‎sentry-spring-7/src/main/java/io/sentry/spring7/checkin/SentryCheckInAdvice.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ public SentryCheckInAdvice(final @NotNull IScopes scopes) {
4444
}
4545

4646
@Override
47-
public Object invoke(final @NotNull MethodInvocation invocation) throws Throwable {
47+
public @Nullable Object invoke(final @NotNull MethodInvocation invocation) throws Throwable {
48+
final @Nullable Object target = invocation.getThis();
4849
final Method mostSpecificMethod =
49-
AopUtils.getMostSpecificMethod(invocation.getMethod(), invocation.getThis().getClass());
50+
AopUtils.getMostSpecificMethod(
51+
invocation.getMethod(), target != null ? target.getClass() : null);
5052

5153
@Nullable
5254
SentryCheckIn checkInAnnotation =

‎sentry-spring-7/src/main/java/io/sentry/spring7/exception/SentryCaptureExceptionParameterAdvice.java‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.aopalliance.intercept.MethodInvocation;
1212
import org.jetbrains.annotations.ApiStatus;
1313
import org.jetbrains.annotations.NotNull;
14+
import org.jetbrains.annotations.Nullable;
1415
import org.springframework.aop.support.AopUtils;
1516
import org.springframework.core.annotation.AnnotationUtils;
1617

@@ -33,17 +34,19 @@ public SentryCaptureExceptionParameterAdvice(final @NotNull IScopes scopes) {
3334
}
3435

3536
@Override
36-
public Object invoke(final @NotNull MethodInvocation invocation) throws Throwable {
37+
public @Nullable Object invoke(final @NotNull MethodInvocation invocation) throws Throwable {
38+
final @Nullable Object target = invocation.getThis();
3739
final Method mostSpecificMethod =
38-
AopUtils.getMostSpecificMethod(invocation.getMethod(), invocation.getThis().getClass());
40+
AopUtils.getMostSpecificMethod(
41+
invocation.getMethod(), target != null ? target.getClass() : null);
3942
SentryCaptureExceptionParameter sentryCaptureExceptionParameter =
4043
AnnotationUtils.findAnnotation(mostSpecificMethod, SentryCaptureExceptionParameter.class);
4144

4245
if (sentryCaptureExceptionParameter != null) {
4346
Object[] args = invocation.getArguments();
4447
for (Object arg : args) {
45-
if (arg instanceof Exception) {
46-
captureException((Exception) arg);
48+
if (arg instanceof Exception exception) {
49+
captureException(exception);
4750
break;
4851
}
4952
}

‎sentry-spring-7/src/main/java/io/sentry/spring7/graphql/SentryBatchLoaderRegistry.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ public void registerMappedBatchLoader(
108108

109109
private @NotNull IScopes scopesFromContext(final @NotNull BatchLoaderEnvironment environment) {
110110
Object context = environment.getContext();
111-
if (context instanceof GraphQLContext) {
112-
GraphQLContext graphqlContext = (GraphQLContext) context;
111+
if (context instanceof GraphQLContext graphqlContext) {
112+
113113
return graphqlContext.getOrDefault(SENTRY_SCOPES_CONTEXT_KEY, NoOpScopes.getInstance());
114114
}
115115

‎sentry-spring-7/src/main/java/io/sentry/spring7/graphql/SentryGraphqlBeanPostProcessor.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public final class SentryGraphqlBeanPostProcessor implements BeanPostProcessor,
1212

1313
@Override
1414
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
15-
if (bean instanceof BatchLoaderRegistry) {
16-
return new SentryBatchLoaderRegistry((BatchLoaderRegistry) bean);
15+
if (bean instanceof BatchLoaderRegistry batchLoaderRegistry) {
16+
return new SentryBatchLoaderRegistry(batchLoaderRegistry);
1717
}
1818
return bean;
1919
}

‎sentry-spring-7/src/main/java/io/sentry/spring7/kafka/SentryKafkaProducerBeanPostProcessor.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public final class SentryKafkaProducerBeanPostProcessor
3636
@SuppressWarnings({"unchecked", "rawtypes"})
3737
public @NotNull Object postProcessAfterInitialization(
3838
final @NotNull Object bean, final @NotNull String beanName) throws BeansException {
39-
if (bean instanceof ProducerFactory) {
40-
final @NotNull ProducerFactory factory = (ProducerFactory) bean;
39+
if (bean instanceof ProducerFactory<?, ?> factory) {
40+
4141
final @NotNull SentryProducerPostProcessor pp = new SentryProducerPostProcessor<>();
4242
factory.addPostProcessor(pp);
4343
if (!factory.getPostProcessors().contains(pp)) {

‎sentry-spring-7/src/main/java/io/sentry/spring7/kafka/SentryKafkaRecordInterceptor.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ private boolean isIgnored() {
196196
if (enqueuedTimeStr != null) {
197197
try {
198198
final double enqueuedTimeSeconds = Double.parseDouble(enqueuedTimeStr);
199-
final double nowSeconds = DateUtils.millisToSeconds(System.currentTimeMillis());
199+
// millisToSeconds takes a double; the cast is explicit for Error Prone's
200+
// LongDoubleConversion check.
201+
final double nowSeconds = DateUtils.millisToSeconds((double) System.currentTimeMillis());
200202
final long latencyMs = (long) ((nowSeconds - enqueuedTimeSeconds) * 1000);
201203
if (latencyMs >= 0) {
202204
transaction.setData(SpanDataConvention.MESSAGING_MESSAGE_RECEIVE_LATENCY, latencyMs);

‎sentry-spring-7/src/main/java/io/sentry/spring7/tracing/SentrySpanAdvice.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,17 @@ public SentrySpanAdvice(final @NotNull IScopes scopes) {
3535

3636
@SuppressWarnings("deprecation")
3737
@Override
38-
public Object invoke(final @NotNull MethodInvocation invocation) throws Throwable {
38+
public @Nullable Object invoke(final @NotNull MethodInvocation invocation) throws Throwable {
3939
final ISpan activeSpan = scopes.getSpan();
4040

4141
if (activeSpan == null || activeSpan.isNoOp()) {
4242
// there is no active transaction, we do not start new span
4343
return invocation.proceed();
4444
} else {
45+
final @Nullable Object target = invocation.getThis();
4546
final Method mostSpecificMethod =
46-
AopUtils.getMostSpecificMethod(invocation.getMethod(), invocation.getThis().getClass());
47+
AopUtils.getMostSpecificMethod(
48+
invocation.getMethod(), target != null ? target.getClass() : null);
4749
final Class<?> targetClass = invocation.getMethod().getDeclaringClass();
4850
SentrySpan sentrySpan = AnnotationUtils.findAnnotation(mostSpecificMethod, SentrySpan.class);
4951
if (sentrySpan == null) {

0 commit comments

Comments
 (0)