From 26ab5beef05ddbf529946ae3531ae163d6bd1b89 Mon Sep 17 00:00:00 2001 From: leonardo-pilastri-sonarsource <115481625+leonardo-pilastri-sonarsource@users.noreply.github.com> Date: Wed, 11 Sep 2024 16:23:29 +0200 Subject: [PATCH] SONARJAVA-4950 Fix S6204 IndexOutOfBoundsException when lombok.val is used (#4857) --- .../src/main/java/checks/CollectorsToListCheckSample.java | 7 +++++++ .../java/org/sonar/java/checks/CollectorsToListCheck.java | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/java-checks-test-sources/default/src/main/java/checks/CollectorsToListCheckSample.java b/java-checks-test-sources/default/src/main/java/checks/CollectorsToListCheckSample.java index 35855b746c2..7f017ef33c4 100644 --- a/java-checks-test-sources/default/src/main/java/checks/CollectorsToListCheckSample.java +++ b/java-checks-test-sources/default/src/main/java/checks/CollectorsToListCheckSample.java @@ -8,8 +8,15 @@ import java.util.stream.Collector; import java.util.stream.Collectors; import java.util.stream.Stream; +import lombok.val; public class CollectorsToListCheckSample { + + void lombok_val() { + // Lombok val is not supported, in this case the symbolType of the collect method invocation is unknown + val unknownSymbolStream = Stream.of("A", "B", "C").collect(Collectors.toList()); // Compliant + } + static class ListWrapper { List strings; } diff --git a/java-checks/src/main/java/org/sonar/java/checks/CollectorsToListCheck.java b/java-checks/src/main/java/org/sonar/java/checks/CollectorsToListCheck.java index 39acd6284d3..729f9318b14 100644 --- a/java-checks/src/main/java/org/sonar/java/checks/CollectorsToListCheck.java +++ b/java-checks/src/main/java/org/sonar/java/checks/CollectorsToListCheck.java @@ -19,6 +19,7 @@ */ package org.sonar.java.checks; +import java.util.List; import javax.annotation.CheckForNull; import org.sonar.check.Rule; import org.sonar.java.checks.methods.AbstractMethodDetection; @@ -107,9 +108,8 @@ private static boolean isInvariantTypeArgument(MethodInvocationTree collectMetho if (streamType.isRawType()) { return true; } - Type collectArgType = collectMethodInvocation.symbolType().typeArguments().get(0); - Type streamArgType = streamType.typeArguments().get(0); - return collectArgType.is(streamArgType.fullyQualifiedName()); + List typeArguments = collectMethodInvocation.symbolType().typeArguments(); + return !typeArguments.isEmpty() && typeArguments.get(0).is(streamType.typeArguments().get(0).fullyQualifiedName()); } private void reportIssue(MethodInvocationTree collector, boolean mutable) {